]> git.saurik.com Git - apple/hfs.git/blob - hfs_util/hfsutil_jnl.c
hfs-522.0.9.tar.gz
[apple/hfs.git] / hfs_util / hfsutil_jnl.c
1 /*
2 * Copyright (c) 1999-2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 Copyright (c) 2002 Apple Computer, Inc.
24 All Rights Reserved.
25
26 This file contains the routine to make an HFS+ volume journaled
27 and a corresponding routine to turn it off.
28
29 */
30
31 #include <sys/types.h>
32 #include <sys/attr.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/sysctl.h>
36 #include <sys/resource.h>
37 #include <sys/vmmeter.h>
38 #include <sys/mount.h>
39 #include <sys/wait.h>
40 #include <sys/ioctl.h>
41
42 #include <sys/disk.h>
43 #include <sys/loadable_fs.h>
44 #include <hfs/hfs_format.h>
45 #include <hfs/hfs_mount.h> /* for hfs sysctl values */
46
47 #include "../core/hfs_fsctl.h"
48
49 #include <System/sys/content_protection.h>
50 #include <TargetConditionals.h>
51
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <libgen.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include <architecture/byte_order.h>
62
63 // just in case these aren't in <hfs/hfs_mount.h> yet
64 #ifndef HFS_ENABLE_JOURNALING
65 #define HFS_ENABLE_JOURNALING 0x082969
66 #endif
67 #ifndef HFS_DISABLE_JOURNALING
68 #define HFS_DISABLE_JOURNALING 0x031272
69 #endif
70 #ifndef HFS_GET_JOURNAL_INFO
71 #define HFS_GET_JOURNAL_INFO 0x6a6e6c69
72 #endif
73
74 /* getattrlist buffers start with an extra length field */
75 struct ExtentsAttrBuf {
76 unsigned long infoLength;
77 HFSPlusExtentRecord extents;
78 };
79 typedef struct ExtentsAttrBuf ExtentsAttrBuf;
80
81 #ifndef HFSIOC_GET_JOURNAL_INFO
82 # include <sys/ioctl.h>
83 struct hfs_journal_info {
84 off_t jstart;
85 off_t jsize;
86 };
87 # define HFSIOC_GET_JOURNAL_INFO _IOR('h', 17, struct hfs_journal_info)
88 #endif
89
90
91 #define kIsInvisible 0x4000
92
93 /*
94 * Generic Finder file/dir data
95 */
96 struct FinderInfo {
97 u_int32_t opaque_1[2];
98 u_int16_t fdFlags; /* Finder flags */
99 int16_t opaque_2[11];
100 };
101 typedef struct FinderInfo FinderInfo;
102
103 /* getattrlist buffers start with an extra length field */
104 struct FinderAttrBuf {
105 unsigned long infoLength;
106 FinderInfo finderInfo;
107 };
108 typedef struct FinderAttrBuf FinderAttrBuf;
109
110
111 int hide_file(const char * file)
112 {
113 struct attrlist alist = {0};
114 FinderAttrBuf finderInfoBuf = {0};
115 int result;
116
117 alist.bitmapcount = ATTR_BIT_MAP_COUNT;
118 alist.commonattr = ATTR_CMN_FNDRINFO;
119
120 result = getattrlist(file, &alist, &finderInfoBuf, sizeof(finderInfoBuf), 0);
121 if (result) {
122 return (errno);
123 }
124
125 if (finderInfoBuf.finderInfo.fdFlags & kIsInvisible) {
126 printf("hide: %s is alreadly invisible\n", file);
127 return (0);
128 }
129
130 finderInfoBuf.finderInfo.fdFlags |= kIsInvisible;
131
132 result = setattrlist(file, &alist, &finderInfoBuf.finderInfo, sizeof(FinderInfo), 0);
133
134 return (result == -1 ? errno : result);
135 }
136
137 off_t
138 get_start_block(const char *file, uint32_t fs_block_size)
139 {
140 off_t cur_pos, phys_start, len;
141 int fd, err;
142 struct log2phys l2p;
143 struct stat st;
144
145 fd = open(file, O_RDONLY);
146 if (fd < 0) {
147 return -1;
148 }
149
150 if (fstat(fd, &st) < 0) {
151 fprintf(stderr, "can't stat %s (%s)\n", file, strerror(errno));
152 close(fd);
153 return -1;
154 }
155
156 fs_block_size = st.st_blksize; // XXXdbg quick hack for now
157
158 phys_start = len = 0;
159 for(cur_pos=0; cur_pos < st.st_size; cur_pos += fs_block_size) {
160 memset(&l2p, 0, sizeof(l2p));
161 lseek(fd, cur_pos, SEEK_SET);
162 err = fcntl(fd, F_LOG2PHYS, &l2p);
163
164 if (phys_start == 0) {
165 phys_start = l2p.l2p_devoffset;
166 len = fs_block_size;
167 } else if (l2p.l2p_devoffset != (phys_start + len)) {
168 // printf(" %lld : %lld - %lld\n", cur_pos, phys_start / fs_block_size, len / fs_block_size);
169 fprintf(stderr, "%s : is not contiguous!\n", file);
170 close(fd);
171 return -1;
172 // phys_start = l2p.l2p_devoffset;
173 // len = fs_block_size;
174 } else {
175 len += fs_block_size;
176 }
177 }
178
179 close(fd);
180
181 //printf("%s start offset %lld; byte len %lld (blksize %d)\n",
182 // file, phys_start, len, fs_block_size);
183
184 if ((phys_start / (unsigned int)fs_block_size) & 0xffffffff00000000LL) {
185 fprintf(stderr, "%s : starting block is > 32bits!\n", file);
186 return -1;
187 }
188
189 return phys_start;
190 }
191
192
193 //
194 // Get the embedded offset (if any) for an hfs+ volume.
195 // This is pretty skanky that we have to do this but
196 // that's life...
197 //
198 #include <sys/disk.h>
199 #include <hfs/hfs_format.h>
200
201 #include <machine/endian.h>
202
203 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
204 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
205
206 #include <libkern/OSByteOrder.h>
207
208 #define SWAP_BE16(x) ntohs(x)
209 #define SWAP_BE32(x) ntohl(x)
210 #define SWAP_BE64(x) OSSwapConstInt64(x)
211
212
213 off_t
214 get_embedded_offset(char *devname)
215 {
216 int fd = -1;
217 off_t ret = 0;
218 char *buff = NULL, rawdev[256];
219 u_int64_t blkcnt;
220 u_int32_t blksize;
221 HFSMasterDirectoryBlock *mdbp;
222 off_t embeddedOffset;
223 struct statfs sfs;
224 struct stat st;
225
226 restart:
227 if (stat(devname, &st) != 0) {
228 fprintf(stderr, "Could not access %s (%s)\n", devname, strerror(errno));
229 ret = -1;
230 goto out;
231 }
232
233 if (S_ISCHR(st.st_mode) == 0) {
234 // hmmm, it's not the character special raw device so we
235 // should try to figure out the real device.
236 if (statfs(devname, &sfs) != 0) {
237 fprintf(stderr, "Can't find out any info about the fs for path %s (%s)\n",
238 devname, strerror(errno));
239 ret = -1;
240 goto out;
241 }
242
243 // This assumes it begins with "/dev/". The old code assumed
244 // it began with five characters. Should probably use strrchr or equivalent.
245 snprintf(rawdev, sizeof(rawdev), "/dev/r%s", &sfs.f_mntfromname[5]);
246 devname = &rawdev[0];
247 goto restart;
248 }
249
250 fd = open(devname, O_RDONLY);
251 if (fd < 0) {
252 fprintf(stderr, "can't open: %s (%s)\n", devname, strerror(errno));
253 ret = -1;
254 goto out;
255 }
256
257 /* Get the real physical block size. */
258 if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) {
259 fprintf(stderr, "can't get the device block size (%s). assuming 512\n", strerror(errno));
260 blksize = 512;
261 ret = -1;
262 goto out;
263 }
264
265 /* Get the number of physical blocks. */
266 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) {
267 struct stat st;
268 fprintf(stderr, "failed to get block count. trying stat().\n");
269 if (fstat(fd, &st) != 0) {
270 ret = -1;
271 goto out;
272 }
273
274 blkcnt = st.st_size / blksize;
275 }
276
277 /*
278 * At this point:
279 * blksize has our prefered physical block size
280 * blkcnt has the total number of physical blocks
281 */
282
283 buff = (char *)malloc(blksize);
284
285 if (pread(fd, buff, blksize, HFS_PRI_SECTOR(blksize)*blksize) != blksize) {
286 fprintf(stderr, "failed to read volume header @ offset %d (%s)\n",
287 HFS_PRI_SECTOR(blksize), strerror(errno));
288 ret = -1;
289 goto out;
290 }
291
292 mdbp = (HFSMasterDirectoryBlock *)(buff + HFS_PRI_OFFSET(blksize));
293 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
294 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
295 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
296 printf ("get_embedded_offset: invalid volume signature \n");
297 ret = -1;
298 goto out;
299 }
300
301 if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) {
302 ret = -1;
303 goto out;
304 } else if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) {
305 /* Get the embedded Volume Header */
306 embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * 512;
307 embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) *
308 (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
309
310 /*
311 * If the embedded volume doesn't start on a block
312 * boundary, then switch the device to a 512-byte
313 * block size so everything will line up on a block
314 * boundary.
315 */
316 if ((embeddedOffset % blksize) != 0) {
317 fprintf(stderr, "HFS Mount: embedded volume offset not"
318 " a multiple of physical block size (%d);"
319 " switching to 512\n", blksize);
320
321 blkcnt *= (blksize / 512);
322 blksize = 512;
323 }
324
325 } else { /* pure HFS+ */
326 embeddedOffset = 0;
327 }
328
329 ret = embeddedOffset;
330
331 out:
332 if (buff) {
333 free(buff);
334 }
335 if (fd >= 0)
336 close(fd);
337
338 return ret;
339 }
340
341
342
343 static const char *journal_fname = ".journal";
344 static const char *jib_fname = ".journal_info_block";
345
346 int
347 DoMakeJournaled(char *volname, int jsize) {
348 int fd, i, block_size, journal_size = 8*1024*1024;
349 char *buf = NULL;
350 int ret;
351 fstore_t fst;
352 int32_t jstart_block, jinfo_block;
353 int sysctl_info[8];
354 JournalInfoBlock jib;
355 struct statfs sfs;
356 static char tmpname[MAXPATHLEN];
357 off_t start_block, embedded_offset;
358
359 if (statfs(volname, &sfs) != 0) {
360 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
361 return 10;
362 }
363
364 // Make sure that we're HFS+. First we check the fstypename.
365 // If that's ok then we try to create a symlink (which won't
366 // work on plain hfs volumes but will work on hfs+ volumes).
367 //
368 if (strcmp(sfs.f_fstypename, "devfs") == 0) {
369 fprintf (stderr, "%s is a device node. Journal enable only works on a mounted HFS+ volume.\n", volname);
370 return 10;
371 }
372 snprintf(tmpname, sizeof(tmpname), "%s/is_vol_hfs_plus", volname);
373 if (strcmp(sfs.f_fstypename, "hfs") != 0 ||
374 ((ret = symlink(tmpname, tmpname)) != 0 && errno == ENOTSUP)) {
375 fprintf(stderr, "%s is not an HFS+ volume. Journaling only works on HFS+ volumes.\n",
376 volname);
377 return 10;
378 }
379 unlink(tmpname);
380
381 if (sfs.f_flags & MNT_JOURNALED) {
382 fprintf(stderr, "Volume %s is already journaled.\n", volname);
383 return 1;
384 }
385
386 if (jsize != 0) {
387 journal_size = jsize;
388 } else {
389 int scale;
390
391 //
392 // we want at least 8 megs of journal for each 100 gigs of
393 // disk space. We cap the size at 512 megs though.
394 //
395 scale = ((long long)sfs.f_bsize * (long long)((unsigned int)sfs.f_blocks)) / (100*1024*1024*1024ULL);
396 journal_size *= (scale + 1);
397 if (journal_size > 512 * 1024 * 1024) {
398 journal_size = 512 * 1024 * 1024;
399 }
400 }
401
402 if (chdir(volname) != 0) {
403 fprintf(stderr, "Can't locate volume %s to make it journaled (%s).\n",
404 volname, strerror(errno));
405 return 10;
406 }
407
408
409 embedded_offset = get_embedded_offset(volname);
410 if (embedded_offset < 0) {
411 fprintf(stderr, "Can't calculate the embedded offset (if any) for %s.\n", volname);
412 fprintf(stderr, "Journal creation failure.\n");
413 return 15;
414 }
415 // printf("Embedded offset == 0x%llx\n", embedded_offset);
416
417 /*
418 * Must use open_dprotected_np to create a class D file. This will
419 * be the same as standard open(2) on systems that do not support content protection
420 */
421 fd = open_dprotected_np (journal_fname, O_CREAT|O_TRUNC|O_RDWR, PROTECTION_CLASS_D, 0, 000);
422 if (fd < 0) {
423 fprintf(stderr, "Can't create journal file on volume %s (%s)\n",
424 volname, strerror(errno));
425 return 5;
426 }
427
428 if (fcntl(fd, F_NOCACHE, 1)) {
429 fprintf(stderr, "Can't create journal file (NC) on volume %s (%s)\n",
430 volname, strerror(errno));
431 return 5;
432 }
433
434
435 // make sure that it has no r/w/x privs (only could happen if
436 // the file already existed since open() doesn't reset the mode
437 // bits).
438 //
439 fchmod(fd, 0);
440
441 block_size = sfs.f_bsize;
442 if ((journal_size % block_size) != 0) {
443 fprintf(stderr, "Journal size %dk is not a multiple of volume %s block size (%d).\n",
444 journal_size/1024, volname, block_size);
445 close(fd);
446 unlink(journal_fname);
447 return 5;
448 }
449
450 retry:
451 memset(&fst, 0, sizeof(fst));
452 fst.fst_flags = F_ALLOCATECONTIG|F_ALLOCATEALL;
453 fst.fst_length = journal_size;
454 fst.fst_posmode = F_PEOFPOSMODE;
455
456 ret = fcntl(fd, F_PREALLOCATE, &fst);
457 if (ret < 0) {
458 if (journal_size >= 2*1024*1024) {
459 fprintf(stderr, "Not enough contiguous space for a %d k journal. Retrying.\n",
460 journal_size/1024);
461 journal_size /= 2;
462 ftruncate(fd, 0); // make sure the file is zero bytes long.
463 goto retry;
464 } else {
465 fprintf(stderr, "Disk too fragmented to enable journaling.\n");
466 fprintf(stderr, "Please run a defragmenter on %s.\n", volname);
467 close(fd);
468 unlink(journal_fname);
469 return 10;
470 }
471 }
472
473 printf("Allocated %lldK for journal file.\n", fst.fst_bytesalloc/1024LL);
474 buf = (char *)calloc(block_size, 1);
475 if (buf) {
476 for(i=0; i < journal_size/block_size; i++) {
477 ret = write(fd, buf, block_size);
478 if (ret != block_size) {
479 break;
480 }
481 }
482
483 if (i*block_size != journal_size) {
484 fprintf(stderr, "Failed to write %dk to journal on volume %s (%s)\n",
485 journal_size/1024, volname, strerror(errno));
486 }
487 } else {
488 printf("Could not allocate memory to write to the journal on volume %s (%s)\n",
489 volname, strerror(errno));
490 }
491
492 fsync(fd);
493 close(fd);
494 hide_file(journal_fname);
495
496 start_block = get_start_block(journal_fname, block_size);
497 if (start_block == (off_t)-1) {
498 fprintf(stderr, "Failed to get start block for %s (%s)\n",
499 journal_fname, strerror(errno));
500 unlink(journal_fname);
501 if (buf)
502 free(buf);
503 return 20;
504 }
505 jstart_block = (start_block / block_size) - (embedded_offset / block_size);
506
507 memset(&jib, 'Z', sizeof(jib));
508 jib.flags = kJIJournalInFSMask;
509 jib.offset = (off_t)((unsigned int)jstart_block) * (off_t)((unsigned int)block_size);
510 jib.size = (off_t)((unsigned int)journal_size);
511
512 /*
513 * Use open_dprotected_np to create JIB as a class D file. This will
514 * behave the same as a standard open(2) on systems that do not support content protection
515 */
516 fd = open_dprotected_np(jib_fname, O_CREAT|O_TRUNC|O_RDWR, PROTECTION_CLASS_D, 0, 000);
517 if (fd < 0) {
518 fprintf(stderr, "Could not create journal info block file on volume %s (%s)\n",
519 volname, strerror(errno));
520 unlink(journal_fname);
521 if (buf)
522 free(buf);
523 return 5;
524 }
525
526 if (fcntl(fd, F_NOCACHE, 1)) {
527 fprintf(stderr, "Could not create journal info block (NC) file on volume %s (%s)\n",
528 volname, strerror(errno));
529 if (buf)
530 free(buf);
531 return 5;
532 }
533
534 // swap the data before we copy it
535 jib.flags = OSSwapBigToHostInt32(jib.flags);
536 jib.offset = OSSwapBigToHostInt64(jib.offset);
537 jib.size = OSSwapBigToHostInt64(jib.size);
538
539 memcpy(buf, &jib, sizeof(jib));
540
541 // now put it back the way it was
542 jib.size = OSSwapBigToHostInt64(jib.size);
543 jib.offset = OSSwapBigToHostInt64(jib.offset);
544 jib.flags = OSSwapBigToHostInt32(jib.flags);
545
546 if (write(fd, buf, block_size) != block_size) {
547 fprintf(stderr, "Failed to write journal info block on volume %s (%s)!\n",
548 volname, strerror(errno));
549 unlink(journal_fname);
550 if (buf)
551 free(buf);
552 return 10;
553 }
554
555 fsync(fd);
556 close(fd);
557 hide_file(jib_fname);
558
559 start_block = get_start_block(jib_fname, block_size);
560 if (start_block == (off_t)-1) {
561 fprintf(stderr, "Failed to get start block for %s (%s)\n",
562 jib_fname, strerror(errno));
563 unlink(journal_fname);
564 unlink(jib_fname);
565 if (buf)
566 free(buf);
567 return 20;
568 }
569 jinfo_block = (start_block / block_size) - (embedded_offset / block_size);
570
571
572 //
573 // Now make the volume journaled!
574 //
575 memset(sysctl_info, 0, sizeof(sysctl_info));
576 sysctl_info[0] = CTL_VFS;
577 sysctl_info[1] = sfs.f_fsid.val[1];
578 sysctl_info[2] = HFS_ENABLE_JOURNALING;
579 sysctl_info[3] = jinfo_block;
580 sysctl_info[4] = jstart_block;
581 sysctl_info[5] = journal_size;
582
583 //printf("fs type: 0x%x\n", sysctl_info[1]);
584 //printf("jinfo block : 0x%x\n", jinfo_block);
585 //printf("jstart block: 0x%x\n", jstart_block);
586 //printf("journal size: 0x%x\n", journal_size);
587
588 ret = sysctl((void *)sysctl_info, 6, NULL, NULL, NULL, 0);
589 if (ret != 0) {
590 fprintf(stderr, "Failed to make volume %s journaled (%s)\n",
591 volname, strerror(errno));
592 unlink(journal_fname);
593 unlink(jib_fname);
594 if (buf)
595 free(buf);
596 return 20;
597 }
598
599 if (buf)
600 free(buf);
601 return 0;
602 }
603
604
605 int
606 DoUnJournal(char *volname) {
607 int result;
608 int sysctl_info[8];
609 struct statfs sfs;
610 char jbuf[MAXPATHLEN];
611
612 if (statfs(volname, &sfs) != 0) {
613 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
614 return 10;
615 }
616
617 if (strcmp(sfs.f_fstypename, "hfs") != 0) {
618 fprintf(stderr, "Volume %s (%s) is not a HFS volume.\n", volname, sfs.f_mntfromname);
619 return 1;
620 }
621
622 if ((sfs.f_flags & MNT_JOURNALED) == 0) {
623 fprintf(stderr, "Volume %s (%s) is not journaled.\n", volname, sfs.f_mntfromname);
624 return 1;
625 }
626
627 if (chdir(volname) != 0) {
628 fprintf(stderr, "Can't locate volume %s to turn off journaling (%s).\n",
629 volname, strerror(errno));
630 return 10;
631 }
632
633 memset(sysctl_info, 0, sizeof(sysctl_info));
634 sysctl_info[0] = CTL_VFS;
635 sysctl_info[1] = sfs.f_fsid.val[1];
636 sysctl_info[2] = HFS_DISABLE_JOURNALING;
637
638 result = sysctl((void *)sysctl_info, 3, NULL, NULL, NULL, 0);
639 if (result != 0) {
640 fprintf(stderr, "Failed to make volume %s UN-journaled (%s)\n",
641 volname, strerror(errno));
642 return 20;
643 }
644
645 snprintf(jbuf, sizeof(jbuf), "%s/%s", volname, journal_fname);
646 if (unlink(jbuf) != 0) {
647 fprintf(stderr, "Failed to remove the journal %s (%s)\n",
648 jbuf, strerror(errno));
649 }
650
651 snprintf(jbuf, sizeof(jbuf), "%s/%s", volname, jib_fname);
652 if (unlink(jbuf) != 0) {
653 fprintf(stderr, "Failed to remove the journal info block %s (%s)\n",
654 jbuf, strerror(errno));
655 }
656
657 printf("Journaling disabled on %s mounted at %s.\n", sfs.f_mntfromname, volname);
658
659 return 0;
660 }
661
662
663
664
665 int
666 get_journal_info(char *devname, struct JournalInfoBlock *jib)
667 {
668 int fd = -1, ret = 0;
669 char *buff = NULL, *buff2 = NULL;
670 u_int64_t disksize;
671 u_int64_t blkcnt;
672 u_int32_t blksize;
673 daddr_t mdb_offset;
674 HFSMasterDirectoryBlock *mdbp;
675 HFSPlusVolumeHeader *vhp;
676 off_t embeddedOffset, pos;
677 struct JournalInfoBlock *myjib;
678
679 fd = open(devname, O_RDONLY);
680 if (fd < 0) {
681 printf("can't open: %s (%s)\n", devname, strerror(errno));
682 ret = -5;
683 goto out;
684 }
685
686 /* Get the real physical block size. */
687 if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) {
688 printf("can't get the device block size (%s). assuming 512\n", strerror(errno));
689 blksize = 512;
690 ret = -1;
691 goto out;
692 }
693
694 /* Get the number of physical blocks. */
695 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) {
696 struct stat st;
697 printf("failed to get block count. trying stat().\n");
698 if (fstat(fd, &st) != 0) {
699 ret = -1;
700 goto out;
701 }
702
703 blkcnt = st.st_size / blksize;
704 }
705
706 /* Compute an accurate disk size */
707 disksize = blkcnt * (u_int64_t)blksize;
708
709 /*
710 * There are only 31 bits worth of block count in
711 * the buffer cache. So for large volumes a 4K
712 * physical block size is needed.
713 */
714 if (blksize == 512 && blkcnt > (u_int64_t)0x000000007fffffff) {
715 blksize = 4096;
716 }
717
718 /*
719 * At this point:
720 * blksize has our prefered physical block size
721 * blkcnt has the total number of physical blocks
722 */
723
724 buff = (char *)malloc(blksize);
725 buff2 = (char *)malloc(blksize);
726
727 if (pread(fd, buff, blksize, HFS_PRI_SECTOR(blksize)*blksize) != blksize) {
728 printf("failed to read volume header @ offset %d (%s)\n",
729 HFS_PRI_SECTOR(blksize), strerror(errno));
730 ret = -1;
731 goto out;
732 }
733
734 mdbp = (HFSMasterDirectoryBlock *)(buff + HFS_PRI_OFFSET(blksize));
735
736 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
737 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
738 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
739 ret = -1;
740 printf("get_journal_info: invalid volume signature\n");
741 goto out;
742 }
743
744 mdbp->drSigWord = SWAP_BE16(mdbp->drSigWord);
745 mdbp->drEmbedSigWord = SWAP_BE16(mdbp->drEmbedSigWord);
746 mdbp->drAlBlSt = SWAP_BE16(mdbp->drAlBlSt);
747 mdbp->drEmbedExtent.startBlock = SWAP_BE16(mdbp->drEmbedExtent.startBlock);
748 mdbp->drAlBlkSiz = SWAP_BE32(mdbp->drAlBlkSiz);
749 mdbp->drEmbedExtent.blockCount = SWAP_BE16(mdbp->drEmbedExtent.blockCount);
750
751
752 if ((mdbp->drSigWord == kHFSSigWord) && (mdbp->drEmbedSigWord != kHFSPlusSigWord)) {
753 // normal hfs can not ever be journaled
754 goto out;
755 }
756
757 /* Get the embedded Volume Header */
758 if (mdbp->drEmbedSigWord == kHFSPlusSigWord) {
759 embeddedOffset = mdbp->drAlBlSt * 512;
760 embeddedOffset += (u_int64_t)mdbp->drEmbedExtent.startBlock *
761 (u_int64_t)mdbp->drAlBlkSiz;
762
763 /*
764 * If the embedded volume doesn't start on a block
765 * boundary, then switch the device to a 512-byte
766 * block size so everything will line up on a block
767 * boundary.
768 */
769 if ((embeddedOffset % blksize) != 0) {
770 printf("HFS Mount: embedded volume offset not"
771 " a multiple of physical block size (%d);"
772 " switching to 512\n", blksize);
773
774 blkcnt *= (blksize / 512);
775 blksize = 512;
776 }
777
778 disksize = (u_int64_t)mdbp->drEmbedExtent.blockCount *
779 (u_int64_t)mdbp->drAlBlkSiz;
780
781 mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize);
782 if (pread(fd, buff, blksize, mdb_offset * blksize) != blksize) {
783 printf("failed to read the embedded vhp @ offset %d\n", mdb_offset * blksize);
784 ret = -1;
785 goto out;
786 }
787
788 vhp = (HFSPlusVolumeHeader*) (buff + HFS_PRI_OFFSET(blksize));
789
790 mdbp = (HFSMasterDirectoryBlock *)vhp;
791 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
792 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
793 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
794 ret = -1;
795
796 printf("get_journal_info: invalid embedded volume signature \n");
797 goto out;
798 }
799
800 } else /* pure HFS+ */ {
801 embeddedOffset = 0;
802 vhp = (HFSPlusVolumeHeader*) mdbp;
803 }
804
805 if ((SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) == 0) {
806 ret = 0;
807 goto out;
808 }
809
810 //
811 // Now read the journal info block... (when calculating the
812 // position, make sure to cast to unsigned first, then to
813 // off_t so that things don't get sign-extended improperly
814 // or truncated).
815 //
816 pos = (off_t)((off_t)embeddedOffset +
817 (off_t)((unsigned int)SWAP_BE32(vhp->journalInfoBlock))*(off_t)((unsigned int)SWAP_BE32(vhp->blockSize)));
818
819 if (pread(fd, buff2, blksize, pos) != blksize) {
820 printf("failed to read the journal info block (%s).\n", strerror(errno));
821 ret = -1;
822 goto out;
823 }
824
825 myjib = (struct JournalInfoBlock *)buff2;
826 myjib->flags = SWAP_BE32(myjib->flags);
827 myjib->offset = SWAP_BE64(myjib->offset);
828 myjib->size = SWAP_BE64(myjib->size);
829
830 memcpy(jib, myjib, sizeof(*myjib));
831
832 ret = 1;
833
834 out:
835 if (buff)
836 free(buff);
837 if (buff2)
838 free(buff2);
839 if (fd >= 0)
840 close(fd);
841
842 return ret;
843 }
844
845
846 int
847 DoGetJournalInfo(char *volname)
848 {
849 struct statfs sfs;
850 struct hfs_journal_info jinfo;
851
852 if (strncmp(volname, "/dev/", 5) == 0 || strncmp(volname, "disk", 4) == 0 || strncmp(volname, "rdisk", 5) == 0) {
853 struct JournalInfoBlock jib;
854 int ret;
855 char fulldevname[256];
856
857 if (strncmp(volname, "disk", 4) == 0 || strncmp(volname, "rdisk", 5) == 0) {
858 snprintf(fulldevname, sizeof(fulldevname), "/dev/%s", volname);
859 volname = &fulldevname[0];
860 }
861
862 // try the name as a device name...
863 ret = get_journal_info(volname, &jib);
864 if (ret == 0) {
865 printf("Volume %s is not journaled.\n", volname);
866 return 0;
867 } else if (ret < 0) {
868 printf("Volume %s does not appear to be an HFS+ volume.\n", volname);
869 return 10;
870 } else {
871 if (jib.flags & kJIJournalInFSMask) {
872 printf("%s : journal size %lld k at offset 0x%llx\n", volname, jib.size/1024, jib.offset);
873 } else {
874 printf("%s: external journal stored on partition with uuid %s on machine w/serial number %s\n",
875 volname, jib.ext_jnl_uuid, jib.machine_serial_num);
876 }
877
878 return 0;
879 }
880
881 }
882
883 if (statfs(volname, &sfs) != 0) {
884 fprintf(stderr, "Unable to get fs info for %s\n", volname);
885 return 10;
886 }
887
888 if ((sfs.f_flags & MNT_JOURNALED) == 0) {
889 fprintf(stderr, "Volume %s is not journaled.\n", volname);
890 return 1;
891 }
892
893 if (fsctl(volname, HFSIOC_GET_JOURNAL_INFO, &jinfo, 0) != 0) {
894 fprintf(stderr, "Failed to get journal info for volume %s (%s)\n",
895 volname, strerror(errno));
896 return 20;
897 }
898
899 if (jinfo.jstart == 0) {
900 char rawdev[256];
901
902 snprintf(rawdev, sizeof(rawdev), "/dev/r%s", &sfs.f_mntfromname[5]);
903
904 // it's an external journal so get the info the
905 // other way.
906 return DoGetJournalInfo(&rawdev[0]);
907 }
908
909 if (jinfo.jsize == 0) {
910 printf("%s : not journaled.\n", volname);
911 } else {
912 printf("%s : journal size %lld k at offset 0x%llx\n", volname, jinfo.jsize/1024, jinfo.jstart);
913 }
914
915 return 0;
916 }
917
918
919 int
920 RawDisableJournaling(char *devname)
921 {
922 int fd = -1, ret = 0;
923 char *buff = NULL, rawdev[256], unrawdev[256];
924 u_int64_t disksize;
925 u_int64_t blkcnt;
926 u_int32_t blksize;
927 daddr_t mdb_offset;
928 HFSMasterDirectoryBlock *mdbp;
929 HFSPlusVolumeHeader *vhp;
930 off_t embeddedOffset, hdr_offset;
931 struct stat st;
932 struct statfs *fsinfo;
933
934 // assume that the name provided is a raw device name
935 strlcpy(rawdev, devname, sizeof(rawdev));
936
937 restart:
938 if (stat(rawdev, &st) != 0) {
939 fprintf(stderr, "Could not access %s (%s)\n", devname, strerror(errno));
940 ret = -1;
941 goto out;
942 }
943
944 if (S_ISCHR(st.st_mode) == 0) {
945 if (S_ISBLK(st.st_mode)) {
946 // this is a block device, convert the name to
947 // raw character device and try again
948 snprintf(rawdev, sizeof(rawdev), "/dev/r%s", devname + 5);
949 goto restart;
950 } else {
951 // probably it is a mount point
952 return DoUnJournal(devname);
953 }
954 } else {
955 // convert the character raw device name to
956 // block device name to compare with getmntinfo output
957 snprintf(unrawdev, sizeof(unrawdev), "/dev/%s", rawdev + 6);
958 }
959
960 // make sure that the file system on the device node is not mounted
961 ret = getmntinfo(&fsinfo, MNT_NOWAIT);
962 if (ret == 0) {
963 fprintf (stderr, "Error getting list of mounted filesystems\n");
964 ret = -1;
965 goto out;
966 }
967
968 while (ret--) {
969 // the file system on this device node is currently mounted
970 if (strcmp(unrawdev, fsinfo[ret].f_mntfromname) == 0) {
971 return DoUnJournal(fsinfo[ret].f_mntonname);
972 }
973 }
974
975 fd = open(rawdev, O_RDWR);
976 if (fd < 0) {
977 fprintf(stderr, "can't open: %s (%s)\n", devname, strerror(errno));
978 ret = -1;
979 goto out;
980 }
981
982 /* Get the real physical block size. */
983 if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) {
984 fprintf(stderr, "can't get the device block size (%s). assuming 512\n", strerror(errno));
985 blksize = 512;
986 ret = -1;
987 goto out;
988 }
989
990 /* Get the number of physical blocks. */
991 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) {
992 struct stat st;
993
994 if (fstat(fd, &st) != 0) {
995 ret = -1;
996 goto out;
997 }
998
999 blkcnt = st.st_size / blksize;
1000 }
1001
1002 /* Compute an accurate disk size */
1003 disksize = blkcnt * (u_int64_t)blksize;
1004
1005 /*
1006 * There are only 31 bits worth of block count in
1007 * the buffer cache. So for large volumes a 4K
1008 * physical block size is needed.
1009 */
1010 if (blksize == 512 && blkcnt > (u_int64_t)0x000000007fffffff) {
1011 blksize = 4096;
1012 }
1013
1014 /*
1015 * At this point:
1016 * blksize has our prefered physical block size
1017 * blkcnt has the total number of physical blocks
1018 */
1019
1020 buff = (char *)malloc(blksize);
1021
1022 hdr_offset = HFS_PRI_SECTOR(blksize)*blksize;
1023 if (pread(fd, buff, blksize, hdr_offset) != blksize) {
1024 fprintf(stderr, "RawDisableJournaling: failed to read volume header @ offset %lld (%s)\n",
1025 hdr_offset, strerror(errno));
1026 ret = -1;
1027 goto out;
1028 }
1029
1030 mdbp = (HFSMasterDirectoryBlock *)(buff + HFS_PRI_OFFSET(blksize));
1031 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
1032 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
1033 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
1034 ret = -1;
1035 printf("RawDisableJournaling: Invalid Volume Signature \n");
1036 goto out;
1037 }
1038
1039 if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) {
1040 // normal hfs can not ever be journaled
1041 fprintf(stderr, "disable_journaling: volume is only regular HFS, not HFS+\n");
1042 goto out;
1043 }
1044
1045 /* Get the embedded Volume Header */
1046 if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) {
1047 embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * 512;
1048 embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
1049
1050 /*
1051 * If the embedded volume doesn't start on a block
1052 * boundary, then switch the device to a 512-byte
1053 * block size so everything will line up on a block
1054 * boundary.
1055 */
1056 if ((embeddedOffset % blksize) != 0) {
1057 fprintf(stderr, "HFS Mount: embedded volume offset not"
1058 " a multiple of physical block size (%d);"
1059 " switching to 512\n", blksize);
1060
1061 blkcnt *= (blksize / 512);
1062 blksize = 512;
1063 }
1064
1065 disksize = (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.blockCount) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
1066
1067 mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize);
1068 hdr_offset = mdb_offset * blksize;
1069 if (pread(fd, buff, blksize, hdr_offset) != blksize) {
1070 fprintf(stderr, "failed to read the embedded vhp @ offset %d\n", mdb_offset * blksize);
1071 ret = -1;
1072 goto out;
1073 }
1074
1075 vhp = (HFSPlusVolumeHeader*) (buff + HFS_PRI_OFFSET(blksize));
1076
1077 mdbp = (HFSMasterDirectoryBlock *)vhp;
1078 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
1079 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
1080 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
1081 ret = -1;
1082
1083 printf("RawDisableJournaling: invalid embedded volume signature \n");
1084 goto out;
1085 }
1086
1087 } else /* pure HFS+ */ {
1088 embeddedOffset = 0;
1089 vhp = (HFSPlusVolumeHeader*) mdbp;
1090 }
1091
1092
1093 if ((SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) != 0) {
1094 unsigned int tmp = SWAP_BE32(vhp->attributes);
1095
1096 tmp &= ~kHFSVolumeJournaledMask;
1097 vhp->attributes = SWAP_BE32(tmp);
1098 if ((tmp = pwrite(fd, buff, blksize, hdr_offset)) != blksize) {
1099 fprintf(stderr, "Update of super-block on %s failed! (%d != %d, %s)\n",
1100 devname, tmp, blksize, strerror(errno));
1101 } else {
1102 fprintf(stderr, "Turned off the journaling bit for %s\n", devname);
1103 }
1104 } else {
1105 fprintf(stderr, "disable_journaling: %s is not journaled.\n", devname);
1106 }
1107
1108
1109 out:
1110 if (buff)
1111 free(buff);
1112 if (fd >= 0)
1113 close(fd);
1114
1115 return ret;
1116 }
1117
1118
1119
1120 int
1121 SetJournalInFSState(const char *devname, int journal_in_fs)
1122 {
1123 int fd = -1, ret = 0;
1124 char *buff = NULL, *buff2 = NULL;
1125 u_int64_t blkcnt;
1126 u_int32_t blksize;
1127 daddr_t mdb_offset;
1128 HFSMasterDirectoryBlock *mdbp;
1129 HFSPlusVolumeHeader *vhp;
1130 off_t embeddedOffset, pos;
1131 struct JournalInfoBlock *myjib;
1132
1133 fd = open(devname, O_RDWR);
1134 if (fd < 0) {
1135 printf("can't open: %s (%s)\n", devname, strerror(errno));
1136 ret = -1;
1137 goto out;
1138 }
1139
1140 /* Get the real physical block size. */
1141 if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) {
1142 printf("can't get the device block size (%s). assuming 512\n", strerror(errno));
1143 blksize = 512;
1144 ret = -1;
1145 goto out;
1146 }
1147
1148 /* Get the number of physical blocks. */
1149 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) {
1150 struct stat st;
1151 printf("failed to get block count. trying stat().\n");
1152 if (fstat(fd, &st) != 0) {
1153 ret = -1;
1154 goto out;
1155 }
1156
1157 blkcnt = st.st_size / blksize;
1158 }
1159
1160 /*
1161 * There used to only be 31 bits worth of block count in
1162 * the buffer cache. So for large volumes a 4K
1163 * physical block size is needed.
1164 */
1165 if (blksize == 512 && blkcnt > (u_int64_t)0x000000007fffffff) {
1166 blksize = 4096;
1167 }
1168
1169 /*
1170 * At this point:
1171 * blksize has our prefered physical block size
1172 * blkcnt has the total number of physical blocks
1173 */
1174
1175 buff = (char *)malloc(blksize);
1176 buff2 = (char *)malloc(blksize);
1177
1178 if (pread(fd, buff, blksize, HFS_PRI_SECTOR(blksize)*blksize) != blksize) {
1179 printf("failed to read volume header @ offset %d (%s)\n",
1180 HFS_PRI_SECTOR(blksize), strerror(errno));
1181 ret = -1;
1182 goto out;
1183 }
1184
1185 mdbp = (HFSMasterDirectoryBlock *)(buff + HFS_PRI_OFFSET(blksize));
1186
1187
1188 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
1189 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
1190 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
1191 ret = -1;
1192 printf ("SetJournalInFSState: Invalid Volume Signature \n");
1193 goto out;
1194 }
1195
1196 if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) {
1197 // normal hfs can not ever be journaled
1198 goto out;
1199 }
1200
1201 /* Get the embedded Volume Header */
1202 if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) {
1203 embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * 512;
1204 embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) *
1205 (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
1206
1207 /*
1208 * If the embedded volume doesn't start on a block
1209 * boundary, then switch the device to a 512-byte
1210 * block size so everything will line up on a block
1211 * boundary.
1212 */
1213 if ((embeddedOffset % blksize) != 0) {
1214 printf("HFS Mount: embedded volume offset not"
1215 " a multiple of physical block size (%d);"
1216 " switching to 512\n", blksize);
1217
1218 blkcnt *= (blksize / 512);
1219 blksize = 512;
1220 }
1221
1222 mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize);
1223 if (pread(fd, buff, blksize, mdb_offset * blksize) != blksize) {
1224 printf("failed to read the embedded vhp @ offset %d\n", mdb_offset * blksize);
1225 ret = -1;
1226 goto out;
1227 }
1228
1229 vhp = (HFSPlusVolumeHeader*) (buff + HFS_PRI_OFFSET(blksize));
1230
1231 mdbp = (HFSMasterDirectoryBlock *)(vhp);
1232 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
1233 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
1234 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
1235 ret = -1;
1236 printf("SetJournalInFSState: Invalid Embedded Volume Signature \n");
1237 goto out;
1238 }
1239
1240
1241 } else /* pure HFS+ */ {
1242 embeddedOffset = 0;
1243 vhp = (HFSPlusVolumeHeader*) mdbp;
1244 }
1245
1246 //printf("vol header attributes: 0x%x\n", SWAP_BE32(vhp->attributes));
1247 if ((SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) == 0) {
1248 ret = 0;
1249 goto out;
1250 }
1251
1252 //
1253 // Now read the journal info block... (when calculating the
1254 // position, make sure to cast to unsigned first, then to
1255 // off_t so that things don't get sign-extended improperly
1256 // or truncated).
1257 //
1258 pos = (off_t)((off_t)embeddedOffset +
1259 (off_t)((unsigned int )SWAP_BE32(vhp->journalInfoBlock))*(off_t)((unsigned int)SWAP_BE32(vhp->blockSize)));
1260
1261 if (pread(fd, buff2, blksize, pos) != blksize) {
1262 printf("failed to read the journal info block (%s).\n", strerror(errno));
1263 ret = -1;
1264 goto out;
1265 }
1266
1267 myjib = (struct JournalInfoBlock *)buff2;
1268
1269 // swap this to host native format so we can diddle with the bits
1270 myjib->flags = SWAP_BE32(myjib->flags);
1271
1272 if (journal_in_fs) {
1273 myjib->flags |= kJIJournalInFSMask;
1274 } else {
1275 myjib->flags &= ~kJIJournalInFSMask;
1276 }
1277 myjib->flags |= kJIJournalNeedInitMask;
1278
1279 // and now swap back before writing it out
1280 myjib->flags = SWAP_BE32(myjib->flags);
1281
1282 if (pwrite(fd, buff2, blksize, pos) != blksize) {
1283 printf("failed to re-write the journal info block (%s).\n", strerror(errno));
1284 ret = -1;
1285 goto out;
1286 }
1287
1288 ret = 0;
1289
1290 out:
1291 if (buff)
1292 free(buff);
1293 if (buff2)
1294 free(buff2);
1295 if (fd >= 0)
1296 close(fd);
1297
1298 return ret;
1299 }