2 * Copyright (c) 1999-2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.2 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 Copyright (c) 2002 Apple Computer, Inc.
26 This file contains the routine to make an HFS+ volume journaled
27 and a corresponding routine to turn it off.
31 #include <sys/types.h>
35 #include <sys/sysctl.h>
36 #include <sys/resource.h>
37 #include <sys/vmmeter.h>
38 #include <sys/mount.h>
40 #include <sys/ioctl.h>
43 #include <sys/loadable_fs.h>
44 #include <hfs/hfs_format.h>
45 #include <hfs/hfs_mount.h> /* for hfs sysctl values */
47 #include <System/hfs/hfs_fsctl.h>
58 #include <architecture/byte_order.h>
60 // just in case these aren't in <hfs/hfs_mount.h> yet
61 #ifndef HFS_ENABLE_JOURNALING
62 #define HFS_ENABLE_JOURNALING 0x082969
64 #ifndef HFS_DISABLE_JOURNALING
65 #define HFS_DISABLE_JOURNALING 0x031272
67 #ifndef HFS_GET_JOURNAL_INFO
68 #define HFS_GET_JOURNAL_INFO 0x6a6e6c69
71 /* getattrlist buffers start with an extra length field */
72 struct ExtentsAttrBuf
{
73 unsigned long infoLength
;
74 HFSPlusExtentRecord extents
;
76 typedef struct ExtentsAttrBuf ExtentsAttrBuf
;
78 #ifndef HFSIOC_GET_JOURNAL_INFO
79 # include <sys/ioctl.h>
80 struct hfs_journal_info
{
84 # define HFSIOC_GET_JOURNAL_INFO _IOR('h', 17, struct hfs_journal_info)
88 #define kIsInvisible 0x4000
91 * Generic Finder file/dir data
94 u_int32_t opaque_1
[2];
95 u_int16_t fdFlags
; /* Finder flags */
98 typedef struct FinderInfo FinderInfo
;
100 /* getattrlist buffers start with an extra length field */
101 struct FinderAttrBuf
{
102 unsigned long infoLength
;
103 FinderInfo finderInfo
;
105 typedef struct FinderAttrBuf FinderAttrBuf
;
108 int hide_file(const char * file
)
110 struct attrlist alist
= {0};
111 FinderAttrBuf finderInfoBuf
= {0};
114 alist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
115 alist
.commonattr
= ATTR_CMN_FNDRINFO
;
117 result
= getattrlist(file
, &alist
, &finderInfoBuf
, sizeof(finderInfoBuf
), 0);
122 if (finderInfoBuf
.finderInfo
.fdFlags
& kIsInvisible
) {
123 printf("hide: %s is alreadly invisible\n", file
);
127 finderInfoBuf
.finderInfo
.fdFlags
|= kIsInvisible
;
129 result
= setattrlist(file
, &alist
, &finderInfoBuf
.finderInfo
, sizeof(FinderInfo
), 0);
131 return (result
== -1 ? errno
: result
);
135 get_start_block(const char *file
, uint32_t fs_block_size
)
137 off_t cur_pos
, phys_start
, len
;
142 fd
= open(file
, O_RDONLY
);
147 if (fstat(fd
, &st
) < 0) {
148 fprintf(stderr
, "can't stat %s (%s)\n", file
, strerror(errno
));
153 fs_block_size
= st
.st_blksize
; // XXXdbg quick hack for now
155 phys_start
= len
= 0;
156 for(cur_pos
=0; cur_pos
< st
.st_size
; cur_pos
+= fs_block_size
) {
157 memset(&l2p
, 0, sizeof(l2p
));
158 lseek(fd
, cur_pos
, SEEK_SET
);
159 err
= fcntl(fd
, F_LOG2PHYS
, &l2p
);
161 if (phys_start
== 0) {
162 phys_start
= l2p
.l2p_devoffset
;
164 } else if (l2p
.l2p_devoffset
!= (phys_start
+ len
)) {
165 // printf(" %lld : %lld - %lld\n", cur_pos, phys_start / fs_block_size, len / fs_block_size);
166 fprintf(stderr
, "%s : is not contiguous!\n", file
);
169 // phys_start = l2p.l2p_devoffset;
170 // len = fs_block_size;
172 len
+= fs_block_size
;
178 //printf("%s start offset %lld; byte len %lld (blksize %d)\n",
179 // file, phys_start, len, fs_block_size);
181 if ((phys_start
/ (unsigned int)fs_block_size
) & 0xffffffff00000000LL
) {
182 fprintf(stderr
, "%s : starting block is > 32bits!\n", file
);
191 // Get the embedded offset (if any) for an hfs+ volume.
192 // This is pretty skanky that we have to do this but
195 #include <sys/disk.h>
196 #include <hfs/hfs_format.h>
198 #include <machine/endian.h>
200 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
201 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
203 #include <libkern/OSByteOrder.h>
205 #define SWAP_BE16(x) ntohs(x)
206 #define SWAP_BE32(x) ntohl(x)
207 #define SWAP_BE64(x) OSSwapConstInt64(x)
211 get_embedded_offset(char *devname
)
215 char *buff
= NULL
, rawdev
[256];
218 HFSMasterDirectoryBlock
*mdbp
;
219 off_t embeddedOffset
;
224 if (stat(devname
, &st
) != 0) {
225 fprintf(stderr
, "Could not access %s (%s)\n", devname
, strerror(errno
));
230 if (S_ISCHR(st
.st_mode
) == 0) {
231 // hmmm, it's not the character special raw device so we
232 // should try to figure out the real device.
233 if (statfs(devname
, &sfs
) != 0) {
234 fprintf(stderr
, "Can't find out any info about the fs for path %s (%s)\n",
235 devname
, strerror(errno
));
240 // This assumes it begins with "/dev/". The old code assumed
241 // it began with five characters. Should probably use strrchr or equivalent.
242 snprintf(rawdev
, sizeof(rawdev
), "/dev/r%s", &sfs
.f_mntfromname
[5]);
243 devname
= &rawdev
[0];
247 fd
= open(devname
, O_RDONLY
);
249 fprintf(stderr
, "can't open: %s (%s)\n", devname
, strerror(errno
));
254 /* Get the real physical block size. */
255 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
256 fprintf(stderr
, "can't get the device block size (%s). assuming 512\n", strerror(errno
));
262 /* Get the number of physical blocks. */
263 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
265 fprintf(stderr
, "failed to get block count. trying stat().\n");
266 if (fstat(fd
, &st
) != 0) {
271 blkcnt
= st
.st_size
/ blksize
;
276 * blksize has our prefered physical block size
277 * blkcnt has the total number of physical blocks
280 buff
= (char *)malloc(blksize
);
282 if (pread(fd
, buff
, blksize
, HFS_PRI_SECTOR(blksize
)*blksize
) != blksize
) {
283 fprintf(stderr
, "failed to read volume header @ offset %d (%s)\n",
284 HFS_PRI_SECTOR(blksize
), strerror(errno
));
289 mdbp
= (HFSMasterDirectoryBlock
*)(buff
+ HFS_PRI_OFFSET(blksize
));
290 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
291 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
292 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
293 printf ("get_embedded_offset: invalid volume signature \n");
298 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) && (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
)) {
301 } else if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
302 /* Get the embedded Volume Header */
303 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * 512;
304 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
305 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
308 * If the embedded volume doesn't start on a block
309 * boundary, then switch the device to a 512-byte
310 * block size so everything will line up on a block
313 if ((embeddedOffset
% blksize
) != 0) {
314 fprintf(stderr
, "HFS Mount: embedded volume offset not"
315 " a multiple of physical block size (%d);"
316 " switching to 512\n", blksize
);
318 blkcnt
*= (blksize
/ 512);
322 } else { /* pure HFS+ */
326 ret
= embeddedOffset
;
340 static const char *journal_fname
= ".journal";
341 static const char *jib_fname
= ".journal_info_block";
344 DoMakeJournaled(char *volname
, int jsize
)
346 int fd
, i
, block_size
, journal_size
= 8*1024*1024;
350 int32_t jstart_block
, jinfo_block
;
352 JournalInfoBlock jib
;
354 static char tmpname
[MAXPATHLEN
];
355 off_t start_block
, embedded_offset
;
357 if (statfs(volname
, &sfs
) != 0) {
358 fprintf(stderr
, "Can't stat volume %s (%s).\n", volname
, strerror(errno
));
362 // Make sure that we're HFS+. First we check the fstypename.
363 // If that's ok then we try to create a symlink (which won't
364 // work on plain hfs volumes but will work on hfs+ volumes).
366 if (strcmp(sfs
.f_fstypename
, "devfs") == 0) {
367 fprintf (stderr
, "%s is a device node. Journal enable only works on a mounted HFS+ volume.\n", volname
);
370 snprintf(tmpname
, sizeof(tmpname
), "%s/is_vol_hfs_plus", volname
);
371 if (strcmp(sfs
.f_fstypename
, "hfs") != 0 ||
372 ((ret
= symlink(tmpname
, tmpname
)) != 0 && errno
== ENOTSUP
)) {
373 fprintf(stderr
, "%s is not an HFS+ volume. Journaling only works on HFS+ volumes.\n",
379 if (sfs
.f_flags
& MNT_JOURNALED
) {
380 fprintf(stderr
, "Volume %s is already journaled.\n", volname
);
385 journal_size
= jsize
;
390 // we want at least 8 megs of journal for each 100 gigs of
391 // disk space. We cap the size at 512 megs though.
393 scale
= ((long long)sfs
.f_bsize
* (long long)((unsigned int)sfs
.f_blocks
)) / (100*1024*1024*1024ULL);
394 journal_size
*= (scale
+ 1);
395 if (journal_size
> 512 * 1024 * 1024) {
396 journal_size
= 512 * 1024 * 1024;
400 if (chdir(volname
) != 0) {
401 fprintf(stderr
, "Can't locate volume %s to make it journaled (%s).\n",
402 volname
, strerror(errno
));
407 embedded_offset
= get_embedded_offset(volname
);
408 if (embedded_offset
< 0) {
409 fprintf(stderr
, "Can't calculate the embedded offset (if any) for %s.\n", volname
);
410 fprintf(stderr
, "Journal creation failure.\n");
413 // printf("Embedded offset == 0x%llx\n", embedded_offset);
415 fd
= open(journal_fname
, O_CREAT
|O_TRUNC
|O_RDWR
, 000);
417 fprintf(stderr
, "Can't create journal file on volume %s (%s)\n",
418 volname
, strerror(errno
));
422 // make sure that it has no r/w/x privs (only could happen if
423 // the file already existed since open() doesn't reset the mode
428 block_size
= sfs
.f_bsize
;
429 if ((journal_size
% block_size
) != 0) {
430 fprintf(stderr
, "Journal size %dk is not a multiple of volume %s block size (%d).\n",
431 journal_size
/1024, volname
, block_size
);
433 unlink(journal_fname
);
438 memset(&fst
, 0, sizeof(fst
));
439 fst
.fst_flags
= F_ALLOCATECONTIG
|F_ALLOCATEALL
;
440 fst
.fst_length
= journal_size
;
441 fst
.fst_posmode
= F_PEOFPOSMODE
;
443 ret
= fcntl(fd
, F_PREALLOCATE
, &fst
);
445 if (journal_size
>= 2*1024*1024) {
446 fprintf(stderr
, "Not enough contiguous space for a %d k journal. Retrying.\n",
449 ftruncate(fd
, 0); // make sure the file is zero bytes long.
452 fprintf(stderr
, "Disk too fragmented to enable journaling.\n");
453 fprintf(stderr
, "Please run a defragmenter on %s.\n", volname
);
455 unlink(journal_fname
);
460 printf("Allocated %lldK for journal file.\n", fst
.fst_bytesalloc
/1024LL);
461 buf
= (char *)calloc(block_size
, 1);
463 for(i
=0; i
< journal_size
/block_size
; i
++) {
464 ret
= write(fd
, buf
, block_size
);
465 if (ret
!= block_size
) {
470 if (i
*block_size
!= journal_size
) {
471 fprintf(stderr
, "Failed to write %dk to journal on volume %s (%s)\n",
472 journal_size
/1024, volname
, strerror(errno
));
475 printf("Could not allocate memory to write to the journal on volume %s (%s)\n",
476 volname
, strerror(errno
));
481 hide_file(journal_fname
);
483 start_block
= get_start_block(journal_fname
, block_size
);
484 if (start_block
== (off_t
)-1) {
485 fprintf(stderr
, "Failed to get start block for %s (%s)\n",
486 journal_fname
, strerror(errno
));
487 unlink(journal_fname
);
490 jstart_block
= (start_block
/ block_size
) - (embedded_offset
/ block_size
);
492 memset(&jib
, 'Z', sizeof(jib
));
493 jib
.flags
= kJIJournalInFSMask
;
494 jib
.offset
= (off_t
)((unsigned int)jstart_block
) * (off_t
)((unsigned int)block_size
);
495 jib
.size
= (off_t
)((unsigned int)journal_size
);
497 fd
= open(jib_fname
, O_CREAT
|O_TRUNC
|O_RDWR
, 000);
499 fprintf(stderr
, "Could not create journal info block file on volume %s (%s)\n",
500 volname
, strerror(errno
));
501 unlink(journal_fname
);
505 // swap the data before we copy it
506 jib
.flags
= OSSwapBigToHostInt32(jib
.flags
);
507 jib
.offset
= OSSwapBigToHostInt64(jib
.offset
);
508 jib
.size
= OSSwapBigToHostInt64(jib
.size
);
510 memcpy(buf
, &jib
, sizeof(jib
));
512 // now put it back the way it was
513 jib
.size
= OSSwapBigToHostInt64(jib
.size
);
514 jib
.offset
= OSSwapBigToHostInt64(jib
.offset
);
515 jib
.flags
= OSSwapBigToHostInt32(jib
.flags
);
517 if (write(fd
, buf
, block_size
) != block_size
) {
518 fprintf(stderr
, "Failed to write journal info block on volume %s (%s)!\n",
519 volname
, strerror(errno
));
520 unlink(journal_fname
);
526 hide_file(jib_fname
);
528 start_block
= get_start_block(jib_fname
, block_size
);
529 if (start_block
== (off_t
)-1) {
530 fprintf(stderr
, "Failed to get start block for %s (%s)\n",
531 jib_fname
, strerror(errno
));
532 unlink(journal_fname
);
536 jinfo_block
= (start_block
/ block_size
) - (embedded_offset
/ block_size
);
540 // Now make the volume journaled!
542 memset(sysctl_info
, 0, sizeof(sysctl_info
));
543 sysctl_info
[0] = CTL_VFS
;
544 sysctl_info
[1] = sfs
.f_fsid
.val
[1];
545 sysctl_info
[2] = HFS_ENABLE_JOURNALING
;
546 sysctl_info
[3] = jinfo_block
;
547 sysctl_info
[4] = jstart_block
;
548 sysctl_info
[5] = journal_size
;
550 //printf("fs type: 0x%x\n", sysctl_info[1]);
551 //printf("jinfo block : 0x%x\n", jinfo_block);
552 //printf("jstart block: 0x%x\n", jstart_block);
553 //printf("journal size: 0x%x\n", journal_size);
555 ret
= sysctl((void *)sysctl_info
, 6, NULL
, NULL
, NULL
, 0);
557 fprintf(stderr
, "Failed to make volume %s journaled (%s)\n",
558 volname
, strerror(errno
));
559 unlink(journal_fname
);
569 DoUnJournal(char *volname
)
574 char jbuf
[MAXPATHLEN
];
576 if (statfs(volname
, &sfs
) != 0) {
577 fprintf(stderr
, "Can't stat volume %s (%s).\n", volname
, strerror(errno
));
581 if (strcmp(sfs
.f_fstypename
, "hfs") != 0) {
582 fprintf(stderr
, "Volume %s (%s) is not a HFS volume.\n", volname
, sfs
.f_mntfromname
);
586 if ((sfs
.f_flags
& MNT_JOURNALED
) == 0) {
587 fprintf(stderr
, "Volume %s (%s) is not journaled.\n", volname
, sfs
.f_mntfromname
);
591 if (chdir(volname
) != 0) {
592 fprintf(stderr
, "Can't locate volume %s to turn off journaling (%s).\n",
593 volname
, strerror(errno
));
597 memset(sysctl_info
, 0, sizeof(sysctl_info
));
598 sysctl_info
[0] = CTL_VFS
;
599 sysctl_info
[1] = sfs
.f_fsid
.val
[1];
600 sysctl_info
[2] = HFS_DISABLE_JOURNALING
;
602 result
= sysctl((void *)sysctl_info
, 3, NULL
, NULL
, NULL
, 0);
604 fprintf(stderr
, "Failed to make volume %s UN-journaled (%s)\n",
605 volname
, strerror(errno
));
609 snprintf(jbuf
, sizeof(jbuf
), "%s/%s", volname
, journal_fname
);
610 if (unlink(jbuf
) != 0) {
611 fprintf(stderr
, "Failed to remove the journal %s (%s)\n",
612 jbuf
, strerror(errno
));
615 snprintf(jbuf
, sizeof(jbuf
), "%s/%s", volname
, jib_fname
);
616 if (unlink(jbuf
) != 0) {
617 fprintf(stderr
, "Failed to remove the journal info block %s (%s)\n",
618 jbuf
, strerror(errno
));
621 printf("Journaling disabled on %s mounted at %s.\n", sfs
.f_mntfromname
, volname
);
630 get_journal_info(char *devname
, struct JournalInfoBlock
*jib
)
632 int fd
= -1, ret
= 0;
633 char *buff
= NULL
, *buff2
= NULL
;
638 HFSMasterDirectoryBlock
*mdbp
;
639 HFSPlusVolumeHeader
*vhp
;
640 off_t embeddedOffset
, pos
;
641 struct JournalInfoBlock
*myjib
;
643 fd
= open(devname
, O_RDONLY
);
645 printf("can't open: %s (%s)\n", devname
, strerror(errno
));
650 /* Get the real physical block size. */
651 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
652 printf("can't get the device block size (%s). assuming 512\n", strerror(errno
));
658 /* Get the number of physical blocks. */
659 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
661 printf("failed to get block count. trying stat().\n");
662 if (fstat(fd
, &st
) != 0) {
667 blkcnt
= st
.st_size
/ blksize
;
670 /* Compute an accurate disk size */
671 disksize
= blkcnt
* (u_int64_t
)blksize
;
674 * There are only 31 bits worth of block count in
675 * the buffer cache. So for large volumes a 4K
676 * physical block size is needed.
678 if (blksize
== 512 && blkcnt
> (u_int64_t
)0x000000007fffffff) {
684 * blksize has our prefered physical block size
685 * blkcnt has the total number of physical blocks
688 buff
= (char *)malloc(blksize
);
689 buff2
= (char *)malloc(blksize
);
691 if (pread(fd
, buff
, blksize
, HFS_PRI_SECTOR(blksize
)*blksize
) != blksize
) {
692 printf("failed to read volume header @ offset %d (%s)\n",
693 HFS_PRI_SECTOR(blksize
), strerror(errno
));
698 mdbp
= (HFSMasterDirectoryBlock
*)(buff
+ HFS_PRI_OFFSET(blksize
));
700 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
701 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
702 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
704 printf("get_journal_info: invalid volume signature\n");
708 mdbp
->drSigWord
= SWAP_BE16(mdbp
->drSigWord
);
709 mdbp
->drEmbedSigWord
= SWAP_BE16(mdbp
->drEmbedSigWord
);
710 mdbp
->drAlBlSt
= SWAP_BE16(mdbp
->drAlBlSt
);
711 mdbp
->drEmbedExtent
.startBlock
= SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
);
712 mdbp
->drAlBlkSiz
= SWAP_BE32(mdbp
->drAlBlkSiz
);
713 mdbp
->drEmbedExtent
.blockCount
= SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
);
716 if ((mdbp
->drSigWord
== kHFSSigWord
) && (mdbp
->drEmbedSigWord
!= kHFSPlusSigWord
)) {
717 // normal hfs can not ever be journaled
721 /* Get the embedded Volume Header */
722 if (mdbp
->drEmbedSigWord
== kHFSPlusSigWord
) {
723 embeddedOffset
= mdbp
->drAlBlSt
* 512;
724 embeddedOffset
+= (u_int64_t
)mdbp
->drEmbedExtent
.startBlock
*
725 (u_int64_t
)mdbp
->drAlBlkSiz
;
728 * If the embedded volume doesn't start on a block
729 * boundary, then switch the device to a 512-byte
730 * block size so everything will line up on a block
733 if ((embeddedOffset
% blksize
) != 0) {
734 printf("HFS Mount: embedded volume offset not"
735 " a multiple of physical block size (%d);"
736 " switching to 512\n", blksize
);
738 blkcnt
*= (blksize
/ 512);
742 disksize
= (u_int64_t
)mdbp
->drEmbedExtent
.blockCount
*
743 (u_int64_t
)mdbp
->drAlBlkSiz
;
745 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
746 if (pread(fd
, buff
, blksize
, mdb_offset
* blksize
) != blksize
) {
747 printf("failed to read the embedded vhp @ offset %d\n", mdb_offset
* blksize
);
752 vhp
= (HFSPlusVolumeHeader
*) (buff
+ HFS_PRI_OFFSET(blksize
));
754 mdbp
= (HFSMasterDirectoryBlock
*)vhp
;
755 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
756 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
757 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
760 printf("get_journal_info: invalid embedded volume signature \n");
764 } else /* pure HFS+ */ {
766 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
769 if ((SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
) == 0) {
775 // Now read the journal info block... (when calculating the
776 // position, make sure to cast to unsigned first, then to
777 // off_t so that things don't get sign-extended improperly
780 pos
= (off_t
)((off_t
)embeddedOffset
+
781 (off_t
)((unsigned int)SWAP_BE32(vhp
->journalInfoBlock
))*(off_t
)((unsigned int)SWAP_BE32(vhp
->blockSize
)));
783 if (pread(fd
, buff2
, blksize
, pos
) != blksize
) {
784 printf("failed to read the journal info block (%s).\n", strerror(errno
));
789 myjib
= (struct JournalInfoBlock
*)buff2
;
790 myjib
->flags
= SWAP_BE32(myjib
->flags
);
791 myjib
->offset
= SWAP_BE64(myjib
->offset
);
792 myjib
->size
= SWAP_BE64(myjib
->size
);
794 memcpy(jib
, myjib
, sizeof(*myjib
));
811 DoGetJournalInfo(char *volname
)
814 struct hfs_journal_info jinfo
;
816 if (strncmp(volname
, "/dev/", 5) == 0 || strncmp(volname
, "disk", 4) == 0 || strncmp(volname
, "rdisk", 5) == 0) {
817 struct JournalInfoBlock jib
;
819 char fulldevname
[256];
821 if (strncmp(volname
, "disk", 4) == 0 || strncmp(volname
, "rdisk", 5) == 0) {
822 snprintf(fulldevname
, sizeof(fulldevname
), "/dev/%s", volname
);
823 volname
= &fulldevname
[0];
826 // try the name as a device name...
827 ret
= get_journal_info(volname
, &jib
);
829 printf("Volume %s is not journaled.\n", volname
);
831 } else if (ret
< 0) {
832 printf("Volume %s does not appear to be an HFS+ volume.\n", volname
);
835 if (jib
.flags
& kJIJournalInFSMask
) {
836 printf("%s : journal size %lld k at offset 0x%llx\n", volname
, jib
.size
/1024, jib
.offset
);
838 printf("%s: external journal stored on partition with uuid %s on machine w/serial number %s\n",
839 volname
, jib
.ext_jnl_uuid
, jib
.machine_serial_num
);
847 if (statfs(volname
, &sfs
) != 0) {
848 fprintf(stderr
, "Unable to get fs info for %s\n", volname
);
852 if ((sfs
.f_flags
& MNT_JOURNALED
) == 0) {
853 fprintf(stderr
, "Volume %s is not journaled.\n", volname
);
857 if (fsctl(volname
, HFSIOC_GET_JOURNAL_INFO
, &jinfo
, 0) != 0) {
858 fprintf(stderr
, "Failed to get journal info for volume %s (%s)\n",
859 volname
, strerror(errno
));
863 if (jinfo
.jstart
== 0) {
866 snprintf(rawdev
, sizeof(rawdev
), "/dev/r%s", &sfs
.f_mntfromname
[5]);
868 // it's an external journal so get the info the
870 return DoGetJournalInfo(&rawdev
[0]);
873 if (jinfo
.jsize
== 0) {
874 printf("%s : not journaled.\n", volname
);
876 printf("%s : journal size %lld k at offset 0x%llx\n", volname
, jinfo
.jsize
/1024, jinfo
.jstart
);
884 RawDisableJournaling(char *devname
)
886 int fd
= -1, ret
= 0;
887 char *buff
= NULL
, rawdev
[256], unrawdev
[256];
892 HFSMasterDirectoryBlock
*mdbp
;
893 HFSPlusVolumeHeader
*vhp
;
894 off_t embeddedOffset
, hdr_offset
;
896 struct statfs
*fsinfo
;
898 // assume that the name provided is a raw device name
899 strlcpy(rawdev
, devname
, sizeof(rawdev
));
902 if (stat(rawdev
, &st
) != 0) {
903 fprintf(stderr
, "Could not access %s (%s)\n", devname
, strerror(errno
));
908 if (S_ISCHR(st
.st_mode
) == 0) {
909 if (S_ISBLK(st
.st_mode
)) {
910 // this is a block device, convert the name to
911 // raw character device and try again
912 snprintf(rawdev
, sizeof(rawdev
), "/dev/r%s", devname
+ 5);
915 // probably it is a mount point
916 return DoUnJournal(devname
);
919 // convert the character raw device name to
920 // block device name to compare with getmntinfo output
921 snprintf(unrawdev
, sizeof(unrawdev
), "/dev/%s", rawdev
+ 6);
924 // make sure that the file system on the device node is not mounted
925 ret
= getmntinfo(&fsinfo
, MNT_NOWAIT
);
927 fprintf (stderr
, "Error getting list of mounted filesystems\n");
933 // the file system on this device node is currently mounted
934 if (strcmp(unrawdev
, fsinfo
[ret
].f_mntfromname
) == 0) {
935 return DoUnJournal(fsinfo
[ret
].f_mntonname
);
939 fd
= open(rawdev
, O_RDWR
);
941 fprintf(stderr
, "can't open: %s (%s)\n", devname
, strerror(errno
));
946 /* Get the real physical block size. */
947 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
948 fprintf(stderr
, "can't get the device block size (%s). assuming 512\n", strerror(errno
));
954 /* Get the number of physical blocks. */
955 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
958 if (fstat(fd
, &st
) != 0) {
963 blkcnt
= st
.st_size
/ blksize
;
966 /* Compute an accurate disk size */
967 disksize
= blkcnt
* (u_int64_t
)blksize
;
970 * There are only 31 bits worth of block count in
971 * the buffer cache. So for large volumes a 4K
972 * physical block size is needed.
974 if (blksize
== 512 && blkcnt
> (u_int64_t
)0x000000007fffffff) {
980 * blksize has our prefered physical block size
981 * blkcnt has the total number of physical blocks
984 buff
= (char *)malloc(blksize
);
986 hdr_offset
= HFS_PRI_SECTOR(blksize
)*blksize
;
987 if (pread(fd
, buff
, blksize
, hdr_offset
) != blksize
) {
988 fprintf(stderr
, "RawDisableJournaling: failed to read volume header @ offset %lld (%s)\n",
989 hdr_offset
, strerror(errno
));
994 mdbp
= (HFSMasterDirectoryBlock
*)(buff
+ HFS_PRI_OFFSET(blksize
));
995 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
996 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
997 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
999 printf("RawDisableJournaling: Invalid Volume Signature \n");
1003 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) && (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
)) {
1004 // normal hfs can not ever be journaled
1005 fprintf(stderr
, "disable_journaling: volume is only regular HFS, not HFS+\n");
1009 /* Get the embedded Volume Header */
1010 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1011 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * 512;
1012 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) * (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1015 * If the embedded volume doesn't start on a block
1016 * boundary, then switch the device to a 512-byte
1017 * block size so everything will line up on a block
1020 if ((embeddedOffset
% blksize
) != 0) {
1021 fprintf(stderr
, "HFS Mount: embedded volume offset not"
1022 " a multiple of physical block size (%d);"
1023 " switching to 512\n", blksize
);
1025 blkcnt
*= (blksize
/ 512);
1029 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) * (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1031 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1032 hdr_offset
= mdb_offset
* blksize
;
1033 if (pread(fd
, buff
, blksize
, hdr_offset
) != blksize
) {
1034 fprintf(stderr
, "failed to read the embedded vhp @ offset %d\n", mdb_offset
* blksize
);
1039 vhp
= (HFSPlusVolumeHeader
*) (buff
+ HFS_PRI_OFFSET(blksize
));
1041 mdbp
= (HFSMasterDirectoryBlock
*)vhp
;
1042 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
1043 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
1044 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
1047 printf("RawDisableJournaling: invalid embedded volume signature \n");
1051 } else /* pure HFS+ */ {
1053 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1057 if ((SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
) != 0) {
1058 unsigned int tmp
= SWAP_BE32(vhp
->attributes
);
1060 tmp
&= ~kHFSVolumeJournaledMask
;
1061 vhp
->attributes
= SWAP_BE32(tmp
);
1062 if ((tmp
= pwrite(fd
, buff
, blksize
, hdr_offset
)) != blksize
) {
1063 fprintf(stderr
, "Update of super-block on %s failed! (%d != %d, %s)\n",
1064 devname
, tmp
, blksize
, strerror(errno
));
1066 fprintf(stderr
, "Turned off the journaling bit for %s\n", devname
);
1069 fprintf(stderr
, "disable_journaling: %s is not journaled.\n", devname
);
1085 SetJournalInFSState(const char *devname
, int journal_in_fs
)
1087 int fd
= -1, ret
= 0;
1088 char *buff
= NULL
, *buff2
= NULL
;
1092 HFSMasterDirectoryBlock
*mdbp
;
1093 HFSPlusVolumeHeader
*vhp
;
1094 off_t embeddedOffset
, pos
;
1095 struct JournalInfoBlock
*myjib
;
1097 fd
= open(devname
, O_RDWR
);
1099 printf("can't open: %s (%s)\n", devname
, strerror(errno
));
1104 /* Get the real physical block size. */
1105 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
1106 printf("can't get the device block size (%s). assuming 512\n", strerror(errno
));
1112 /* Get the number of physical blocks. */
1113 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
1115 printf("failed to get block count. trying stat().\n");
1116 if (fstat(fd
, &st
) != 0) {
1121 blkcnt
= st
.st_size
/ blksize
;
1125 * There used to only be 31 bits worth of block count in
1126 * the buffer cache. So for large volumes a 4K
1127 * physical block size is needed.
1129 if (blksize
== 512 && blkcnt
> (u_int64_t
)0x000000007fffffff) {
1135 * blksize has our prefered physical block size
1136 * blkcnt has the total number of physical blocks
1139 buff
= (char *)malloc(blksize
);
1140 buff2
= (char *)malloc(blksize
);
1142 if (pread(fd
, buff
, blksize
, HFS_PRI_SECTOR(blksize
)*blksize
) != blksize
) {
1143 printf("failed to read volume header @ offset %d (%s)\n",
1144 HFS_PRI_SECTOR(blksize
), strerror(errno
));
1149 mdbp
= (HFSMasterDirectoryBlock
*)(buff
+ HFS_PRI_OFFSET(blksize
));
1152 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
1153 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
1154 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
1156 printf ("SetJournalInFSState: Invalid Volume Signature \n");
1160 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) && (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
)) {
1161 // normal hfs can not ever be journaled
1165 /* Get the embedded Volume Header */
1166 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1167 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * 512;
1168 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1169 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1172 * If the embedded volume doesn't start on a block
1173 * boundary, then switch the device to a 512-byte
1174 * block size so everything will line up on a block
1177 if ((embeddedOffset
% blksize
) != 0) {
1178 printf("HFS Mount: embedded volume offset not"
1179 " a multiple of physical block size (%d);"
1180 " switching to 512\n", blksize
);
1182 blkcnt
*= (blksize
/ 512);
1186 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1187 if (pread(fd
, buff
, blksize
, mdb_offset
* blksize
) != blksize
) {
1188 printf("failed to read the embedded vhp @ offset %d\n", mdb_offset
* blksize
);
1193 vhp
= (HFSPlusVolumeHeader
*) (buff
+ HFS_PRI_OFFSET(blksize
));
1195 mdbp
= (HFSMasterDirectoryBlock
*)(vhp
);
1196 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
1197 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
1198 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
1200 printf("SetJournalInFSState: Invalid Embedded Volume Signature \n");
1205 } else /* pure HFS+ */ {
1207 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1210 //printf("vol header attributes: 0x%x\n", SWAP_BE32(vhp->attributes));
1211 if ((SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
) == 0) {
1217 // Now read the journal info block... (when calculating the
1218 // position, make sure to cast to unsigned first, then to
1219 // off_t so that things don't get sign-extended improperly
1222 pos
= (off_t
)((off_t
)embeddedOffset
+
1223 (off_t
)((unsigned int )SWAP_BE32(vhp
->journalInfoBlock
))*(off_t
)((unsigned int)SWAP_BE32(vhp
->blockSize
)));
1225 if (pread(fd
, buff2
, blksize
, pos
) != blksize
) {
1226 printf("failed to read the journal info block (%s).\n", strerror(errno
));
1231 myjib
= (struct JournalInfoBlock
*)buff2
;
1233 // swap this to host native format so we can diddle with the bits
1234 myjib
->flags
= SWAP_BE32(myjib
->flags
);
1236 if (journal_in_fs
) {
1237 myjib
->flags
|= kJIJournalInFSMask
;
1239 myjib
->flags
&= ~kJIJournalInFSMask
;
1241 myjib
->flags
|= kJIJournalNeedInitMask
;
1243 // and now swap back before writing it out
1244 myjib
->flags
= SWAP_BE32(myjib
->flags
);
1246 if (pwrite(fd
, buff2
, blksize
, pos
) != blksize
) {
1247 printf("failed to re-write the journal info block (%s).\n", strerror(errno
));