Whamcloud - gitweb
Add RAID stride support to resize2fs
[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 <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 #ifdef HAVE_CONIO_H
18 #undef HAVE_TERMIOS_H
19 #include <conio.h>
20 #define read_a_char()   getch()
21 #else
22 #ifdef HAVE_TERMIOS_H
23 #include <termios.h>
24 #endif
25 #include <stdio.h>
26 #endif
27
28 #ifdef HAVE_MALLOC_H
29 #include <malloc.h>
30 #endif
31
32 #include "e2fsck.h"
33
34 extern e2fsck_t e2fsck_global_ctx;   /* Try your very best not to use this! */
35
36 #include <sys/time.h>
37 #include <sys/resource.h>
38
39 void fatal_error(e2fsck_t ctx, const char *msg)
40 {
41         if (msg) 
42                 fprintf (stderr, "e2fsck: %s\n", msg);
43         if (ctx->fs && ctx->fs->io) {
44                 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
45                         io_channel_flush(ctx->fs->io);
46                 else
47                         fprintf(stderr, "e2fsck: io manager magic bad!\n");
48         }
49         ctx->flags |= E2F_FLAG_ABORT;
50         if (ctx->flags & E2F_FLAG_SETJMP_OK)
51                 longjmp(ctx->abort_loc, 1);
52         exit(FSCK_ERROR);
53 }
54
55 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
56                              const char *description)
57 {
58         void *ret;
59         char buf[256];
60
61 #ifdef DEBUG_ALLOCATE_MEMORY
62         printf("Allocating %d bytes for %s...\n", size, description);
63 #endif
64         ret = malloc(size);
65         if (!ret) {
66                 sprintf(buf, "Can't allocate %s\n", description);
67                 fatal_error(ctx, buf);
68         }
69         memset(ret, 0, size);
70         return ret;
71 }
72
73 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)), 
74                   const char *str, int len)
75 {
76         char    *ret;
77         
78         if (!str)
79                 return NULL;
80         if (!len)
81                 len = strlen(str);
82         ret = malloc(len+1);
83         if (ret) {
84                 strncpy(ret, str, len);
85                 ret[len] = 0;
86         }
87         return ret;
88 }
89
90 #ifndef HAVE_STRNLEN
91 /*
92  * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
93  * provide our own.
94  */
95 int e2fsck_strnlen(const char * s, int count)
96 {
97         const char *cp = s;
98
99         while (count-- && *cp)
100                 cp++;
101         return cp - s;
102 }
103 #endif
104
105 #ifndef HAVE_CONIO_H
106 static int read_a_char(void)
107 {
108         char    c;
109         int     r;
110         int     fail = 0;
111
112         while(1) {
113                 if (e2fsck_global_ctx &&
114                     (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
115                         return 3;
116                 }
117                 r = read(0, &c, 1);
118                 if (r == 1)
119                         return c;
120                 if (fail++ > 100)
121                         break;
122         }
123         return EOF;
124 }
125 #endif
126
127 int ask_yn(const char * string, int def)
128 {
129         int             c;
130         const char      *defstr;
131         const char      *short_yes = _("yY");
132         const char      *short_no = _("nN");
133
134 #ifdef HAVE_TERMIOS_H
135         struct termios  termios, tmp;
136
137         tcgetattr (0, &termios);
138         tmp = termios;
139         tmp.c_lflag &= ~(ICANON | ECHO);
140         tmp.c_cc[VMIN] = 1;
141         tmp.c_cc[VTIME] = 0;
142         tcsetattr (0, TCSANOW, &tmp);
143 #endif
144
145         if (def == 1)
146                 defstr = _(_("<y>"));
147         else if (def == 0)
148                 defstr = _(_("<n>"));
149         else
150                 defstr = _(" (y/n)");
151         printf("%s%s? ", string, defstr);
152         while (1) {
153                 fflush (stdout);
154                 if ((c = read_a_char()) == EOF)
155                         break;
156                 if (c == 3) {
157 #ifdef HAVE_TERMIOS_H
158                         tcsetattr (0, TCSANOW, &termios);
159 #endif
160                         if (e2fsck_global_ctx &&
161                             e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
162                                 puts("\n");
163                                 longjmp(e2fsck_global_ctx->abort_loc, 1);
164                         }
165                         puts(_("cancelled!\n"));
166                         return 0;
167                 }
168                 if (strchr(short_yes, (char) c)) {
169                         def = 1;
170                         break;
171                 }
172                 else if (strchr(short_no, (char) c)) {
173                         def = 0;
174                         break;
175                 }
176                 else if ((c == ' ' || c == '\n') && (def != -1))
177                         break;
178         }
179         if (def)
180                 puts(_("yes\n"));
181         else
182                 puts (_("no\n"));
183 #ifdef HAVE_TERMIOS_H
184         tcsetattr (0, TCSANOW, &termios);
185 #endif
186         return def;
187 }
188
189 int ask (e2fsck_t ctx, const char * string, int def)
190 {
191         if (ctx->options & E2F_OPT_NO) {
192                 printf (_("%s? no\n\n"), string);
193                 return 0;
194         }
195         if (ctx->options & E2F_OPT_YES) {
196                 printf (_("%s? yes\n\n"), string);
197                 return 1;
198         }
199         if (ctx->options & E2F_OPT_PREEN) {
200                 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
201                 return def;
202         }
203         return ask_yn(string, def);
204 }
205
206 void e2fsck_read_bitmaps(e2fsck_t ctx)
207 {
208         ext2_filsys fs = ctx->fs;
209         errcode_t       retval;
210
211         if (ctx->invalid_bitmaps) {
212                 com_err(ctx->program_name, 0,
213                     _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
214                         ctx->device_name);
215                 fatal_error(ctx, 0);
216         }
217
218         ehandler_operation(_("reading inode and block bitmaps"));
219         retval = ext2fs_read_bitmaps(fs);
220         ehandler_operation(0);
221         if (retval) {
222                 com_err(ctx->program_name, retval,
223                         _("while retrying to read bitmaps for %s"),
224                         ctx->device_name);
225                 fatal_error(ctx, 0);
226         }
227 }
228
229 void e2fsck_write_bitmaps(e2fsck_t ctx)
230 {
231         ext2_filsys fs = ctx->fs;
232         errcode_t       retval;
233
234         if (ext2fs_test_bb_dirty(fs)) {
235                 ehandler_operation(_("writing block bitmaps"));
236                 retval = ext2fs_write_block_bitmap(fs);
237                 ehandler_operation(0);
238                 if (retval) {
239                         com_err(ctx->program_name, retval,
240                             _("while retrying to write block bitmaps for %s"),
241                                 ctx->device_name);
242                         fatal_error(ctx, 0);
243                 }
244         }
245
246         if (ext2fs_test_ib_dirty(fs)) {
247                 ehandler_operation(_("writing inode bitmaps"));
248                 retval = ext2fs_write_inode_bitmap(fs);
249                 ehandler_operation(0);
250                 if (retval) {
251                         com_err(ctx->program_name, retval,
252                             _("while retrying to write inode bitmaps for %s"),
253                                 ctx->device_name);
254                         fatal_error(ctx, 0);
255                 }
256         }
257 }
258
259 void preenhalt(e2fsck_t ctx)
260 {
261         ext2_filsys fs = ctx->fs;
262
263         if (!(ctx->options & E2F_OPT_PREEN))
264                 return;
265         fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
266                 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
267                ctx->device_name);
268         if (fs != NULL) {
269                 fs->super->s_state |= EXT2_ERROR_FS;
270                 ext2fs_mark_super_dirty(fs);
271                 ext2fs_close(fs);
272         }
273         exit(FSCK_UNCORRECTED);
274 }
275
276 #ifdef RESOURCE_TRACK
277 void init_resource_track(struct resource_track *track)
278 {
279 #ifdef HAVE_GETRUSAGE
280         struct rusage r;
281 #endif
282         
283         track->brk_start = sbrk(0);
284         gettimeofday(&track->time_start, 0);
285 #ifdef HAVE_GETRUSAGE
286 #ifdef sun
287         memset(&r, 0, sizeof(struct rusage));
288 #endif
289         getrusage(RUSAGE_SELF, &r);
290         track->user_start = r.ru_utime;
291         track->system_start = r.ru_stime;
292 #else
293         track->user_start.tv_sec = track->user_start.tv_usec = 0;
294         track->system_start.tv_sec = track->system_start.tv_usec = 0;
295 #endif
296 }
297
298 #ifdef __GNUC__
299 #define _INLINE_ __inline__
300 #else
301 #define _INLINE_
302 #endif
303
304 static _INLINE_ float timeval_subtract(struct timeval *tv1,
305                                        struct timeval *tv2)
306 {
307         return ((tv1->tv_sec - tv2->tv_sec) +
308                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
309 }
310
311 void print_resource_track(const char *desc, struct resource_track *track)
312 {
313 #ifdef HAVE_GETRUSAGE
314         struct rusage r;
315 #endif
316 #ifdef HAVE_MALLINFO
317         struct mallinfo malloc_info;
318 #endif
319         struct timeval time_end;
320
321         gettimeofday(&time_end, 0);
322
323         if (desc)
324                 printf("%s: ", desc);
325
326 #ifdef HAVE_MALLINFO
327 #define kbytes(x)       (((x) + 1023) / 1024)
328         
329         malloc_info = mallinfo();
330         printf(_("Memory used: %dk/%dk (%dk/%dk), "),
331                kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
332                kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
333 #else
334         printf(_("Memory used: %d, "),
335                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
336 #endif  
337 #ifdef HAVE_GETRUSAGE
338         getrusage(RUSAGE_SELF, &r);
339
340         printf(_("time: %5.2f/%5.2f/%5.2f\n"),
341                timeval_subtract(&time_end, &track->time_start),
342                timeval_subtract(&r.ru_utime, &track->user_start),
343                timeval_subtract(&r.ru_stime, &track->system_start));
344 #else
345         printf(_("elapsed time: %6.3f\n"),
346                timeval_subtract(&time_end, &track->time_start));
347 #endif
348 }
349 #endif /* RESOURCE_TRACK */
350
351 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
352                               struct ext2_inode * inode, const char *proc)
353 {
354         int retval;
355
356         retval = ext2fs_read_inode(ctx->fs, ino, inode);
357         if (retval) {
358                 com_err("ext2fs_read_inode", retval,
359                         _("while reading inode %ld in %s"), ino, proc);
360                 fatal_error(ctx, 0);
361         }
362 }
363
364 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
365                                struct ext2_inode * inode, int bufsize,
366                                const char *proc)
367 {
368         int retval;
369
370         retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
371         if (retval) {
372                 com_err("ext2fs_write_inode", retval,
373                         _("while writing inode %ld in %s"), ino, proc);
374                 fatal_error(ctx, 0);
375         }
376 }
377
378 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
379                                struct ext2_inode * inode, const char *proc)
380 {
381         int retval;
382
383         retval = ext2fs_write_inode(ctx->fs, ino, inode);
384         if (retval) {
385                 com_err("ext2fs_write_inode", retval,
386                         _("while writing inode %ld in %s"), ino, proc);
387                 fatal_error(ctx, 0);
388         }
389 }
390
391 #ifdef MTRACE
392 void mtrace_print(char *mesg)
393 {
394         FILE    *malloc_get_mallstream();
395         FILE    *f = malloc_get_mallstream();
396
397         if (f)
398                 fprintf(f, "============= %s\n", mesg);
399 }
400 #endif
401
402 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
403                    io_manager manager)
404 {
405         struct ext2_super_block *sb;
406         io_channel              io = NULL;
407         void                    *buf = NULL;
408         int                     blocksize;
409         blk_t                   superblock, ret_sb = 8193;
410         
411         if (fs && fs->super) {
412                 ret_sb = (fs->super->s_blocks_per_group +
413                           fs->super->s_first_data_block);
414                 if (ctx) {
415                         ctx->superblock = ret_sb;
416                         ctx->blocksize = fs->blocksize;
417                 }
418                 return ret_sb;
419         }
420                 
421         if (ctx) {
422                 if (ctx->blocksize) {
423                         ret_sb = ctx->blocksize * 8;
424                         if (ctx->blocksize == 1024)
425                                 ret_sb++;
426                         ctx->superblock = ret_sb;
427                         return ret_sb;
428                 }
429                 ctx->superblock = ret_sb;
430                 ctx->blocksize = 1024;
431         }
432
433         if (!name || !manager)
434                 goto cleanup;
435
436         if (manager->open(name, 0, &io) != 0)
437                 goto cleanup;
438
439         if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
440                 goto cleanup;
441         sb = (struct ext2_super_block *) buf;
442
443         for (blocksize = EXT2_MIN_BLOCK_SIZE;
444              blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
445                 superblock = blocksize*8;
446                 if (blocksize == 1024)
447                         superblock++;
448                 io_channel_set_blksize(io, blocksize);
449                 if (io_channel_read_blk(io, superblock,
450                                         -SUPERBLOCK_SIZE, buf))
451                         continue;
452 #ifdef EXT2FS_ENABLE_SWAPFS
453                 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
454                         ext2fs_swap_super(sb);
455 #endif
456                 if (sb->s_magic == EXT2_SUPER_MAGIC) {
457                         ret_sb = superblock;
458                         if (ctx) {
459                                 ctx->superblock = superblock;
460                                 ctx->blocksize = blocksize;
461                         }
462                         break;
463                 }
464         }
465
466 cleanup:
467         if (io)
468                 io_channel_close(io);
469         if (buf)
470                 ext2fs_free_mem(&buf);
471         return (ret_sb);
472 }
473
474 /*
475  * Given a mode, return the ext2 file type
476  */
477 int ext2_file_type(unsigned int mode)
478 {
479         if (LINUX_S_ISREG(mode))
480                 return EXT2_FT_REG_FILE;
481
482         if (LINUX_S_ISDIR(mode))
483                 return EXT2_FT_DIR;
484         
485         if (LINUX_S_ISCHR(mode))
486                 return EXT2_FT_CHRDEV;
487         
488         if (LINUX_S_ISBLK(mode))
489                 return EXT2_FT_BLKDEV;
490         
491         if (LINUX_S_ISLNK(mode))
492                 return EXT2_FT_SYMLINK;
493
494         if (LINUX_S_ISFIFO(mode))
495                 return EXT2_FT_FIFO;
496         
497         if (LINUX_S_ISSOCK(mode))
498                 return EXT2_FT_SOCK;
499         
500         return 0;
501 }