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