Whamcloud - gitweb
e2fsck: add a 'yes to all' response in interactive mode
[tools/e2fsprogs.git] / e2fsck / util.c
1 /*
2  * util.c --- miscellaneous utilities
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <ctype.h>
18 #ifdef __linux__
19 #include <sys/utsname.h>
20 #endif
21
22 #ifdef HAVE_CONIO_H
23 #undef HAVE_TERMIOS_H
24 #include <conio.h>
25 #define read_a_char()   getch()
26 #else
27 #ifdef HAVE_TERMIOS_H
28 #include <termios.h>
29 #endif
30 #endif
31
32 #ifdef HAVE_MALLOC_H
33 #include <malloc.h>
34 #endif
35
36 #ifdef HAVE_ERRNO_H
37 #include <errno.h>
38 #endif
39
40 #include "e2fsck.h"
41
42 extern e2fsck_t e2fsck_global_ctx;   /* Try your very best not to use this! */
43
44 #include <stdarg.h>
45 #include <time.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48
49 void fatal_error(e2fsck_t ctx, const char *msg)
50 {
51         ext2_filsys fs = ctx->fs;
52         int exit_value = FSCK_ERROR;
53
54         if (msg)
55                 fprintf (stderr, "e2fsck: %s\n", msg);
56         if (!fs)
57                 goto out;
58         if (fs->io && fs->super) {
59                 ext2fs_mmp_stop(ctx->fs);
60                 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
61                         io_channel_flush(ctx->fs->io);
62                 else
63                         log_err(ctx, "e2fsck: io manager magic bad!\n");
64         }
65         if (ext2fs_test_changed(fs)) {
66                 exit_value |= FSCK_NONDESTRUCT;
67                 log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
68                         ctx->device_name);
69                 if (ctx->mount_flags & EXT2_MF_ISROOT)
70                         exit_value |= FSCK_REBOOT;
71         }
72         if (!ext2fs_test_valid(fs)) {
73                 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
74                                "errors **********\n\n"), ctx->device_name);
75                 exit_value |= FSCK_UNCORRECTED;
76                 exit_value &= ~FSCK_NONDESTRUCT;
77         }
78 out:
79         ctx->flags |= E2F_FLAG_ABORT;
80         if (ctx->flags & E2F_FLAG_SETJMP_OK)
81                 longjmp(ctx->abort_loc, 1);
82         exit(exit_value);
83 }
84
85 void log_out(e2fsck_t ctx, const char *fmt, ...)
86 {
87         va_list pvar;
88
89         va_start(pvar, fmt);
90         vprintf(fmt, pvar);
91         va_end(pvar);
92         if (ctx->logf) {
93                 va_start(pvar, fmt);
94                 vfprintf(ctx->logf, fmt, pvar);
95                 va_end(pvar);
96         }
97 }
98
99 void log_err(e2fsck_t ctx, const char *fmt, ...)
100 {
101         va_list pvar;
102
103         va_start(pvar, fmt);
104         vfprintf(stderr, fmt, pvar);
105         va_end(pvar);
106         if (ctx->logf) {
107                 va_start(pvar, fmt);
108                 vfprintf(ctx->logf, fmt, pvar);
109                 va_end(pvar);
110         }
111 }
112
113 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
114                              const char *description)
115 {
116         void *ret;
117         char buf[256];
118
119 #ifdef DEBUG_ALLOCATE_MEMORY
120         printf("Allocating %u bytes for %s...\n", size, description);
121 #endif
122         ret = malloc(size);
123         if (!ret) {
124                 sprintf(buf, "Can't allocate %u bytes for %s\n",
125                         size, description);
126                 fatal_error(ctx, buf);
127         }
128         memset(ret, 0, size);
129         return ret;
130 }
131
132 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
133                   const char *str, int len)
134 {
135         char    *ret;
136
137         if (!str)
138                 return NULL;
139         if (!len)
140                 len = strlen(str);
141         ret = malloc(len+1);
142         if (ret) {
143                 strncpy(ret, str, len);
144                 ret[len] = 0;
145         }
146         return ret;
147 }
148
149 #ifndef HAVE_STRNLEN
150 /*
151  * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
152  * provide our own.
153  */
154 int e2fsck_strnlen(const char * s, int count)
155 {
156         const char *cp = s;
157
158         while (count-- && *cp)
159                 cp++;
160         return cp - s;
161 }
162 #endif
163
164 #ifndef HAVE_CONIO_H
165 static int read_a_char(void)
166 {
167         char    c;
168         int     r;
169         int     fail = 0;
170
171         while(1) {
172                 if (e2fsck_global_ctx &&
173                     (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
174                         return 3;
175                 }
176                 r = read(0, &c, 1);
177                 if (r == 1)
178                         return c;
179                 if (fail++ > 100)
180                         break;
181         }
182         return EOF;
183 }
184 #endif
185
186 int ask_yn(e2fsck_t ctx, const char * string, int def)
187 {
188         int             c;
189         const char      *defstr;
190         const char      *short_yes = _("yY");
191         const char      *short_no = _("nN");
192         const char      *short_yesall = _("aA");
193         const char      *yesall_prompt = _(" ('a' enables 'yes' to all) ");
194         const char      *extra_prompt = "";
195         static int      yes_answers;
196
197 #ifdef HAVE_TERMIOS_H
198         struct termios  termios, tmp;
199
200         tcgetattr (0, &termios);
201         tmp = termios;
202         tmp.c_lflag &= ~(ICANON | ECHO);
203         tmp.c_cc[VMIN] = 1;
204         tmp.c_cc[VTIME] = 0;
205         tcsetattr (0, TCSANOW, &tmp);
206 #endif
207
208         if (def == 1)
209                 defstr = _(_("<y>"));
210         else if (def == 0)
211                 defstr = _(_("<n>"));
212         else
213                 defstr = _(" (y/n)");
214         /*
215          * If the user presses 'y' more than 8 (but less than 12) times in
216          * succession without pressing anything else, display a hint about
217          * yes-to-all mode.
218          */
219         if (yes_answers > 12)
220                 yes_answers = -1;
221         else if (yes_answers > 8)
222                 extra_prompt = yesall_prompt;
223         log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
224         while (1) {
225                 fflush (stdout);
226                 if ((c = read_a_char()) == EOF)
227                         break;
228                 if (c == 3) {
229 #ifdef HAVE_TERMIOS_H
230                         tcsetattr (0, TCSANOW, &termios);
231 #endif
232                         if (ctx->flags & E2F_FLAG_SETJMP_OK) {
233                                 log_out(ctx, "\n");
234                                 longjmp(e2fsck_global_ctx->abort_loc, 1);
235                         }
236                         log_out(ctx, "%s", _("cancelled!\n"));
237                         yes_answers = 0;
238                         return 0;
239                 }
240                 if (strchr(short_yes, (char) c)) {
241                         def = 1;
242                         if (yes_answers >= 0)
243                                 yes_answers++;
244                         break;
245                 } else if (strchr(short_no, (char) c)) {
246                         def = 0;
247                         yes_answers = -1;
248                         break;
249                 } else if (strchr(short_yesall, (char)c)) {
250                         def = 2;
251                         yes_answers = -1;
252                         ctx->options |= E2F_OPT_YES;
253                         break;
254                 } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
255                         yes_answers = -1;
256                         break;
257                 }
258         }
259         if (def == 2)
260                 log_out(ctx, "%s", _("yes to all\n"));
261         else if (def)
262                 log_out(ctx, "%s", _("yes\n"));
263         else
264                 log_out(ctx, "%s", _("no\n"));
265 #ifdef HAVE_TERMIOS_H
266         tcsetattr (0, TCSANOW, &termios);
267 #endif
268         return def;
269 }
270
271 int ask (e2fsck_t ctx, const char * string, int def)
272 {
273         if (ctx->options & E2F_OPT_NO) {
274                 log_out(ctx, _("%s? no\n\n"), string);
275                 return 0;
276         }
277         if (ctx->options & E2F_OPT_YES) {
278                 log_out(ctx, _("%s? yes\n\n"), string);
279                 return 1;
280         }
281         if (ctx->options & E2F_OPT_PREEN) {
282                 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
283                 return def;
284         }
285         return ask_yn(ctx, string, def);
286 }
287
288 void e2fsck_read_bitmaps(e2fsck_t ctx)
289 {
290         ext2_filsys fs = ctx->fs;
291         errcode_t       retval;
292         const char      *old_op;
293         unsigned int    save_type;
294         int             flags;
295
296         if (ctx->invalid_bitmaps) {
297                 com_err(ctx->program_name, 0,
298                     _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
299                         ctx->device_name);
300                 fatal_error(ctx, 0);
301         }
302
303         old_op = ehandler_operation(_("reading inode and block bitmaps"));
304         e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
305                                &save_type);
306         flags = ctx->fs->flags;
307         ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
308         retval = ext2fs_read_bitmaps(fs);
309         ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
310                          (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
311         fs->default_bitmap_type = save_type;
312         ehandler_operation(old_op);
313         if (retval) {
314                 com_err(ctx->program_name, retval,
315                         _("while retrying to read bitmaps for %s"),
316                         ctx->device_name);
317                 fatal_error(ctx, 0);
318         }
319 }
320
321 void e2fsck_write_bitmaps(e2fsck_t ctx)
322 {
323         ext2_filsys fs = ctx->fs;
324         errcode_t       retval;
325         const char      *old_op;
326
327         old_op = ehandler_operation(_("writing block and inode bitmaps"));
328         retval = ext2fs_write_bitmaps(fs);
329         ehandler_operation(old_op);
330         if (retval) {
331                 com_err(ctx->program_name, retval,
332                         _("while rewriting block and inode bitmaps for %s"),
333                         ctx->device_name);
334                 fatal_error(ctx, 0);
335         }
336 }
337
338 void preenhalt(e2fsck_t ctx)
339 {
340         ext2_filsys fs = ctx->fs;
341
342         if (!(ctx->options & E2F_OPT_PREEN))
343                 return;
344         log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
345                 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
346                ctx->device_name);
347         ctx->flags |= E2F_FLAG_EXITING;
348         if (fs != NULL) {
349                 fs->super->s_state |= EXT2_ERROR_FS;
350                 ext2fs_mark_super_dirty(fs);
351                 ext2fs_close_free(&fs);
352         }
353         exit(FSCK_UNCORRECTED);
354 }
355
356 #ifdef RESOURCE_TRACK
357 void init_resource_track(struct resource_track *track, io_channel channel)
358 {
359 #ifdef HAVE_GETRUSAGE
360         struct rusage r;
361 #endif
362         io_stats io_start = 0;
363
364         track->brk_start = sbrk(0);
365         gettimeofday(&track->time_start, 0);
366 #ifdef HAVE_GETRUSAGE
367 #ifdef sun
368         memset(&r, 0, sizeof(struct rusage));
369 #endif
370         getrusage(RUSAGE_SELF, &r);
371         track->user_start = r.ru_utime;
372         track->system_start = r.ru_stime;
373 #else
374         track->user_start.tv_sec = track->user_start.tv_usec = 0;
375         track->system_start.tv_sec = track->system_start.tv_usec = 0;
376 #endif
377         track->bytes_read = 0;
378         track->bytes_written = 0;
379         if (channel && channel->manager && channel->manager->get_stats)
380                 channel->manager->get_stats(channel, &io_start);
381         if (io_start) {
382                 track->bytes_read = io_start->bytes_read;
383                 track->bytes_written = io_start->bytes_written;
384         }
385 }
386
387 #ifdef __GNUC__
388 #define _INLINE_ __inline__
389 #else
390 #define _INLINE_
391 #endif
392
393 static _INLINE_ float timeval_subtract(struct timeval *tv1,
394                                        struct timeval *tv2)
395 {
396         return ((tv1->tv_sec - tv2->tv_sec) +
397                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
398 }
399
400 void print_resource_track(e2fsck_t ctx, const char *desc,
401                           struct resource_track *track, io_channel channel)
402 {
403 #ifdef HAVE_GETRUSAGE
404         struct rusage r;
405 #endif
406 #ifdef HAVE_MALLINFO
407         struct mallinfo malloc_info;
408 #endif
409         struct timeval time_end;
410
411         if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
412             (!desc && !(ctx->options & E2F_OPT_TIME)))
413                 return;
414
415         e2fsck_clear_progbar(ctx);
416         gettimeofday(&time_end, 0);
417
418         if (desc)
419                 log_out(ctx, "%s: ", desc);
420
421 #ifdef HAVE_MALLINFO
422 #define kbytes(x)       (((unsigned long)(x) + 1023) / 1024)
423
424         malloc_info = mallinfo();
425         log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
426                 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
427                 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
428 #else
429         log_out(ctx, _("Memory used: %lu, "),
430                 (unsigned long) (((char *) sbrk(0)) -
431                                  ((char *) track->brk_start)));
432 #endif
433 #ifdef HAVE_GETRUSAGE
434         getrusage(RUSAGE_SELF, &r);
435
436         log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
437                 timeval_subtract(&time_end, &track->time_start),
438                 timeval_subtract(&r.ru_utime, &track->user_start),
439                 timeval_subtract(&r.ru_stime, &track->system_start));
440 #else
441         log_out(ctx, _("elapsed time: %6.3f\n"),
442                 timeval_subtract(&time_end, &track->time_start));
443 #endif
444 #define mbytes(x)       (((x) + 1048575) / 1048576)
445         if (channel && channel->manager && channel->manager->get_stats) {
446                 io_stats delta = 0;
447                 unsigned long long bytes_read = 0;
448                 unsigned long long bytes_written = 0;
449
450                 if (desc)
451                         log_out(ctx, "%s: ", desc);
452
453                 channel->manager->get_stats(channel, &delta);
454                 if (delta) {
455                         bytes_read = delta->bytes_read - track->bytes_read;
456                         bytes_written = delta->bytes_written -
457                                 track->bytes_written;
458                 }
459                 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
460                         "rate: %.2fMB/s\n",
461                         mbytes(bytes_read), mbytes(bytes_written),
462                         (double)mbytes(bytes_read + bytes_written) /
463                         timeval_subtract(&time_end, &track->time_start));
464         }
465 }
466 #endif /* RESOURCE_TRACK */
467
468 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
469                               struct ext2_inode * inode, const char *proc)
470 {
471         errcode_t retval;
472
473         retval = ext2fs_read_inode(ctx->fs, ino, inode);
474         if (retval) {
475                 com_err("ext2fs_read_inode", retval,
476                         _("while reading inode %lu in %s"), ino, proc);
477                 fatal_error(ctx, 0);
478         }
479 }
480
481 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
482                             struct ext2_inode *inode, int bufsize,
483                             const char *proc)
484 {
485         errcode_t retval;
486
487         retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
488         if (retval) {
489                 com_err("ext2fs_read_inode_full", retval,
490                         _("while reading inode %lu in %s"), ino, proc);
491                 fatal_error(ctx, 0);
492         }
493 }
494
495 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
496                              struct ext2_inode * inode, int bufsize,
497                              const char *proc)
498 {
499         errcode_t retval;
500
501         retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
502         if (retval) {
503                 com_err("ext2fs_write_inode", retval,
504                         _("while writing inode %lu in %s"), ino, proc);
505                 fatal_error(ctx, 0);
506         }
507 }
508
509 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
510                         struct ext2_inode * inode, const char *proc)
511 {
512         errcode_t retval;
513
514         retval = ext2fs_write_inode(ctx->fs, ino, inode);
515         if (retval) {
516                 com_err("ext2fs_write_inode", retval,
517                         _("while writing inode %lu in %s"), ino, proc);
518                 fatal_error(ctx, 0);
519         }
520 }
521
522 #ifdef MTRACE
523 void mtrace_print(char *mesg)
524 {
525         FILE    *malloc_get_mallstream();
526         FILE    *f = malloc_get_mallstream();
527
528         if (f)
529                 fprintf(f, "============= %s\n", mesg);
530 }
531 #endif
532
533 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
534                       io_manager manager)
535 {
536         struct ext2_super_block *sb;
537         io_channel              io = NULL;
538         void                    *buf = NULL;
539         int                     blocksize;
540         blk64_t                 superblock, ret_sb = 8193;
541
542         if (fs && fs->super) {
543                 ret_sb = (fs->super->s_blocks_per_group +
544                           fs->super->s_first_data_block);
545                 if (ctx) {
546                         ctx->superblock = ret_sb;
547                         ctx->blocksize = fs->blocksize;
548                 }
549                 return ret_sb;
550         }
551
552         if (ctx) {
553                 if (ctx->blocksize) {
554                         ret_sb = ctx->blocksize * 8;
555                         if (ctx->blocksize == 1024)
556                                 ret_sb++;
557                         ctx->superblock = ret_sb;
558                         return ret_sb;
559                 }
560                 ctx->superblock = ret_sb;
561                 ctx->blocksize = 1024;
562         }
563
564         if (!name || !manager)
565                 goto cleanup;
566
567         if (manager->open(name, 0, &io) != 0)
568                 goto cleanup;
569
570         if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
571                 goto cleanup;
572         sb = (struct ext2_super_block *) buf;
573
574         for (blocksize = EXT2_MIN_BLOCK_SIZE;
575              blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
576                 superblock = blocksize*8;
577                 if (blocksize == 1024)
578                         superblock++;
579                 io_channel_set_blksize(io, blocksize);
580                 if (io_channel_read_blk64(io, superblock,
581                                         -SUPERBLOCK_SIZE, buf))
582                         continue;
583 #ifdef WORDS_BIGENDIAN
584                 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
585                         ext2fs_swap_super(sb);
586 #endif
587                 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
588                     (EXT2_BLOCK_SIZE(sb) == blocksize)) {
589                         ret_sb = superblock;
590                         if (ctx) {
591                                 ctx->superblock = superblock;
592                                 ctx->blocksize = blocksize;
593                         }
594                         break;
595                 }
596         }
597
598 cleanup:
599         if (io)
600                 io_channel_close(io);
601         if (buf)
602                 ext2fs_free_mem(&buf);
603         return (ret_sb);
604 }
605
606 /*
607  * Given a mode, return the ext2 file type
608  */
609 int ext2_file_type(unsigned int mode)
610 {
611         if (LINUX_S_ISREG(mode))
612                 return EXT2_FT_REG_FILE;
613
614         if (LINUX_S_ISDIR(mode))
615                 return EXT2_FT_DIR;
616
617         if (LINUX_S_ISCHR(mode))
618                 return EXT2_FT_CHRDEV;
619
620         if (LINUX_S_ISBLK(mode))
621                 return EXT2_FT_BLKDEV;
622
623         if (LINUX_S_ISLNK(mode))
624                 return EXT2_FT_SYMLINK;
625
626         if (LINUX_S_ISFIFO(mode))
627                 return EXT2_FT_FIFO;
628
629         if (LINUX_S_ISSOCK(mode))
630                 return EXT2_FT_SOCK;
631
632         return 0;
633 }
634
635 /*
636  * Check to see if a filesystem is in /proc/filesystems.
637  * Returns 1 if found, 0 if not
638  */
639 int fs_proc_check(const char *fs_name)
640 {
641         FILE    *f;
642         char    buf[80], *cp, *t;
643
644         f = fopen("/proc/filesystems", "r");
645         if (!f)
646                 return (0);
647         while (!feof(f)) {
648                 if (!fgets(buf, sizeof(buf), f))
649                         break;
650                 cp = buf;
651                 if (!isspace(*cp)) {
652                         while (*cp && !isspace(*cp))
653                                 cp++;
654                 }
655                 while (*cp && isspace(*cp))
656                         cp++;
657                 if ((t = strchr(cp, '\n')) != NULL)
658                         *t = 0;
659                 if ((t = strchr(cp, '\t')) != NULL)
660                         *t = 0;
661                 if ((t = strchr(cp, ' ')) != NULL)
662                         *t = 0;
663                 if (!strcmp(fs_name, cp)) {
664                         fclose(f);
665                         return (1);
666                 }
667         }
668         fclose(f);
669         return (0);
670 }
671
672 /*
673  * Check to see if a filesystem is available as a module
674  * Returns 1 if found, 0 if not
675  */
676 int check_for_modules(const char *fs_name)
677 {
678 #ifdef __linux__
679         struct utsname  uts;
680         FILE            *f;
681         char            buf[1024], *cp, *t;
682         int             i;
683
684         if (uname(&uts))
685                 return (0);
686         snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
687
688         f = fopen(buf, "r");
689         if (!f)
690                 return (0);
691         while (!feof(f)) {
692                 if (!fgets(buf, sizeof(buf), f))
693                         break;
694                 if ((cp = strchr(buf, ':')) != NULL)
695                         *cp = 0;
696                 else
697                         continue;
698                 if ((cp = strrchr(buf, '/')) != NULL)
699                         cp++;
700                 else
701                         cp = buf;
702                 i = strlen(cp);
703                 if (i > 3) {
704                         t = cp + i - 3;
705                         if (!strcmp(t, ".ko"))
706                                 *t = 0;
707                 }
708                 if (!strcmp(cp, fs_name)) {
709                         fclose(f);
710                         return (1);
711                 }
712         }
713         fclose(f);
714 #endif /* __linux__ */
715         return (0);
716 }
717
718 /*
719  * Helper function that does the right thing if write returns a
720  * partial write, or an EGAIN/EINTR error.
721  */
722 int write_all(int fd, char *buf, size_t count)
723 {
724         ssize_t ret;
725         int c = 0;
726
727         while (count > 0) {
728                 ret = write(fd, buf, count);
729                 if (ret < 0) {
730                         if ((errno == EAGAIN) || (errno == EINTR))
731                                 continue;
732                         return -1;
733                 }
734                 count -= ret;
735                 buf += ret;
736                 c += ret;
737         }
738         return c;
739 }
740
741 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
742 {
743
744         if (msg)
745                 printf("MMP check failed: %s\n", msg);
746         if (mmp) {
747                 time_t t = mmp->mmp_time;
748
749                 printf("MMP error info: last update: %s node: %s device: %s\n",
750                        ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
751         }
752 }
753
754 errcode_t e2fsck_mmp_update(ext2_filsys fs)
755 {
756         errcode_t retval;
757
758         retval = ext2fs_mmp_update(fs);
759         if (retval == EXT2_ET_MMP_CHANGE_ABORT)
760                 dump_mmp_msg(fs->mmp_cmp,
761                              _("UNEXPECTED INCONSISTENCY: the filesystem is "
762                                "being modified while fsck is running.\n"));
763
764         return retval;
765 }
766
767 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
768                             const char *profile_name, unsigned int *old_type)
769 {
770         unsigned type;
771         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
772
773         if (old_type)
774                 *old_type = fs->default_bitmap_type;
775         profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
776                          default_type, &type);
777         profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
778         fs->default_bitmap_type = type ? type : default_type;
779 }
780
781 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
782                                        int deftype,
783                                        const char *name,
784                                        ext2fs_inode_bitmap *ret)
785 {
786         errcode_t       retval;
787         unsigned int    save_type;
788
789         e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
790         retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
791         fs->default_bitmap_type = save_type;
792         return retval;
793 }
794
795 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
796                                        int deftype,
797                                        const char *name,
798                                        ext2fs_block_bitmap *ret)
799 {
800         errcode_t       retval;
801         unsigned int    save_type;
802
803         e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
804         retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
805         fs->default_bitmap_type = save_type;
806         return retval;
807 }
808
809 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
810                                             int deftype,
811                                             const char *name,
812                                             ext2fs_block_bitmap *ret)
813 {
814         errcode_t       retval;
815         unsigned int    save_type;
816
817         e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
818         retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
819         fs->default_bitmap_type = save_type;
820         return retval;
821 }