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