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>
73 char *rawname
__P((char *name
));
74 char *unrawname
__P((char *name
));
84 pfatal("INTERNAL ERROR: GOT TO reply()");
85 persevere
= !strcmp(question
, "CONTINUE");
87 if (!persevere
&& (nflag
|| fswritefd
< 0)) {
88 plog("%s? no\n\n", question
);
91 if (yflag
|| (persevere
&& nflag
)) {
92 plog("%s? yes\n\n", question
);
96 plog("%s? [yn] ", question
);
97 (void) fflush(stdout
);
99 while (c
!= '\n' && getc(stdin
) != '\n')
102 } while (c
!= 'y' && c
!= 'Y' && c
!= 'n' && c
!= 'N');
104 if (c
== 'y' || c
== 'Y')
114 // register struct bufarea *bp, *nbp;
115 // int ofsmodified, cnt = 0;
117 (void) CacheDestroy(&fscache
);
120 (void)close(fsreadfd
);
124 flush(fswritefd
, &sblk
);
125 if (havesb
&& sblk
.b_bno
!= SBOFF
/ dev_bsize
&&
126 !preen
&& reply("UPDATE STANDARD SUPERBLOCK")) {
127 sblk
.b_bno
= SBOFF
/ dev_bsize
;
129 flush(fswritefd
, &sblk
);
131 flush(fswritefd
, &cgblk
);
132 free(cgblk
.b_un
.b_buf
);
133 for (bp
= bufhead
.b_prev
; bp
&& bp
!= &bufhead
; bp
= nbp
) {
135 flush(fswritefd
, bp
);
137 free(bp
->b_un
.b_buf
);
140 if (bufhead
.b_size
!= cnt
)
141 errx(EEXIT
, "Panic: lost %d buffers", bufhead
.b_size
- cnt
);
142 pbp
= pdirbp
= (struct bufarea
*)0;
143 if (markclean
&& sblock
.fs_clean
== 0) {
146 ofsmodified
= fsmodified
;
147 flush(fswritefd
, &sblk
);
148 fsmodified
= ofsmodified
;
150 plog("\n***** FILE SYSTEM MARKED CLEAN *****\n");
153 plog("cache missed %ld of %ld (%d%%)\n", diskreads
,
154 totalreads
, (int)(diskreads
* 100 / totalreads
));
156 (void)close(fsreadfd
);
157 (void)close(fswritefd
);
162 blockcheck(char *origname
)
164 struct stat stslash
, stblock
, stchar
;
169 if (stat("/", &stslash
) < 0) {
171 plog("Can't stat root\n");
176 if (stat(newname
, &stblock
) < 0) {
178 plog("Can't stat %s\n", newname
);
181 if ((stblock
.st_mode
& S_IFMT
) == S_IFBLK
) {
182 if (stslash
.st_dev
== stblock
.st_rdev
)
184 raw
= rawname(newname
);
185 if (stat(raw
, &stchar
) < 0) {
187 plog("Can't stat %s\n", raw
);
190 if ((stchar
.st_mode
& S_IFMT
) == S_IFCHR
) {
193 plog("%s is not a character device\n", raw
);
196 } else if ((stblock
.st_mode
& S_IFMT
) == S_IFCHR
&& !retried
) {
197 newname
= unrawname(newname
);
202 * Not a block or character device, just return name and
203 * let the caller decide whether to use it.
213 static char rawbuf
[32];
216 if ((dp
= strrchr(name
, '/')) == 0)
219 (void)strlcpy(rawbuf
, name
, sizeof(rawbuf
));
221 (void)strlcat(rawbuf
, "/r", sizeof(rawbuf
));
222 (void)strlcat(rawbuf
, &dp
[1], sizeof(rawbuf
));
229 unrawname(char *name
)
234 if ((dp
= strrchr(name
, '/')) == 0)
236 if (stat(name
, &stb
) < 0)
238 if ((stb
.st_mode
& S_IFMT
) != S_IFCHR
)
242 memmove(&dp
[1], &dp
[2], strlen(&dp
[2]) + 1);
266 #define FSCK_LOG_FILE "/var/log/fsck_hfs.log"
268 extern char lflag
; // indicates if we're doing a live fsck (defined in fsck_hfs.c)
269 extern char guiControl
; // indicates if we're outputting for the gui (defined in fsck_hfs.c)
271 FILE *log_file
= NULL
;
273 /* Variables for in-memory log for strings that will be written to log file */
274 char *in_mem_log
= NULL
;
275 char *cur_in_mem_log
= NULL
;
276 size_t in_mem_log_size
= 0;
278 /* Variables for in-memory log for strings that will be printed on standard out */
279 char *in_mem_out
= NULL
;
280 char *cur_in_mem_out
= NULL
;
281 size_t in_mem_out_size
= 0;
285 #define DEFAULT_IN_MEM_SIZE 4096
287 static pthread_mutex_t mem_buf_lock
= PTHREAD_MUTEX_INITIALIZER
;
288 static pthread_cond_t mem_buf_cond
;
290 static pthread_t printing_thread
;
291 static pthread_t logging_thread
;
292 static volatile int keep_going
= 1;
298 void print_to_mem(int type
, int mem_type
, const char *fmt
, const char *str
, va_list ap
);
300 #define DO_VPRINT 1 // types for print_to_mem
303 /* Types for mem_type */
304 #define IN_MEM_LOG 1 // in-memory log strings
305 #define IN_MEM_OUT 2 // in-memory stdout strings
308 fsck_logging_thread(void *arg
)
311 char buff
[1024], *ptr
;
313 /* Handle writing to the log file */
314 while(keep_going
|| cur_in_mem_log
!= in_mem_log
) {
316 pthread_mutex_lock(&mem_buf_lock
);
317 while (keep_going
!= 0 && cur_in_mem_log
== in_mem_log
) {
320 err
= pthread_cond_wait(&mem_buf_cond
, &mem_buf_lock
);
322 fprintf(stderr
, "error %d from cond wait\n", err
);
327 copy_amt
= (cur_in_mem_log
- in_mem_log
);
329 pthread_mutex_unlock(&mem_buf_lock
);
333 if (copy_amt
>= sizeof(buff
)) {
334 copy_amt
= sizeof(buff
) - 1;
335 memcpy(buff
, in_mem_log
, copy_amt
);
337 memmove(in_mem_log
, &in_mem_log
[copy_amt
], (cur_in_mem_log
- in_mem_log
) - copy_amt
);
338 cur_in_mem_log
-= copy_amt
;
340 memcpy(buff
, in_mem_log
, copy_amt
);
341 cur_in_mem_log
= in_mem_log
;
344 buff
[copy_amt
] = '\0';
346 pthread_mutex_unlock(&mem_buf_lock
);
348 for(ptr
=buff
; *ptr
; ) {
352 while(*ptr
&& *ptr
!= '\n') {
358 fprintf(log_file
, "%s: %s\n", cdevname
? cdevname
: "UNKNOWN-DEV", start
);
362 fprintf(log_file
, "%s", start
);
375 fsck_printing_thread(void *arg
)
378 char buff
[1024], *ptr
;
380 /* Handle writing to the out file */
381 while(keep_going
|| cur_in_mem_out
!= in_mem_out
) {
383 pthread_mutex_lock(&mem_buf_lock
);
384 while (keep_going
!= 0 && cur_in_mem_out
== in_mem_out
) {
387 err
= pthread_cond_wait(&mem_buf_cond
, &mem_buf_lock
);
389 fprintf(stderr
, "error %d from cond wait\n", err
);
394 copy_amt
= (cur_in_mem_out
- in_mem_out
);
396 pthread_mutex_unlock(&mem_buf_lock
);
400 if (copy_amt
>= sizeof(buff
)) {
401 copy_amt
= sizeof(buff
) - 1;
402 memcpy(buff
, in_mem_out
, copy_amt
);
404 memmove(in_mem_out
, &in_mem_out
[copy_amt
], (cur_in_mem_out
- in_mem_out
) - copy_amt
);
405 cur_in_mem_out
-= copy_amt
;
407 memcpy(buff
, in_mem_out
, copy_amt
);
408 cur_in_mem_out
= in_mem_out
;
411 buff
[copy_amt
] = '\0';
413 pthread_mutex_unlock(&mem_buf_lock
);
415 for(ptr
=buff
; *ptr
; ) {
419 while(*ptr
&& *ptr
!= '\n') {
424 printf("%s\n", start
);
438 safely_open_log_file(const char *path
)
440 int fd
= open(path
, O_CREAT
| O_APPEND
| O_WRONLY
| O_NOFOLLOW
, 0666);
445 if (fstat(fd
, &sb
) || !S_ISREG(sb
.st_mode
)) {
451 return fdopen(fd
, "a");
454 int was_signaled
= 0;
457 shutdown_logging(void)
462 /* Log fsck_hfs check completion time */
465 va_list empty_list
= {0};
466 print_to_mem(DO_STR
, IN_MEM_LOG
, "fsck_hfs completed at %s\n", ctime(&t
), empty_list
);
468 fprintf(log_file
, "%s: fsck_hfs completed at %s\n", cdevname
? cdevname
: "UNKNOWN-DEV", ctime(&t
));
472 // if we were signaled, we can't really call any of these
473 // functions from the context of a signal handler (which
474 // is how we're called if we don't have a signal handler).
475 // so we have our own signal handler which sets this var
476 // which tells us to just bail out.
480 if (log_file
&& !live_fsck
) {
484 } else if ((in_mem_out
|| in_mem_log
) && live_fsck
&& log_file
) {
485 // make sure the printing and logging threads are woken up...
486 pthread_mutex_lock(&mem_buf_lock
);
487 pthread_cond_broadcast(&mem_buf_cond
);
488 pthread_mutex_unlock(&mem_buf_lock
);
490 // then wait for them
491 pthread_join(printing_thread
, NULL
);
492 pthread_join(logging_thread
, NULL
);
495 in_mem_out
= cur_in_mem_out
= NULL
;
499 in_mem_log
= cur_in_mem_log
= NULL
;
507 } else if (in_mem_log
) {
511 // just in case, flush any pending output
516 // fork so that the child can wait around until the
517 // root volume is mounted read-write and we can add
518 // our output to the log
522 // if we're not root we don't need to fork
527 char *fname
= FSCK_LOG_FILE
, path
[PATH_MAX
];
529 // Disk Management waits for fsck_hfs' stdout to close rather
530 // than the process death to understand if fsck_hfs has exited
531 // or not. Since we do not use stdout any further, close all
532 // the file descriptors so that Disk Management does not wait
533 // for 60 seconds unnecessarily on read-only boot volumes.
538 // non-root will never be able to write to /var/log
539 // so point the file somewhere else.
543 // each user will get their own log as ~/Library/Logs/fsck_hfs.log
544 pwd
= getpwuid(getuid());
546 snprintf(path
, sizeof(path
), "%s/Library/Logs/fsck_hfs.log", pwd
->pw_dir
);
551 for(i
=0; i
< 60; i
++) {
552 log_file
= safely_open_log_file(fname
);
554 fwrite(in_mem_log
, cur_in_mem_log
- in_mem_log
, 1, log_file
);
561 in_mem_log
= cur_in_mem_log
= NULL
;
566 // hmmm, failed to open the output file so wait
567 // a while only if the fs is read-only and then
569 if (errno
== EROFS
) {
581 my_sighandler(int sig
)
592 static int at_exit_setup
= 0;
595 // if this is set, we don't have to do anything
605 // our copy of this variable since we may
606 // need to change it to make the right thing
607 // happen for fsck on the root volume.
608 live_fsck
= (int)lflag
;
610 if (log_file
== NULL
) {
611 log_file
= safely_open_log_file(FSCK_LOG_FILE
);
613 setlinebuf(log_file
);
616 // if we can't open the output file it's either because
617 // we're being run on the root volume during early boot
618 // or we were not run as the root user and so we can't
619 // write to /var/log/fsck_hfs.log. in either case we
620 // turn off "live_fsck" so that the right thing happens
621 // in here with respect to where output goes.
626 if (!live_fsck
&& log_file
) {
628 fprintf(log_file
, "\n%s: fsck_hfs started at %s", cdevname
? cdevname
: "UNKNOWN-DEV", ctime(&t
));
631 } else if (live_fsck
|| in_mem_log
== NULL
|| in_mem_out
== NULL
) {
633 // hmm, we couldn't open the log file (or it's a
634 // live fsck). let's just squirrel away a copy
635 // of the data in memory and then deal with it
636 // later (or print it out from a separate thread
637 // if we're doing a live fsck).
639 in_mem_log
= (char *)malloc(DEFAULT_IN_MEM_SIZE
);
640 in_mem_out
= (char *)malloc(DEFAULT_IN_MEM_SIZE
);
641 if ((in_mem_log
!= NULL
) && (in_mem_out
!= NULL
)) {
642 in_mem_log_size
= DEFAULT_IN_MEM_SIZE
;
643 in_mem_log
[0] = '\0';
644 cur_in_mem_log
= in_mem_log
;
646 in_mem_out_size
= DEFAULT_IN_MEM_SIZE
;
647 in_mem_out
[0] = '\0';
648 cur_in_mem_out
= in_mem_out
;
651 va_list empty_list
= {0};
652 print_to_mem(DO_STR
, IN_MEM_LOG
, "\nfsck_hfs started at %s", ctime(&t
), empty_list
);
654 if (live_fsck
&& log_file
) {
655 pthread_cond_init(&mem_buf_cond
, NULL
);
657 signal(SIGINT
, my_sighandler
);
658 signal(SIGHUP
, my_sighandler
);
659 signal(SIGTERM
, my_sighandler
);
660 signal(SIGQUIT
, my_sighandler
);
661 signal(SIGBUS
, my_sighandler
);
662 signal(SIGSEGV
, my_sighandler
);
663 signal(SIGILL
, my_sighandler
);
665 pthread_create(&printing_thread
, NULL
, fsck_printing_thread
, NULL
);
666 pthread_create(&logging_thread
, NULL
, fsck_logging_thread
, NULL
);
672 if (at_exit_setup
== 0 && (log_file
|| in_mem_log
|| in_mem_out
)) {
673 atexit(shutdown_logging
);
681 print_to_mem(int type
, int mem_type
, const char *fmt
, const char *str
, va_list ap
)
684 size_t size_remaining
;
688 size_t in_mem_data_size
;
690 if (type
== DO_VPRINT
) {
691 va_copy(ap_copy
, ap
);
694 /* Grab the lock only when adding output strings to the in-memory data */
695 if (live_fsck
&& (mem_type
== IN_MEM_OUT
)) {
696 pthread_mutex_lock(&mem_buf_lock
);
699 if (mem_type
== IN_MEM_LOG
) {
700 cur_in_mem
= cur_in_mem_log
;
701 in_mem_data
= in_mem_log
;
702 in_mem_data_size
= in_mem_log_size
;
704 cur_in_mem
= cur_in_mem_out
;
705 in_mem_data
= in_mem_out
;
706 in_mem_data_size
= in_mem_out_size
;
709 size_remaining
= in_mem_data_size
- (ptrdiff_t)(cur_in_mem
- in_mem_data
);
710 if (type
== DO_VPRINT
) {
711 ret
= vsnprintf(cur_in_mem
, size_remaining
, fmt
, ap
);
713 ret
= snprintf(cur_in_mem
, size_remaining
, fmt
, str
);
715 if (ret
> size_remaining
) {
719 if (ret
>= DEFAULT_IN_MEM_SIZE
) {
720 amt
= (ret
+ 4095) & (~4095); // round up to a 4k boundary
722 amt
= DEFAULT_IN_MEM_SIZE
;
725 new_log
= realloc(in_mem_data
, in_mem_data_size
+ amt
);
729 in_mem_data_size
+= amt
;
730 cur_in_mem
= new_log
+ (cur_in_mem
- in_mem_data
);
731 in_mem_data
= new_log
;
732 size_remaining
= in_mem_data_size
- (ptrdiff_t)(cur_in_mem
- new_log
);
733 if (type
== DO_VPRINT
) {
734 ret
= vsnprintf(cur_in_mem
, size_remaining
, fmt
, ap_copy
);
736 ret
= snprintf(cur_in_mem
, size_remaining
, fmt
, str
);
738 if (ret
<= size_remaining
) {
747 if (mem_type
== IN_MEM_LOG
) {
748 cur_in_mem_log
= cur_in_mem
;
749 in_mem_log
= in_mem_data
;
750 in_mem_log_size
= in_mem_data_size
;
752 cur_in_mem_out
= cur_in_mem
;
753 in_mem_out
= in_mem_data
;
754 in_mem_out_size
= in_mem_data_size
;
757 if (live_fsck
&& (mem_type
== IN_MEM_OUT
)) {
758 pthread_cond_signal(&mem_buf_cond
);
759 pthread_mutex_unlock(&mem_buf_lock
);
762 if (type
== DO_VPRINT
) {
768 static int need_prefix
=1;
772 fprintf(log_file, "%s: ", cdevname); \
773 if (strchr(fmt, '\n')) { \
778 } else if (strchr(fmt, '\n')) { \
782 /* Print output string on given stream or store it into in-memory buffer */
783 #define VOUT(stream, fmt, ap) \
785 vfprintf(stream, fmt, ap); \
787 print_to_mem(DO_VPRINT, IN_MEM_OUT, fmt, NULL, ap); \
790 #define FOUT(fmt, str) \
791 print_to_mem(DO_STR, IN_MEM_OUT, fmt, str, NULL);
793 /* Store output string written to fsck_hfs.log into file or in-memory buffer */
794 #define VLOG(fmt, ap) \
796 VLOG_INTERNAL(fmt, ap);
798 #define VLOG_INTERNAL(fmt, ap) \
799 if (log_file && !live_fsck) { \
801 vfprintf(log_file, fmt, ap); \
803 print_to_mem(DO_VPRINT, IN_MEM_LOG, fmt, NULL, ap); \
806 #define FLOG(fmt, str) \
807 if (log_file && !live_fsck) { \
809 fprintf(log_file, fmt, str); \
811 va_list empty_list = {0}; \
812 print_to_mem(DO_STR, IN_MEM_LOG, fmt, str, empty_list); \
823 * An unexpected inconsistency occurred.
824 * Die if preening, otherwise just print message and continue.
828 pfatal(const char *fmt
, ...)
830 pfatal(fmt
, va_alist
)
845 (void)vfprintf(stderr
, fmt
, ap
);
851 (void)fprintf(stderr
, "%s: ", cdevname
);
852 FLOG("%s: ", cdevname
);
855 (void)vfprintf(stderr
, fmt
, ap
);
859 (void)fprintf(stderr
,
860 "\n%s: UNEXPECTED INCONSISTENCY; RUN fsck_hfs MANUALLY.\n",
862 FLOG("\n%s: UNEXPECTED INCONSISTENCY; RUN fsck_hfs MANUALLY.\n", cdevname
);
868 * Pwarn just prints a message when not preening,
869 * or a warning (preceded by filename) when preening.
873 pwarn(const char *fmt
, ...)
890 (void)fprintf(stderr
, "%s: ", cdevname
);
891 FLOG("%s: ", cdevname
);
894 (void)vfprintf(stderr
, fmt
, ap
);
900 /* Write a string and parameters, if any, directly to the log file.
901 * These strings will not be printed to standard out/error.
904 logstring(void *c
, const char *str
)
909 /* Write a string and parameters, if any, directly to standard out/error.
910 * These strings will not be printed to log file.
913 outstring(void *c
, const char *str
)
918 /* Write to both standard out and log file */
920 plog(const char *fmt
, ...)
928 /* Write to only standard out */
930 olog(const char *fmt
, ...)
937 /* For live fsck_hfs, add output strings to in-memory log,
938 * and for non-live fsck_hfs, print output to stdout.
940 VOUT(stdout
, fmt
, ap
);
945 /* Write to only log file */
947 llog(const char *fmt
, ...)
959 /* Write to both standard out and log file */
961 vplog(const char *fmt
, va_list ap
)
965 va_copy(copy_ap
, ap
);
969 /* Always print prefix to strings written to log files */
972 /* Handle output strings, print to stdout or store in-memory */
973 VOUT(stdout
, fmt
, ap
);
975 /* Add log strings to the log file. VLOG() handles live case internally */
976 VLOG_INTERNAL(fmt
, copy_ap
);
979 /* Write to both standard out and log file */
981 fplog(FILE *stream
, const char *fmt
, ...)
985 va_copy(copy_ap
, ap
);
990 /* Handle output strings, print to given stream or store in-memory */
991 VOUT(stream
, fmt
, ap
);
993 /* Add log strings to the log file. VLOG() handles live case internally */
999 #define kProgressToggle "kern.progressmeterenable"
1000 #define kProgress "kern.progressmeter"
1003 start_progress(void)
1009 rv
= sysctlbyname(kProgressToggle
, NULL
, NULL
, &enable
, sizeof(enable
));
1010 if (debug
&& rv
== -1 && errno
!= ENOENT
) {
1011 warn("sysctl(%s) failed", kProgressToggle
);
1016 draw_progress(int pct
)
1021 rv
= sysctlbyname(kProgress
, NULL
, NULL
, &pct
, sizeof(pct
));
1022 if (debug
&& rv
== -1 && errno
!= ENOENT
) {
1023 warn("sysctl(%s) failed", kProgress
);
1034 rv
= sysctlbyname(kProgressToggle
, NULL
, NULL
, &enable
, sizeof(enable
));
1035 if (debug
&& rv
== -1 && errno
!= ENOENT
) {
1036 warn("sysctl(%s) failed", kProgressToggle
);