1 /*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)utilities.c 8.6 (Berkeley) 5/19/95
30 * $FreeBSD: src/sbin/fsck/utilities.c,v 1.11.2.3 2001/01/23 23:11:07 iedowse Exp $
31 */
32
33 #include <sys/param.h>
34
35 #include <vfs/ufs/dinode.h>
36 #include <vfs/ufs/dir.h>
37 #include <vfs/ufs/fs.h>
38
39 #include <err.h>
40 #include <string.h>
41
42 #include "fsck.h"
43
44 long diskreads, totalreads; /* Disk cache statistics */
45 struct bufarea bufhead; /* head of list of other blks in filesys */
46 char *cdevname; /* name of device being checked */
47 char yflag; /* assume a yes response */
48 char nflag; /* assume a no response */
49 int debug; /* output debugging info */
50 char resolved; /* cleared if unresolved changes => not clean */
51 int fsmodified; /* 1 => write done to file system */
52 int got_siginfo; /* received a SIGINFO */
53
54 static void rwerror(char *mesg, ufs_daddr_t blk);
55
56 int
ftypeok(struct ufs1_dinode * dp)57 ftypeok(struct ufs1_dinode *dp)
58 {
59 switch (dp->di_mode & IFMT) {
60
61 case IFDIR:
62 case IFREG:
63 case IFBLK:
64 case IFCHR:
65 case IFLNK:
66 case IFSOCK:
67 case IFIFO:
68 return (1);
69
70 default:
71 if (debug)
72 printf("bad file type 0%o\n", dp->di_mode);
73 return (0);
74 }
75 }
76
77 int
reply(char * question)78 reply(char *question)
79 {
80 int persevere;
81 char c;
82
83 if (preen)
84 pfatal("INTERNAL ERROR: GOT TO reply()");
85 persevere = !strcmp(question, "CONTINUE");
86 printf("\n");
87 if (!persevere && (nflag || fswritefd < 0)) {
88 printf("%s? no\n\n", question);
89 resolved = 0;
90 return (0);
91 }
92 if (yflag || (persevere && nflag)) {
93 printf("%s? yes\n\n", question);
94 return (1);
95 }
96 do {
97 printf("%s? [yn] ", question);
98 fflush(stdout);
99 c = getc(stdin);
100 while (c != '\n' && getc(stdin) != '\n') {
101 if (feof(stdin)) {
102 resolved = 0;
103 return (0);
104 }
105 }
106 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
107 printf("\n");
108 if (c == 'y' || c == 'Y')
109 return (1);
110 resolved = 0;
111 return (0);
112 }
113
114 /*
115 * Look up state information for an inode.
116 */
117 struct inostat *
inoinfo(ufs1_ino_t inum)118 inoinfo(ufs1_ino_t inum)
119 {
120 static struct inostat unallocated = { USTATE, 0, 0 };
121 struct inostatlist *ilp;
122 int iloff;
123
124 if (inum > maxino)
125 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
126 ilp = &inostathead[inum / sblock.fs_ipg];
127 iloff = inum % sblock.fs_ipg;
128 if (iloff >= ilp->il_numalloced)
129 return (&unallocated);
130 return (&ilp->il_stat[iloff]);
131 }
132
133 /*
134 * Malloc buffers and set up cache.
135 */
136 void
bufinit(void)137 bufinit(void)
138 {
139 struct bufarea *bp;
140 long bufcnt, i;
141 char *bufp;
142
143 pbp = pdirbp = NULL;
144 bufp = malloc((unsigned int)sblock.fs_bsize);
145 if (bufp == NULL)
146 errx(EEXIT, "cannot allocate buffer pool");
147 cgblk.b_un.b_buf = bufp;
148 initbarea(&cgblk);
149 bufhead.b_next = bufhead.b_prev = &bufhead;
150 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
151 if (bufcnt < MINBUFS)
152 bufcnt = MINBUFS;
153 for (i = 0; i < bufcnt; i++) {
154 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
155 bufp = malloc((unsigned int)sblock.fs_bsize);
156 if (bp == NULL || bufp == NULL) {
157 if (i >= MINBUFS)
158 break;
159 errx(EEXIT, "cannot allocate buffer pool");
160 }
161 bp->b_un.b_buf = bufp;
162 bp->b_prev = &bufhead;
163 bp->b_next = bufhead.b_next;
164 bufhead.b_next->b_prev = bp;
165 bufhead.b_next = bp;
166 initbarea(bp);
167 }
168 bufhead.b_size = i; /* save number of buffers */
169 }
170
171 /*
172 * Manage a cache of directory blocks.
173 */
174 struct bufarea *
getdatablk(ufs_daddr_t blkno,long size)175 getdatablk(ufs_daddr_t blkno, long size)
176 {
177 struct bufarea *bp;
178
179 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
180 if (bp->b_bno == fsbtodb(&sblock, blkno))
181 goto foundit;
182 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
183 if ((bp->b_flags & B_INUSE) == 0)
184 break;
185 if (bp == &bufhead)
186 errx(EEXIT, "deadlocked buffer pool");
187 getblk(bp, blkno, size);
188 /* fall through */
189 foundit:
190 totalreads++;
191 bp->b_prev->b_next = bp->b_next;
192 bp->b_next->b_prev = bp->b_prev;
193 bp->b_prev = &bufhead;
194 bp->b_next = bufhead.b_next;
195 bufhead.b_next->b_prev = bp;
196 bufhead.b_next = bp;
197 bp->b_flags |= B_INUSE;
198 return (bp);
199 }
200
201 void
getblk(struct bufarea * bp,ufs_daddr_t blk,long size)202 getblk(struct bufarea *bp, ufs_daddr_t blk, long size)
203 {
204 ufs_daddr_t dblk;
205
206 dblk = fsbtodb(&sblock, blk);
207 if (bp->b_bno != dblk) {
208 flush(fswritefd, bp);
209 diskreads++;
210 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
211 bp->b_bno = dblk;
212 bp->b_size = size;
213 }
214 }
215
216 void
flush(int fd,struct bufarea * bp)217 flush(int fd, struct bufarea *bp)
218 {
219 int i, j;
220
221 if (!bp->b_dirty)
222 return;
223 if (bp->b_errs != 0)
224 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
225 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
226 bp->b_bno);
227 bp->b_dirty = 0;
228 bp->b_errs = 0;
229 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
230 if (bp != &sblk)
231 return;
232 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
233 bwrite(fswritefd, (char *)sblock.fs_csp + i,
234 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
235 sblock.fs_cssize - i < sblock.fs_bsize ?
236 sblock.fs_cssize - i : sblock.fs_bsize);
237 }
238 }
239
240 static void
rwerror(char * mesg,ufs_daddr_t blk)241 rwerror(char *mesg, ufs_daddr_t blk)
242 {
243
244 if (preen == 0)
245 printf("\n");
246 pfatal("CANNOT %s: BLK %d", mesg, blk);
247 if (reply("CONTINUE") == 0)
248 exit(EEXIT);
249 }
250
251 void
ckfini(int markclean)252 ckfini(int markclean)
253 {
254 struct bufarea *bp, *nbp;
255 int ofsmodified, cnt = 0;
256
257 if (fswritefd < 0) {
258 close(fsreadfd);
259 return;
260 }
261 flush(fswritefd, &sblk);
262 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
263 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
264 sblk.b_bno = SBOFF / dev_bsize;
265 sbdirty();
266 flush(fswritefd, &sblk);
267 }
268 flush(fswritefd, &cgblk);
269 free(cgblk.b_un.b_buf);
270 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
271 cnt++;
272 flush(fswritefd, bp);
273 nbp = bp->b_prev;
274 free(bp->b_un.b_buf);
275 free((char *)bp);
276 }
277 if (bufhead.b_size != cnt)
278 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
279 pbp = pdirbp = NULL;
280 if (sblock.fs_clean != markclean) {
281 sblock.fs_clean = markclean;
282 sbdirty();
283 ofsmodified = fsmodified;
284 flush(fswritefd, &sblk);
285 fsmodified = ofsmodified;
286 if (!preen) {
287 printf("\n***** FILE SYSTEM MARKED %s *****\n",
288 markclean ? "CLEAN" : "DIRTY");
289 if (!markclean)
290 rerun = 1;
291 }
292 } else if (!preen && !markclean) {
293 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
294 rerun = 1;
295 }
296 if (debug)
297 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
298 totalreads, (int)(diskreads * 100 / totalreads));
299 close(fsreadfd);
300 close(fswritefd);
301 }
302
303 int
bread(int fd,char * buf,ufs_daddr_t blk,long size)304 bread(int fd, char *buf, ufs_daddr_t blk, long size)
305 {
306 char *cp;
307 int i, errs;
308 off_t offset;
309
310 offset = blk;
311 offset *= dev_bsize;
312 if (lseek(fd, offset, 0) < 0)
313 rwerror("SEEK", blk);
314 else if (read(fd, buf, (int)size) == size)
315 return (0);
316 rwerror("READ", blk);
317 if (lseek(fd, offset, 0) < 0)
318 rwerror("SEEK", blk);
319 errs = 0;
320 memset(buf, 0, (size_t)size);
321 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
322 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
323 if (read(fd, cp, (int)secsize) != secsize) {
324 lseek(fd, offset + i + secsize, 0);
325 if (secsize != dev_bsize && dev_bsize != 1)
326 printf(" %ld (%ld),",
327 (blk * dev_bsize + i) / secsize,
328 blk + i / dev_bsize);
329 else
330 printf(" %ld,", blk + i / dev_bsize);
331 errs++;
332 }
333 }
334 printf("\n");
335 if (errs)
336 resolved = 0;
337 return (errs);
338 }
339
340 void
bwrite(int fd,char * buf,ufs_daddr_t blk,long size)341 bwrite(int fd, char *buf, ufs_daddr_t blk, long size)
342 {
343 int i;
344 char *cp;
345 off_t offset;
346
347 if (fd < 0)
348 return;
349 offset = blk;
350 offset *= dev_bsize;
351 if (lseek(fd, offset, 0) < 0)
352 rwerror("SEEK", blk);
353 else if (write(fd, buf, (int)size) == size) {
354 fsmodified = 1;
355 return;
356 }
357 resolved = 0;
358 rwerror("WRITE", blk);
359 if (lseek(fd, offset, 0) < 0)
360 rwerror("SEEK", blk);
361 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
362 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
363 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
364 lseek(fd, offset + i + dev_bsize, 0);
365 printf(" %ld,", blk + i / dev_bsize);
366 }
367 printf("\n");
368 return;
369 }
370
371 /*
372 * allocate a data block with the specified number of fragments
373 */
374 ufs_daddr_t
allocblk(long frags)375 allocblk(long frags)
376 {
377 int i, j, k, cg, baseblk;
378 struct cg *cgp = &cgrp;
379
380 if (frags <= 0 || frags > sblock.fs_frag)
381 return (0);
382 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
383 for (j = 0; j <= sblock.fs_frag - frags; j++) {
384 if (testbmap(i + j))
385 continue;
386 for (k = 1; k < frags; k++)
387 if (testbmap(i + j + k))
388 break;
389 if (k < frags) {
390 j += k;
391 continue;
392 }
393 cg = dtog(&sblock, i + j);
394 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
395 if (!cg_chkmagic(cgp))
396 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
397 baseblk = dtogd(&sblock, i + j);
398 for (k = 0; k < frags; k++) {
399 setbmap(i + j + k);
400 clrbit(cg_blksfree(cgp), baseblk + k);
401 }
402 n_blks += frags;
403 if (frags == sblock.fs_frag)
404 cgp->cg_cs.cs_nbfree--;
405 else
406 cgp->cg_cs.cs_nffree -= frags;
407 cgdirty();
408 return (i + j);
409 }
410 }
411 return (0);
412 }
413
414 /*
415 * Free a previously allocated block
416 */
417 void
freeblk(ufs_daddr_t blkno,long frags)418 freeblk(ufs_daddr_t blkno, long frags)
419 {
420 struct inodesc idesc;
421
422 idesc.id_blkno = blkno;
423 idesc.id_numfrags = frags;
424 pass4check(&idesc);
425 }
426
427 /*
428 * Find a pathname
429 */
430 void
getpathname(char * namebuf,ufs1_ino_t curdir,ufs1_ino_t ino)431 getpathname(char *namebuf, ufs1_ino_t curdir, ufs1_ino_t ino)
432 {
433 int len;
434 char *cp;
435 struct inodesc idesc;
436 static int busy = 0;
437
438 if (curdir == ino && ino == UFS_ROOTINO) {
439 strcpy(namebuf, "/");
440 return;
441 }
442 if (busy ||
443 (inoinfo(curdir)->ino_state != DSTATE &&
444 inoinfo(curdir)->ino_state != DFOUND)) {
445 strcpy(namebuf, "?");
446 return;
447 }
448 busy = 1;
449 memset(&idesc, 0, sizeof(struct inodesc));
450 idesc.id_type = DATA;
451 idesc.id_fix = IGNORE;
452 cp = &namebuf[MAXPATHLEN - 1];
453 *cp = '\0';
454 if (curdir != ino) {
455 idesc.id_parent = curdir;
456 goto namelookup;
457 }
458 while (ino != UFS_ROOTINO) {
459 idesc.id_number = ino;
460 idesc.id_func = findino;
461 idesc.id_name = "..";
462 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
463 break;
464 namelookup:
465 idesc.id_number = idesc.id_parent;
466 idesc.id_parent = ino;
467 idesc.id_func = findname;
468 idesc.id_name = namebuf;
469 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
470 break;
471 len = strlen(namebuf);
472 cp -= len;
473 memmove(cp, namebuf, (size_t)len);
474 *--cp = '/';
475 if (cp < &namebuf[MAXNAMLEN])
476 break;
477 ino = idesc.id_number;
478 }
479 busy = 0;
480 if (ino != UFS_ROOTINO)
481 *--cp = '?';
482 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
483 }
484
485 /* ARGSUSED */
486 void
catch(int sig)487 catch(int sig)
488 {
489 if (!doinglevel2)
490 ckfini(0);
491 exit(12);
492 }
493
494 /*
495 * When preening, allow a single quit to signal
496 * a special exit after filesystem checks complete
497 * so that reboot sequence may be interrupted.
498 */
499 /* ARGSUSED */
500 void
catchquit(int sig)501 catchquit(int sig)
502 {
503 printf("returning to single-user after filesystem check\n");
504 returntosingle = 1;
505 signal(SIGQUIT, SIG_DFL);
506 }
507
508 /*
509 * Ignore a single quit signal; wait and flush just in case.
510 * Used by child processes in preen.
511 */
512 /* ARGSUSED */
513 void
voidquit(int sig)514 voidquit(int sig)
515 {
516
517 sleep(1);
518 signal(SIGQUIT, SIG_IGN);
519 signal(SIGQUIT, SIG_DFL);
520 }
521
522 /* ARGSUSED */
523 void
infohandler(int sig)524 infohandler(int sig)
525 {
526 got_siginfo = 1;
527 }
528
529 /*
530 * determine whether an inode should be fixed.
531 */
532 int
dofix(struct inodesc * idesc,char * msg)533 dofix(struct inodesc *idesc, char *msg)
534 {
535
536 switch (idesc->id_fix) {
537
538 case DONTKNOW:
539 if (idesc->id_type == DATA)
540 direrror(idesc->id_number, msg);
541 else
542 pwarn("%s", msg);
543 if (preen) {
544 printf(" (SALVAGED)\n");
545 idesc->id_fix = FIX;
546 return (ALTERED);
547 }
548 if (reply("SALVAGE") == 0) {
549 idesc->id_fix = NOFIX;
550 return (0);
551 }
552 idesc->id_fix = FIX;
553 return (ALTERED);
554
555 case FIX:
556 return (ALTERED);
557
558 case NOFIX:
559 case IGNORE:
560 return (0);
561
562 default:
563 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
564 }
565 /* NOTREACHED */
566 return (0);
567 }
568
569 #include <stdarg.h>
570
571 /*
572 * An unexpected inconsistency occured.
573 * Die if preening or filesystem is running with soft dependency protocol,
574 * otherwise just print message and continue.
575 */
576 void
pfatal(const char * fmt,...)577 pfatal(const char *fmt, ...)
578 {
579 va_list ap;
580 va_start(ap, fmt);
581 if (!preen) {
582 vfprintf(stderr, fmt, ap);
583 va_end(ap);
584 if (usedsoftdep)
585 fprintf(stderr,
586 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
587 return;
588 }
589 if (cdevname == NULL)
590 cdevname = "fsck";
591 fprintf(stderr, "%s: ", cdevname);
592 vfprintf(stderr, fmt, ap);
593 fprintf(stderr,
594 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
595 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
596 ckfini(0);
597 exit(EEXIT);
598 }
599
600 /*
601 * Pwarn just prints a message when not preening or running soft dependency
602 * protocol, or a warning (preceded by filename) when preening.
603 */
604 void
pwarn(const char * fmt,...)605 pwarn(const char *fmt, ...)
606 {
607 va_list ap;
608 va_start(ap, fmt);
609 if (preen)
610 fprintf(stderr, "%s: ", cdevname);
611 vfprintf(stderr, fmt, ap);
612 va_end(ap);
613 }
614
615 /*
616 * Stub for routines from kernel.
617 */
618 void
panic(const char * fmt,...)619 panic(const char *fmt, ...)
620 {
621 va_list ap;
622
623 va_start(ap, fmt);
624 pfatal("INTERNAL INCONSISTENCY:");
625 vfprintf(stderr, fmt, ap);
626 va_end(ap);
627 exit(EEXIT);
628 }
629