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