summaryrefslogtreecommitdiff
path: root/ufs-fsck/pass1.c
blob: 0b9a04cf144b0be2d342dd2216f939d1bb4c8fb7 (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
/* Pass one of GNU fsck -- count blocks and verify inodes
   Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
   Written by Michael I. Bushnell.

   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. */



#include "fsck.h"

static struct dinode zino;

/* Find all the blocks in use by files and filesystem reserved blocks. 
   Set them in the global block map.  For each file, if a block is found
   allocated twice, then record the block and inode in DUPLIST.
   Initialize INODESTATE, LINKCOUNT, and TYPEMAP. */
void
pass1 ()
{
  ino_t number;
  ino_t i;
  int cg;
  struct dinode dino;
  struct dinode *dp = &dino;
  mode_t mode, type;
  int ndb;
  int holdallblocks;
  int lbn;
  int nblocks;
  int blkerror;
  int nblkrngerrors;
  int nblkduperrors;

  /* This function is called for each block of DP.  Check to see
     if the block number is valid.  If so, set the entry in the
     block map.  If the block map entry is already set, then keep
     track of this block and see if the user wants to clear the
     node.  Increment NBLOCKS by the number of data blocks held.
     Set BLKERROR if this block is invalid.
     Return RET_GOOD, RET_BAD, RET_STOP if the block is good,
     bad, or if we should entirely stop checking blocks in this 
     inode. */
  int
  checkblock (daddr_t bno, int nfrags, off_t offset)
    {
#define MAXBAD 10
      int outofrange;
      struct dups *dlp, *new;
      int wasbad = 0;

      /* Check to see if this block is in range */
      outofrange = check_range (bno, nfrags);
      if (outofrange)
	{
	  blkerror = 1;
	  wasbad = 1;
	  if (nblkrngerrors == 0)
	    warning (0, "I=%d HAS BAD BLOCKS", number);
	  if (nblkrngerrors++ > MAXBAD)
	    {
	      problem (0, "EXCESSIVE BAD BLKS I=%d", number);
	      if (preen || reply ("SKIP"))
		{
		  pfail ("SKIPPING");
		  return RET_STOP;
		}
	      else
		pfix ("CONTINUING");
	    }
	}

      for (; nfrags > 0; bno++, nfrags--)
	{
	  if (outofrange && check_range (bno, 1))
	    warning (0, "BAD BLOCK %lu", bno);
	  else
	    {
	      if (!testbmap (bno))
		setbmap (bno);
	      else
		{
		  blkerror = 1;
		  if (nblkduperrors == 0)
		    warning (0, "I=%d HAS DUPLICATE BLOCKS", number);
		  warning (0, "DUPLICATE BLOCK %ld", bno);
		  wasbad = 1;
		  if (nblkduperrors++ > MAXBAD)
		    {
		      problem (0, "EXCESSIVE DUP BLKS I=%d", number);
		      if (preen || reply ("SKIP"))
			{
			  pfail ("SKIPPING");
			  return RET_STOP;
			}
		      else
			pfix ("CONTINUING");
		    }
		  new = malloc (sizeof (struct dups));
		  new->dup = bno;
		  if (muldup == 0)
		    {
		      duplist = muldup = new;
		      new->next = 0;
		    }
		  else
		    {
		      new->next = muldup->next;
		      muldup->next = new;
		    }
		  for (dlp = duplist; dlp != muldup; dlp = dlp->next)
		    if (dlp->dup == bno)
		      break;
		  if (dlp == muldup && dlp->dup != bno)
		    muldup = new;
		}
	    }
	  nblocks += sblock->fs_fsize / DEV_BSIZE;
	}
      return wasbad ? RET_BAD : RET_GOOD;
    }
  

  /* Account for blocks used by meta data */
  for (cg = 0; cg < sblock->fs_ncg; cg++)
    {
      daddr_t firstdata, firstcgblock, bno;
      
      /* Each cylinder group past the first reserves data
	 from its cylinder group copy to (but not including)
	 the first datablock. 

	 The first, however, reserves from the very front of the
	 cylinder group (thus including the boot block), and it also
	 reserves the data blocks holding the csum information. */
      firstdata = cgdmin (sblock, cg);
      if (cg == 0)
	{
	  firstcgblock = cgbase (sblock, cg);
	  firstdata += howmany (sblock->fs_cssize, sblock->fs_fsize);
	}
      else
	firstcgblock = cgsblock (sblock, cg);

      /* Mark the blocks set */
      for (bno = firstcgblock; bno < firstdata; bno++)
	setbmap (bno);
    }
  
  /* Loop through each inode, doing initial checks */
  for (number = 0, cg = 0; cg < sblock->fs_ncg; cg++)
    for (i = 0; i < sblock->fs_ipg; i++, number++)
      {
	/* These record whether we've already complained about extra
	   direct/indirect blocks.  */
	int dbwarn = 0, ibwarn = 0;

/*	if (!preen && !(number % 10000))
	  printf ("I=%d\n", number); */

	if (number < ROOTINO)
	  continue;
	
	getinode (number, dp);
	mode = DI_MODE (dp);
	type = mode & IFMT;
	
	/* If the node is not allocated, then make sure it's
	   properly clear */
	if (type == 0)
	  {
	    if (bcmp (dp->di_db, zino.di_db, NDADDR * sizeof (daddr_t))
		|| bcmp (dp->di_ib, zino.di_ib, NIADDR * sizeof (daddr_t))
		|| dp->di_trans
		|| DI_MODE (dp)
		|| dp->di_size)
	      {
		problem (0, "PARTIALLY ALLOCATED INODE I=%d", number);
		if (preen || reply ("CLEAR"))
		  {
		    clear_inode (number, dp);
		    pfix ("CLEARED");
		  }
		else
		  pfail (0);
	      }
	    inodestate[number] = UNALLOC;
	  }
	else
	  {
	    /* Node is allocated. */
	    
	    /* Check to see if we think the node should be cleared */

	    /* Verify size for basic validity */
	    holdallblocks = 0;
	    
	    if (dp->di_size + sblock->fs_bsize - 1 < dp->di_size)
	      {
		problem (1, "OVERFLOW IN FILE SIZE I=%d (SIZE == %lld)", number, 
			dp->di_size);
		if (reply ("CLEAR"))
		  {
		    clear_inode (number, dp);
		    inodestate[number] = UNALLOC;
		    continue;
		  }
		inodestate[number] = UNALLOC;
		warning (0, "WILL TREAT ANY BLOCKS HELD BY I=%d AS ALLOCATED",
			number);
		holdallblocks = 1;
	      }

	    /* Decode type and set NDB 
	       also set inodestate correctly. */
	    inodestate[number] = REG;
	    switch (type)
	      {
	      case IFBLK:
	      case IFCHR:
		ndb = 1;
		break;
		    
	      case IFIFO:
	      case IFSOCK:
		ndb = 0;
		break;
		    
	      case IFLNK:
		if (sblock->fs_maxsymlinklen != -1)
		  {
		    /* Check to see if this is a fastlink.  The
		       old fast link format has fs_maxsymlinklen
		       of zero and di_blocks zero; the new format has
		       fs_maxsymlinklen set and we ignore di_blocks. 
		       So check for either. */
		    if ((sblock->fs_maxsymlinklen
			 && dp->di_size < sblock->fs_maxsymlinklen)
			|| (!sblock->fs_maxsymlinklen && !dp->di_blocks))
		      {
			/* Fake NDB value so that we will check
			   all the block pointers past the symlink */
			ndb = howmany (dp->di_size, sizeof (daddr_t));
			if (ndb > NDADDR)
			  {
			    int j = ndb - NDADDR;
			    for (ndb = 1; j > 1; i--)
			      ndb *= NINDIR (sblock);
			    ndb += NDADDR;
			  }
		      }
		    else
		      ndb = howmany (dp->di_size, sblock->fs_bsize);
		  }
		else
		  ndb = howmany (dp->di_size, sblock->fs_bsize);
		break;
		    
	      case IFDIR:
		inodestate[number] = DIRECTORY;
		/* Fall through */
	      case IFREG:
		ndb = howmany (dp->di_size, sblock->fs_bsize);
		break;
		
	      default:
		problem (1, "UNKNOWN FILE TYPE I=%d (MODE=%ol)", number, mode);
		if (reply ("CLEAR"))
		  {
		    clear_inode (number, dp);
		    inodestate[number] = UNALLOC;
		    continue;
		  }
		inodestate[number] = UNALLOC;
		holdallblocks = 1;
		warning (0, "WILL TREAT ANY BLOCKS HELD BY I=%d "
			"AS ALLOCATED", number);
		ndb = 0;
	      }

	    if (ndb < 0)
	      {
		problem (1, "BAD FILE SIZE I= %d (SIZE == %lld)", number,
			dp->di_size);
		if (reply ("CLEAR"))
		  {
		    clear_inode (number, dp);
		    inodestate[number] = UNALLOC;
		    continue;
		  }
		inodestate[number] = UNALLOC;
		warning (0, "WILL TREAT ANY BLOCKS HELD BY I=%d AS ALLOCATED", 
			 number);
		holdallblocks = 1;
	      }

	    /* Make sure that direct and indirect block pointers
	       past the file size are zero.  If the size is bogus, then
	       don't bother (they should all be zero, but the user has
	       requested that they be treated as allocated). */
	    if (!holdallblocks)
	      {
		if (dp->di_size
		    && (type == IFBLK || type == IFCHR 
			|| type == IFSOCK || type == IFIFO))
		  {
		    problem (1, "SPECIAL NODE I=%d (MODE=%ol) HAS SIZE %lld",
			    number, mode, dp->di_size);
		    if (reply ("TRUNCATE"))
		      {
			dp->di_size = 0;
			write_inode (number, dp);
		      }
		  }
		
		/* If we haven't set NDB speciall above, then it is set from
		   the file size correctly by the size check. */
		
		/* Check all the direct and indirect blocks that are past the
		   amount necessary to be zero. */
		for (lbn = ndb; lbn < NDADDR; lbn++)
		  {
		    if (dp->di_db[lbn])
		      {
			if (!dbwarn)
			  {
			    dbwarn = 1;
			    problem (0, "INODE I=%d HAS EXTRA DIRECT BLOCKS", 
				   number);
			    if (preen || reply ("DEALLOCATE"))
			      {
				dp->di_db[lbn] = 0;
				dbwarn = 2;
				pfix ("DEALLOCATED");
			      }
			    else
			      pfail (0);
			  }
			else if (dbwarn == 2)
			  dp->di_db[lbn] = 0;
		      }
		    if (dbwarn == 2)
		      write_inode (number, dp);
		  }

		for (lbn = 0, ndb -= NDADDR; ndb > 0; lbn++)
		  ndb /= NINDIR (sblock);
		for (; lbn < NIADDR; lbn++)
		  {
		    if (dp->di_ib[lbn])
		      {
			if (ibwarn)
			  {
			    ibwarn = 1;
			    problem (0, "INODE I=%d HAS EXTRA INDIRECT BLOCKS",
				   number);
			    if (preen || reply ("DEALLOCATE"))
			      {
				dp->di_ib[lbn] = 0;
				ibwarn = 2;
				pfix ("DEALLOCATED");
			      }
			    else
			      pfail (0);
			  }
			else if (ibwarn == 2)
			  dp->di_ib[lbn] = 0;
		      }
		    if (ibwarn == 2)
		      write_inode (number, dp);
		  }
	      }
	    
	    /* If this node is really allocated (as opposed to something
	       that we should clear but the user won't) then set LINKCOUNT
	       and TYPEMAP entries. */
	    if (inodestate[number] != UNALLOC)
	      {
		linkcount[number] = dp->di_nlink;
		typemap[number] = IFTODT (mode);
	      }

	    /* Iterate over the blocks of the file,
	       calling CHECKBLOCK for each file. */
	    nblocks = 0;
	    blkerror = 0;
	    nblkduperrors = 0;
	    nblkrngerrors = 0;
	    allblock_iterate (dp, checkblock);

	    if (blkerror)
	      {
		if (preen)
		  warning (1, "DUPLICATE or BAD BLOCKS");
		else
		  {
		    problem (0, "I=%d has ", number);
		    if (nblkduperrors)
		      {
			pextend ("%d DUPLICATE BLOCKS", nblkduperrors);
			if (nblkrngerrors)
			  pextend (" and ");
		      }
		    if (nblkrngerrors)
		      pextend ("%d BAD BLOCKS", nblkrngerrors);
		    if (reply ("CLEAR"))
		      {
			clear_inode (number, dp);
			inodestate[number] = UNALLOC;
			continue;
		      }
		    else if (inodestate[number] == DIRECTORY)
		      inodestate[number] = BADDIR;
		  }
	      }
	    else if (dp->di_blocks != nblocks)
	      {
		problem (0, "INCORRECT BLOCK COUNT I=%d (%ld should be %d)",
		       number, dp->di_blocks, nblocks);
		if (preen || reply ("CORRECT"))
		  {
		    dp->di_blocks = nblocks;
		    write_inode (number, dp);
		    pfix ("CORRECTED");
		  }
		else
		  pfail (0);
	      }

	    num_files++;

	    if (type == IFDIR)
	      record_directory (dp, number);
	  }
      }
}