utilities.c revision 1.21 1 /* $NetBSD: utilities.c,v 1.21 1997/09/20 06:16:34 lukem 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/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
40 #else
41 __RCSID("$NetBSD: utilities.c,v 1.21 1997/09/20 06:16:34 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ffs/fs.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "fsutil.h"
60 #include "fsck.h"
61 #include "extern.h"
62
63 long diskreads, totalreads; /* Disk cache statistics */
64
65 static void rwerror __P((char *, ufs_daddr_t));
66
67 int
68 ftypeok(dp)
69 struct dinode *dp;
70 {
71 switch (dp->di_mode & IFMT) {
72
73 case IFDIR:
74 case IFREG:
75 case IFBLK:
76 case IFCHR:
77 case IFLNK:
78 case IFSOCK:
79 case IFIFO:
80 return (1);
81
82 default:
83 if (debug)
84 printf("bad file type 0%o\n", dp->di_mode);
85 return (0);
86 }
87 }
88
89 int
90 reply(question)
91 char *question;
92 {
93 int persevere;
94 char c;
95
96 if (preen)
97 pfatal("INTERNAL ERROR: GOT TO reply()");
98 persevere = !strcmp(question, "CONTINUE");
99 printf("\n");
100 if (!persevere && (nflag || fswritefd < 0)) {
101 printf("%s? no\n\n", question);
102 return (0);
103 }
104 if (yflag || (persevere && nflag)) {
105 printf("%s? yes\n\n", question);
106 return (1);
107 }
108 do {
109 printf("%s? [yn] ", question);
110 (void) fflush(stdout);
111 c = getc(stdin);
112 while (c != '\n' && getc(stdin) != '\n')
113 if (feof(stdin))
114 return (0);
115 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
116 printf("\n");
117 if (c == 'y' || c == 'Y')
118 return (1);
119 return (0);
120 }
121
122 /*
123 * Malloc buffers and set up cache.
124 */
125 void
126 bufinit()
127 {
128 struct bufarea *bp;
129 long bufcnt, i;
130 char *bufp;
131
132 pbp = pdirbp = (struct bufarea *)0;
133 bufp = malloc((unsigned int)sblock.fs_bsize);
134 if (bufp == 0)
135 errx(EEXIT, "cannot allocate buffer pool");
136 cgblk.b_un.b_buf = bufp;
137 initbarea(&cgblk);
138 bufhead.b_next = bufhead.b_prev = &bufhead;
139 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
140 if (bufcnt < MINBUFS)
141 bufcnt = MINBUFS;
142 for (i = 0; i < bufcnt; i++) {
143 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
144 bufp = malloc((unsigned int)sblock.fs_bsize);
145 if (bp == NULL || bufp == NULL) {
146 if (i >= MINBUFS)
147 break;
148 errx(EEXIT, "cannot allocate buffer pool");
149 }
150 bp->b_un.b_buf = bufp;
151 bp->b_prev = &bufhead;
152 bp->b_next = bufhead.b_next;
153 bufhead.b_next->b_prev = bp;
154 bufhead.b_next = bp;
155 initbarea(bp);
156 }
157 bufhead.b_size = i; /* save number of buffers */
158 }
159
160 /*
161 * Manage a cache of directory blocks.
162 */
163 struct bufarea *
164 getdatablk(blkno, size)
165 ufs_daddr_t blkno;
166 long size;
167 {
168 struct bufarea *bp;
169
170 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
171 if (bp->b_bno == fsbtodb(&sblock, blkno))
172 goto foundit;
173 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
174 if ((bp->b_flags & B_INUSE) == 0)
175 break;
176 if (bp == &bufhead)
177 errx(EEXIT, "deadlocked buffer pool");
178 getblk(bp, blkno, size);
179 /* fall through */
180 foundit:
181 totalreads++;
182 bp->b_prev->b_next = bp->b_next;
183 bp->b_next->b_prev = bp->b_prev;
184 bp->b_prev = &bufhead;
185 bp->b_next = bufhead.b_next;
186 bufhead.b_next->b_prev = bp;
187 bufhead.b_next = bp;
188 bp->b_flags |= B_INUSE;
189 return (bp);
190 }
191
192 void
193 getblk(bp, blk, size)
194 struct bufarea *bp;
195 ufs_daddr_t blk;
196 long size;
197 {
198 ufs_daddr_t dblk;
199
200 dblk = fsbtodb(&sblock, blk);
201 if (bp->b_bno != dblk) {
202 flush(fswritefd, bp);
203 diskreads++;
204 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
205 bp->b_bno = dblk;
206 bp->b_size = size;
207 }
208 }
209
210 void
211 flush(fd, bp)
212 int fd;
213 struct bufarea *bp;
214 {
215 int i, j;
216
217 if (!bp->b_dirty)
218 return;
219 if (bp->b_errs != 0)
220 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
221 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
222 bp->b_bno);
223 bp->b_dirty = 0;
224 bp->b_errs = 0;
225 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
226 if (bp != &sblk)
227 return;
228 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
229 bwrite(fswritefd, (char *)sblock.fs_csp[j],
230 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
231 sblock.fs_cssize - i < sblock.fs_bsize ?
232 sblock.fs_cssize - i : sblock.fs_bsize);
233 }
234 }
235
236 static void
237 rwerror(mesg, blk)
238 char *mesg;
239 ufs_daddr_t blk;
240 {
241
242 if (preen == 0)
243 printf("\n");
244 pfatal("CANNOT %s: BLK %d", mesg, blk);
245 if (reply("CONTINUE") == 0)
246 exit(EEXIT);
247 }
248
249 void
250 ckfini(markclean)
251 int markclean;
252 {
253 struct bufarea *bp, *nbp;
254 int ofsmodified, cnt = 0;
255
256 if (fswritefd < 0) {
257 (void)close(fsreadfd);
258 return;
259 }
260 flush(fswritefd, &sblk);
261 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
262 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
263 sblk.b_bno = SBOFF / dev_bsize;
264 sbdirty();
265 flush(fswritefd, &sblk);
266 }
267 flush(fswritefd, &cgblk);
268 free(cgblk.b_un.b_buf);
269 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
270 cnt++;
271 flush(fswritefd, bp);
272 nbp = bp->b_prev;
273 free(bp->b_un.b_buf);
274 free((char *)bp);
275 }
276 if (bufhead.b_size != cnt)
277 errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
278 pbp = pdirbp = (struct bufarea *)0;
279 if (markclean && (sblock.fs_clean & FS_ISCLEAN) == 0) {
280 /*
281 * Mark the file system as clean, and sync the superblock.
282 */
283 if (preen)
284 pwarn("MARKING FILE SYSTEM CLEAN\n");
285 else if (!reply("MARK FILE SYSTEM CLEAN"))
286 markclean = 0;
287 if (markclean) {
288 sblock.fs_clean = FS_ISCLEAN;
289 sbdirty();
290 ofsmodified = fsmodified;
291 flush(fswritefd, &sblk);
292 fsmodified = ofsmodified;
293 if (!preen)
294 printf(
295 "\n***** FILE SYSTEM MARKED CLEAN *****\n");
296 }
297 }
298 if (debug)
299 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
300 totalreads, (int)(diskreads * 100 / totalreads));
301 (void)close(fsreadfd);
302 (void)close(fswritefd);
303 }
304
305 int
306 bread(fd, buf, blk, size)
307 int fd;
308 char *buf;
309 ufs_daddr_t blk;
310 long size;
311 {
312 char *cp;
313 int i, errs;
314 off_t offset;
315
316 offset = blk;
317 offset *= dev_bsize;
318 if (lseek(fd, offset, 0) < 0)
319 rwerror("SEEK", blk);
320 else if (read(fd, buf, (int)size) == size)
321 return (0);
322 rwerror("READ", blk);
323 if (lseek(fd, offset, 0) < 0)
324 rwerror("SEEK", blk);
325 errs = 0;
326 memset(buf, 0, (size_t)size);
327 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
328 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
329 if (read(fd, cp, (int)secsize) != secsize) {
330 (void)lseek(fd, offset + i + secsize, 0);
331 if (secsize != dev_bsize && dev_bsize != 1)
332 printf(" %ld (%ld),",
333 (blk * dev_bsize + i) / secsize,
334 blk + i / dev_bsize);
335 else
336 printf(" %ld,", blk + i / dev_bsize);
337 errs++;
338 }
339 }
340 printf("\n");
341 return (errs);
342 }
343
344 void
345 bwrite(fd, buf, blk, size)
346 int fd;
347 char *buf;
348 ufs_daddr_t blk;
349 long size;
350 {
351 int i;
352 char *cp;
353 off_t offset;
354
355 if (fd < 0)
356 return;
357 offset = blk;
358 offset *= dev_bsize;
359 if (lseek(fd, offset, 0) < 0)
360 rwerror("SEEK", blk);
361 else if (write(fd, buf, (int)size) == size) {
362 fsmodified = 1;
363 return;
364 }
365 rwerror("WRITE", blk);
366 if (lseek(fd, offset, 0) < 0)
367 rwerror("SEEK", blk);
368 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
369 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
370 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
371 (void)lseek(fd, offset + i + dev_bsize, 0);
372 printf(" %ld,", blk + i / dev_bsize);
373 }
374 printf("\n");
375 return;
376 }
377
378 /*
379 * allocate a data block with the specified number of fragments
380 */
381 ufs_daddr_t
382 allocblk(frags)
383 long frags;
384 {
385 int i, j, k;
386
387 if (frags <= 0 || frags > sblock.fs_frag)
388 return (0);
389 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
390 for (j = 0; j <= sblock.fs_frag - frags; j++) {
391 if (testbmap(i + j))
392 continue;
393 for (k = 1; k < frags; k++)
394 if (testbmap(i + j + k))
395 break;
396 if (k < frags) {
397 j += k;
398 continue;
399 }
400 for (k = 0; k < frags; k++)
401 setbmap(i + j + k);
402 n_blks += frags;
403 return (i + j);
404 }
405 }
406 return (0);
407 }
408
409 /*
410 * Free a previously allocated block
411 */
412 void
413 freeblk(blkno, frags)
414 ufs_daddr_t blkno;
415 long frags;
416 {
417 struct inodesc idesc;
418
419 idesc.id_blkno = blkno;
420 idesc.id_numfrags = frags;
421 (void)pass4check(&idesc);
422 }
423
424 /*
425 * Find a pathname
426 */
427 void
428 getpathname(namebuf, curdir, ino)
429 char *namebuf;
430 ino_t curdir, ino;
431 {
432 int len;
433 char *cp;
434 struct inodesc idesc;
435 static int busy = 0;
436
437 if (curdir == ino && ino == ROOTINO) {
438 (void)strcpy(namebuf, "/");
439 return;
440 }
441 if (busy ||
442 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
443 (void)strcpy(namebuf, "?");
444 return;
445 }
446 busy = 1;
447 memset(&idesc, 0, sizeof(struct inodesc));
448 idesc.id_type = DATA;
449 idesc.id_fix = IGNORE;
450 cp = &namebuf[MAXPATHLEN - 1];
451 *cp = '\0';
452 if (curdir != ino) {
453 idesc.id_parent = curdir;
454 goto namelookup;
455 }
456 while (ino != ROOTINO) {
457 idesc.id_number = ino;
458 idesc.id_func = findino;
459 idesc.id_name = "..";
460 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
461 break;
462 namelookup:
463 idesc.id_number = idesc.id_parent;
464 idesc.id_parent = ino;
465 idesc.id_func = findname;
466 idesc.id_name = namebuf;
467 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
468 break;
469 len = strlen(namebuf);
470 cp -= len;
471 memmove(cp, namebuf, (size_t)len);
472 *--cp = '/';
473 if (cp < &namebuf[MAXNAMLEN])
474 break;
475 ino = idesc.id_number;
476 }
477 busy = 0;
478 if (ino != ROOTINO)
479 *--cp = '?';
480 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
481 }
482
483 void
484 catch(sig)
485 int sig;
486 {
487 if (!doinglevel2)
488 ckfini(0);
489 exit(12);
490 }
491
492 /*
493 * When preening, allow a single quit to signal
494 * a special exit after filesystem checks complete
495 * so that reboot sequence may be interrupted.
496 */
497 void
498 catchquit(sig)
499 int sig;
500 {
501 extern returntosingle;
502
503 printf("returning to single-user after filesystem check\n");
504 returntosingle = 1;
505 (void)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 void
513 voidquit(sig)
514 int sig;
515 {
516
517 sleep(1);
518 (void)signal(SIGQUIT, SIG_IGN);
519 (void)signal(SIGQUIT, SIG_DFL);
520 }
521
522 /*
523 * determine whether an inode should be fixed.
524 */
525 int
526 dofix(idesc, msg)
527 struct inodesc *idesc;
528 char *msg;
529 {
530
531 switch (idesc->id_fix) {
532
533 case DONTKNOW:
534 if (idesc->id_type == DATA)
535 direrror(idesc->id_number, msg);
536 else
537 pwarn(msg);
538 if (preen) {
539 printf(" (SALVAGED)\n");
540 idesc->id_fix = FIX;
541 return (ALTERED);
542 }
543 if (reply("SALVAGE") == 0) {
544 idesc->id_fix = NOFIX;
545 return (0);
546 }
547 idesc->id_fix = FIX;
548 return (ALTERED);
549
550 case FIX:
551 return (ALTERED);
552
553 case NOFIX:
554 case IGNORE:
555 return (0);
556
557 default:
558 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
559 }
560 /* NOTREACHED */
561 return (0);
562 }
563