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