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