]> git.saurik.com Git - apple/hfs.git/blame - hfs_util/hfsutil_jnl.c
hfs-165.tar.gz
[apple/hfs.git] / hfs_util / hfsutil_jnl.c
CommitLineData
08cdaae8
A
1/*
2 * Copyright (c) 1999-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
635a1ee0
A
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.
08cdaae8 11 *
635a1ee0
A
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
08cdaae8
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
635a1ee0
A
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.
08cdaae8
A
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>
16f263f5 40#include <sys/ioctl.h>
08cdaae8 41
19348fcb 42#include <sys/disk.h>
08cdaae8
A
43#include <sys/loadable_fs.h>
44#include <hfs/hfs_format.h>
19348fcb 45#include <hfs/hfs_mount.h> /* for hfs sysctl values */
08cdaae8
A
46
47#include <errno.h>
48#include <fcntl.h>
49#include <libgen.h>
50#include <pwd.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55
56#include <architecture/byte_order.h>
57
19348fcb
A
58// just in case these aren't in <hfs/hfs_mount.h> yet
59#ifndef HFS_ENABLE_JOURNALING
60#define HFS_ENABLE_JOURNALING 0x082969
61#endif
62#ifndef HFS_DISABLE_JOURNALING
63#define HFS_DISABLE_JOURNALING 0x031272
64#endif
65#ifndef HFS_GET_JOURNAL_INFO
66#define HFS_GET_JOURNAL_INFO 0x6a6e6c69
67#endif
08cdaae8
A
68
69/* getattrlist buffers start with an extra length field */
70struct ExtentsAttrBuf {
71 unsigned long infoLength;
72 HFSPlusExtentRecord extents;
73};
74typedef struct ExtentsAttrBuf ExtentsAttrBuf;
75
76
77
78#define kIsInvisible 0x4000
79
80/*
81 * Generic Finder file/dir data
82 */
83struct FinderInfo {
84 u_int32_t opaque_1[2];
85 u_int16_t fdFlags; /* Finder flags */
86 int16_t opaque_2[11];
87};
88typedef struct FinderInfo FinderInfo;
89
90/* getattrlist buffers start with an extra length field */
91struct FinderAttrBuf {
92 unsigned long infoLength;
93 FinderInfo finderInfo;
94};
95typedef struct FinderAttrBuf FinderAttrBuf;
96
97
98int hide_file(const char * file)
99{
100 struct attrlist alist = {0};
101 FinderAttrBuf finderInfoBuf = {0};
102 int result;
103
104 alist.bitmapcount = ATTR_BIT_MAP_COUNT;
105 alist.commonattr = ATTR_CMN_FNDRINFO;
106
107 result = getattrlist(file, &alist, &finderInfoBuf, sizeof(finderInfoBuf), 0);
108 if (result) {
19348fcb
A
109 return (errno);
110 }
08cdaae8
A
111
112 if (finderInfoBuf.finderInfo.fdFlags & kIsInvisible) {
19348fcb
A
113 printf("hide: %s is alreadly invisible\n", file);
114 return (0);
08cdaae8
A
115 }
116
117 finderInfoBuf.finderInfo.fdFlags |= kIsInvisible;
118
119 result = setattrlist(file, &alist, &finderInfoBuf.finderInfo, sizeof(FinderInfo), 0);
120
121 return (result == -1 ? errno : result);
122}
123
16f263f5
A
124off_t
125get_start_block(const char *file, uint32_t fs_block_size)
08cdaae8 126{
16f263f5
A
127 off_t cur_pos, phys_start, len;
128 int fd, err;
129 struct log2phys l2p;
130 struct stat st;
08cdaae8 131
16f263f5
A
132 fd = open(file, O_RDONLY);
133 if (fd < 0) {
134 return -1;
135 }
08cdaae8 136
16f263f5
A
137 if (fstat(fd, &st) < 0) {
138 fprintf(stderr, "can't stat %s (%s)\n", file, strerror(errno));
139 close(fd);
19348fcb 140 return -1;
08cdaae8
A
141 }
142
16f263f5
A
143 fs_block_size = st.st_blksize; // XXXdbg quick hack for now
144
145 phys_start = len = 0;
146 for(cur_pos=0; cur_pos < st.st_size; cur_pos += fs_block_size) {
147 memset(&l2p, 0, sizeof(l2p));
148 lseek(fd, cur_pos, SEEK_SET);
149 err = fcntl(fd, F_LOG2PHYS, &l2p);
150
151 if (phys_start == 0) {
152 phys_start = l2p.l2p_devoffset;
153 len = fs_block_size;
154 } else if (l2p.l2p_devoffset != (phys_start + len)) {
155 // printf(" %lld : %lld - %lld\n", cur_pos, phys_start / fs_block_size, len / fs_block_size);
156 fprintf(stderr, "%s : is not contiguous!\n", file);
157 close(fd);
158 return -1;
159 // phys_start = l2p.l2p_devoffset;
160 // len = fs_block_size;
161 } else {
162 len += fs_block_size;
163 }
164 }
165
166 close(fd);
167
168 //printf("%s start offset %lld; byte len %lld (blksize %d)\n",
169 // file, phys_start, len, fs_block_size);
170
171 if ((phys_start / (unsigned)fs_block_size) & 0xffffffff00000000LL) {
172 fprintf(stderr, "%s : starting block is > 32bits!\n", file);
19348fcb 173 return -1;
08cdaae8 174 }
16f263f5
A
175
176 return phys_start;
177}
178
179
180//
181// Get the embedded offset (if any) for an hfs+ volume.
182// This is pretty skanky that we have to do this but
183// that's life...
184//
185#include <sys/disk.h>
186#include <hfs/hfs_format.h>
187
188#include <machine/endian.h>
189
190#define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
191#define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
192
193#define SWAP_BE16(x) ntohs(x)
194#define SWAP_BE32(x) ntohl(x)
195
196
197off_t
198get_embedded_offset(char *devname)
199{
200 int fd = -1;
201 off_t ret = 0;
202 char *buff = NULL, rawdev[256];
203 u_int64_t blkcnt;
204 u_int32_t blksize;
205 HFSMasterDirectoryBlock *mdbp;
206 off_t embeddedOffset;
207 struct statfs sfs;
208 struct stat st;
209
210 restart:
211 if (stat(devname, &st) != 0) {
212 fprintf(stderr, "Could not access %s (%s)\n", devname, strerror(errno));
213 ret = -1;
214 goto out;
215 }
216
217 if (S_ISCHR(st.st_mode) == 0) {
218 // hmmm, it's not the character special raw device so we
219 // should try to figure out the real device.
220 if (statfs(devname, &sfs) != 0) {
221 fprintf(stderr, "Can't find out any info about the fs for path %s (%s)\n",
222 devname, strerror(errno));
223 ret = -1;
224 goto out;
225 }
226
227 // copy the "/dev/"
228 strncpy(rawdev, sfs.f_mntfromname, 5);
229 rawdev[5] = 'r';
230 strcpy(&rawdev[6], &sfs.f_mntfromname[5]);
231 devname = &rawdev[0];
232 goto restart;
233 }
08cdaae8 234
16f263f5
A
235 fd = open(devname, O_RDONLY);
236 if (fd < 0) {
237 fprintf(stderr, "can't open: %s (%s)\n", devname, strerror(errno));
238 ret = -1;
239 goto out;
240 }
241
242 /* Get the real physical block size. */
243 if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) {
244 fprintf(stderr, "can't get the device block size (%s). assuming 512\n", strerror(errno));
245 blksize = 512;
246 ret = -1;
247 goto out;
248 }
249
250 /* Get the number of physical blocks. */
251 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) {
252 struct stat st;
253 fprintf(stderr, "failed to get block count. trying stat().\n");
254 if (fstat(fd, &st) != 0) {
255 ret = -1;
256 goto out;
257 }
258
259 blkcnt = st.st_size / blksize;
260 }
261
16f263f5
A
262 /*
263 * At this point:
264 * blksize has our prefered physical block size
265 * blkcnt has the total number of physical blocks
266 */
267
268 buff = (char *)malloc(blksize);
269
270 if (pread(fd, buff, blksize, HFS_PRI_SECTOR(blksize)*blksize) != blksize) {
271 fprintf(stderr, "failed to read volume header @ offset %d (%s)\n",
272 HFS_PRI_SECTOR(blksize), strerror(errno));
273 ret = -1;
274 goto out;
275 }
276
277 mdbp = (HFSMasterDirectoryBlock *)buff;
278 if ( (SWAP_BE16(mdbp->drSigWord) != kHFSSigWord)
279 && (SWAP_BE16(mdbp->drSigWord) != kHFSPlusSigWord)
280 && (SWAP_BE16(mdbp->drSigWord) != kHFSXSigWord)) {
281 ret = -1;
282 goto out;
283 }
284
285 if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) {
286 ret = -1;
287 goto out;
288 } else if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) {
289 /* Get the embedded Volume Header */
290 embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * 512;
291 embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) *
292 (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
293
294 /*
295 * If the embedded volume doesn't start on a block
296 * boundary, then switch the device to a 512-byte
297 * block size so everything will line up on a block
298 * boundary.
299 */
300 if ((embeddedOffset % blksize) != 0) {
301 fprintf(stderr, "HFS Mount: embedded volume offset not"
302 " a multiple of physical block size (%d);"
303 " switching to 512\n", blksize);
304
305 blkcnt *= (blksize / 512);
306 blksize = 512;
307 }
308
309 } else { /* pure HFS+ */
310 embeddedOffset = 0;
311 }
312
313 ret = embeddedOffset;
314
315 out:
316 if (buff) {
317 free(buff);
318 }
319 if (fd >= 0)
320 close(fd);
321
322 return ret;
08cdaae8
A
323}
324
16f263f5
A
325
326
08cdaae8
A
327static const char *journal_fname = ".journal";
328static const char *jib_fname = ".journal_info_block";
329
330int
331DoMakeJournaled(char *volname, int jsize)
332{
333 int fd, i, block_size, journal_size = 8*1024*1024;
334 char *buf;
335 int ret;
336 fstore_t fst;
16f263f5
A
337 int32_t jstart_block, jinfo_block;
338 int sysctl_info[8];
08cdaae8 339 JournalInfoBlock jib;
19348fcb
A
340 struct statfs sfs;
341 static char tmpname[MAXPATHLEN];
16f263f5 342 off_t start_block, embedded_offset;
08cdaae8 343
19348fcb
A
344 if (statfs(volname, &sfs) != 0) {
345 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
346 return 10;
347 }
08cdaae8 348
19348fcb
A
349 // Make sure that we're HFS+. First we check the fstypename.
350 // If that's ok then we try to create a symlink (which won't
351 // work on plain hfs volumes but will work on hfs+ volumes).
352 //
353 sprintf(tmpname, "%s/is_vol_hfs_plus", volname);
354 if (strcmp(sfs.f_fstypename, "hfs") != 0 ||
355 ((ret = symlink(tmpname, tmpname)) != 0 && errno == ENOTSUP)) {
356 fprintf(stderr, "%s is not an HFS+ volume. Journaling only works on HFS+ volumes.\n",
357 volname);
358 return 10;
359 }
360 unlink(tmpname);
08cdaae8 361
19348fcb
A
362 if (sfs.f_flags & MNT_JOURNALED) {
363 fprintf(stderr, "Volume %s is already journaled.\n", volname);
364 return 1;
365 }
366
367 if (jsize != 0) {
368 journal_size = jsize;
369 } else {
370 int scale;
08cdaae8 371
19348fcb
A
372 //
373 // we want at least 8 megs of journal for each 100 gigs of
374 // disk space. We cap the size at 512 megs though.
375 //
376 scale = ((long long)sfs.f_bsize * (long long)((unsigned)sfs.f_blocks)) / (100*1024*1024*1024ULL);
377 journal_size *= (scale + 1);
378 if (journal_size > 512 * 1024 * 1024) {
379 journal_size = 512 * 1024 * 1024;
08cdaae8 380 }
19348fcb 381 }
08cdaae8
A
382
383 if (chdir(volname) != 0) {
19348fcb
A
384 fprintf(stderr, "Can't locate volume %s to make it journaled (%s).\n",
385 volname, strerror(errno));
386 return 10;
387 }
08cdaae8 388
16f263f5
A
389
390 embedded_offset = get_embedded_offset(volname);
391 if (embedded_offset < 0) {
392 fprintf(stderr, "Can't calculate the embedded offset (if any) for %s.\n", volname);
393 fprintf(stderr, "Journal creation failure.\n");
394 return 15;
395 }
396 // printf("Embedded offset == 0x%llx\n", embedded_offset);
397
08cdaae8
A
398 fd = open(journal_fname, O_CREAT|O_TRUNC|O_RDWR, 000);
399 if (fd < 0) {
19348fcb
A
400 fprintf(stderr, "Can't create journal file on volume %s (%s)\n",
401 volname, strerror(errno));
402 return 5;
08cdaae8
A
403 }
404
19348fcb
A
405 // make sure that it has no r/w/x privs (only could happen if
406 // the file already existed since open() doesn't reset the mode
407 // bits).
408 //
409 fchmod(fd, 0);
08cdaae8
A
410
411 block_size = sfs.f_bsize;
412 if ((journal_size % block_size) != 0) {
19348fcb
A
413 fprintf(stderr, "Journal size %dk is not a multiple of volume %s block size (%d).\n",
414 journal_size/1024, volname, block_size);
415 close(fd);
416 unlink(journal_fname);
417 return 5;
08cdaae8
A
418 }
419
19348fcb 420 retry:
08cdaae8
A
421 memset(&fst, 0, sizeof(fst));
422 fst.fst_flags = F_ALLOCATECONTIG|F_ALLOCATEALL;
423 fst.fst_length = journal_size;
424 fst.fst_posmode = F_PEOFPOSMODE;
425
426 ret = fcntl(fd, F_PREALLOCATE, &fst);
427 if (ret < 0) {
19348fcb
A
428 if (journal_size >= 2*1024*1024) {
429 fprintf(stderr, "Not enough contiguous space for a %d k journal. Retrying.\n",
430 journal_size/1024);
431 journal_size /= 2;
432 ftruncate(fd, 0); // make sure the file is zero bytes long.
433 goto retry;
434 } else {
435 fprintf(stderr, "Disk too fragmented to enable journaling.\n");
436 fprintf(stderr, "Please run a defragmenter on %s.\n", volname);
437 close(fd);
438 unlink(journal_fname);
439 return 10;
440 }
08cdaae8
A
441 }
442
443 printf("Allocated %lldK for journal file.\n", fst.fst_bytesalloc/1024LL);
444 buf = (char *)calloc(block_size, 1);
445 if (buf) {
19348fcb
A
446 for(i=0; i < journal_size/block_size; i++) {
447 ret = write(fd, buf, block_size);
448 if (ret != block_size) {
449 break;
450 }
451 }
08cdaae8 452
19348fcb
A
453 if (i*block_size != journal_size) {
454 fprintf(stderr, "Failed to write %dk to journal on volume %s (%s)\n",
455 journal_size/1024, volname, strerror(errno));
456 }
08cdaae8 457 } else {
19348fcb
A
458 printf("Could not allocate memory to write to the journal on volume %s (%s)\n",
459 volname, strerror(errno));
08cdaae8
A
460 }
461
462 fsync(fd);
463 close(fd);
464 hide_file(journal_fname);
465
16f263f5
A
466 start_block = get_start_block(journal_fname, block_size);
467 if (start_block == (off_t)-1) {
468 fprintf(stderr, "Failed to get start block for %s (%s)\n",
469 journal_fname, strerror(errno));
470 unlink(journal_fname);
471 return 20;
472 }
473 jstart_block = (start_block / block_size) - (embedded_offset / block_size);
08cdaae8 474
16f263f5 475 memset(&jib, 'Z', sizeof(jib));
08cdaae8 476 jib.flags = kJIJournalInFSMask;
19348fcb
A
477 jib.offset = (off_t)((unsigned)jstart_block) * (off_t)((unsigned)block_size);
478 jib.size = (off_t)((unsigned)journal_size);
08cdaae8
A
479
480 fd = open(jib_fname, O_CREAT|O_TRUNC|O_RDWR, 000);
481 if (fd < 0) {
19348fcb
A
482 fprintf(stderr, "Could not create journal info block file on volume %s (%s)\n",
483 volname, strerror(errno));
484 unlink(journal_fname);
485 return 5;
08cdaae8
A
486 }
487
19348fcb 488 // swap the data before we copy it
e74bfeef
A
489 jib.flags = OSSwapBigToHostInt32(jib.flags);
490 jib.offset = OSSwapBigToHostInt64(jib.offset);
491 jib.size = OSSwapBigToHostInt64(jib.size);
19348fcb 492
08cdaae8 493 memcpy(buf, &jib, sizeof(jib));
19348fcb
A
494
495 // now put it back the way it was
e74bfeef
A
496 jib.size = OSSwapBigToHostInt64(jib.size);
497 jib.offset = OSSwapBigToHostInt64(jib.offset);
498 jib.flags = OSSwapBigToHostInt32(jib.flags);
19348fcb 499
08cdaae8 500 if (write(fd, buf, block_size) != block_size) {
19348fcb
A
501 fprintf(stderr, "Failed to write journal info block on volume %s (%s)!\n",
502 volname, strerror(errno));
503 unlink(journal_fname);
504 return 10;
08cdaae8
A
505 }
506
507 fsync(fd);
508 close(fd);
509 hide_file(jib_fname);
510
16f263f5
A
511 start_block = get_start_block(jib_fname, block_size);
512 if (start_block == (off_t)-1) {
513 fprintf(stderr, "Failed to get start block for %s (%s)\n",
514 jib_fname, strerror(errno));
515 unlink(journal_fname);
516 unlink(jib_fname);
517 return 20;
518 }
519 jinfo_block = (start_block / block_size) - (embedded_offset / block_size);
08cdaae8
A
520
521
522 //
523 // Now make the volume journaled!
524 //
525 memset(sysctl_info, 0, sizeof(sysctl_info));
526 sysctl_info[0] = CTL_VFS;
527 sysctl_info[1] = sfs.f_fsid.val[1];
19348fcb 528 sysctl_info[2] = HFS_ENABLE_JOURNALING;
08cdaae8
A
529 sysctl_info[3] = jinfo_block;
530 sysctl_info[4] = jstart_block;
531 sysctl_info[5] = journal_size;
532
533 //printf("fs type: 0x%x\n", sysctl_info[1]);
534 //printf("jinfo block : 0x%x\n", jinfo_block);
535 //printf("jstart block: 0x%x\n", jstart_block);
536 //printf("journal size: 0x%x\n", journal_size);
537
538 ret = sysctl((void *)sysctl_info, 6, NULL, NULL, NULL, 0);
539 if (ret != 0) {
19348fcb
A
540 fprintf(stderr, "Failed to make volume %s journaled (%s)\n",
541 volname, strerror(errno));
542 unlink(journal_fname);
543 unlink(jib_fname);
544 return 20;
08cdaae8 545 }
19348fcb
A
546
547 return 0;
08cdaae8
A
548}
549
550
551int
552DoUnJournal(char *volname)
553{
19348fcb 554 int result;
08cdaae8 555 int sysctl_info[8];
19348fcb
A
556 struct statfs sfs;
557 char jbuf[MAXPATHLEN];
08cdaae8 558
19348fcb
A
559 if (statfs(volname, &sfs) != 0) {
560 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
561 return 10;
562 }
08cdaae8 563
19348fcb
A
564 if ((sfs.f_flags & MNT_JOURNALED) == 0) {
565 fprintf(stderr, "Volume %s is not journaled.\n", volname);
566 return 1;
567 }
08cdaae8 568
19348fcb
A
569 if (chdir(volname) != 0) {
570 fprintf(stderr, "Can't locate volume %s to turn off journaling (%s).\n",
571 volname, strerror(errno));
572 return 10;
573 }
08cdaae8 574
19348fcb
A
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_DISABLE_JOURNALING;
08cdaae8 579
19348fcb
A
580 result = sysctl((void *)sysctl_info, 3, NULL, NULL, NULL, 0);
581 if (result != 0) {
582 fprintf(stderr, "Failed to make volume %s UN-journaled (%s)\n",
583 volname, strerror(errno));
584 return 20;
585 }
08cdaae8 586
19348fcb
A
587 sprintf(jbuf, "%s/%s", volname, journal_fname);
588 if (unlink(jbuf) != 0) {
589 fprintf(stderr, "Failed to remove the journal %s (%s)\n",
590 jbuf, strerror(errno));
591 }
08cdaae8 592
19348fcb
A
593 sprintf(jbuf, "%s/%s", volname, jib_fname);
594 if (unlink(jbuf) != 0) {
595 fprintf(stderr, "Failed to remove the journal info block %s (%s)\n",
596 jbuf, strerror(errno));
597 }
08cdaae8 598
19348fcb 599 printf("Journaling disabled on %s\n", volname);
08cdaae8 600
19348fcb
A
601 return 0;
602}
603
604
605int
606DoGetJournalInfo(char *volname)
607{
608 int result;
609 int sysctl_info[8];
610 struct statfs sfs;
611 off_t jstart, jsize;
612
613 if (statfs(volname, &sfs) != 0) {
614 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
615 return 10;
616 }
617
618 if ((sfs.f_flags & MNT_JOURNALED) == 0) {
619 fprintf(stderr, "Volume %s is not journaled.\n", volname);
620 return 1;
621 }
622
623 if (chdir(volname) != 0) {
624 fprintf(stderr, "Can't cd to volume %s to get journal info (%s).\n",
625 volname, strerror(errno));
626 return 10;
627 }
628
629 memset(sysctl_info, 0, sizeof(sysctl_info));
630 sysctl_info[0] = CTL_VFS;
631 sysctl_info[1] = sfs.f_fsid.val[1];
632 sysctl_info[2] = HFS_GET_JOURNAL_INFO;
633 sysctl_info[3] = (int)&jstart;
634 sysctl_info[4] = (int)&jsize;
635
636 result = sysctl((void *)sysctl_info, 5, NULL, NULL, NULL, 0);
637 if (result != 0) {
638 fprintf(stderr, "Failed to get journal info for volume %s (%s)\n",
639 volname, strerror(errno));
640 return 20;
641 }
642
643 if (jsize == 0) {
644 printf("%s : not journaled.\n", volname);
645 } else {
646 printf("%s : journal size %lld k at offset 0x%llx\n", volname, jsize/1024, jstart);
647 }
648
649 return 0;
08cdaae8 650}
e74bfeef
A
651
652
653int
654RawDisableJournaling(char *devname)
655{
656 int fd = -1, ret = 0;
657 char *buff = NULL, rawdev[256];
658 u_int64_t disksize;
659 u_int64_t blkcnt;
660 u_int32_t blksize;
661 daddr_t mdb_offset;
662 HFSMasterDirectoryBlock *mdbp;
663 HFSPlusVolumeHeader *vhp;
664 off_t embeddedOffset, hdr_offset;
665 struct statfs sfs;
666 struct stat st;
667
668 restart:
669 if (stat(devname, &st) != 0) {
670 fprintf(stderr, "Could not access %s (%s)\n", devname, strerror(errno));
671 ret = -1;
672 goto out;
673 }
674
675 if (S_ISCHR(st.st_mode) == 0) {
676 // hmmm, it's not the character special raw device so we
677 // should try to figure out the real device.
678 if (S_ISBLK(st.st_mode)) {
679 strcpy(rawdev, "/dev/r");
680 strcat(rawdev, devname + 5);
681 } else {
682 if (statfs(devname, &sfs) != 0) {
683 fprintf(stderr, "Can't find out any info about the fs for path %s (%s)\n",
684 devname, strerror(errno));
685 ret = -1;
686 goto out;
687 }
688
689 // copy the "/dev/"
690 strncpy(rawdev, sfs.f_mntfromname, 5);
691 rawdev[5] = 'r';
692 strcpy(&rawdev[6], &sfs.f_mntfromname[5]);
693 }
694
695 devname = &rawdev[0];
696 goto restart;
697 }
698
699 fd = open(devname, O_RDWR);
700 if (fd < 0) {
701 fprintf(stderr, "can't open: %s (%s)\n", devname, strerror(errno));
702 ret = -1;
703 goto out;
704 }
705
706 /* Get the real physical block size. */
707 if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) {
708 fprintf(stderr, "can't get the device block size (%s). assuming 512\n", strerror(errno));
709 blksize = 512;
710 ret = -1;
711 goto out;
712 }
713
714 /* Get the number of physical blocks. */
715 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) {
716 struct stat st;
717
718 if (fstat(fd, &st) != 0) {
719 ret = -1;
720 goto out;
721 }
722
723 blkcnt = st.st_size / blksize;
724 }
725
726 /* Compute an accurate disk size */
727 disksize = blkcnt * (u_int64_t)blksize;
728
729 /*
730 * There are only 31 bits worth of block count in
731 * the buffer cache. So for large volumes a 4K
732 * physical block size is needed.
733 */
734 if (blksize == 512 && blkcnt > (u_int64_t)0x000000007fffffff) {
735 blksize = 4096;
736 }
737
738 /*
739 * At this point:
740 * blksize has our prefered physical block size
741 * blkcnt has the total number of physical blocks
742 */
743
744 buff = (char *)malloc(blksize);
745
746 hdr_offset = HFS_PRI_SECTOR(blksize)*blksize;
747 if (pread(fd, buff, blksize, hdr_offset) != blksize) {
748 fprintf(stderr, "failed to read volume header @ offset %lld (%s)\n",
749 hdr_offset, strerror(errno));
750 ret = -1;
751 goto out;
752 }
753
754 // if we read in an empty bunch of junk at location zero, then
755 // retry at offset 0x400 which is where the header normally is.
756 if (*(int *)buff == 0 && hdr_offset == 0) {
757 hdr_offset = 0x400;
758 if (pread(fd, buff, blksize, hdr_offset) != blksize) {
759 fprintf(stderr, "failed to read volume header @ offset %lld (%s)\n",
760 hdr_offset, strerror(errno));
761 ret = -1;
762 goto out;
763 }
764 }
765
766
767
768 mdbp = (HFSMasterDirectoryBlock *)buff;
769 if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) {
770 // normal hfs can not ever be journaled
771 fprintf(stderr, "disable_journaling: volume is only regular HFS, not HFS+\n");
772 goto out;
773 }
774
775 /* Get the embedded Volume Header */
776 if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) {
777 embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * 512;
778 embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
779
780 /*
781 * If the embedded volume doesn't start on a block
782 * boundary, then switch the device to a 512-byte
783 * block size so everything will line up on a block
784 * boundary.
785 */
786 if ((embeddedOffset % blksize) != 0) {
787 fprintf(stderr, "HFS Mount: embedded volume offset not"
788 " a multiple of physical block size (%d);"
789 " switching to 512\n", blksize);
790
791 blkcnt *= (blksize / 512);
792 blksize = 512;
793 }
794
795 disksize = (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.blockCount) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
796
797 mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize);
798 hdr_offset = mdb_offset * blksize;
799 if (pread(fd, buff, blksize, hdr_offset) != blksize) {
800 fprintf(stderr, "failed to read the embedded vhp @ offset %d\n", mdb_offset * blksize);
801 ret = -1;
802 goto out;
803 }
804
805 vhp = (HFSPlusVolumeHeader*) buff;
806 } else /* pure HFS+ */ {
807 embeddedOffset = 0;
808 vhp = (HFSPlusVolumeHeader*) mdbp;
809 }
810
811
812 if ((SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) != 0) {
813 unsigned int tmp = SWAP_BE32(vhp->attributes);
814
815 tmp &= ~kHFSVolumeJournaledMask;
816 vhp->attributes = SWAP_BE32(tmp);
817 if ((tmp = pwrite(fd, buff, blksize, hdr_offset)) != blksize) {
818 fprintf(stderr, "Update of super-block on %s failed! (%d != %d, %s)\n",
819 devname, tmp, blksize, strerror(errno));
820 } else {
821 fprintf(stderr, "Turned off the journaling bit for %s\n", devname);
822 }
823 } else {
824 fprintf(stderr, "disable_journaling: volume was not journaled.\n");
825 }
826
827
828 out:
829 if (buff)
830 free(buff);
831 if (fd >= 0)
832 close(fd);
833
834 return ret;
835}