utilities.c revision 1.7 1 /* $NetBSD: utilities.c,v 1.7 2003/01/24 21:55:07 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1997 Manuel Bouyer.
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)utilities.c 8.1 (Berkeley) 6/5/93";
41 #else
42 __RCSID("$NetBSD: utilities.c,v 1.7 2003/01/24 21:55:07 fvdl Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <ufs/ext2fs/ext2fs_dinode.h>
49 #include <ufs/ext2fs/ext2fs_dir.h>
50 #include <ufs/ext2fs/ext2fs.h>
51 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <unistd.h>
57
58 #include "fsutil.h"
59 #include "fsck.h"
60 #include "extern.h"
61
62 long diskreads, totalreads; /* Disk cache statistics */
63
64 static void rwerror __P((char *, daddr_t));
65
66 extern int returntosingle;
67
68 int
69 ftypeok(dp)
70 struct ext2fs_dinode *dp;
71 {
72 switch (fs2h16(dp->e2di_mode) & IFMT) {
73
74 case IFDIR:
75 case IFREG:
76 case IFBLK:
77 case IFCHR:
78 case IFLNK:
79 case IFSOCK:
80 case IFIFO:
81 return (1);
82
83 default:
84 if (debug)
85 printf("bad file type 0%o\n", fs2h16(dp->e2di_mode));
86 return (0);
87 }
88 }
89
90 int
91 reply(question)
92 char *question;
93 {
94 int persevere;
95 char c;
96
97 if (preen)
98 pfatal("INTERNAL ERROR: GOT TO reply()");
99 persevere = !strcmp(question, "CONTINUE");
100 printf("\n");
101 if (!persevere && (nflag || fswritefd < 0)) {
102 printf("%s? no\n\n", question);
103 return (0);
104 }
105 if (yflag || (persevere && nflag)) {
106 printf("%s? yes\n\n", question);
107 return (1);
108 }
109 do {
110 printf("%s? [yn] ", question);
111 (void) fflush(stdout);
112 c = getc(stdin);
113 while (c != '\n' && getc(stdin) != '\n')
114 if (feof(stdin))
115 return (0);
116 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
117 printf("\n");
118 if (c == 'y' || c == 'Y')
119 return (1);
120 return (0);
121 }
122
123 /*
124 * Malloc buffers and set up cache.
125 */
126 void
127 bufinit()
128 {
129 struct bufarea *bp;
130 long bufcnt, i;
131 char *bufp;
132
133 diskreads = totalreads = 0;
134 pbp = pdirbp = (struct bufarea *)0;
135 bufhead.b_next = bufhead.b_prev = &bufhead;
136 bufcnt = MAXBUFSPACE / sblock.e2fs_bsize;
137 if (bufcnt < MINBUFS)
138 bufcnt = MINBUFS;
139 for (i = 0; i < bufcnt; i++) {
140 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
141 bufp = malloc((unsigned int)sblock.e2fs_bsize);
142 if (bp == NULL || bufp == NULL) {
143 if (i >= MINBUFS)
144 break;
145 errexit("cannot allocate buffer pool\n");
146 }
147 bp->b_un.b_buf = bufp;
148 bp->b_prev = &bufhead;
149 bp->b_next = bufhead.b_next;
150 bufhead.b_next->b_prev = bp;
151 bufhead.b_next = bp;
152 initbarea(bp);
153 }
154 bufhead.b_size = i; /* save number of buffers */
155 }
156
157 /*
158 * Manage a cache of directory blocks.
159 */
160 struct bufarea *
161 getdatablk(blkno, size)
162 daddr_t blkno;
163 long size;
164 {
165 struct bufarea *bp;
166
167 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
168 if (bp->b_bno == fsbtodb(&sblock, blkno))
169 goto foundit;
170 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
171 if ((bp->b_flags & B_INUSE) == 0)
172 break;
173 if (bp == &bufhead)
174 errexit("deadlocked buffer pool\n");
175 getblk(bp, blkno, size);
176 diskreads++;
177 /* fall through */
178 foundit:
179 totalreads++;
180 bp->b_prev->b_next = bp->b_next;
181 bp->b_next->b_prev = bp->b_prev;
182 bp->b_prev = &bufhead;
183 bp->b_next = bufhead.b_next;
184 bufhead.b_next->b_prev = bp;
185 bufhead.b_next = bp;
186 bp->b_flags |= B_INUSE;
187 return (bp);
188 }
189
190 void
191 getblk(bp, blk, size)
192 struct bufarea *bp;
193 daddr_t blk;
194 long size;
195 {
196 daddr_t dblk;
197
198 dblk = fsbtodb(&sblock, blk);
199 if (bp->b_bno != dblk) {
200 flush(fswritefd, bp);
201 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
202 bp->b_bno = dblk;
203 bp->b_size = size;
204 }
205 }
206
207 void
208 flush(fd, bp)
209 int fd;
210 struct bufarea *bp;
211 {
212 int i;
213
214 if (!bp->b_dirty)
215 return;
216 if (bp->b_errs != 0)
217 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
218 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
219 (long long)bp->b_bno);
220 bp->b_dirty = 0;
221 bp->b_errs = 0;
222 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
223 if (bp != &sblk)
224 return;
225 for (i = 0; i < sblock.e2fs_ngdb; i++) {
226 bwrite(fswritefd, (char *)
227 &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
228 fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
229 sblock.e2fs_bsize);
230 }
231 }
232
233 static void
234 rwerror(mesg, blk)
235 char *mesg;
236 daddr_t blk;
237 {
238
239 if (preen == 0)
240 printf("\n");
241 pfatal("CANNOT %s: BLK %lld", mesg, (long long)blk);
242 if (reply("CONTINUE") == 0)
243 errexit("Program terminated\n");
244 }
245
246 void
247 ckfini(markclean)
248 int markclean;
249 {
250 struct bufarea *bp, *nbp;
251 int cnt = 0;
252
253 if (fswritefd < 0) {
254 (void)close(fsreadfd);
255 return;
256 }
257 flush(fswritefd, &sblk);
258 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
259 !preen && reply("UPDATE STANDARD SUPERBLOCKS")) {
260 sblk.b_bno = SBOFF / dev_bsize;
261 sbdirty();
262 flush(fswritefd, &sblk);
263 copyback_sb(&asblk);
264 asblk.b_dirty = 1;
265 flush(fswritefd, &asblk);
266 }
267 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
268 cnt++;
269 flush(fswritefd, bp);
270 nbp = bp->b_prev;
271 free(bp->b_un.b_buf);
272 free((char *)bp);
273 }
274 if (bufhead.b_size != cnt)
275 errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
276 pbp = pdirbp = (struct bufarea *)0;
277 if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) {
278 /*
279 * Mark the file system as clean, and sync the superblock.
280 */
281 if (preen)
282 pwarn("MARKING FILE SYSTEM CLEAN\n");
283 else if (!reply("MARK FILE SYSTEM CLEAN"))
284 markclean = 0;
285 if (markclean) {
286 sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
287 sbdirty();
288 flush(fswritefd, &sblk);
289 }
290 }
291 if (debug)
292 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
293 totalreads, (int)(diskreads * 100 / totalreads));
294 (void)close(fsreadfd);
295 (void)close(fswritefd);
296 }
297
298 int
299 bread(fd, buf, blk, size)
300 int fd;
301 char *buf;
302 daddr_t blk;
303 long size;
304 {
305 char *cp;
306 int i, errs;
307 off_t offset;
308
309 offset = blk;
310 offset *= dev_bsize;
311 if (lseek(fd, offset, 0) < 0)
312 rwerror("SEEK", blk);
313 else if (read(fd, buf, (int)size) == size)
314 return (0);
315 rwerror("READ", blk);
316 if (lseek(fd, offset, 0) < 0)
317 rwerror("SEEK", blk);
318 errs = 0;
319 memset(buf, 0, (size_t)size);
320 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
321 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
322 if (read(fd, cp, (int)secsize) != secsize) {
323 (void)lseek(fd, offset + i + secsize, 0);
324 if (secsize != dev_bsize && dev_bsize != 1)
325 printf(" %lld (%lld),",
326 (long long)((blk*dev_bsize + i) / secsize),
327 (long long)(blk + i / dev_bsize));
328 else
329 printf(" %lld,", (long long)(blk +
330 i / dev_bsize));
331 errs++;
332 }
333 }
334 printf("\n");
335 return (errs);
336 }
337
338 void
339 bwrite(fd, buf, blk, size)
340 int fd;
341 char *buf;
342 daddr_t blk;
343 long size;
344 {
345 int i;
346 char *cp;
347 off_t offset;
348
349 if (fd < 0)
350 return;
351 offset = blk;
352 offset *= dev_bsize;
353 if (lseek(fd, offset, 0) < 0)
354 rwerror("SEEK", blk);
355 else if (write(fd, buf, (int)size) == size) {
356 fsmodified = 1;
357 return;
358 }
359 rwerror("WRITE", blk);
360 if (lseek(fd, offset, 0) < 0)
361 rwerror("SEEK", blk);
362 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
363 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
364 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
365 (void)lseek(fd, offset + i + dev_bsize, 0);
366 printf(" %lld,", (long long)(blk + i / dev_bsize));
367 }
368 printf("\n");
369 return;
370 }
371
372 /*
373 * allocate a data block
374 */
375 int
376 allocblk()
377 {
378 int i;
379
380 for (i = 0; i < maxfsblock - 1; i++) {
381 if (testbmap(i))
382 continue;
383 setbmap(i);
384 n_blks ++;
385 return (i);
386 }
387 return (0);
388 }
389
390 /*
391 * Free a previously allocated block
392 */
393 void
394 freeblk(blkno)
395 daddr_t blkno;
396 {
397 struct inodesc idesc;
398
399 idesc.id_blkno = blkno;
400 idesc.id_numfrags = 1;
401 (void)pass4check(&idesc);
402 }
403
404 /*
405 * Find a pathname
406 */
407 void
408 getpathname(namebuf, curdir, ino)
409 char *namebuf;
410 ino_t curdir, ino;
411 {
412 int len;
413 char *cp;
414 struct inodesc idesc;
415 static int busy = 0;
416
417 if (curdir == ino && ino == EXT2_ROOTINO) {
418 (void)strcpy(namebuf, "/");
419 return;
420 }
421 if (busy ||
422 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
423 (void)strcpy(namebuf, "?");
424 return;
425 }
426 busy = 1;
427 memset(&idesc, 0, sizeof(struct inodesc));
428 idesc.id_type = DATA;
429 idesc.id_fix = IGNORE;
430 cp = &namebuf[MAXPATHLEN - 1];
431 *cp = '\0';
432 if (curdir != ino) {
433 idesc.id_parent = curdir;
434 goto namelookup;
435 }
436 while (ino != EXT2_ROOTINO) {
437 idesc.id_number = ino;
438 idesc.id_func = findino;
439 idesc.id_name = "..";
440 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
441 break;
442 namelookup:
443 idesc.id_number = idesc.id_parent;
444 idesc.id_parent = ino;
445 idesc.id_func = findname;
446 idesc.id_name = namebuf;
447 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
448 break;
449 len = strlen(namebuf);
450 cp -= len;
451 memcpy(cp, namebuf, (size_t)len);
452 *--cp = '/';
453 if (cp < &namebuf[EXT2FS_MAXNAMLEN])
454 break;
455 ino = idesc.id_number;
456 }
457 busy = 0;
458 if (ino != EXT2_ROOTINO)
459 *--cp = '?';
460 memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
461 }
462
463 void
464 catch(n)
465 int n;
466 {
467 ckfini(0);
468 exit(12);
469 }
470
471 /*
472 * When preening, allow a single quit to signal
473 * a special exit after filesystem checks complete
474 * so that reboot sequence may be interrupted.
475 */
476 void
477 catchquit(n)
478 int n;
479 {
480 printf("returning to single-user after filesystem check\n");
481 returntosingle = 1;
482 (void)signal(SIGQUIT, SIG_DFL);
483 }
484
485 /*
486 * Ignore a single quit signal; wait and flush just in case.
487 * Used by child processes in preen.
488 */
489 void
490 voidquit(n)
491 int n;
492 {
493
494 sleep(1);
495 (void)signal(SIGQUIT, SIG_IGN);
496 (void)signal(SIGQUIT, SIG_DFL);
497 }
498
499 /*
500 * determine whether an inode should be fixed.
501 */
502 int
503 dofix(idesc, msg)
504 struct inodesc *idesc;
505 char *msg;
506 {
507
508 switch (idesc->id_fix) {
509
510 case DONTKNOW:
511 if (idesc->id_type == DATA)
512 direrror(idesc->id_number, msg);
513 else
514 pwarn("%s", msg);
515 if (preen) {
516 printf(" (SALVAGED)\n");
517 idesc->id_fix = FIX;
518 return (ALTERED);
519 }
520 if (reply("SALVAGE") == 0) {
521 idesc->id_fix = NOFIX;
522 return (0);
523 }
524 idesc->id_fix = FIX;
525 return (ALTERED);
526
527 case FIX:
528 return (ALTERED);
529
530 case NOFIX:
531 case IGNORE:
532 return (0);
533
534 default:
535 errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
536 }
537 /* NOTREACHED */
538 }
539