]>
git.saurik.com Git - apple/hfs.git/blob - fsck_hfs/fsck_hfs.c
2 * Copyright (c) 1999-2000, 2002-2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/ucred.h>
27 #include <sys/mount.h>
28 #include <sys/ioctl.h>
30 #include <sys/sysctl.h>
34 #include <hfs/hfs_mount.h>
45 #include <TargetConditionals.h>
48 #include "fsck_msgnums.h"
49 #include "fsck_hfs_msgnums.h"
51 #include "fsck_debug.h"
52 #include "dfalib/CheckHFS.h"
55 * These definitions are duplicated from xnu's hfs_readwrite.c, and could live
56 * in a shared header file if desired. On the other hand, the freeze and thaw
57 * commands are not really supposed to be public.
60 #define F_FREEZE_FS 53 /* "freeze" all fs operations */
61 #define F_THAW_FS 54 /* "thaw" all fs operations */
64 /* Global Variables for front end */
65 const char *cdevname
; /* name of device being checked */
67 char lflag
; /* live fsck */
68 char nflag
; /* assume a no response */
69 char yflag
; /* assume a yes response */
70 char preen
; /* just fix normal inconsistencies */
71 char force
; /* force fsck even if clean (preen only) */
72 char quick
; /* quick check returns clean, dirty, or failure */
73 char debug
; /* output debugging info */
74 char disable_journal
; /* If debug, and set, do not simulate journal replay */
75 char scanflag
; /* Scan entire disk for bad blocks */
82 char hotroot
; /* checking root device */
83 char hotmount
; /* checking read-only mounted device */
84 char guiControl
; /* this app should output info for gui control */
85 char xmlControl
; /* Output XML (plist) messages -- implies guiControl as well */
86 char rebuildBTree
; /* Rebuild requested btree files */
87 int rebuildOptions
; /* Options to indicate which btree should be rebuilt */
88 char modeSetting
; /* set the mode when creating "lost+found" directory */
89 char errorOnExit
= 0; /* Exit on first error */
90 int upgrading
; /* upgrading format */
91 int lostAndFoundMode
= 0; /* octal mode used when creating "lost+found" directory */
92 uint64_t reqCacheSize
; /* Cache size requested by the caller (may be specified by the user via -c) */
93 int detonator_run
= 0;
95 int fsmodified
; /* 1 => write done to file system */
96 int fsreadfd
; /* file descriptor for reading file system */
97 int fswritefd
; /* file descriptor for writing file system */
101 * Variables used to map physical block numbers to file paths
103 enum { BLOCK_LIST_INCREMENT
= 512 };
104 int gBlkListEntries
= 0;
105 u_int64_t
*gBlockList
= NULL
;
106 int gFoundBlockEntries
= 0;
107 struct found_blocks
*gFoundBlocksList
= NULL
;
108 long gBlockSize
= 512;
109 static void ScanDisk(int);
110 static int getblocklist(const char *filepath
);
113 static int checkfilesys
__P((char * filesys
));
114 static int setup
__P(( char *dev
, int *canWritePtr
));
115 static void usage
__P((void));
116 static void getWriteAccess
__P(( char *dev
, int *canWritePtr
));
117 extern char *unrawname
__P((char *name
));
130 if ((progname
= strrchr(*argv
, '/')))
135 while ((ch
= getopt(argc
, argv
, "b:B:c:D:e:Edfglm:npqrR:SuyxJ")) != EOF
) {
138 gBlockSize
= atoi(optarg
);
139 if ((gBlockSize
< 512) || (gBlockSize
& (gBlockSize
-1))) {
140 (void) fprintf(stderr
, "%s invalid block size %d\n",
141 progname
, gBlockSize
);
149 getblocklist(optarg
);
152 /* Cache size to use in fsck_hfs */
153 reqCacheSize
= strtoull(optarg
, &lastChar
, 0);
155 switch (tolower(*lastChar
)) {
157 reqCacheSize
*= 1024ULL;
160 reqCacheSize
*= 1024ULL;
163 reqCacheSize
*= 1024ULL;
180 /* Input value should be in hex example: -D 0x5 */
181 cur_debug_level
= strtoul(optarg
, NULL
, 0);
182 if (cur_debug_level
== 0) {
183 (void) fplog (stderr
, "%s: invalid debug development argument. Assuming zero\n", progname
);
189 if (strcasecmp(optarg
, "embedded") == 0)
191 else if (strcasecmp(optarg
, "desktop") == 0)
197 /* Exit on first error, after logging it */
222 lostAndFoundMode
= strtol( optarg
, NULL
, 8 );
223 if ( lostAndFoundMode
== 0 )
225 (void) fplog(stderr
, "%s: invalid mode argument \n", progname
);
244 // rebuild catalog btree
246 rebuildOptions
|= REBUILD_CATALOG
;
255 // rebuild attribute btree
257 rebuildOptions
|= REBUILD_ATTRIBUTE
;
261 // rebuild catalog btree
263 rebuildOptions
|= REBUILD_CATALOG
;
267 // rebuild extents overflow btree
269 rebuildOptions
|= REBUILD_EXTENTS
;
273 fprintf(stderr
, "%s: unknown btree rebuild code `%c' (%#x)\n", progname
, *cp
, *cp
);
296 if (debug
== 0 && disable_journal
!= 0)
299 if (gBlkListEntries
!= 0 && gBlockSize
== 0)
303 debug
= 0; /* debugging is for command line only */
305 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
306 (void)signal(SIGINT
, catch);
309 (void) fplog(stderr
, "%s: missing special-device\n", progname
);
315 char *pcBlkChk
= blockcheck(*argv
++);
316 ret
|= checkfilesys(pcBlkChk
);
322 int fs_fd
=-1; // fd to the root-dir of the fs we're checking (only w/lfag == 1)
328 fcntl(fs_fd
, F_THAW_FS
, NULL
);
335 mountpoint(const char *cdev
)
338 struct statfs
*fsinfo
;
346 unraw
= strdup(cdev
);
352 result
= getmntinfo(&fsinfo
, MNT_NOWAIT
);
354 for (i
= 0; i
< result
; i
++) {
355 if (strcmp(unraw
, fsinfo
[i
].f_mntfromname
) == 0) {
356 retval
= strdup(fsinfo
[i
].f_mntonname
);
369 checkfilesys(char * filesys
)
373 int chkLev
, repLev
, logLev
;
375 char *mntonname
= NULL
;
376 fsck_ctx_t context
= NULL
;
380 hotmount
= hotroot
; // hotroot will be 1 or 0 by this time
383 // initialize the printing/logging without actually printing anything
384 // DO NOT DELETE THIS or else you can deadlock during a live fsck
385 // when something is printed and we try to create the log file.
389 context
= fsckCreate();
391 mntonname
= mountpoint(cdevname
);
395 mntonname
= strdup("/");
398 if (lflag
&& !detonator_run
) {
402 * Ensure that, if we're doing a live verify, that we're not trying
403 * to do input or output to the same device. This would cause a deadlock.
406 if (stat(cdevname
, &fs_stat
) != -1 &&
407 (((fs_stat
.st_mode
& S_IFMT
) == S_IFCHR
) ||
408 ((fs_stat
.st_mode
& S_IFMT
) == S_IFBLK
))) {
411 if (fstat(fileno(stdin
), &io_stat
) != -1 &&
412 (fs_stat
.st_rdev
== io_stat
.st_dev
)) {
413 plog("ERROR: input redirected from target volume for live verify.\n");
416 if (fstat(fileno(stdout
), &io_stat
) != -1 &&
417 (fs_stat
.st_rdev
== io_stat
.st_dev
)) {
418 plog("ERROR: output redirected to target volume for live verify.\n");
421 if (fstat(fileno(stderr
), &io_stat
) != -1 &&
422 (fs_stat
.st_rdev
== io_stat
.st_dev
)) {
423 plog("ERROR: error output redirected to target volume for live verify.\n");
431 * If the device is mounted somewhere, then we need to make sure that it's
432 * a read-only device, or that a live-verify has been requested.
434 if (mntonname
!= NULL
) {
435 struct statfs stfs_buf
;
437 if (statfs(mntonname
, &stfs_buf
) == 0) {
439 // Need to try to freeze it
440 fs_fd
= open(mntonname
, O_RDONLY
);
442 plog("ERROR: could not open %s to freeze the volume.\n", mntonname
);
447 if (fcntl(fs_fd
, F_FREEZE_FS
, NULL
) != 0) {
449 plog("ERROR: could not freeze volume (%s)\n", strerror(errno
));
452 } else if (stfs_buf
.f_flags
& MNT_RDONLY
) {
455 /* MNT_RDONLY is not set and this is not a live verification */
456 plog("ERROR: volume %s is mounted with write access. Re-run with (-l) to freeze volume.\n", mntonname
);
465 if (setup( filesys
, &canWrite
) == 0) {
467 pfatal("CAN'T CHECK FILE SYSTEM.");
469 goto ExitThisRoutine
;
473 if (hotroot
&& !guiControl
)
474 plog("** Root file system\n");
477 /* start with defaults for dfa back-end */
478 chkLev
= kAlwaysCheck
;
479 repLev
= kMajorRepairs
;
480 logLev
= kVerboseLog
;
483 repLev
= kMajorRepairs
;
486 chkLev
= kNeverCheck
;
487 repLev
= kNeverRepair
;
490 chkLev
= kForceCheck
;
493 repLev
= kMinorRepairs
;
494 chkLev
= force
? kAlwaysCheck
: kDirtyCheck
;
501 repLev
= kNeverRepair
;
503 if ( rebuildBTree
) {
504 chkLev
= kPartialCheck
;
505 repLev
= kForceRepairs
; // this will force rebuild of B-Tree file
508 fsckSetVerbosity(context
, logLev
);
509 /* All of fsck_hfs' output should go thorugh logstring */
510 fsckSetOutput(context
, NULL
);
511 /* Setup writer that will output to standard out */
512 fsckSetWriter(context
, &outstring
);
514 /* Setup logger that will write to log file */
515 fsckSetLogger(context
, &logstring
);
518 fsckSetOutputStyle(context
, fsckOutputXML
);
520 fsckSetOutputStyle(context
, fsckOutputGUI
);
522 fsckSetOutputStyle(context
, fsckOutputTraditional
);
525 if (errorOnExit
&& nflag
) {
526 chkLev
= kMajorCheck
;
530 * go check HFS volume...
533 if (rebuildOptions
&& canWrite
== 0) {
534 plog("BTree rebuild requested but writing disabled\n");
536 goto ExitThisRoutine
;
539 if (gBlockList
!= NULL
&& scanflag
!= 0) {
540 plog("Cannot scan for bad blocks and ask for listed blocks to file mapping\n");
542 goto ExitThisRoutine
;
545 plog("Scanning entire disk for bad blocks\n");
549 result
= CheckHFS( filesys
, fsreadfd
, fswritefd
, chkLev
, repLev
, context
,
550 lostAndFoundMode
, canWrite
, &fsmodified
,
551 lflag
, rebuildOptions
);
553 plog("\tCheckHFS returned %d, fsmodified = %d\n", result
, fsmodified
);
559 pwarn("QUICKCHECK ONLY; FILESYSTEM CLEAN\n");
561 goto ExitThisRoutine
;
562 } else if (result
== R_Dirty
) {
563 pwarn("QUICKCHECK ONLY; FILESYSTEM DIRTY\n");
565 goto ExitThisRoutine
;
566 } else if (result
== R_BadSig
) {
567 pwarn("QUICKCHECK ONLY; NO HFS SIGNATURE FOUND\n");
569 goto ExitThisRoutine
;
572 goto ExitThisRoutine
;
576 struct statfs stfs_buf
;
579 * Check to see if root is mounted read-write.
581 if (statfs(mntonname
, &stfs_buf
) == 0)
582 flags
= stfs_buf
.f_flags
;
585 ckfini(flags
& MNT_RDONLY
);
588 /* XXX free any allocated memory here */
590 if (hotmount
&& fsmodified
) {
591 struct hfs_mount_args args
;
593 * We modified the root. Do a mount update on
594 * it, unless it is read-write, so we can continue.
597 fsckPrint(context
, fsckVolumeModified
);
598 if (flags
& MNT_RDONLY
) {
599 bzero(&args
, sizeof(args
));
600 flags
|= MNT_UPDATE
| MNT_RELOAD
;
602 fprintf(stderr
, "doing update / reload mount for %s now\n", mntonname
);
603 if (mount("hfs", mntonname
, flags
, &args
) == 0) {
606 goto ExitThisRoutine
;
609 fprintf(stderr
, "update/reload mount for %s failed: %s\n", mntonname
, strerror(errno
));
613 plog("\n***** REBOOT NOW *****\n");
615 result
= FIXEDROOTEXIT
;
616 goto ExitThisRoutine
;
619 if (result
!= 0 && result
!= MAJOREXIT
)
625 fcntl(fs_fd
, F_THAW_FS
, NULL
);
634 fsckDestroy(context
);
641 * Setup for I/O to device
642 * Return 1 if successful, 0 if unsuccessful.
643 * canWrite - 1 if we can safely write to the raw device or 0 if not.
646 setup( char *dev
, int *canWritePtr
)
650 uint32_t cacheBlockSize
;
651 uint32_t cacheTotalBlocks
;
659 if (stat(dev
, &statb
) < 0) {
660 plog("Can't stat %s: %s\n", dev
, strerror(errno
));
663 if ((statb
.st_mode
& S_IFMT
) != S_IFCHR
) {
664 pfatal("%s is not a character device", dev
);
665 if (reply("CONTINUE") == 0)
668 /* Always attempt to replay the journal */
669 if (!nflag
&& !quick
) {
670 // We know we have a character device by now.
671 if (strncmp(dev
, "/dev/rdisk", 10) == 0) {
672 char block_device
[MAXPATHLEN
+1];
674 snprintf(block_device
, sizeof(block_device
), "/dev/%s", dev
+ 6);
675 rv
= journal_replay(block_device
);
677 plog("journal_replay(%s) returned %d\n", block_device
, rv
);
680 /* attempt to get write access to the block device and if not check if volume is */
681 /* mounted read-only. */
682 if (nflag
== 0 && quick
== 0) {
683 getWriteAccess( dev
, canWritePtr
);
686 if (nflag
|| quick
|| (fswritefd
= open(dev
, O_RDWR
| (hotmount
? 0 : O_EXLOCK
))) < 0) {
689 pfatal("** %s (NO WRITE ACCESS)\n", dev
);
692 } else { // detonator run
693 plog("fsck_hfs: detonator_run (%s).\n", dev
);
695 fswritefd
= (int)strtol(dev
+8, &end_ptr
, 10);
698 err(1, "fsck_hfs: Invalid file descriptor path: %s", dev
);
702 int error
= fstat(fswritefd
, &info
);
705 err(1, "fsck_hfs: fstat %s", dev
);
708 error
= lseek(fswritefd
, 0, SEEK_SET
);
711 err(1, "fsck_hfs: Could not seek %d for dev: %s, errorno %d", fswritefd
, dev
, errno
);
717 if (preen
== 0 && !guiControl
) {
718 if (nflag
|| quick
|| fswritefd
== -1) {
719 plog("** %s (NO WRITE)\n", dev
);
721 plog("** %s\n", dev
);
725 if (fswritefd
== -1) {
726 if ((fsreadfd
= open(dev
, O_RDONLY
)) < 0) {
727 plog("Can't open %s: %s\n", dev
, strerror(errno
));
731 fsreadfd
= dup(fswritefd
);
733 plog("Can't dup fd for reading on %s: %s\n", dev
, strerror(errno
));
739 /* Get device block size to initialize cache */
740 if (ioctl(fsreadfd
, DKIOCGETBLOCKSIZE
, &devBlockSize
) < 0) {
741 pfatal ("Can't get device block size\n");
746 * Calculate the cache block size and total blocks.
748 * If a quick check was requested, we'll only be checking to see if
749 * the volume was cleanly unmounted or journalled, so we won't need
750 * a lot of cache. Since lots of quick checks can be run in parallel
751 * when a new disk with several partitions comes on line, let's avoid
752 * the memory usage when we don't need it.
754 if (reqCacheSize
== 0 && quick
== 0) {
756 * Auto-pick the cache size. The cache code will deal with minimum
757 * maximum values, so we just need to find out the size of memory, and
758 * how much of it we'll use.
760 * If we're looking at the root device, and it's not a live verify (lflag),
761 * then we will use half of physical memory; otherwise, we'll use an eigth.
765 size_t dsize
= sizeof(memSize
);
768 rv
= sysctlbyname("hw.memsize", &memSize
, &dsize
, NULL
, 0);
770 (void)fplog(stderr
, "sysctlbyname failed, not auto-setting cache size\n");
772 int d
= (hotroot
&& !lflag
) ? 2 : 8;
773 if (!detonator_run
) {
775 dsize
= sizeof(safeMode
);
776 rv
= sysctlbyname("kern.safeboot", &safeMode
, &dsize
, NULL
, 0);
777 if (rv
!= -1 && safeMode
!= 0 && hotroot
&& !lflag
) {
778 #define kMaxSafeModeMem ((size_t)2 * 1024 * 1024 * 1024) /* 2Gbytes, means cache will max out at 1gbyte */
780 (void)fplog(stderr
, "Safe mode and single-user, setting memsize to a maximum of 2gbytes\n");
782 memSize
= (memSize
< kMaxSafeModeMem
) ? memSize
: kMaxSafeModeMem
;
785 reqCacheSize
= memSize
/ d
;
789 CalculateCacheSizes(reqCacheSize
, &cacheBlockSize
, &cacheTotalBlocks
, debug
);
791 preTouchMem
= (hotroot
!= 0) && (lflag
!= 0);
792 /* Initialize the cache */
793 if (CacheInit (&fscache
, fsreadfd
, fswritefd
, devBlockSize
,
794 cacheBlockSize
, cacheTotalBlocks
, CacheHashSize
, preTouchMem
) != EOK
) {
795 pfatal("Can't initialize disk cache\n");
804 // This routine will attempt to open the block device with write access for the target
805 // volume in order to block others from mounting the volume with write access while we
806 // check / repair it. If we cannot get write access then we check to see if the volume
807 // has been mounted read-only. If it is read-only then we should be OK to write to
808 // the raw device. Note that this does not protect use from someone upgrading the mount
809 // from read-only to read-write.
811 static void getWriteAccess( char *dev
, int *canWritePtr
)
817 struct statfs
* myBufPtr
;
819 int blockDevice_fd
= -1;
822 myNamePtr
= malloc( strlen(dev
) + 2 );
823 if ( myNamePtr
== NULL
)
826 strcpy( (char *)myNamePtr
, dev
);
827 if ( (myCharPtr
= strrchr( (char *)myNamePtr
, '/' )) != 0 ) {
828 if ( myCharPtr
[1] == 'r' ) {
829 memmove(&myCharPtr
[1], &myCharPtr
[2], strlen(&myCharPtr
[2]) + 1);
830 blockDevice_fd
= open( (char *)myNamePtr
, O_WRONLY
| (hotmount
? 0 : O_EXLOCK
) );
834 if ( blockDevice_fd
> 0 ) {
835 // we got write access to the block device so we can safely write to raw device
837 goto ExitThisRoutine
;
840 // get count of mounts then get the info for each
841 myMountsCount
= getfsstat( NULL
, 0, MNT_NOWAIT
);
842 if ( myMountsCount
< 0 )
843 goto ExitThisRoutine
;
845 myPtr
= (void *) malloc( sizeof(struct statfs
) * myMountsCount
);
847 goto ExitThisRoutine
;
848 myMountsCount
= getfsstat( myPtr
,
849 (int)(sizeof(struct statfs
) * myMountsCount
),
851 if ( myMountsCount
< 0 )
852 goto ExitThisRoutine
;
854 myBufPtr
= (struct statfs
*) myPtr
;
855 for ( i
= 0; i
< myMountsCount
; i
++ )
857 if ( strcmp( myBufPtr
->f_mntfromname
, myNamePtr
) == 0 ) {
858 if ( myBufPtr
->f_flags
& MNT_RDONLY
)
860 goto ExitThisRoutine
;
864 *canWritePtr
= 1; // single user will get us here, f_mntfromname is not /dev/diskXXXX
870 if ( myNamePtr
!= NULL
)
873 if (blockDevice_fd
!= -1) {
874 close(blockDevice_fd
);
879 } /* getWriteAccess */
885 (void) fplog(stderr
, "usage: %s [-b [size] B [path] c [size] e [mode] ESdfglx m [mode] npqruy] special-device\n", progname
);
886 (void) fplog(stderr
, " b size = size of physical blocks (in bytes) for -B option\n");
887 (void) fplog(stderr
, " B path = file containing physical block numbers to map to paths\n");
888 (void) fplog(stderr
, " c size = cache size (ex. 512m, 1g)\n");
889 (void) fplog(stderr
, " e mode = emulate 'embedded' or 'desktop'\n");
890 (void) fplog(stderr
, " E = exit on first major error\n");
891 (void) fplog(stderr
, " d = output debugging info\n");
892 (void) fplog(stderr
, " f = force fsck even if clean (preen only) \n");
893 (void) fplog(stderr
, " g = GUI output mode\n");
894 (void) fplog(stderr
, " x = XML output mode\n");
895 (void) fplog(stderr
, " l = live fsck (lock down and test-only)\n");
896 (void) fplog(stderr
, " m arg = octal mode used when creating lost+found directory \n");
897 (void) fplog(stderr
, " n = assume a no response \n");
898 (void) fplog(stderr
, " p = just fix normal inconsistencies \n");
899 (void) fplog(stderr
, " q = quick check returns clean, dirty, or failure \n");
900 (void) fplog(stderr
, " r = rebuild catalog btree \n");
901 (void) fplog(stderr
, " S = Scan disk for bad blocks\n");
902 (void) fplog(stderr
, " u = usage \n");
903 (void) fplog(stderr
, " y = assume a yes response \n");
910 AddBlockToList(long long block
)
913 if ((gBlkListEntries
% BLOCK_LIST_INCREMENT
) == 0) {
916 // gBlkListEntries += BLOCK_LIST_INCREMENT;
917 tmp
= realloc(gBlockList
, (gBlkListEntries
+ BLOCK_LIST_INCREMENT
) * sizeof(u_int64_t
));
919 pfatal("Can't allocate memory for block list (%llu entries).\n", gBlkListEntries
);
921 gBlockList
= (u_int64_t
*)tmp
;
923 gBlockList
[gBlkListEntries
++] = block
;
927 static int printStatus
;
937 uint32_t devBlockSize
= 512;
938 uint64_t devBlockTotal
;
940 uint8_t *buffer
= NULL
;
941 size_t bufSize
= 1024 * 1024;
944 void (*oldhandler
)(int);
945 uint32_t numErrors
= 0;
946 uint32_t maxErrors
= 40; // Something more variable?
948 oldhandler
= signal(SIGINFO
, &siginfo
);
953 fprintf(stderr, "Scanning offset %lld of %lld (%d%%)\n", \
954 curPos, diskSize, (int)((curPos * 100) / diskSize)); \
956 fprintf(stderr, "Scanning offset %lld\n", curPos); \
961 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, &devBlockSize
) == -1) {
965 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, &devBlockTotal
) == -1) {
968 diskSize
= devBlockTotal
* devBlockSize
;
970 while (buffer
== NULL
&& bufSize
>= devBlockSize
) {
971 buffer
= malloc(bufSize
);
972 if (buffer
== NULL
) {
976 if (buffer
== NULL
) {
977 pfatal("Cannot allocate buffer for disk scan.\n");
985 while ((nread
= pread(fd
, buffer
, bufSize
, curPos
)) == bufSize
) {
993 /* We're done with the disk */
998 /* Try reading devBlockSize blocks */
1000 for (total
= 0; total
< bufSize
; total
+= devBlockSize
) {
1001 nread
= pread(fd
, buffer
, devBlockSize
, curPos
+ total
);
1005 fprintf(stderr
, "Bad block at offset %lld\n", curPos
+ total
);
1006 AddBlockToList((curPos
+ total
) / gBlockSize
);
1007 if (++numErrors
> maxErrors
) {
1009 fprintf(stderr
, "Got %u errors, maxing out so stopping scan\n", numErrors
);
1014 pfatal("Got a non I/O error reading disk at offset %llu: %s\n",
1015 curPos
+ total
, strerror(errno
));
1016 // Hey, pfatal wasn't fatal!
1017 // But that seems to work out for us for some reason.
1021 /* End of disk, somehow. */
1024 if (nread
!= devBlockSize
) {
1025 pwarn("During disk scan, did not get block size (%zd) read, got %zd instead. Skipping rest of this block.\n", (size_t)devBlockSize
, nread
);
1031 } else if (errno
== EINTR
) {
1034 pfatal("Got a non I/O error reading disk at offset %llu: %s\n", curPos
, strerror(errno
));
1038 if (nread
< bufSize
) {
1039 if ((nread
% devBlockSize
) == 0) {
1042 curPos
= curPos
+ (((nread
% devBlockSize
) + 1) * devBlockSize
);
1050 signal(SIGINFO
, oldhandler
);
1056 getblocklist(const char *filepath
)
1060 size_t blockListCount
; /* Number of elements allocated to gBlockList array */
1062 blockListCount
= BLOCK_LIST_INCREMENT
;
1063 gBlockList
= (u_int64_t
*) malloc(blockListCount
* sizeof(u_int64_t
));
1064 if (gBlockList
== NULL
)
1065 pfatal("Can't allocate memory for block list.\n");
1067 // printf("getblocklist: processing blocklist %s...\n", filepath);
1069 if ((file
= fopen(filepath
, "r")) == NULL
)
1070 pfatal("Can't open %s\n", filepath
);
1072 while (fscanf(file
, "%lli", &block
) > 0) {
1073 AddBlockToList(block
);
1076 (void) fclose(file
);
1078 printf("%d blocks to match:\n", gBlkListEntries
);