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