]> git.saurik.com Git - apple/hfs.git/blob - hfs_util/hfsutil_jnl.c
hfs-157.tar.gz
[apple/hfs.git] / hfs_util / hfsutil_jnl.c
1 /*
2 * Copyright (c) 1999-2001 Apple Computer, 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 <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
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
68
69 /* getattrlist buffers start with an extra length field */
70 struct ExtentsAttrBuf {
71 unsigned long infoLength;
72 HFSPlusExtentRecord extents;
73 };
74 typedef struct ExtentsAttrBuf ExtentsAttrBuf;
75
76
77
78 #define kIsInvisible 0x4000
79
80 /*
81 * Generic Finder file/dir data
82 */
83 struct FinderInfo {
84 u_int32_t opaque_1[2];
85 u_int16_t fdFlags; /* Finder flags */
86 int16_t opaque_2[11];
87 };
88 typedef struct FinderInfo FinderInfo;
89
90 /* getattrlist buffers start with an extra length field */
91 struct FinderAttrBuf {
92 unsigned long infoLength;
93 FinderInfo finderInfo;
94 };
95 typedef struct FinderAttrBuf FinderAttrBuf;
96
97
98 int 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) {
109 return (errno);
110 }
111
112 if (finderInfoBuf.finderInfo.fdFlags & kIsInvisible) {
113 printf("hide: %s is alreadly invisible\n", file);
114 return (0);
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
124 off_t
125 get_start_block(const char *file, uint32_t fs_block_size)
126 {
127 off_t cur_pos, phys_start, len;
128 int fd, err;
129 struct log2phys l2p;
130 struct stat st;
131
132 fd = open(file, O_RDONLY);
133 if (fd < 0) {
134 return -1;
135 }
136
137 if (fstat(fd, &st) < 0) {
138 fprintf(stderr, "can't stat %s (%s)\n", file, strerror(errno));
139 close(fd);
140 return -1;
141 }
142
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);
173 return -1;
174 }
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
197 off_t
198 get_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 }
234
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
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;
323 }
324
325
326
327 static const char *journal_fname = ".journal";
328 static const char *jib_fname = ".journal_info_block";
329
330 int
331 DoMakeJournaled(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;
337 int32_t jstart_block, jinfo_block;
338 int sysctl_info[8];
339 JournalInfoBlock jib;
340 struct statfs sfs;
341 static char tmpname[MAXPATHLEN];
342 off_t start_block, embedded_offset;
343
344 if (statfs(volname, &sfs) != 0) {
345 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
346 return 10;
347 }
348
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);
361
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;
371
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;
380 }
381 }
382
383 if (chdir(volname) != 0) {
384 fprintf(stderr, "Can't locate volume %s to make it journaled (%s).\n",
385 volname, strerror(errno));
386 return 10;
387 }
388
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
398 fd = open(journal_fname, O_CREAT|O_TRUNC|O_RDWR, 000);
399 if (fd < 0) {
400 fprintf(stderr, "Can't create journal file on volume %s (%s)\n",
401 volname, strerror(errno));
402 return 5;
403 }
404
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);
410
411 block_size = sfs.f_bsize;
412 if ((journal_size % block_size) != 0) {
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;
418 }
419
420 retry:
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) {
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 }
441 }
442
443 printf("Allocated %lldK for journal file.\n", fst.fst_bytesalloc/1024LL);
444 buf = (char *)calloc(block_size, 1);
445 if (buf) {
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 }
452
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 }
457 } else {
458 printf("Could not allocate memory to write to the journal on volume %s (%s)\n",
459 volname, strerror(errno));
460 }
461
462 fsync(fd);
463 close(fd);
464 hide_file(journal_fname);
465
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);
474
475 memset(&jib, 'Z', sizeof(jib));
476 jib.flags = kJIJournalInFSMask;
477 jib.offset = (off_t)((unsigned)jstart_block) * (off_t)((unsigned)block_size);
478 jib.size = (off_t)((unsigned)journal_size);
479
480 fd = open(jib_fname, O_CREAT|O_TRUNC|O_RDWR, 000);
481 if (fd < 0) {
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;
486 }
487
488 // swap the data before we copy it
489 jib.flags = NXSwapBigLongToHost(jib.flags);
490 jib.offset = NXSwapBigLongLongToHost(jib.offset);
491 jib.size = NXSwapBigLongLongToHost(jib.size);
492
493 memcpy(buf, &jib, sizeof(jib));
494
495 // now put it back the way it was
496 jib.size = NXSwapBigLongLongToHost(jib.size);
497 jib.offset = NXSwapBigLongLongToHost(jib.offset);
498 jib.flags = NXSwapBigLongToHost(jib.flags);
499
500 if (write(fd, buf, block_size) != block_size) {
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;
505 }
506
507 fsync(fd);
508 close(fd);
509 hide_file(jib_fname);
510
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);
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];
528 sysctl_info[2] = HFS_ENABLE_JOURNALING;
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) {
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;
545 }
546
547 return 0;
548 }
549
550
551 int
552 DoUnJournal(char *volname)
553 {
554 int result;
555 int sysctl_info[8];
556 struct statfs sfs;
557 char jbuf[MAXPATHLEN];
558
559 if (statfs(volname, &sfs) != 0) {
560 fprintf(stderr, "Can't stat volume %s (%s).\n", volname, strerror(errno));
561 return 10;
562 }
563
564 if ((sfs.f_flags & MNT_JOURNALED) == 0) {
565 fprintf(stderr, "Volume %s is not journaled.\n", volname);
566 return 1;
567 }
568
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 }
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_DISABLE_JOURNALING;
579
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 }
586
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 }
592
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 }
598
599 printf("Journaling disabled on %s\n", volname);
600
601 return 0;
602 }
603
604
605 int
606 DoGetJournalInfo(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;
650 }