blob: 3f2ff5be800ab90937c12185c343a40b4fb5e051 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * util.c --- miscellaneous utilities
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdlib.h>
Theodore Ts'o80875db2008-10-12 23:09:26 -040013#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000014#include <unistd.h>
15#include <string.h>
16#include <ctype.h>
Theodore Ts'o80875db2008-10-12 23:09:26 -040017#ifdef __linux__
18#include <sys/utsname.h>
19#endif
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000020
21#ifdef HAVE_CONIO_H
22#undef HAVE_TERMIOS_H
23#include <conio.h>
Theodore Ts'oe6597041999-10-26 02:30:16 +000024#define read_a_char() getch()
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000025#else
26#ifdef HAVE_TERMIOS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <termios.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000028#endif
29#endif
30
Theodore Ts'o4a9f5931999-03-16 19:32:52 +000031#ifdef HAVE_MALLOC_H
32#include <malloc.h>
33#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
Jose R. Santos49a73602007-10-21 21:04:03 -050035#ifdef HAVE_ERRNO_H
36#include <errno.h>
37#endif
38
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include "e2fsck.h"
40
Theodore Ts'oaa75ecc2003-03-17 10:01:22 -050041extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
Theodore Ts'o542867f2002-07-24 07:42:02 -040042
JP Abgralle0ed7402014-03-19 19:08:39 -070043#include <stdarg.h>
44#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000045#include <sys/time.h>
46#include <sys/resource.h>
47
Theodore Ts'of8188ff1997-11-14 05:23:04 +000048void fatal_error(e2fsck_t ctx, const char *msg)
Theodore Ts'o3839e651997-04-26 13:21:57 +000049{
JP Abgralle0ed7402014-03-19 19:08:39 -070050 ext2_filsys fs = ctx->fs;
51 int exit_value = FSCK_ERROR;
52
Theodore Ts'oefc6f622008-08-27 23:07:54 -040053 if (msg)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054 fprintf (stderr, "e2fsck: %s\n", msg);
JP Abgralle0ed7402014-03-19 19:08:39 -070055 if (!fs)
56 goto out;
57 if (fs->io && fs->super) {
58 ext2fs_mmp_stop(ctx->fs);
Theodore Ts'oe39ac922001-05-05 06:47:24 +000059 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
Theodore Ts'o58a75172001-01-12 20:59:19 +000060 io_channel_flush(ctx->fs->io);
61 else
JP Abgralle0ed7402014-03-19 19:08:39 -070062 log_err(ctx, "e2fsck: io manager magic bad!\n");
Theodore Ts'o58a75172001-01-12 20:59:19 +000063 }
JP Abgralle0ed7402014-03-19 19:08:39 -070064 if (ext2fs_test_changed(fs)) {
65 exit_value |= FSCK_NONDESTRUCT;
66 log_out(ctx, _("\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 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
73 "errors **********\n\n"), ctx->device_name);
74 exit_value |= FSCK_UNCORRECTED;
75 exit_value &= ~FSCK_NONDESTRUCT;
76 }
77out:
Theodore Ts'of8188ff1997-11-14 05:23:04 +000078 ctx->flags |= E2F_FLAG_ABORT;
79 if (ctx->flags & E2F_FLAG_SETJMP_OK)
80 longjmp(ctx->abort_loc, 1);
JP Abgralle0ed7402014-03-19 19:08:39 -070081 exit(exit_value);
82}
83
84void log_out(e2fsck_t ctx, const char *fmt, ...)
85{
86 va_list pvar;
87
88 va_start(pvar, fmt);
89 vprintf(fmt, pvar);
90 va_end(pvar);
91 if (ctx->logf) {
92 va_start(pvar, fmt);
93 vfprintf(ctx->logf, fmt, pvar);
94 va_end(pvar);
95 }
96}
97
98void log_err(e2fsck_t ctx, const char *fmt, ...)
99{
100 va_list pvar;
101
102 va_start(pvar, fmt);
103 vfprintf(stderr, fmt, pvar);
104 va_end(pvar);
105 if (ctx->logf) {
106 va_start(pvar, fmt);
107 vfprintf(ctx->logf, fmt, pvar);
108 va_end(pvar);
109 }
Theodore Ts'ob0e91c82012-03-17 23:21:00 -0400110}
111
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000112void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
113 const char *description)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114{
115 void *ret;
116 char buf[256];
117
118#ifdef DEBUG_ALLOCATE_MEMORY
Andreas Dilger611378f2008-10-28 14:44:21 -0600119 printf("Allocating %u bytes for %s...\n", size, description);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120#endif
121 ret = malloc(size);
122 if (!ret) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700123 sprintf(buf, "Can't allocate %u bytes for %s\n",
124 size, description);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000125 fatal_error(ctx, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 }
127 memset(ret, 0, size);
128 return ret;
129}
130
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400131char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500132 const char *str, int len)
Theodore Ts'of3640932003-03-01 19:47:44 -0500133{
134 char *ret;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400135
Theodore Ts'of3640932003-03-01 19:47:44 -0500136 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 */
153int 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
Theodore Ts'o542867f2002-07-24 07:42:02 -0400163#ifndef HAVE_CONIO_H
Theodore Ts'o3e699062002-10-13 23:56:28 -0400164static int read_a_char(void)
Theodore Ts'o542867f2002-07-24 07:42:02 -0400165{
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
JP Abgralle0ed7402014-03-19 19:08:39 -0700185int ask_yn(e2fsck_t ctx, const char * string, int def)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186{
187 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 const char *defstr;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000189 const char *short_yes = _("yY");
190 const char *short_no = _("nN");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000192#ifdef HAVE_TERMIOS_H
193 struct termios termios, tmp;
194
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 tcgetattr (0, &termios);
196 tmp = termios;
197 tmp.c_lflag &= ~(ICANON | ECHO);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000198 tmp.c_cc[VMIN] = 1;
199 tmp.c_cc[VTIME] = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 tcsetattr (0, TCSANOW, &tmp);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000201#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202
203 if (def == 1)
Theodore Ts'o542867f2002-07-24 07:42:02 -0400204 defstr = _(_("<y>"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 else if (def == 0)
Theodore Ts'o542867f2002-07-24 07:42:02 -0400206 defstr = _(_("<n>"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000208 defstr = _(" (y/n)");
JP Abgralle0ed7402014-03-19 19:08:39 -0700209 log_out(ctx, "%s%s? ", string, defstr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 while (1) {
211 fflush (stdout);
Theodore Ts'oe6597041999-10-26 02:30:16 +0000212 if ((c = read_a_char()) == EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213 break;
Theodore Ts'o542867f2002-07-24 07:42:02 -0400214 if (c == 3) {
215#ifdef HAVE_TERMIOS_H
216 tcsetattr (0, TCSANOW, &termios);
217#endif
JP Abgralle0ed7402014-03-19 19:08:39 -0700218 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
219 log_out(ctx, "\n");
Theodore Ts'o542867f2002-07-24 07:42:02 -0400220 longjmp(e2fsck_global_ctx->abort_loc, 1);
221 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700222 log_out(ctx, "%s", _("cancelled!\n"));
Theodore Ts'o542867f2002-07-24 07:42:02 -0400223 return 0;
224 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000225 if (strchr(short_yes, (char) c)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 def = 1;
227 break;
228 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000229 else if (strchr(short_no, (char) c)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 def = 0;
231 break;
232 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700233 else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234 break;
235 }
236 if (def)
JP Abgralle0ed7402014-03-19 19:08:39 -0700237 log_out(ctx, "%s", _("yes\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 else
JP Abgralle0ed7402014-03-19 19:08:39 -0700239 log_out(ctx, "%s", _("no\n"));
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000240#ifdef HAVE_TERMIOS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 tcsetattr (0, TCSANOW, &termios);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000242#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 return def;
244}
245
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000246int ask (e2fsck_t ctx, const char * string, int def)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000248 if (ctx->options & E2F_OPT_NO) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700249 log_out(ctx, _("%s? no\n\n"), string);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 return 0;
251 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000252 if (ctx->options & E2F_OPT_YES) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700253 log_out(ctx, _("%s? yes\n\n"), string);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 return 1;
255 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000256 if (ctx->options & E2F_OPT_PREEN) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700257 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258 return def;
259 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700260 return ask_yn(ctx, string, def);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261}
262
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000263void e2fsck_read_bitmaps(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000265 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 errcode_t retval;
Theodore Ts'oe94bc632007-04-14 09:29:02 -0400267 const char *old_op;
JP Abgralle0ed7402014-03-19 19:08:39 -0700268 unsigned int save_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000270 if (ctx->invalid_bitmaps) {
271 com_err(ctx->program_name, 0,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000272 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000273 ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000274 fatal_error(ctx, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000276
Theodore Ts'oe94bc632007-04-14 09:29:02 -0400277 old_op = ehandler_operation(_("reading inode and block bitmaps"));
JP Abgralle0ed7402014-03-19 19:08:39 -0700278 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
279 &save_type);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 retval = ext2fs_read_bitmaps(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -0700281 fs->default_bitmap_type = save_type;
Theodore Ts'oe94bc632007-04-14 09:29:02 -0400282 ehandler_operation(old_op);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000283 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000284 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000285 _("while retrying to read bitmaps for %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000286 ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000287 fatal_error(ctx, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 }
289}
290
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000291void e2fsck_write_bitmaps(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000293 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294 errcode_t retval;
Theodore Ts'oe94bc632007-04-14 09:29:02 -0400295 const char *old_op;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296
Andreas Dilger1bb6f6c2008-08-23 20:56:07 -0600297 old_op = ehandler_operation(_("writing block and inode bitmaps"));
298 retval = ext2fs_write_bitmaps(fs);
299 ehandler_operation(old_op);
300 if (retval) {
301 com_err(ctx->program_name, retval,
302 _("while rewriting block and inode bitmaps for %s"),
303 ctx->device_name);
304 fatal_error(ctx, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 }
306}
307
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000308void preenhalt(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000310 ext2_filsys fs = ctx->fs;
311
312 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 return;
JP Abgralle0ed7402014-03-19 19:08:39 -0700314 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000315 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000316 ctx->device_name);
Eric Sandeen79cc3362008-10-10 17:14:08 -0500317 ctx->flags |= E2F_FLAG_EXITING;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000318 if (fs != NULL) {
319 fs->super->s_state |= EXT2_ERROR_FS;
320 ext2fs_mark_super_dirty(fs);
321 ext2fs_close(fs);
322 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323 exit(FSCK_UNCORRECTED);
324}
325
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000326#ifdef RESOURCE_TRACK
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400327void init_resource_track(struct resource_track *track, io_channel channel)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000329#ifdef HAVE_GETRUSAGE
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330 struct rusage r;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000331#endif
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400332 io_stats io_start = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400333
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334 track->brk_start = sbrk(0);
335 gettimeofday(&track->time_start, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000336#ifdef HAVE_GETRUSAGE
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000337#ifdef sun
338 memset(&r, 0, sizeof(struct rusage));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000339#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 getrusage(RUSAGE_SELF, &r);
341 track->user_start = r.ru_utime;
342 track->system_start = r.ru_stime;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000343#else
344 track->user_start.tv_sec = track->user_start.tv_usec = 0;
345 track->system_start.tv_sec = track->system_start.tv_usec = 0;
346#endif
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400347 track->bytes_read = 0;
348 track->bytes_written = 0;
349 if (channel && channel->manager && channel->manager->get_stats)
350 channel->manager->get_stats(channel, &io_start);
351 if (io_start) {
352 track->bytes_read = io_start->bytes_read;
353 track->bytes_written = io_start->bytes_written;
354 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000355}
356
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000357#ifdef __GNUC__
358#define _INLINE_ __inline__
359#else
360#define _INLINE_
361#endif
362
363static _INLINE_ float timeval_subtract(struct timeval *tv1,
364 struct timeval *tv2)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365{
366 return ((tv1->tv_sec - tv2->tv_sec) +
367 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
368}
369
Ken Chen9facd072009-05-28 09:55:10 -0400370void print_resource_track(e2fsck_t ctx, const char *desc,
371 struct resource_track *track, io_channel channel)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000373#ifdef HAVE_GETRUSAGE
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 struct rusage r;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000375#endif
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000376#ifdef HAVE_MALLINFO
377 struct mallinfo malloc_info;
378#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000379 struct timeval time_end;
380
Ken Chen9facd072009-05-28 09:55:10 -0400381 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
382 (!desc && !(ctx->options & E2F_OPT_TIME)))
383 return;
384
385 e2fsck_clear_progbar(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386 gettimeofday(&time_end, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000387
388 if (desc)
JP Abgralle0ed7402014-03-19 19:08:39 -0700389 log_out(ctx, "%s: ", desc);
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000390
391#ifdef HAVE_MALLINFO
Andreas Dilger70eabde2009-05-28 00:47:37 -0600392#define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400393
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000394 malloc_info = mallinfo();
JP Abgralle0ed7402014-03-19 19:08:39 -0700395 log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
396 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
397 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000398#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700399 log_out(ctx, _("Memory used: %lu, "),
400 (unsigned long) (((char *) sbrk(0)) -
401 ((char *) track->brk_start)));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400402#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000403#ifdef HAVE_GETRUSAGE
Theodore Ts'o3839e651997-04-26 13:21:57 +0000404 getrusage(RUSAGE_SELF, &r);
405
JP Abgralle0ed7402014-03-19 19:08:39 -0700406 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
407 timeval_subtract(&time_end, &track->time_start),
408 timeval_subtract(&r.ru_utime, &track->user_start),
409 timeval_subtract(&r.ru_stime, &track->system_start));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000410#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700411 log_out(ctx, _("elapsed time: %6.3f\n"),
412 timeval_subtract(&time_end, &track->time_start));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000413#endif
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400414#define mbytes(x) (((x) + 1048575) / 1048576)
415 if (channel && channel->manager && channel->manager->get_stats) {
416 io_stats delta = 0;
417 unsigned long long bytes_read = 0;
418 unsigned long long bytes_written = 0;
419
420 if (desc)
JP Abgralle0ed7402014-03-19 19:08:39 -0700421 log_out(ctx, "%s: ", desc);
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400422
423 channel->manager->get_stats(channel, &delta);
424 if (delta) {
425 bytes_read = delta->bytes_read - track->bytes_read;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400426 bytes_written = delta->bytes_written -
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400427 track->bytes_written;
428 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700429 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
430 "rate: %.2fMB/s\n",
431 mbytes(bytes_read), mbytes(bytes_written),
432 (double)mbytes(bytes_read + bytes_written) /
433 timeval_subtract(&time_end, &track->time_start));
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400434 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435}
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000436#endif /* RESOURCE_TRACK */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437
Theodore Ts'o08b21301997-11-03 19:42:40 +0000438void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000439 struct ext2_inode * inode, const char *proc)
440{
JP Abgralle0ed7402014-03-19 19:08:39 -0700441 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000442
Theodore Ts'o08b21301997-11-03 19:42:40 +0000443 retval = ext2fs_read_inode(ctx->fs, ino, inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000444 if (retval) {
445 com_err("ext2fs_read_inode", retval,
Andreas Dilger611378f2008-10-28 14:44:21 -0600446 _("while reading inode %lu in %s"), ino, proc);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000447 fatal_error(ctx, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000448 }
449}
450
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700451void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
452 struct ext2_inode *inode, int bufsize,
453 const char *proc)
454{
JP Abgralle0ed7402014-03-19 19:08:39 -0700455 errcode_t retval;
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700456
457 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
458 if (retval) {
459 com_err("ext2fs_read_inode_full", retval,
Andreas Dilger611378f2008-10-28 14:44:21 -0600460 _("while reading inode %lu in %s"), ino, proc);
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700461 fatal_error(ctx, 0);
462 }
463}
464
JP Abgralle0ed7402014-03-19 19:08:39 -0700465void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
466 struct ext2_inode * inode, int bufsize,
467 const char *proc)
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500468{
JP Abgralle0ed7402014-03-19 19:08:39 -0700469 errcode_t retval;
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500470
471 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
472 if (retval) {
473 com_err("ext2fs_write_inode", retval,
Andreas Dilger611378f2008-10-28 14:44:21 -0600474 _("while writing inode %lu in %s"), ino, proc);
Theodore Ts'ocebe48a2005-03-21 13:15:45 -0500475 fatal_error(ctx, 0);
476 }
477}
478
JP Abgralle0ed7402014-03-19 19:08:39 -0700479void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
480 struct ext2_inode * inode, const char *proc)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000481{
JP Abgralle0ed7402014-03-19 19:08:39 -0700482 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000483
Theodore Ts'o08b21301997-11-03 19:42:40 +0000484 retval = ext2fs_write_inode(ctx->fs, ino, inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000485 if (retval) {
486 com_err("ext2fs_write_inode", retval,
Andreas Dilger611378f2008-10-28 14:44:21 -0600487 _("while writing inode %lu in %s"), ino, proc);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000488 fatal_error(ctx, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000489 }
490}
491
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492#ifdef MTRACE
493void mtrace_print(char *mesg)
494{
495 FILE *malloc_get_mallstream();
496 FILE *f = malloc_get_mallstream();
497
498 if (f)
499 fprintf(f, "============= %s\n", mesg);
500}
501#endif
502
JP Abgralle0ed7402014-03-19 19:08:39 -0700503blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
504 io_manager manager)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000505{
Theodore Ts'of1a17612001-12-23 22:27:52 -0500506 struct ext2_super_block *sb;
507 io_channel io = NULL;
Theodore Ts'ofae9bfa2001-12-23 22:56:12 -0500508 void *buf = NULL;
Theodore Ts'of1a17612001-12-23 22:27:52 -0500509 int blocksize;
JP Abgralle0ed7402014-03-19 19:08:39 -0700510 blk64_t superblock, ret_sb = 8193;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400511
Theodore Ts'of1a17612001-12-23 22:27:52 -0500512 if (fs && fs->super) {
513 ret_sb = (fs->super->s_blocks_per_group +
514 fs->super->s_first_data_block);
515 if (ctx) {
516 ctx->superblock = ret_sb;
517 ctx->blocksize = fs->blocksize;
518 }
519 return ret_sb;
520 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400521
Theodore Ts'of1a17612001-12-23 22:27:52 -0500522 if (ctx) {
523 if (ctx->blocksize) {
524 ret_sb = ctx->blocksize * 8;
525 if (ctx->blocksize == 1024)
526 ret_sb++;
527 ctx->superblock = ret_sb;
528 return ret_sb;
529 }
530 ctx->superblock = ret_sb;
531 ctx->blocksize = 1024;
532 }
533
534 if (!name || !manager)
535 goto cleanup;
536
537 if (manager->open(name, 0, &io) != 0)
538 goto cleanup;
539
540 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
541 goto cleanup;
542 sb = (struct ext2_super_block *) buf;
543
Andreas Dilger932a4892002-05-16 03:20:07 -0600544 for (blocksize = EXT2_MIN_BLOCK_SIZE;
545 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
Theodore Ts'of1a17612001-12-23 22:27:52 -0500546 superblock = blocksize*8;
547 if (blocksize == 1024)
548 superblock++;
549 io_channel_set_blksize(io, blocksize);
JP Abgralle0ed7402014-03-19 19:08:39 -0700550 if (io_channel_read_blk64(io, superblock,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500551 -SUPERBLOCK_SIZE, buf))
552 continue;
Theodore Ts'o2eae0932007-08-11 02:57:31 -0400553#ifdef WORDS_BIGENDIAN
Theodore Ts'of1a17612001-12-23 22:27:52 -0500554 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
555 ext2fs_swap_super(sb);
556#endif
Daniel Drakee7b5d3c2007-05-31 11:56:17 -0400557 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
558 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
Theodore Ts'o557ddb22003-08-24 21:36:38 -0400559 ret_sb = superblock;
560 if (ctx) {
561 ctx->superblock = superblock;
562 ctx->blocksize = blocksize;
563 }
Theodore Ts'of1a17612001-12-23 22:27:52 -0500564 break;
565 }
566 }
567
568cleanup:
569 if (io)
570 io_channel_close(io);
571 if (buf)
572 ext2fs_free_mem(&buf);
573 return (ret_sb);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000574}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000576/*
577 * Given a mode, return the ext2 file type
578 */
579int ext2_file_type(unsigned int mode)
580{
581 if (LINUX_S_ISREG(mode))
582 return EXT2_FT_REG_FILE;
583
584 if (LINUX_S_ISDIR(mode))
585 return EXT2_FT_DIR;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400586
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000587 if (LINUX_S_ISCHR(mode))
588 return EXT2_FT_CHRDEV;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400589
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000590 if (LINUX_S_ISBLK(mode))
591 return EXT2_FT_BLKDEV;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400592
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000593 if (LINUX_S_ISLNK(mode))
594 return EXT2_FT_SYMLINK;
595
596 if (LINUX_S_ISFIFO(mode))
597 return EXT2_FT_FIFO;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400598
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000599 if (LINUX_S_ISSOCK(mode))
600 return EXT2_FT_SOCK;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400601
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000602 return 0;
603}
Jose R. Santos49a73602007-10-21 21:04:03 -0500604
605#define STRIDE_LENGTH 8
606/*
607 * Helper function which zeros out _num_ blocks starting at _blk_. In
608 * case of an error, the details of the error is returned via _ret_blk_
609 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
610 * success, and an error code on an error.
611 *
612 * As a special case, if the first argument is NULL, then it will
613 * attempt to free the static zeroizing buffer. (This is to keep
614 * programs that check for memory leaks happy.)
615 */
616errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
617 blk_t *ret_blk, int *ret_count)
618{
JP Abgralle0ed7402014-03-19 19:08:39 -0700619 int j, count;
Jose R. Santos49a73602007-10-21 21:04:03 -0500620 static char *buf;
621 errcode_t retval;
622
623 /* If fs is null, clean up the static buffer and return */
624 if (!fs) {
625 if (buf) {
626 free(buf);
627 buf = 0;
628 }
629 return 0;
630 }
631 /* Allocate the zeroizing buffer if necessary */
632 if (!buf) {
633 buf = malloc(fs->blocksize * STRIDE_LENGTH);
634 if (!buf) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700635 com_err("malloc", ENOMEM, "%s",
Jose R. Santos49a73602007-10-21 21:04:03 -0500636 _("while allocating zeroizing buffer"));
637 exit(1);
638 }
639 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
640 }
641 /* OK, do the write loop */
Jose R. Santos49a73602007-10-21 21:04:03 -0500642 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
643 count = num - j;
644 if (count > STRIDE_LENGTH)
645 count = STRIDE_LENGTH;
JP Abgralle0ed7402014-03-19 19:08:39 -0700646 retval = io_channel_write_blk64(fs->io, blk, count, buf);
Jose R. Santos49a73602007-10-21 21:04:03 -0500647 if (retval) {
648 if (ret_count)
649 *ret_count = count;
650 if (ret_blk)
651 *ret_blk = blk;
652 return retval;
653 }
654 }
655 return 0;
656}
Theodore Ts'o80875db2008-10-12 23:09:26 -0400657
658/*
659 * Check to see if a filesystem is in /proc/filesystems.
660 * Returns 1 if found, 0 if not
661 */
662int fs_proc_check(const char *fs_name)
663{
664 FILE *f;
665 char buf[80], *cp, *t;
666
667 f = fopen("/proc/filesystems", "r");
668 if (!f)
669 return (0);
670 while (!feof(f)) {
671 if (!fgets(buf, sizeof(buf), f))
672 break;
673 cp = buf;
674 if (!isspace(*cp)) {
675 while (*cp && !isspace(*cp))
676 cp++;
677 }
678 while (*cp && isspace(*cp))
679 cp++;
680 if ((t = strchr(cp, '\n')) != NULL)
681 *t = 0;
682 if ((t = strchr(cp, '\t')) != NULL)
683 *t = 0;
684 if ((t = strchr(cp, ' ')) != NULL)
685 *t = 0;
686 if (!strcmp(fs_name, cp)) {
687 fclose(f);
688 return (1);
689 }
690 }
691 fclose(f);
692 return (0);
693}
694
695/*
696 * Check to see if a filesystem is available as a module
697 * Returns 1 if found, 0 if not
698 */
699int check_for_modules(const char *fs_name)
700{
701#ifdef __linux__
702 struct utsname uts;
703 FILE *f;
704 char buf[1024], *cp, *t;
705 int i;
706
707 if (uname(&uts))
708 return (0);
709 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
710
711 f = fopen(buf, "r");
712 if (!f)
713 return (0);
714 while (!feof(f)) {
715 if (!fgets(buf, sizeof(buf), f))
716 break;
717 if ((cp = strchr(buf, ':')) != NULL)
718 *cp = 0;
719 else
720 continue;
721 if ((cp = strrchr(buf, '/')) != NULL)
722 cp++;
Eric Sandeenf2fe5da2009-02-24 15:13:39 -0600723 else
724 cp = buf;
Theodore Ts'o80875db2008-10-12 23:09:26 -0400725 i = strlen(cp);
726 if (i > 3) {
727 t = cp + i - 3;
728 if (!strcmp(t, ".ko"))
729 *t = 0;
730 }
731 if (!strcmp(cp, fs_name)) {
732 fclose(f);
733 return (1);
734 }
735 }
736 fclose(f);
737#endif /* __linux__ */
738 return (0);
739}
Theodore Ts'ob0258cb2009-04-22 15:09:41 -0400740
741/*
742 * Helper function that does the right thing if write returns a
743 * partial write, or an EGAIN/EINTR error.
744 */
745int write_all(int fd, char *buf, size_t count)
746{
747 ssize_t ret;
748 int c = 0;
749
750 while (count > 0) {
751 ret = write(fd, buf, count);
752 if (ret < 0) {
753 if ((errno == EAGAIN) || (errno == EINTR))
754 continue;
755 return -1;
756 }
757 count -= ret;
758 buf += ret;
759 c += ret;
760 }
761 return c;
762}
JP Abgralle0ed7402014-03-19 19:08:39 -0700763
764void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
765{
766
767 if (msg)
768 printf("MMP check failed: %s\n", msg);
769 if (mmp) {
770 time_t t = mmp->mmp_time;
771
772 printf("MMP error info: last update: %s node: %s device: %s\n",
773 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
774 }
775}
776
777errcode_t e2fsck_mmp_update(ext2_filsys fs)
778{
779 errcode_t retval;
780
781 retval = ext2fs_mmp_update(fs);
782 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
783 dump_mmp_msg(fs->mmp_cmp,
784 _("UNEXPECTED INCONSISTENCY: the filesystem is "
785 "being modified while fsck is running.\n"));
786
787 return retval;
788}
789
790void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
791 const char *profile_name, unsigned int *old_type)
792{
793 unsigned type;
794
795 if (old_type)
796 *old_type = fs->default_bitmap_type;
797#ifdef HAVE_SIGNAL_H
798 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
799 profile_name, 0, default_type, &type);
800 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
801 "all", 0, type, &type);
802 fs->default_bitmap_type = type ? type : default_type;
803#else
804 fs->default_bitmap_type = default_type;
805#endif
806}
807
808errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
809 int deftype,
810 const char *name,
811 ext2fs_inode_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_inode_bitmap(fs, descr, ret);
818 fs->default_bitmap_type = save_type;
819 return retval;
820}
821
822errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
823 int deftype,
824 const char *name,
825 ext2fs_block_bitmap *ret)
826{
827 errcode_t retval;
828 unsigned int save_type;
829
830 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
831 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
832 fs->default_bitmap_type = save_type;
833 return retval;
834}
835
836errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
837 int deftype,
838 const char *name,
839 ext2fs_block_bitmap *ret)
840{
841 errcode_t retval;
842 unsigned int save_type;
843
844 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
845 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
846 fs->default_bitmap_type = save_type;
847 return retval;
848}