2 * Copyright (c) 1999-2008 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
;
290 if ( (SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
)
291 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
)
292 && (SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
)) {
297 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) && (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
)) {
300 } else if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
301 /* Get the embedded Volume Header */
302 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * 512;
303 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
304 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
307 * If the embedded volume doesn't start on a block
308 * boundary, then switch the device to a 512-byte
309 * block size so everything will line up on a block
312 if ((embeddedOffset
% blksize
) != 0) {
313 fprintf(stderr
, "HFS Mount: embedded volume offset not"
314 " a multiple of physical block size (%d);"
315 " switching to 512\n", blksize
);
317 blkcnt
*= (blksize
/ 512);
321 } else { /* pure HFS+ */
325 ret
= embeddedOffset
;
339 static const char *journal_fname
= ".journal";
340 static const char *jib_fname
= ".journal_info_block";
343 DoMakeJournaled(char *volname
, int jsize
)
345 int fd
, i
, block_size
, journal_size
= 8*1024*1024;
349 int32_t jstart_block
, jinfo_block
;
351 JournalInfoBlock jib
;
353 static char tmpname
[MAXPATHLEN
];
354 off_t start_block
, embedded_offset
;
356 if (statfs(volname
, &sfs
) != 0) {
357 fprintf(stderr
, "Can't stat volume %s (%s).\n", volname
, strerror(errno
));
361 // Make sure that we're HFS+. First we check the fstypename.
362 // If that's ok then we try to create a symlink (which won't
363 // work on plain hfs volumes but will work on hfs+ volumes).
365 if (strcmp(sfs
.f_fstypename
, "devfs") == 0) {
366 fprintf (stderr
, "%s is a device node. Journal enable only works on a mounted HFS+ volume.\n", volname
);
369 snprintf(tmpname
, sizeof(tmpname
), "%s/is_vol_hfs_plus", volname
);
370 if (strcmp(sfs
.f_fstypename
, "hfs") != 0 ||
371 ((ret
= symlink(tmpname
, tmpname
)) != 0 && errno
== ENOTSUP
)) {
372 fprintf(stderr
, "%s is not an HFS+ volume. Journaling only works on HFS+ volumes.\n",
378 if (sfs
.f_flags
& MNT_JOURNALED
) {
379 fprintf(stderr
, "Volume %s is already journaled.\n", volname
);
384 journal_size
= jsize
;
389 // we want at least 8 megs of journal for each 100 gigs of
390 // disk space. We cap the size at 512 megs though.
392 scale
= ((long long)sfs
.f_bsize
* (long long)((unsigned int)sfs
.f_blocks
)) / (100*1024*1024*1024ULL);
393 journal_size
*= (scale
+ 1);
394 if (journal_size
> 512 * 1024 * 1024) {
395 journal_size
= 512 * 1024 * 1024;
399 if (chdir(volname
) != 0) {
400 fprintf(stderr
, "Can't locate volume %s to make it journaled (%s).\n",
401 volname
, strerror(errno
));
406 embedded_offset
= get_embedded_offset(volname
);
407 if (embedded_offset
< 0) {
408 fprintf(stderr
, "Can't calculate the embedded offset (if any) for %s.\n", volname
);
409 fprintf(stderr
, "Journal creation failure.\n");
412 // printf("Embedded offset == 0x%llx\n", embedded_offset);
414 fd
= open(journal_fname
, O_CREAT
|O_TRUNC
|O_RDWR
, 000);
416 fprintf(stderr
, "Can't create journal file on volume %s (%s)\n",
417 volname
, strerror(errno
));
421 // make sure that it has no r/w/x privs (only could happen if
422 // the file already existed since open() doesn't reset the mode
427 block_size
= sfs
.f_bsize
;
428 if ((journal_size
% block_size
) != 0) {
429 fprintf(stderr
, "Journal size %dk is not a multiple of volume %s block size (%d).\n",
430 journal_size
/1024, volname
, block_size
);
432 unlink(journal_fname
);
437 memset(&fst
, 0, sizeof(fst
));
438 fst
.fst_flags
= F_ALLOCATECONTIG
|F_ALLOCATEALL
;
439 fst
.fst_length
= journal_size
;
440 fst
.fst_posmode
= F_PEOFPOSMODE
;
442 ret
= fcntl(fd
, F_PREALLOCATE
, &fst
);
444 if (journal_size
>= 2*1024*1024) {
445 fprintf(stderr
, "Not enough contiguous space for a %d k journal. Retrying.\n",
448 ftruncate(fd
, 0); // make sure the file is zero bytes long.
451 fprintf(stderr
, "Disk too fragmented to enable journaling.\n");
452 fprintf(stderr
, "Please run a defragmenter on %s.\n", volname
);
454 unlink(journal_fname
);
459 printf("Allocated %lldK for journal file.\n", fst
.fst_bytesalloc
/1024LL);
460 buf
= (char *)calloc(block_size
, 1);
462 for(i
=0; i
< journal_size
/block_size
; i
++) {
463 ret
= write(fd
, buf
, block_size
);
464 if (ret
!= block_size
) {
469 if (i
*block_size
!= journal_size
) {
470 fprintf(stderr
, "Failed to write %dk to journal on volume %s (%s)\n",
471 journal_size
/1024, volname
, strerror(errno
));
474 printf("Could not allocate memory to write to the journal on volume %s (%s)\n",
475 volname
, strerror(errno
));
480 hide_file(journal_fname
);
482 start_block
= get_start_block(journal_fname
, block_size
);
483 if (start_block
== (off_t
)-1) {
484 fprintf(stderr
, "Failed to get start block for %s (%s)\n",
485 journal_fname
, strerror(errno
));
486 unlink(journal_fname
);
489 jstart_block
= (start_block
/ block_size
) - (embedded_offset
/ block_size
);
491 memset(&jib
, 'Z', sizeof(jib
));
492 jib
.flags
= kJIJournalInFSMask
;
493 jib
.offset
= (off_t
)((unsigned int)jstart_block
) * (off_t
)((unsigned int)block_size
);
494 jib
.size
= (off_t
)((unsigned int)journal_size
);
496 fd
= open(jib_fname
, O_CREAT
|O_TRUNC
|O_RDWR
, 000);
498 fprintf(stderr
, "Could not create journal info block file on volume %s (%s)\n",
499 volname
, strerror(errno
));
500 unlink(journal_fname
);
504 // swap the data before we copy it
505 jib
.flags
= OSSwapBigToHostInt32(jib
.flags
);
506 jib
.offset
= OSSwapBigToHostInt64(jib
.offset
);
507 jib
.size
= OSSwapBigToHostInt64(jib
.size
);
509 memcpy(buf
, &jib
, sizeof(jib
));
511 // now put it back the way it was
512 jib
.size
= OSSwapBigToHostInt64(jib
.size
);
513 jib
.offset
= OSSwapBigToHostInt64(jib
.offset
);
514 jib
.flags
= OSSwapBigToHostInt32(jib
.flags
);
516 if (write(fd
, buf
, block_size
) != block_size
) {
517 fprintf(stderr
, "Failed to write journal info block on volume %s (%s)!\n",
518 volname
, strerror(errno
));
519 unlink(journal_fname
);
525 hide_file(jib_fname
);
527 start_block
= get_start_block(jib_fname
, block_size
);
528 if (start_block
== (off_t
)-1) {
529 fprintf(stderr
, "Failed to get start block for %s (%s)\n",
530 jib_fname
, strerror(errno
));
531 unlink(journal_fname
);
535 jinfo_block
= (start_block
/ block_size
) - (embedded_offset
/ block_size
);
539 // Now make the volume journaled!
541 memset(sysctl_info
, 0, sizeof(sysctl_info
));
542 sysctl_info
[0] = CTL_VFS
;
543 sysctl_info
[1] = sfs
.f_fsid
.val
[1];
544 sysctl_info
[2] = HFS_ENABLE_JOURNALING
;
545 sysctl_info
[3] = jinfo_block
;
546 sysctl_info
[4] = jstart_block
;
547 sysctl_info
[5] = journal_size
;
549 //printf("fs type: 0x%x\n", sysctl_info[1]);
550 //printf("jinfo block : 0x%x\n", jinfo_block);
551 //printf("jstart block: 0x%x\n", jstart_block);
552 //printf("journal size: 0x%x\n", journal_size);
554 ret
= sysctl((void *)sysctl_info
, 6, NULL
, NULL
, NULL
, 0);
556 fprintf(stderr
, "Failed to make volume %s journaled (%s)\n",
557 volname
, strerror(errno
));
558 unlink(journal_fname
);
568 DoUnJournal(char *volname
)
573 char jbuf
[MAXPATHLEN
];
575 if (statfs(volname
, &sfs
) != 0) {
576 fprintf(stderr
, "Can't stat volume %s (%s).\n", volname
, strerror(errno
));
580 if (strcmp(sfs
.f_fstypename
, "hfs") != 0) {
581 fprintf(stderr
, "Volume %s (%s) is not a HFS volume.\n", volname
, sfs
.f_mntfromname
);
585 if ((sfs
.f_flags
& MNT_JOURNALED
) == 0) {
586 fprintf(stderr
, "Volume %s (%s) is not journaled.\n", volname
, sfs
.f_mntfromname
);
590 if (chdir(volname
) != 0) {
591 fprintf(stderr
, "Can't locate volume %s to turn off journaling (%s).\n",
592 volname
, strerror(errno
));
596 memset(sysctl_info
, 0, sizeof(sysctl_info
));
597 sysctl_info
[0] = CTL_VFS
;
598 sysctl_info
[1] = sfs
.f_fsid
.val
[1];
599 sysctl_info
[2] = HFS_DISABLE_JOURNALING
;
601 result
= sysctl((void *)sysctl_info
, 3, NULL
, NULL
, NULL
, 0);
603 fprintf(stderr
, "Failed to make volume %s UN-journaled (%s)\n",
604 volname
, strerror(errno
));
608 snprintf(jbuf
, sizeof(jbuf
), "%s/%s", volname
, journal_fname
);
609 if (unlink(jbuf
) != 0) {
610 fprintf(stderr
, "Failed to remove the journal %s (%s)\n",
611 jbuf
, strerror(errno
));
614 snprintf(jbuf
, sizeof(jbuf
), "%s/%s", volname
, jib_fname
);
615 if (unlink(jbuf
) != 0) {
616 fprintf(stderr
, "Failed to remove the journal info block %s (%s)\n",
617 jbuf
, strerror(errno
));
620 printf("Journaling disabled on %s mounted at %s.\n", sfs
.f_mntfromname
, volname
);
629 get_journal_info(char *devname
, struct JournalInfoBlock
*jib
)
631 int fd
= -1, ret
= 0;
632 char *buff
= NULL
, *buff2
= NULL
;
637 HFSMasterDirectoryBlock
*mdbp
;
638 HFSPlusVolumeHeader
*vhp
;
639 off_t embeddedOffset
, pos
;
640 struct JournalInfoBlock
*myjib
;
642 fd
= open(devname
, O_RDONLY
);
644 printf("can't open: %s (%s)\n", devname
, strerror(errno
));
649 /* Get the real physical block size. */
650 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
651 printf("can't get the device block size (%s). assuming 512\n", strerror(errno
));
657 /* Get the number of physical blocks. */
658 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
660 printf("failed to get block count. trying stat().\n");
661 if (fstat(fd
, &st
) != 0) {
666 blkcnt
= st
.st_size
/ blksize
;
669 /* Compute an accurate disk size */
670 disksize
= blkcnt
* (u_int64_t
)blksize
;
673 * There are only 31 bits worth of block count in
674 * the buffer cache. So for large volumes a 4K
675 * physical block size is needed.
677 if (blksize
== 512 && blkcnt
> (u_int64_t
)0x000000007fffffff) {
683 * blksize has our prefered physical block size
684 * blkcnt has the total number of physical blocks
687 buff
= (char *)malloc(blksize
);
688 buff2
= (char *)malloc(blksize
);
690 if (pread(fd
, buff
, blksize
, HFS_PRI_SECTOR(blksize
)*blksize
) != blksize
) {
691 printf("failed to read volume header @ offset %d (%s)\n",
692 HFS_PRI_SECTOR(blksize
), strerror(errno
));
697 if (blksize
== 512) {
698 mdbp
= (HFSMasterDirectoryBlock
*)buff
;
700 mdbp
= (HFSMasterDirectoryBlock
*)(buff
+ 1024);
703 mdbp
->drSigWord
= SWAP_BE16(mdbp
->drSigWord
);
704 mdbp
->drEmbedSigWord
= SWAP_BE16(mdbp
->drEmbedSigWord
);
705 mdbp
->drAlBlSt
= SWAP_BE16(mdbp
->drAlBlSt
);
706 mdbp
->drEmbedExtent
.startBlock
= SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
);
707 mdbp
->drAlBlkSiz
= SWAP_BE32(mdbp
->drAlBlkSiz
);
708 mdbp
->drEmbedExtent
.blockCount
= SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
);
710 // first check if it's even hfs at all...
711 if ( mdbp
->drSigWord
!= kHFSSigWord
712 && mdbp
->drSigWord
!= kHFSPlusSigWord
713 && mdbp
->drSigWord
!= kHFSXSigWord
) {
719 if ((mdbp
->drSigWord
== kHFSSigWord
) && (mdbp
->drEmbedSigWord
!= kHFSPlusSigWord
)) {
720 // normal hfs can not ever be journaled
724 /* Get the embedded Volume Header */
725 if (mdbp
->drEmbedSigWord
== kHFSPlusSigWord
) {
726 embeddedOffset
= mdbp
->drAlBlSt
* 512;
727 embeddedOffset
+= (u_int64_t
)mdbp
->drEmbedExtent
.startBlock
*
728 (u_int64_t
)mdbp
->drAlBlkSiz
;
731 * If the embedded volume doesn't start on a block
732 * boundary, then switch the device to a 512-byte
733 * block size so everything will line up on a block
736 if ((embeddedOffset
% blksize
) != 0) {
737 printf("HFS Mount: embedded volume offset not"
738 " a multiple of physical block size (%d);"
739 " switching to 512\n", blksize
);
741 blkcnt
*= (blksize
/ 512);
745 disksize
= (u_int64_t
)mdbp
->drEmbedExtent
.blockCount
*
746 (u_int64_t
)mdbp
->drAlBlkSiz
;
748 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
749 if (pread(fd
, buff
, blksize
, mdb_offset
* blksize
) != blksize
) {
750 printf("failed to read the embedded vhp @ offset %d\n", mdb_offset
* blksize
);
755 vhp
= (HFSPlusVolumeHeader
*) buff
;
756 } else /* pure HFS+ */ {
758 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
761 if ((SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
) == 0) {
767 // Now read the journal info block... (when calculating the
768 // position, make sure to cast to unsigned first, then to
769 // off_t so that things don't get sign-extended improperly
772 pos
= (off_t
)((off_t
)embeddedOffset
+
773 (off_t
)((unsigned int)SWAP_BE32(vhp
->journalInfoBlock
))*(off_t
)((unsigned int)SWAP_BE32(vhp
->blockSize
)));
775 if (pread(fd
, buff2
, blksize
, pos
) != blksize
) {
776 printf("failed to read the journal info block (%s).\n", strerror(errno
));
781 myjib
= (struct JournalInfoBlock
*)buff2
;
782 myjib
->flags
= SWAP_BE32(myjib
->flags
);
783 myjib
->offset
= SWAP_BE64(myjib
->offset
);
784 myjib
->size
= SWAP_BE64(myjib
->size
);
786 memcpy(jib
, myjib
, sizeof(*myjib
));
803 DoGetJournalInfo(char *volname
)
806 struct hfs_journal_info jinfo
;
808 if (strncmp(volname
, "/dev/", 5) == 0 || strncmp(volname
, "disk", 4) == 0 || strncmp(volname
, "rdisk", 5) == 0) {
809 struct JournalInfoBlock jib
;
811 char fulldevname
[256];
813 if (strncmp(volname
, "disk", 4) == 0 || strncmp(volname
, "rdisk", 5) == 0) {
814 snprintf(fulldevname
, sizeof(fulldevname
), "/dev/%s", volname
);
815 volname
= &fulldevname
[0];
818 // try the name as a device name...
819 ret
= get_journal_info(volname
, &jib
);
821 printf("Volume %s is not journaled.\n", volname
);
823 } else if (ret
< 0) {
824 printf("Volume %s does not appear to be an HFS+ volume.\n", volname
);
827 if (jib
.flags
& kJIJournalInFSMask
) {
828 printf("%s : journal size %lld k at offset 0x%llx\n", volname
, jib
.size
/1024, jib
.offset
);
830 printf("%s: external journal stored on partition with uuid %s on machine w/serial number %s\n",
831 volname
, jib
.ext_jnl_uuid
, jib
.machine_serial_num
);
839 if (statfs(volname
, &sfs
) != 0) {
840 fprintf(stderr
, "Unable to get fs info for %s\n", volname
);
844 if ((sfs
.f_flags
& MNT_JOURNALED
) == 0) {
845 fprintf(stderr
, "Volume %s is not journaled.\n", volname
);
849 if (fsctl(volname
, HFSIOC_GET_JOURNAL_INFO
, &jinfo
, 0) != 0) {
850 fprintf(stderr
, "Failed to get journal info for volume %s (%s)\n",
851 volname
, strerror(errno
));
855 if (jinfo
.jstart
== 0) {
858 snprintf(rawdev
, sizeof(rawdev
), "/dev/r%s", &sfs
.f_mntfromname
[5]);
860 // it's an external journal so get the info the
862 return DoGetJournalInfo(&rawdev
[0]);
865 if (jinfo
.jsize
== 0) {
866 printf("%s : not journaled.\n", volname
);
868 printf("%s : journal size %lld k at offset 0x%llx\n", volname
, jinfo
.jsize
/1024, jinfo
.jstart
);
876 RawDisableJournaling(char *devname
)
878 int fd
= -1, ret
= 0;
879 char *buff
= NULL
, rawdev
[256], unrawdev
[256];
884 HFSMasterDirectoryBlock
*mdbp
;
885 HFSPlusVolumeHeader
*vhp
;
886 off_t embeddedOffset
, hdr_offset
;
888 struct statfs
*fsinfo
;
890 // assume that the name provided is a raw device name
891 strlcpy(rawdev
, devname
, sizeof(rawdev
));
894 if (stat(rawdev
, &st
) != 0) {
895 fprintf(stderr
, "Could not access %s (%s)\n", devname
, strerror(errno
));
900 if (S_ISCHR(st
.st_mode
) == 0) {
901 if (S_ISBLK(st
.st_mode
)) {
902 // this is a block device, convert the name to
903 // raw character device and try again
904 snprintf(rawdev
, sizeof(rawdev
), "/dev/r%s", devname
+ 5);
907 // probably it is a mount point
908 return DoUnJournal(devname
);
911 // convert the character raw device name to
912 // block device name to compare with getmntinfo output
913 snprintf(unrawdev
, sizeof(unrawdev
), "/dev/%s", rawdev
+ 6);
916 // make sure that the file system on the device node is not mounted
917 ret
= getmntinfo(&fsinfo
, MNT_NOWAIT
);
919 fprintf (stderr
, "Error getting list of mounted filesystems\n");
925 // the file system on this device node is currently mounted
926 if (strcmp(unrawdev
, fsinfo
[ret
].f_mntfromname
) == 0) {
927 return DoUnJournal(fsinfo
[ret
].f_mntonname
);
931 fd
= open(rawdev
, O_RDWR
);
933 fprintf(stderr
, "can't open: %s (%s)\n", devname
, strerror(errno
));
938 /* Get the real physical block size. */
939 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
940 fprintf(stderr
, "can't get the device block size (%s). assuming 512\n", strerror(errno
));
946 /* Get the number of physical blocks. */
947 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
950 if (fstat(fd
, &st
) != 0) {
955 blkcnt
= st
.st_size
/ blksize
;
958 /* Compute an accurate disk size */
959 disksize
= blkcnt
* (u_int64_t
)blksize
;
962 * There are only 31 bits worth of block count in
963 * the buffer cache. So for large volumes a 4K
964 * physical block size is needed.
966 if (blksize
== 512 && blkcnt
> (u_int64_t
)0x000000007fffffff) {
972 * blksize has our prefered physical block size
973 * blkcnt has the total number of physical blocks
976 buff
= (char *)malloc(blksize
);
978 hdr_offset
= HFS_PRI_SECTOR(blksize
)*blksize
;
979 if (pread(fd
, buff
, blksize
, hdr_offset
) != blksize
) {
980 fprintf(stderr
, "failed to read volume header @ offset %lld (%s)\n",
981 hdr_offset
, strerror(errno
));
986 // if we read in an empty bunch of junk at location zero, then
987 // retry at offset 0x400 which is where the header normally is.
988 if (*(int *)buff
== 0 && hdr_offset
== 0) {
990 if (pread(fd
, buff
, blksize
, hdr_offset
) != blksize
) {
991 fprintf(stderr
, "failed to read volume header @ offset %lld (%s)\n",
992 hdr_offset
, strerror(errno
));
1000 mdbp
= (HFSMasterDirectoryBlock
*)buff
;
1001 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) && (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
)) {
1002 // normal hfs can not ever be journaled
1003 fprintf(stderr
, "disable_journaling: volume is only regular HFS, not HFS+\n");
1007 /* Get the embedded Volume Header */
1008 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1009 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * 512;
1010 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) * (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1013 * If the embedded volume doesn't start on a block
1014 * boundary, then switch the device to a 512-byte
1015 * block size so everything will line up on a block
1018 if ((embeddedOffset
% blksize
) != 0) {
1019 fprintf(stderr
, "HFS Mount: embedded volume offset not"
1020 " a multiple of physical block size (%d);"
1021 " switching to 512\n", blksize
);
1023 blkcnt
*= (blksize
/ 512);
1027 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) * (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1029 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1030 hdr_offset
= mdb_offset
* blksize
;
1031 if (pread(fd
, buff
, blksize
, hdr_offset
) != blksize
) {
1032 fprintf(stderr
, "failed to read the embedded vhp @ offset %d\n", mdb_offset
* blksize
);
1037 vhp
= (HFSPlusVolumeHeader
*) buff
;
1038 } else /* pure HFS+ */ {
1040 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1044 if ((SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
) != 0) {
1045 unsigned int tmp
= SWAP_BE32(vhp
->attributes
);
1047 tmp
&= ~kHFSVolumeJournaledMask
;
1048 vhp
->attributes
= SWAP_BE32(tmp
);
1049 if ((tmp
= pwrite(fd
, buff
, blksize
, hdr_offset
)) != blksize
) {
1050 fprintf(stderr
, "Update of super-block on %s failed! (%d != %d, %s)\n",
1051 devname
, tmp
, blksize
, strerror(errno
));
1053 fprintf(stderr
, "Turned off the journaling bit for %s\n", devname
);
1056 fprintf(stderr
, "disable_journaling: %s is not journaled.\n", devname
);
1072 SetJournalInFSState(const char *devname
, int journal_in_fs
)
1074 int fd
= -1, ret
= 0;
1075 char *buff
= NULL
, *buff2
= NULL
;
1079 HFSMasterDirectoryBlock
*mdbp
;
1080 HFSPlusVolumeHeader
*vhp
;
1081 off_t embeddedOffset
, pos
;
1082 struct JournalInfoBlock
*myjib
;
1084 fd
= open(devname
, O_RDWR
);
1086 printf("can't open: %s (%s)\n", devname
, strerror(errno
));
1091 /* Get the real physical block size. */
1092 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
) != 0) {
1093 printf("can't get the device block size (%s). assuming 512\n", strerror(errno
));
1099 /* Get the number of physical blocks. */
1100 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
)) {
1102 printf("failed to get block count. trying stat().\n");
1103 if (fstat(fd
, &st
) != 0) {
1108 blkcnt
= st
.st_size
/ blksize
;
1112 * There used to only be 31 bits worth of block count in
1113 * the buffer cache. So for large volumes a 4K
1114 * physical block size is needed.
1116 if (blksize
== 512 && blkcnt
> (u_int64_t
)0x000000007fffffff) {
1122 * blksize has our prefered physical block size
1123 * blkcnt has the total number of physical blocks
1126 buff
= (char *)malloc(blksize
);
1127 buff2
= (char *)malloc(blksize
);
1129 if (pread(fd
, buff
, blksize
, HFS_PRI_SECTOR(blksize
)*blksize
) != blksize
) {
1130 printf("failed to read volume header @ offset %d (%s)\n",
1131 HFS_PRI_SECTOR(blksize
), strerror(errno
));
1136 if (blksize
== 512) {
1137 mdbp
= (HFSMasterDirectoryBlock
*)buff
;
1139 mdbp
= (HFSMasterDirectoryBlock
*)(buff
+ 1024);
1142 // first check if it's even hfs at all...
1143 if ( SWAP_BE16(mdbp
->drSigWord
) != kHFSSigWord
1144 && SWAP_BE16(mdbp
->drSigWord
) != kHFSPlusSigWord
1145 && SWAP_BE16(mdbp
->drSigWord
) != kHFSXSigWord
) {
1151 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) && (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
)) {
1152 // normal hfs can not ever be journaled
1156 /* Get the embedded Volume Header */
1157 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1158 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * 512;
1159 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1160 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1163 * If the embedded volume doesn't start on a block
1164 * boundary, then switch the device to a 512-byte
1165 * block size so everything will line up on a block
1168 if ((embeddedOffset
% blksize
) != 0) {
1169 printf("HFS Mount: embedded volume offset not"
1170 " a multiple of physical block size (%d);"
1171 " switching to 512\n", blksize
);
1173 blkcnt
*= (blksize
/ 512);
1177 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1178 if (pread(fd
, buff
, blksize
, mdb_offset
* blksize
) != blksize
) {
1179 printf("failed to read the embedded vhp @ offset %d\n", mdb_offset
* blksize
);
1184 vhp
= (HFSPlusVolumeHeader
*) buff
;
1185 } else /* pure HFS+ */ {
1187 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1190 //printf("vol header attributes: 0x%x\n", SWAP_BE32(vhp->attributes));
1191 if ((SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
) == 0) {
1197 // Now read the journal info block... (when calculating the
1198 // position, make sure to cast to unsigned first, then to
1199 // off_t so that things don't get sign-extended improperly
1202 pos
= (off_t
)((off_t
)embeddedOffset
+
1203 (off_t
)((unsigned int )SWAP_BE32(vhp
->journalInfoBlock
))*(off_t
)((unsigned int)SWAP_BE32(vhp
->blockSize
)));
1205 if (pread(fd
, buff2
, blksize
, pos
) != blksize
) {
1206 printf("failed to read the journal info block (%s).\n", strerror(errno
));
1211 myjib
= (struct JournalInfoBlock
*)buff2
;
1213 // swap this to host native format so we can diddle with the bits
1214 myjib
->flags
= SWAP_BE32(myjib
->flags
);
1216 if (journal_in_fs
) {
1217 myjib
->flags
|= kJIJournalInFSMask
;
1219 myjib
->flags
&= ~kJIJournalInFSMask
;
1221 myjib
->flags
|= kJIJournalNeedInitMask
;
1223 // and now swap back before writing it out
1224 myjib
->flags
= SWAP_BE32(myjib
->flags
);
1226 if (pwrite(fd
, buff2
, blksize
, pos
) != blksize
) {
1227 printf("failed to re-write the journal info block (%s).\n", strerror(errno
));