2 * Copyright (c) 1999-2000, 2002, 2004, 2007-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@
24 * Copyright (c) 1980, 1986, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/types.h>
58 #include <sys/errno.h>
59 #include <sys/syslimits.h>
68 #include <sys/sysctl.h>
72 char *rawname
__P((char *name
));
73 char *unrawname
__P((char *name
));
83 pfatal("INTERNAL ERROR: GOT TO reply()");
84 persevere
= !strcmp(question
, "CONTINUE");
86 if (!persevere
&& (nflag
|| fswritefd
< 0)) {
87 plog("%s? no\n\n", question
);
90 if (yflag
|| (persevere
&& nflag
)) {
91 plog("%s? yes\n\n", question
);
95 plog("%s? [yn] ", question
);
96 (void) fflush(stdout
);
98 while (c
!= '\n' && getc(stdin
) != '\n')
101 } while (c
!= 'y' && c
!= 'Y' && c
!= 'n' && c
!= 'N');
103 if (c
== 'y' || c
== 'Y')
113 // register struct bufarea *bp, *nbp;
114 // int ofsmodified, cnt = 0;
116 (void) CacheDestroy(&fscache
);
119 (void)close(fsreadfd
);
123 flush(fswritefd
, &sblk
);
124 if (havesb
&& sblk
.b_bno
!= SBOFF
/ dev_bsize
&&
125 !preen
&& reply("UPDATE STANDARD SUPERBLOCK")) {
126 sblk
.b_bno
= SBOFF
/ dev_bsize
;
128 flush(fswritefd
, &sblk
);
130 flush(fswritefd
, &cgblk
);
131 free(cgblk
.b_un
.b_buf
);
132 for (bp
= bufhead
.b_prev
; bp
&& bp
!= &bufhead
; bp
= nbp
) {
134 flush(fswritefd
, bp
);
136 free(bp
->b_un
.b_buf
);
139 if (bufhead
.b_size
!= cnt
)
140 errx(EEXIT
, "Panic: lost %d buffers", bufhead
.b_size
- cnt
);
141 pbp
= pdirbp
= (struct bufarea
*)0;
142 if (markclean
&& sblock
.fs_clean
== 0) {
145 ofsmodified
= fsmodified
;
146 flush(fswritefd
, &sblk
);
147 fsmodified
= ofsmodified
;
149 plog("\n***** FILE SYSTEM MARKED CLEAN *****\n");
152 plog("cache missed %ld of %ld (%d%%)\n", diskreads
,
153 totalreads
, (int)(diskreads
* 100 / totalreads
));
155 (void)close(fsreadfd
);
156 (void)close(fswritefd
);
161 blockcheck(char *origname
)
163 struct stat stslash
, stblock
, stchar
;
168 if (stat("/", &stslash
) < 0) {
170 plog("Can't stat root\n");
175 if (stat(newname
, &stblock
) < 0) {
177 plog("Can't stat %s\n", newname
);
180 if ((stblock
.st_mode
& S_IFMT
) == S_IFBLK
) {
181 if (stslash
.st_dev
== stblock
.st_rdev
)
183 raw
= rawname(newname
);
184 if (stat(raw
, &stchar
) < 0) {
186 plog("Can't stat %s\n", raw
);
189 if ((stchar
.st_mode
& S_IFMT
) == S_IFCHR
) {
192 plog("%s is not a character device\n", raw
);
195 } else if ((stblock
.st_mode
& S_IFMT
) == S_IFCHR
&& !retried
) {
196 newname
= unrawname(newname
);
201 * Not a block or character device, just return name and
202 * let the caller decide whether to use it.
212 static char rawbuf
[32];
215 if ((dp
= strrchr(name
, '/')) == 0)
218 (void)strlcpy(rawbuf
, name
, sizeof(rawbuf
));
220 (void)strlcat(rawbuf
, "/r", sizeof(rawbuf
));
221 (void)strlcat(rawbuf
, &dp
[1], sizeof(rawbuf
));
228 unrawname(char *name
)
233 if ((dp
= strrchr(name
, '/')) == 0)
235 if (stat(name
, &stb
) < 0)
237 if ((stb
.st_mode
& S_IFMT
) != S_IFCHR
)
241 memmove(&dp
[1], &dp
[2], strlen(&dp
[2]) + 1);
265 #define FSCK_LOG_FILE "/var/log/fsck_hfs.log"
267 extern char lflag
; // indicates if we're doing a live fsck (defined in fsck_hfs.c)
268 extern char guiControl
; // indicates if we're outputting for the gui (defined in fsck_hfs.c)
270 FILE *log_file
= NULL
;
272 /* Variables for in-memory log for strings that will be written to log file */
273 char *in_mem_log
= NULL
;
274 char *cur_in_mem_log
= NULL
;
275 size_t in_mem_log_size
= 0;
277 /* Variables for in-memory log for strings that will be printed on standard out */
278 char *in_mem_out
= NULL
;
279 char *cur_in_mem_out
= NULL
;
280 size_t in_mem_out_size
= 0;
284 #define DEFAULT_IN_MEM_SIZE 4096
286 static pthread_mutex_t mem_buf_lock
= PTHREAD_MUTEX_INITIALIZER
;
287 static pthread_cond_t mem_buf_cond
;
289 static pthread_t printing_thread
;
290 static pthread_t logging_thread
;
291 static volatile int keep_going
= 1;
297 void print_to_mem(int type
, int mem_type
, const char *fmt
, const char *str
, va_list ap
);
299 #define DO_VPRINT 1 // types for print_to_mem
302 /* Types for mem_type */
303 #define IN_MEM_LOG 1 // in-memory log strings
304 #define IN_MEM_OUT 2 // in-memory stdout strings
307 fsck_logging_thread(void *arg
)
310 char buff
[1024], *ptr
;
312 /* Handle writing to the log file */
313 while(keep_going
|| cur_in_mem_log
!= in_mem_log
) {
315 pthread_mutex_lock(&mem_buf_lock
);
316 while (keep_going
!= 0 && cur_in_mem_log
== in_mem_log
) {
319 err
= pthread_cond_wait(&mem_buf_cond
, &mem_buf_lock
);
321 fprintf(stderr
, "error %d from cond wait\n", err
);
326 copy_amt
= (cur_in_mem_log
- in_mem_log
);
328 pthread_mutex_unlock(&mem_buf_lock
);
332 if (copy_amt
>= sizeof(buff
)) {
333 copy_amt
= sizeof(buff
) - 1;
334 memcpy(buff
, in_mem_log
, copy_amt
);
336 memmove(in_mem_log
, &in_mem_log
[copy_amt
], (cur_in_mem_log
- in_mem_log
) - copy_amt
);
337 cur_in_mem_log
-= copy_amt
;
339 memcpy(buff
, in_mem_log
, copy_amt
);
340 cur_in_mem_log
= in_mem_log
;
343 buff
[copy_amt
] = '\0';
345 pthread_mutex_unlock(&mem_buf_lock
);
347 for(ptr
=buff
; *ptr
; ) {
351 while(*ptr
&& *ptr
!= '\n') {
357 fprintf(log_file
, "%s: %s\n", cdevname
? cdevname
: "UNKNOWN-DEV", start
);
361 fprintf(log_file
, "%s", start
);
374 fsck_printing_thread(void *arg
)
377 char buff
[1024], *ptr
;
379 /* Handle writing to the out file */
380 while(keep_going
|| cur_in_mem_out
!= in_mem_out
) {
382 pthread_mutex_lock(&mem_buf_lock
);
383 while (keep_going
!= 0 && cur_in_mem_out
== in_mem_out
) {
386 err
= pthread_cond_wait(&mem_buf_cond
, &mem_buf_lock
);
388 fprintf(stderr
, "error %d from cond wait\n", err
);
393 copy_amt
= (cur_in_mem_out
- in_mem_out
);
395 pthread_mutex_unlock(&mem_buf_lock
);
399 if (copy_amt
>= sizeof(buff
)) {
400 copy_amt
= sizeof(buff
) - 1;
401 memcpy(buff
, in_mem_out
, copy_amt
);
403 memmove(in_mem_out
, &in_mem_out
[copy_amt
], (cur_in_mem_out
- in_mem_out
) - copy_amt
);
404 cur_in_mem_out
-= copy_amt
;
406 memcpy(buff
, in_mem_out
, copy_amt
);
407 cur_in_mem_out
= in_mem_out
;
410 buff
[copy_amt
] = '\0';
412 pthread_mutex_unlock(&mem_buf_lock
);
414 for(ptr
=buff
; *ptr
; ) {
418 while(*ptr
&& *ptr
!= '\n') {
423 printf("%s\n", start
);
437 int was_signaled
= 0;
440 shutdown_logging(void)
445 /* Log fsck_hfs check completion time */
448 print_to_mem(DO_STR
, IN_MEM_LOG
, "fsck_hfs completed at %s\n", ctime(&t
), NULL
);
450 fprintf(log_file
, "%s: fsck_hfs completed at %s\n", cdevname
? cdevname
: "UNKNOWN-DEV", ctime(&t
));
454 // if we were signaled, we can't really call any of these
455 // functions from the context of a signal handler (which
456 // is how we're called if we don't have a signal handler).
457 // so we have our own signal handler which sets this var
458 // which tells us to just bail out.
462 if (log_file
&& !live_fsck
) {
466 } else if ((in_mem_out
|| in_mem_log
) && live_fsck
&& log_file
) {
467 // make sure the printing and logging threads are woken up...
468 pthread_mutex_lock(&mem_buf_lock
);
469 pthread_cond_broadcast(&mem_buf_cond
);
470 pthread_mutex_unlock(&mem_buf_lock
);
472 // then wait for them
473 pthread_join(printing_thread
, NULL
);
474 pthread_join(logging_thread
, NULL
);
477 in_mem_out
= cur_in_mem_out
= NULL
;
481 in_mem_log
= cur_in_mem_log
= NULL
;
489 } else if (in_mem_log
) {
493 // just in case, flush any pending output
498 // fork so that the child can wait around until the
499 // root volume is mounted read-write and we can add
500 // our output to the log
504 // if we're not root we don't need to fork
509 char *fname
= FSCK_LOG_FILE
, path
[PATH_MAX
];
511 // Disk Management waits for fsck_hfs' stdout to close rather
512 // than the process death to understand if fsck_hfs has exited
513 // or not. Since we do not use stdout any further, close all
514 // the file descriptors so that Disk Management does not wait
515 // for 60 seconds unnecessarily on read-only boot volumes.
520 // non-root will never be able to write to /var/log
521 // so point the file somewhere else.
525 // each user will get their own log as ~/Library/Logs/fsck_hfs.log
526 pwd
= getpwuid(getuid());
528 snprintf(path
, sizeof(path
), "%s/Library/Logs/fsck_hfs.log", pwd
->pw_dir
);
533 for(i
=0; i
< 60; i
++) {
534 log_file
= fopen(fname
, "a");
536 fwrite(in_mem_log
, cur_in_mem_log
- in_mem_log
, 1, log_file
);
543 in_mem_log
= cur_in_mem_log
= NULL
;
548 // hmmm, failed to open the output file so wait
549 // a while only if the fs is read-only and then
551 if (errno
== EROFS
) {
563 my_sighandler(int sig
)
574 static int at_exit_setup
= 0;
577 // if this is set, we don't have to do anything
587 // our copy of this variable since we may
588 // need to change it to make the right thing
589 // happen for fsck on the root volume.
590 live_fsck
= (int)lflag
;
592 if (log_file
== NULL
) {
593 log_file
= fopen(FSCK_LOG_FILE
, "a");
595 setlinebuf(log_file
);
598 // if we can't open the output file it's either because
599 // we're being run on the root volume during early boot
600 // or we were not run as the root user and so we can't
601 // write to /var/log/fsck_hfs.log. in either case we
602 // turn off "live_fsck" so that the right thing happens
603 // in here with respect to where output goes.
608 if (!live_fsck
&& log_file
) {
610 fprintf(log_file
, "\n%s: fsck_hfs started at %s", cdevname
? cdevname
: "UNKNOWN-DEV", ctime(&t
));
613 } else if (live_fsck
|| in_mem_log
== NULL
|| in_mem_out
== NULL
) {
615 // hmm, we couldn't open the log file (or it's a
616 // live fsck). let's just squirrel away a copy
617 // of the data in memory and then deal with it
618 // later (or print it out from a separate thread
619 // if we're doing a live fsck).
621 in_mem_log
= (char *)malloc(DEFAULT_IN_MEM_SIZE
);
622 in_mem_out
= (char *)malloc(DEFAULT_IN_MEM_SIZE
);
623 if ((in_mem_log
!= NULL
) && (in_mem_out
!= NULL
)) {
624 in_mem_log_size
= DEFAULT_IN_MEM_SIZE
;
625 in_mem_log
[0] = '\0';
626 cur_in_mem_log
= in_mem_log
;
628 in_mem_out_size
= DEFAULT_IN_MEM_SIZE
;
629 in_mem_out
[0] = '\0';
630 cur_in_mem_out
= in_mem_out
;
633 print_to_mem(DO_STR
, IN_MEM_LOG
, "\nfsck_hfs started at %s", ctime(&t
), NULL
);
635 if (live_fsck
&& log_file
) {
636 pthread_cond_init(&mem_buf_cond
, NULL
);
638 signal(SIGINT
, my_sighandler
);
639 signal(SIGHUP
, my_sighandler
);
640 signal(SIGTERM
, my_sighandler
);
641 signal(SIGQUIT
, my_sighandler
);
642 signal(SIGBUS
, my_sighandler
);
643 signal(SIGSEGV
, my_sighandler
);
644 signal(SIGILL
, my_sighandler
);
646 pthread_create(&printing_thread
, NULL
, fsck_printing_thread
, NULL
);
647 pthread_create(&logging_thread
, NULL
, fsck_logging_thread
, NULL
);
653 if (at_exit_setup
== 0 && (log_file
|| in_mem_log
|| in_mem_out
)) {
654 atexit(shutdown_logging
);
662 print_to_mem(int type
, int mem_type
, const char *fmt
, const char *str
, va_list ap
)
665 size_t size_remaining
;
669 size_t in_mem_data_size
;
671 if (type
== DO_VPRINT
) {
672 va_copy(ap_copy
, ap
);
675 if (mem_type
== IN_MEM_LOG
) {
676 cur_in_mem
= cur_in_mem_log
;
677 in_mem_data
= in_mem_log
;
678 in_mem_data_size
= in_mem_log_size
;
680 cur_in_mem
= cur_in_mem_out
;
681 in_mem_data
= in_mem_out
;
682 in_mem_data_size
= in_mem_out_size
;
685 /* Grab the lock only when adding output strings to the in-memory data */
686 if (live_fsck
&& (mem_type
== IN_MEM_OUT
)) {
687 pthread_mutex_lock(&mem_buf_lock
);
690 size_remaining
= in_mem_data_size
- (ptrdiff_t)(cur_in_mem
- in_mem_data
);
691 if (type
== DO_VPRINT
) {
692 ret
= vsnprintf(cur_in_mem
, size_remaining
, fmt
, ap
);
694 ret
= snprintf(cur_in_mem
, size_remaining
, fmt
, str
);
696 if (ret
> size_remaining
) {
700 if (ret
>= DEFAULT_IN_MEM_SIZE
) {
701 amt
= (ret
+ 4095) & (~4095); // round up to a 4k boundary
703 amt
= DEFAULT_IN_MEM_SIZE
;
706 new_log
= realloc(in_mem_data
, in_mem_data_size
+ amt
);
707 if (new_log
== NULL
) {
708 if (live_fsck
&& (mem_type
== IN_MEM_OUT
)) {
709 pthread_cond_signal(&mem_buf_cond
);
710 pthread_mutex_unlock(&mem_buf_lock
);
715 in_mem_data_size
+= amt
;
716 cur_in_mem
= new_log
+ (cur_in_mem
- in_mem_data
);
717 in_mem_data
= new_log
;
718 size_remaining
= in_mem_data_size
- (ptrdiff_t)(cur_in_mem
- new_log
);
719 if (type
== DO_VPRINT
) {
720 ret
= vsnprintf(cur_in_mem
, size_remaining
, fmt
, ap_copy
);
722 ret
= snprintf(cur_in_mem
, size_remaining
, fmt
, str
);
724 if (ret
<= size_remaining
) {
731 if (live_fsck
&& (mem_type
== IN_MEM_OUT
)) {
732 pthread_cond_signal(&mem_buf_cond
);
733 pthread_mutex_unlock(&mem_buf_lock
);
738 if (mem_type
== IN_MEM_LOG
) {
739 cur_in_mem_log
= cur_in_mem
;
740 in_mem_log
= in_mem_data
;
741 in_mem_log_size
= in_mem_data_size
;
743 cur_in_mem_out
= cur_in_mem
;
744 in_mem_out
= in_mem_data
;
745 in_mem_out_size
= in_mem_data_size
;
748 if (type
== DO_VPRINT
) {
754 static int need_prefix
=1;
758 fprintf(log_file, "%s: ", cdevname); \
759 if (strchr(fmt, '\n')) { \
764 } else if (strchr(fmt, '\n')) { \
768 /* Print output string on given stream or store it into in-memory buffer */
769 #define VOUT(stream, fmt, ap) \
771 vfprintf(stream, fmt, ap); \
773 print_to_mem(DO_VPRINT, IN_MEM_OUT, fmt, NULL, ap); \
776 #define FOUT(fmt, str) \
777 print_to_mem(DO_STR, IN_MEM_OUT, fmt, str, NULL);
779 /* Store output string written to fsck_hfs.log into file or in-memory buffer */
780 #define VLOG(fmt, ap) \
782 VLOG_INTERNAL(fmt, ap);
784 #define VLOG_INTERNAL(fmt, ap) \
785 if (log_file && !live_fsck) { \
787 vfprintf(log_file, fmt, ap); \
789 print_to_mem(DO_VPRINT, IN_MEM_LOG, fmt, NULL, ap); \
792 #define FLOG(fmt, str) \
793 if (log_file && !live_fsck) { \
795 fprintf(log_file, fmt, str); \
797 print_to_mem(DO_STR, IN_MEM_LOG, fmt, str, NULL); \
808 * An unexpected inconsistency occurred.
809 * Die if preening, otherwise just print message and continue.
813 pfatal(const char *fmt
, ...)
815 pfatal(fmt
, va_alist
)
830 (void)vfprintf(stderr
, fmt
, ap
);
836 (void)fprintf(stderr
, "%s: ", cdevname
);
837 FLOG("%s: ", cdevname
);
840 (void)vfprintf(stderr
, fmt
, ap
);
844 (void)fprintf(stderr
,
845 "\n%s: UNEXPECTED INCONSISTENCY; RUN fsck_hfs MANUALLY.\n",
847 FLOG("\n%s: UNEXPECTED INCONSISTENCY; RUN fsck_hfs MANUALLY.\n", cdevname
);
853 * Pwarn just prints a message when not preening,
854 * or a warning (preceded by filename) when preening.
858 pwarn(const char *fmt
, ...)
875 (void)fprintf(stderr
, "%s: ", cdevname
);
876 FLOG("%s: ", cdevname
);
879 (void)vfprintf(stderr
, fmt
, ap
);
885 /* Write a string and parameters, if any, directly to the log file.
886 * These strings will not be printed to standard out/error.
889 logstring(void *c
, const char *str
)
894 /* Write a string and parameters, if any, directly to standard out/error.
895 * These strings will not be printed to log file.
898 outstring(void *c
, const char *str
)
903 /* Write to both standard out and log file */
905 plog(const char *fmt
, ...)
913 /* Write to only standard out */
915 olog(const char *fmt
, ...)
922 /* For live fsck_hfs, add output strings to in-memory log,
923 * and for non-live fsck_hfs, print output to stdout.
925 VOUT(stdout
, fmt
, ap
);
930 /* Write to only log file */
932 llog(const char *fmt
, ...)
944 /* Write to both standard out and log file */
946 vplog(const char *fmt
, va_list ap
)
950 va_copy(copy_ap
, ap
);
954 /* Always print prefix to strings written to log files */
957 /* Handle output strings, print to stdout or store in-memory */
958 VOUT(stdout
, fmt
, ap
);
960 /* Add log strings to the log file. VLOG() handles live case internally */
961 VLOG_INTERNAL(fmt
, copy_ap
);
964 /* Write to both standard out and log file */
966 fplog(FILE *stream
, const char *fmt
, ...)
970 va_copy(copy_ap
, ap
);
975 /* Handle output strings, print to given stream or store in-memory */
976 VOUT(stream
, fmt
, ap
);
978 /* Add log strings to the log file. VLOG() handles live case internally */
984 #define kProgressToggle "kern.progressmeterenable"
985 #define kProgress "kern.progressmeter"
994 rv
= sysctlbyname(kProgressToggle
, NULL
, NULL
, &enable
, sizeof(enable
));
995 if (debug
&& rv
== -1 && errno
!= ENOENT
) {
996 warn("sysctl(%s) failed", kProgressToggle
);
1001 draw_progress(int pct
)
1006 rv
= sysctlbyname(kProgress
, NULL
, NULL
, &pct
, sizeof(pct
));
1007 if (debug
&& rv
== -1 && errno
!= ENOENT
) {
1008 warn("sysctl(%s) failed", kProgress
);
1019 rv
= sysctlbyname(kProgressToggle
, NULL
, NULL
, &enable
, sizeof(enable
));
1020 if (debug
&& rv
== -1 && errno
!= ENOENT
) {
1021 warn("sysctl(%s) failed", kProgressToggle
);