summaryrefslogtreecommitdiff
path: root/sutils/fsck.c
blob: 161c275dd013dbf79a8cabbef046609809a87a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* Hurd-aware fsck wrapper

   Copyright (C) 1996 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* This wrapper runs other file-system specific fsck programs.  They are
   expected to accept at least the following options:

     -p  Terse automatic mode
     -y  Automatically answer yes to all questions
     -n  Automatically answer no to all questions
     -f  Check even if clean
     -s  Only print diagostic messages
   
   They should also return exit-status codes as following:

     0   Filesystem was clean
     1,2 Filesystem fixed (and is now clean)
     4,8 Filesystem was broken, but couldn't be fixed
     ... Anything else is assumed be some horrible error

   The exit-status from this wrapper is the greatest status returned from any
   individual fsck.

   Although it knows something about the hurd, this fsck still uses
   /etc/fstab, and is generally not very integrated.  That will have to wait
   until the appropiate mechanisms for doing so are decided.  */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <error.h>
#include <argp.h>
#include <argz.h>
#include <assert.h>

#include "fstab.h"

char *argp_program_version = "fsck 1.0 (GNU " HURD_RELEASE ")";

/* for debugging  */
static int _debug = 0;
#define debug(fmt, args...)						      \
  do { if (_debug) {							      \
 	 fprintf (stderr, "[%s: ", __FUNCTION__);			      \
	 fprintf (stderr, fmt , ##args);				      \
	 fprintf (stderr, "]\n"); } } while (0)
#define fs_debug(fs, fmt, args...)					      \
  debug ("%s: " fmt, (fs)->mntent.mnt_dir , ##args)

#define FSCK_SEARCH_FMTS "/sbin/fsck.%s"

/* Exit codes we return.  */
#define FSCK_EX_OK       0      /* No errors */
#define FSCK_EX_FIXED    1      /* File system errors corrected */
#define FSCK_EX_BROKEN   4      /* File system errors left uncorrected */
#define FSCK_EX_QUIT	 12	/* Got SIGQUIT */
#define FSCK_EX_SIGNAL	 20	/* Signalled (not SIGQUIT) */
#define FSCK_EX_ERROR	 50
#define FSCK_EX_EXEC	 99	/* Exec failed */
/* Everything else is some sort of fsck problem.  */

/* Things we know about what child fsck's might return.  */
#define FSCK_EX_IS_FIXED(st) ({ int _st = (st); _st >= 1 || _st <= 2; })
#define FSCK_EX_IS_BROKEN(st) ({ int _st = (st); _st >= 4 || _st <= 8; })

/* Common fsck flags.  */
#define FSCK_F_PREEN	0x1
#define FSCK_F_YES	0x2
#define FSCK_F_NO	0x4
#define FSCK_F_FORCE	0x8
#define FSCK_F_SILENT	0x10

/* The following are only used internally.  */
#define FSCK_F_VERBOSE	0x100
#define FSCK_F_WRITABLE	0x200	/* Make writable after fscking.  */
#define FSCK_F_AUTO	0x400	/* Do all filesystems in fstab.  */

static int got_sigquit = 0, got_sigint = 0;

static void sigquit ()
{
  got_sigquit = 1;
}

static void sigint ()
{
  got_sigint = 1;
}

struct fsck
{
  struct fs *fs;		/* Filesystem being fscked.  */
  int pid;			/* Pid for process.  */
  int make_writable :1;		/* Make writable after fscking if possible.  */
  struct fsck *next, **self;
};

struct fscks
{
  struct fsck *running;		/* Fsck processes now running.  */
  int free_slots;		/* Number of fsck processes we can start.  */
  int flags;
};

/* Starts FS's fsck program on FS's device, returning the pid of the process.
   If an error is encountered, prints an error message and returns 0.
   Filesystems that need not be fscked at all also return 0 (but don't print
   an error message).  */
static pid_t
fs_start_fsck (struct fs *fs, int flags)
{
  pid_t pid;
  char flags_buf[10];
  char *argv[4], **argp = argv;
  struct fstype *type;
  error_t err = fs_type (fs, &type);

  assert_perror (err);		/* Should already have been checked for. */
  assert (type->program);

  *argp++ = type->program;

  if (flags & (FSCK_F_PREEN|FSCK_F_YES|FSCK_F_NO|FSCK_F_FORCE|FSCK_F_SILENT))
    {
      char *p = flags_buf;
      *argp++ = flags_buf;
      *p++ = '-';
      if (flags & FSCK_F_PREEN)  *p++ = 'p';
      if (flags & FSCK_F_YES)    *p++ = 'y';
      if (flags & FSCK_F_NO)     *p++ = 'n';
      if (flags & FSCK_F_FORCE)  *p++ = 'f';
      if (flags & FSCK_F_SILENT) *p++ = 's';
      *p = '\0';
    }

  *argp++ = fs->mntent.mnt_fsname;
  *argp = 0;

  pid = fork ();
  if (pid < 0)
    {
      error (0, errno, "fork");
      return 0;
    }

  if (pid == 0)
    /* Child.  */
    {
      execv (type->program, argv);
      exit (FSCK_EX_EXEC);	/* Exec failed. */
    }

  if ((flags & FSCK_F_VERBOSE) || _debug)
    {
      char *argz;
      size_t argz_len;
      argz_create (argv, &argz, &argz_len);
      argz_stringify (argz, argz_len, ' ');
      fs_debug (fs, "Spawned pid %d: %s", pid, argz);
      if (flags & FSCK_F_VERBOSE)
	puts (argz);
      free (argz);
    }

  return pid;
}

/* Start a fsck process for FS running, and add an entry for it to FSCKS.
   This also ensures that if FS is currently mounted, it will be made
   readonly first.  If the fsck is successfully started, 0 is returned,
   otherwise FSCK_EX_ERROR.  */
static int
fscks_start_fsck (struct fscks *fscks, struct fs *fs)
{
  error_t err;
  int mounted, make_writable;
  struct fsck *fsck;

  if (got_sigint)
    /* We got SIGINT, so we pretend that all fscks got a signal without even
       attempting to run them.  */
    {
      fs_debug (fs, "Forcing signal");
      return FSCK_EX_SIGNAL;
    }

#define CK(err, fmt, args...) \
    do { if (err) { error (0, err, fmt , ##args); return FSCK_EX_ERROR; } } while (0)

  fs_debug (fs, "Checking mounted state");
  err = fs_mounted (fs, &mounted);
  CK (err, "%s: Cannot check mounted state", fs->mntent.mnt_dir);
  
  if (mounted)
    {
      int readonly;

      fs_debug (fs, "Checking readonly state");
      err = fs_readonly (fs, &readonly);
      CK (err, "%s: Cannot check readonly state", fs->mntent.mnt_dir);

      if (! readonly)
	{
	  fs_debug (fs, "Making readonly");
	  err = fs_set_readonly (fs, 1);
	  CK (err, "%s: Cannot make readonly", fs->mntent.mnt_dir);
	}

      make_writable = !readonly
	|| ((fscks->flags & FSCK_F_WRITABLE) && hasmntopt (&fs->mntent, "rw"));
      if (make_writable)
	{
	  fs_debug (fs, "Will make writable after fscking if possible");
	  make_writable = 1;
	}
    }
  else
    make_writable = 0;

#undef CK

  /* Ok, any mounted filesystem is safely readonly.  */

  fsck = malloc (sizeof (struct fsck));
  if (! fsck)
    {
      error (0, ENOMEM, "malloc");
      return FSCK_EX_ERROR;
    }

  fsck->fs = fs;
  fsck->make_writable = make_writable;
  fsck->next = fscks->running;
  if (fsck->next)
    fsck->next->self = &fsck->next;
  fsck->self = &fscks->running;
  fsck->pid = fs_start_fsck (fs, fscks->flags);
  fscks->running = fsck;

  if (fsck->pid)
    fscks->free_slots--;

  return 0;
}

/* Cleanup after fscking with FSCK.  If REMOUNT is true, ask the filesystem
   to remount itself (to incorporate changes made by the fsck program).  If
   MAKE_WRITABLE is true, then if the filesystem should be made writable, do
   so (after remounting if applicable).  */
static void
fsck_cleanup (struct fsck *fsck, int remount, int make_writable)
{
  error_t err = 0;
  struct fs *fs = fsck->fs;

  *fsck->self = fsck->next;	/* Remove from chain.  */

  fs_debug (fs, "Cleaning up after fsck (remount = %d, make_writable = %d)",
	    remount, make_writable);

  if (fs->mounted > 0)
    /* It's currently mounted; if the fsck modified the device, tell the
       running filesystem to remount it.  Also we may make it writable.  */
    {
      if (remount)
	{
	  fs_debug (fs, "Remounting");
	  err = fs_remount (fs);
	  if (err)
	    error (0, err, "%s: Cannot remount", fs->mntent.mnt_dir);
	}
      if (!err && make_writable && fsck->make_writable)
	{
	  fs_debug (fs, "Making writable");
	  err = fs_set_readonly (fs, 0);
	  if (err)
	    error (0, err, "%s: Cannot make writable", fs->mntent.mnt_dir);
	}
    }

   free (fsck);
}

/* Wait for some fsck process to exit, cleaning up after it, and return its
   exit-status.  */
static int
fscks_wait (struct fscks *fscks)
{
  pid_t pid;
  int wstatus, status;
  struct fsck *fsck, *next;

  /* Cleanup fscks that didn't even start.  */
  for (fsck = fscks->running; fsck; fsck = next)
    {
      next = fsck->next;
      if (fsck->pid == 0)
	{
	  fs_debug (fsck->fs, "Pruning failed fsck");
	  fsck_cleanup (fsck, 0, 1);
	}
    }

  debug ("Waiting...");

  do 
    pid = wait (&wstatus);
  while (pid < 0 && errno == EINTR);

  if (pid > 0)
    {
      if (WIFEXITED (wstatus))
	status = WEXITSTATUS (wstatus);
      else if (WIFSIGNALED (wstatus))
	status = FSCK_EX_SIGNAL;
      else
	status = FSCK_EX_ERROR;

      for (fsck = fscks->running; fsck; fsck = fsck->next)
	if (fsck->pid == pid)
	  {
	    int remount = (status != 0);
	    int make_writable = (status == 0 || FSCK_EX_IS_FIXED (status));
	    fs_debug (fsck->fs, "Fsck finished (status = %d)", status);
	    fsck_cleanup (fsck, remount, make_writable);
	    fscks->free_slots++;
	    break;
	  }
      if (! fsck)
	error (0, 0, "%d: Unknown process exited", pid);
    }
  else if (errno == ECHILD)
    /* There are apparently no child processes left, and we weren't told of
       their demise.  This can't happen.  */
    {
      while (fscks->running)
	{
	  error (0, 0, "%s: Fsck process disappeared!",
		 fscks->running->fs->mntent.mnt_fsname);
	  /* Be pessimistic -- remount the filesystem, but leave it
	     readonly.  */
	  fsck_cleanup (fscks->running, 1, 0);
	  fscks->free_slots++;
	}
      status = FSCK_EX_ERROR;
    }
  else
    status = FSCK_EX_ERROR;	/* What happened?  */

  return status;
}

/* Fsck all the filesystems in FSTAB, with the flags in FLAGS, doing at most
   MAX_PARALLEL parallel fscks.  The greatest exit code returned by any one
   fsck is returned.  */
static int
fsck (struct fstab *fstab, int flags, int max_parallel)
{
  int pass;
  struct fs *fs;
  int autom = (flags & FSCK_F_AUTO);
  int summary_status = 0;
  struct fscks fscks = { running: 0, flags: flags };

  void merge_status (int status)
    {
      if (status > summary_status)
	summary_status = status;
    }

  /* Do in pass order; if in automatic mode, we skip pass 0.  */
  for (pass = autom ? 1 : 0; pass >= 0; pass = fstab_next_pass (fstab, pass))
    /* Submit all filesystems in the given pass, up to MAX_PARALLEL at a
       time.  There should currently be no fscks running.  */
    {
      debug ("Pass %d", pass);

      /* Do pass 0 filesystems one at a time.  */
      fscks.free_slots = (pass == 0 ? 1 : max_parallel);

      /* Try and fsck every filesystem in this pass.  */
      for (fs = fstab->entries; fs; fs = fs->next)
	if (fs->mntent.mnt_passno == pass
	    && !(autom && hasmntopt (&fs->mntent, "noauto")))
	  /* FS is applicable for this pass.  */
	  {
	    struct fstype *type;
	    error_t err = fs_type (fs, &type);

	    if (err)
	      {
		error (0, err, "%s: Cannot find fsck program (type %s)",
		       fs->mntent.mnt_dir, fs->mntent.mnt_type);
		merge_status (FSCK_EX_ERROR);
	      }
	    else if (type->program)
	      /* This is a fsckable filesystem.  */
	      {
		fs_debug (fs, "Fsckable; free_slots = %d", fscks.free_slots);
		while (fscks.free_slots == 0)
		  /* No room; wait for another fsck to finish.  */
		  merge_status (fscks_wait (&fscks));
		merge_status (fscks_start_fsck (&fscks, fs));
	      }
	    else
	      fs_debug (fs, "Not fsckable");
	  }

      /* Now wait for them all to finish.  */
      while (fscks.running)
	merge_status (fscks_wait (&fscks));
    }

  return summary_status;
}

static const struct argp_option
options[] =
{
  {"preen",      'p', 0,      0, "Terse automatic mode", 1},
  {"yes",        'y', 0,      0, "Automatically answer yes to all questions"},
  {"no",         'n', 0,      0, "Automatically answer no to all questions"},
  {"fstab",	 't', "FILE", 0, "File to use instead of " _PATH_MNTTAB},
  {"parallel",   'l', "NUM",  0, "Limit the number of parallel checks to NUM"},
  {"verbose",	 'v', 0,      0, "Print informational messages"},
  {"writable",   'w', 0,      0,
     "Make RW filesystems writable after fscking, if possible"},
  {"debug",	 'D', 0,      OPTION_HIDDEN },
  {"search-fmts",'S', "FMTS", 0,
     "`:' separated list of formats to use for finding fsck programs"},
  {0, 0, 0, 0, "In --preen mode, the following also apply:", 2},
  {"force",	 'f', 0,      0, "Check even if clean"},
  {"silent",     's', 0,      0, "Only print diagostic messages"},
  {"quiet",      'q', 0,      OPTION_ALIAS | OPTION_HIDDEN },
  {0, 0}
};
static const char *args_doc = "[ DEVICE|FSYS... ]";
static const char *doc = "Filesystem consistency check and repair";

int
main (int argc, char **argv)
{
  error_t err;
  struct fstab *fstab, *check;
  struct fstypes *types;
  int status;			/* exit status */
  int flags = 0;
  char *names = 0;
  size_t names_len = 0;
  char *search_fmts = FSCK_SEARCH_FMTS;
  size_t search_fmts_len = sizeof FSCK_SEARCH_FMTS;
  char *fstab_path = _PATH_MNTTAB;
  int max_parallel = -1;	/* -1 => use default */

  error_t parse_opt (int key, char *arg, struct argp_state *state)
    {
      switch (key)
	{
	case 'p': flags |= FSCK_F_PREEN; break;
	case 'y': flags |= FSCK_F_YES; break;
	case 'n': flags |= FSCK_F_NO; break;
	case 'f': flags |= FSCK_F_FORCE; break;
	case 's': flags |= FSCK_F_SILENT; break;
	case 'v': flags |= FSCK_F_VERBOSE; break;
	case 'w': flags |= FSCK_F_WRITABLE; break;
	case 't': fstab_path = arg; break; 
	case 'D': _debug = 1; break;
	case 'l':
	  max_parallel = atoi (arg);
	  if (! max_parallel)
	    argp_error (state, "%s: Invalid value for --max-parellel", arg);
	  break;
	case 'S':
	  argz_create_sep (arg, ':', &search_fmts, &search_fmts_len);
	  break;
	case ARGP_KEY_ARG:
	  err = argz_add (&names, &names_len, arg);
	  if (err)
	    argp_failure (state, 100, ENOMEM, "%s", arg);
	  break;
	default:
	  return ARGP_ERR_UNKNOWN;
	}
      return 0;
    }
  struct argp argp = {options, parse_opt, args_doc, doc};

  argp_parse (&argp, argc, argv, 0, 0, 0);

  err = fstypes_create (search_fmts, search_fmts_len, &types);
  if (err)
    error (102, err, "fstypes_create");

  err = fstab_create (types, &fstab);
  if (err)
    error (101, err, "fstab_create");

  debug ("Reading %s...", fstab_path);
  err = fstab_read (fstab, fstab_path);
  if (err)
    error (103, err, "%s", fstab_path);

  if (names)
    /* Fsck specified filesystems; also look at /var/run/mtab.  */
    {
      char *name;

      debug ("Reading %s...", _PATH_MOUNTED);
      err = fstab_read (fstab, _PATH_MOUNTED);
      if (err && err != ENOENT)
	error (104, err, "%s", _PATH_MOUNTED);

      err = fstab_create (types, &check);
      if (err)
	error (105, err, "fstab_create");

      for (name = names; name; name = argz_next (names, names_len, name))
	{
	  struct fs *fs = fstab_find (fstab, name);
	  if (! fs)
	    error (106, 0, "%s: Unknown device or filesystem", name);
	  fs_debug (fs, "Adding to checked filesystems");
	  fstab_add_fs (check, fs, 0);
	}
    }
  else
    /* Fsck everything in /etc/fstab.  */
    {
      check = fstab;
      flags |= FSCK_F_AUTO;
    }

  if (max_parallel <= 0)
    if (flags & FSCK_F_PREEN)
      max_parallel = 100;	/* In preen mode, do lots in parallel.  */
    else
      max_parallel = 1;		/* Do one at a time to keep output rational. */

  /* If the user send a SIGQUIT (usually ^\), then do all checks, but
     regardless of their outcome, return a status that will cause the
     automatic reboot to stop after fscking is complete.  */
  signal (SIGQUIT, sigquit);

  /* Let currently running fscks complete (each such program can handle
     signals as it sees fit), and cause not-yet-run fscks to act as if they
     got a signal.  */
  signal (SIGINT, sigint);

  debug ("Fscking...");
  status = fsck (check, flags, max_parallel);
  if (got_sigquit && status < FSCK_EX_QUIT)
    status = FSCK_EX_QUIT;

  exit (status);
}