xref: /titanic_50/usr/src/cmd/fs.d/udfs/fsck/utilities.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
1 /*
2  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms are permitted
14  * provided that: (1) source distributions retain this entire copyright
15  * notice and comment, and (2) distributions including binaries display
16  * the following acknowledgement:  ``This product includes software
17  * developed by the University of California, Berkeley and its contributors''
18  * in the documentation or other materials provided with the distribution
19  * and in all advertising materials mentioning features or use of this
20  * software. Neither the name of the University nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <ctype.h>
39 #include <malloc.h>
40 #include <signal.h>
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/mntent.h>
44 #include <sys/filio.h>
45 #include <sys/vnode.h>
46 #include <sys/mnttab.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/vfstab.h>
50 #include <sys/sysmacros.h>
51 #include <sys/fs/udf_volume.h>
52 #include "fsck.h"
53 #include <sys/lockfs.h>
54 #include <locale.h>
55 
56 extern int32_t	verifytag(struct tag *, uint32_t, struct tag *, int);
57 extern char	*tagerrs[];
58 extern void	maketag(struct tag *, struct tag *);
59 extern char	*hasvfsopt(struct vfstab *, char *);
60 static struct bufarea *getdatablk(daddr_t, long);
61 static struct bufarea *getblk(struct bufarea *, daddr_t, long);
62 
63 void	flush(int32_t, struct bufarea *);
64 int32_t	bread(int32_t, char *, daddr_t, long);
65 void	bwrite(int, char *, daddr_t, long);
66 static int32_t	getline(FILE *, char *, int32_t);
67 
68 static long	diskreads, totalreads;	/* Disk cache statistics */
69 offset_t	llseek();
70 extern unsigned int largefile_count;
71 
72 /*
73  * An unexpected inconsistency occured.
74  * Die if preening, otherwise just print message and continue.
75  */
76 /* VARARGS1 */
77 void
78 pfatal(s, a1, a2, a3)
79 	char *s;
80 {
81 
82 	if (preen) {
83 		(void) printf("%s: ", devname);
84 		(void) printf(s, a1, a2, a3);
85 		(void) printf("\n");
86 		(void) printf(
87 		    gettext("%s: UNEXPECTED INCONSISTENCY; RUN fsck "
88 			"MANUALLY.\n"), devname);
89 		exit(36);
90 	}
91 	(void) printf(s, a1, a2, a3);
92 }
93 
94 /*
95  * Pwarn just prints a message when not preening,
96  * or a warning (preceded by filename) when preening.
97  */
98 /* VARARGS1 */
99 void
100 pwarn(s, a1, a2, a3, a4, a5, a6)
101 	char *s;
102 {
103 
104 	if (preen)
105 		(void) printf("%s: ", devname);
106 	(void) printf(s, a1, a2, a3, a4, a5, a6);
107 }
108 
109 
110 /* VARARGS1 */
111 void
112 errexit(s1, s2, s3, s4)
113 	char *s1;
114 {
115 	(void) printf(s1, s2, s3, s4);
116 	exit(39);
117 }
118 
119 void
120 markbusy(daddr_t block, long count)
121 {
122 	register int i;
123 
124 	count = roundup(count, secsize) / secsize;
125 	for (i = 0; i < count; i++, block++) {
126 		if ((unsigned)block > part_len) {
127 			pwarn(gettext("Block %lx out of range\n"), block);
128 			break;
129 		}
130 		if (testbusy(block))
131 			pwarn(gettext("Dup block %lx\n"), block);
132 		else {
133 			n_blks++;
134 			setbusy(block);
135 		}
136 	}
137 }
138 
139 void
140 printfree()
141 {
142 	int i, startfree, endfree;
143 
144 	startfree = -1;
145 	for (i = 0; i < part_len; i++) {
146 		if (!testbusy(i)) {
147 			if (startfree <= 0)
148 				startfree = i;
149 			endfree = i;
150 		} else if (startfree >= 0) {
151 			(void) printf("free: %x-%x\n", startfree, endfree - 1);
152 			startfree = -1;
153 		}
154 	}
155 	if (startfree >= 0) {
156 		(void) printf("free: %x-%x\n", startfree, endfree);
157 	}
158 }
159 
160 struct bufarea *
161 getfilentry(uint32_t block, int len)
162 {
163 	struct bufarea *bp;
164 	struct file_entry *fp;
165 	int err;
166 
167 	if (len > fsbsize) {
168 		(void) printf(gettext("File entry at %x is too long "
169 			"(%d bytes)\n"), block, len);
170 		len = fsbsize;
171 	}
172 	bp = getdatablk((daddr_t)(block + part_start), fsbsize);
173 	if (bp->b_errs) {
174 		bp->b_flags &= ~B_INUSE;
175 		return (NULL);
176 	}
177 	/* LINTED */
178 	fp = (struct file_entry *)bp->b_un.b_buf;
179 	err = verifytag(&fp->fe_tag, block, &fp->fe_tag, UD_FILE_ENTRY);
180 	if (err) {
181 		(void) printf(gettext("Tag error %s or bad file entry, "
182 			"tag=%d\n"), tagerrs[err], fp->fe_tag.tag_id);
183 		bp->b_flags &= ~B_INUSE;
184 		return (NULL);
185 	}
186 	return (bp);
187 }
188 
189 void
190 putfilentry(struct bufarea *bp)
191 {
192 	struct file_entry *fp;
193 
194 	/* LINTED */
195 	fp = (struct file_entry *)bp->b_un.b_buf;
196 	maketag(&fp->fe_tag, &fp->fe_tag);
197 }
198 
199 
200 int32_t
201 reply(char *question)
202 {
203 	char line[80];
204 
205 	if (preen)
206 		pfatal(gettext("INTERNAL ERROR: GOT TO reply()"));
207 	(void) printf("\n%s? ", question);
208 	if (nflag || fswritefd < 0) {
209 		(void) printf(gettext(" no\n\n"));
210 		iscorrupt = 1;		/* known to be corrupt */
211 		return (0);
212 	}
213 	if (yflag) {
214 		(void) printf(gettext(" yes\n\n"));
215 		return (1);
216 	}
217 	if (getline(stdin, line, sizeof (line)) == EOF)
218 		errexit("\n");
219 	(void) printf("\n");
220 	if (line[0] == 'y' || line[0] == 'Y')
221 		return (1);
222 	else {
223 		iscorrupt = 1;		/* known to be corrupt */
224 		return (0);
225 	}
226 }
227 
228 int32_t
229 getline(FILE *fp, char *loc, int32_t maxlen)
230 {
231 	register n;
232 	register char *p, *lastloc;
233 
234 	p = loc;
235 	lastloc = &p[maxlen-1];
236 	while ((n = getc(fp)) != '\n') {
237 		if (n == EOF)
238 			return (EOF);
239 		if (!isspace(n) && p < lastloc)
240 			*p++ = n;
241 	}
242 	*p = 0;
243 	return (p - loc);
244 }
245 /*
246  * Malloc buffers and set up cache.
247  */
248 void
249 bufinit()
250 {
251 	register struct bufarea *bp;
252 	long bufcnt, i;
253 	char *bufp;
254 
255 	bufp = malloc((unsigned int)fsbsize);
256 	if (bufp == 0)
257 		errexit(gettext("cannot allocate buffer pool\n"));
258 	bufhead.b_next = bufhead.b_prev = &bufhead;
259 	bufcnt = MAXBUFSPACE / fsbsize;
260 	if (bufcnt < MINBUFS)
261 		bufcnt = MINBUFS;
262 	for (i = 0; i < bufcnt; i++) {
263 		bp = (struct bufarea *)malloc(sizeof (struct bufarea));
264 		bufp = malloc((unsigned int)fsbsize);
265 		if (bp == NULL || bufp == NULL) {
266 			if (i >= MINBUFS)
267 				break;
268 			errexit(gettext("cannot allocate buffer pool\n"));
269 		}
270 		bp->b_un.b_buf = bufp;
271 		bp->b_prev = &bufhead;
272 		bp->b_next = bufhead.b_next;
273 		bufhead.b_next->b_prev = bp;
274 		bufhead.b_next = bp;
275 		initbarea(bp);
276 	}
277 	bufhead.b_size = i;	/* save number of buffers */
278 	pbp = pdirbp = NULL;
279 }
280 
281 /*
282  * Manage a cache of directory blocks.
283  */
284 static struct bufarea *
285 getdatablk(daddr_t blkno, long size)
286 {
287 	register struct bufarea *bp;
288 
289 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
290 		if (bp->b_bno == fsbtodb(blkno))
291 			goto foundit;
292 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
293 		if ((bp->b_flags & B_INUSE) == 0)
294 			break;
295 	if (bp == &bufhead)
296 		errexit(gettext("deadlocked buffer pool\n"));
297 	(void) getblk(bp, blkno, size);
298 	/* fall through */
299 foundit:
300 	totalreads++;
301 	bp->b_prev->b_next = bp->b_next;
302 	bp->b_next->b_prev = bp->b_prev;
303 	bp->b_prev = &bufhead;
304 	bp->b_next = bufhead.b_next;
305 	bufhead.b_next->b_prev = bp;
306 	bufhead.b_next = bp;
307 	bp->b_flags |= B_INUSE;
308 	return (bp);
309 }
310 
311 static struct bufarea *
312 getblk(struct bufarea *bp, daddr_t blk, long size)
313 {
314 	daddr_t dblk;
315 
316 	dblk = fsbtodb(blk);
317 	if (bp->b_bno == dblk)
318 		return (bp);
319 	flush(fswritefd, bp);
320 	diskreads++;
321 	bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
322 	bp->b_bno = dblk;
323 	bp->b_size = size;
324 	return (bp);
325 }
326 
327 void
328 flush(int32_t fd, struct bufarea *bp)
329 {
330 	if (!bp->b_dirty)
331 		return;
332 	if (bp->b_errs != 0)
333 		pfatal(gettext("WRITING ZERO'ED BLOCK %d TO DISK\n"),
334 			bp->b_bno);
335 	bp->b_dirty = 0;
336 	bp->b_errs = 0;
337 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
338 }
339 
340 static void
341 rwerror(char *mesg, daddr_t blk)
342 {
343 
344 	if (preen == 0)
345 		(void) printf("\n");
346 	pfatal(gettext("CANNOT %s: BLK %ld"), mesg, blk);
347 	if (reply(gettext("CONTINUE")) == 0)
348 		errexit(gettext("Program terminated\n"));
349 }
350 
351 void
352 ckfini()
353 {
354 	struct bufarea *bp, *nbp;
355 	int cnt = 0;
356 
357 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
358 		cnt++;
359 		flush(fswritefd, bp);
360 		nbp = bp->b_prev;
361 		free(bp->b_un.b_buf);
362 		free((char *)bp);
363 	}
364 	pbp = pdirbp = NULL;
365 	if (bufhead.b_size != cnt)
366 		errexit(gettext("Panic: lost %d buffers\n"),
367 			bufhead.b_size - cnt);
368 	if (debug)
369 		(void) printf("cache missed %ld of %ld (%ld%%)\n",
370 		    diskreads, totalreads,
371 		    totalreads ? diskreads * 100 / totalreads : 0);
372 	(void) close(fsreadfd);
373 	(void) close(fswritefd);
374 }
375 
376 int32_t
377 bread(int fd, char *buf, daddr_t blk, long size)
378 {
379 	char *cp;
380 	int i, errs;
381 	offset_t offset = ldbtob(blk);
382 	offset_t addr;
383 
384 	if (llseek(fd, offset, 0) < 0)
385 		rwerror(gettext("SEEK"), blk);
386 	else if (read(fd, buf, (int)size) == size)
387 		return (0);
388 	rwerror(gettext("READ"), blk);
389 	if (llseek(fd, offset, 0) < 0)
390 		rwerror(gettext("SEEK"), blk);
391 	errs = 0;
392 	bzero(buf, (int)size);
393 	pwarn(gettext("THE FOLLOWING SECTORS COULD NOT BE READ:"));
394 	for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
395 		addr = ldbtob(blk + i);
396 		if (llseek(fd, addr, SEEK_CUR) < 0 ||
397 		    read(fd, cp, (int)secsize) < 0) {
398 			(void) printf(" %ld", blk + i);
399 			errs++;
400 		}
401 	}
402 	(void) printf("\n");
403 	return (errs);
404 }
405 
406 void
407 bwrite(int fd, char *buf, daddr_t blk, long size)
408 {
409 	int i, n;
410 	char *cp;
411 	offset_t offset = ldbtob(blk);
412 	offset_t addr;
413 
414 	if (fd < 0)
415 		return;
416 	if (llseek(fd, offset, 0) < 0)
417 		rwerror(gettext("SEEK"), blk);
418 	else if (write(fd, buf, (int)size) == size) {
419 		fsmodified = 1;
420 		return;
421 	}
422 	rwerror(gettext("WRITE"), blk);
423 	if (llseek(fd, offset, 0) < 0)
424 		rwerror(gettext("SEEK"), blk);
425 	pwarn(gettext("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"));
426 	for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
427 		n = 0;
428 		addr = ldbtob(blk + i);
429 		if (llseek(fd, addr, SEEK_CUR) < 0 ||
430 		    (n = write(fd, cp, DEV_BSIZE)) < 0) {
431 			(void) printf(" %ld", blk + i);
432 		} else if (n > 0) {
433 			fsmodified = 1;
434 		}
435 
436 	}
437 	(void) printf("\n");
438 }
439 
440 void
441 catch()
442 {
443 	ckfini();
444 	exit(37);
445 }
446 
447 /*
448  * When preening, allow a single quit to signal
449  * a special exit after filesystem checks complete
450  * so that reboot sequence may be interrupted.
451  */
452 void
453 catchquit()
454 {
455 	extern returntosingle;
456 
457 	(void) printf(gettext("returning to single-user after filesystem "
458 		"check\n"));
459 	returntosingle = 1;
460 	(void) signal(SIGQUIT, SIG_DFL);
461 }
462 
463 /*
464  * determine whether an inode should be fixed.
465  */
466 /* ARGSUSED1 */
467 int32_t
468 dofix(struct inodesc *idesc, char *msg)
469 {
470 
471 	switch (idesc->id_fix) {
472 
473 	case DONTKNOW:
474 		pwarn(msg);
475 		if (preen) {
476 			(void) printf(gettext(" (SALVAGED)\n"));
477 			idesc->id_fix = FIX;
478 			return (ALTERED);
479 		}
480 		if (reply(gettext("SALVAGE")) == 0) {
481 			idesc->id_fix = NOFIX;
482 			return (0);
483 		}
484 		idesc->id_fix = FIX;
485 		return (ALTERED);
486 
487 	case FIX:
488 		return (ALTERED);
489 
490 	case NOFIX:
491 		return (0);
492 
493 	default:
494 		errexit(gettext("UNKNOWN INODESC FIX MODE %d\n"),
495 			idesc->id_fix);
496 	}
497 	/* NOTREACHED */
498 }
499 
500 /*
501  * Check to see if unraw version of name is already mounted.
502  * Since we do not believe /etc/mnttab, we stat the mount point
503  * to see if it is really looks mounted.
504  */
505 mounted(char *name)
506 {
507 	int found = 0;
508 	struct mnttab mnt;
509 	FILE *mnttab;
510 	struct stat device_stat, mount_stat;
511 	char *blkname, *unrawname();
512 	int err;
513 
514 	mnttab = fopen(MNTTAB, "r");
515 	if (mnttab == NULL) {
516 		(void) printf(gettext("can't open %s\n"), MNTTAB);
517 		return (0);
518 	}
519 	blkname = unrawname(name);
520 	while ((getmntent(mnttab, &mnt)) == NULL) {
521 		if (strcmp(mnt.mnt_fstype, MNTTYPE_UDFS) != 0) {
522 			continue;
523 		}
524 		if (strcmp(blkname, mnt.mnt_special) == 0) {
525 			err = stat(mnt.mnt_mountp, &mount_stat);
526 			err |= stat(mnt.mnt_special, &device_stat);
527 			if (err < 0)
528 				continue;
529 			if (device_stat.st_rdev == mount_stat.st_dev) {
530 				(void) strncpy(mnt.mnt_mountp, mountpoint,
531 					sizeof (mountpoint));
532 				if (hasmntopt(&mnt, MNTOPT_RO) != 0)
533 					found = 2;	/* mounted as RO */
534 				else
535 					found = 1; 	/* mounted as R/W */
536 			}
537 			break;
538 		}
539 	}
540 	(void) fclose(mnttab);
541 	return (found);
542 }
543 
544 /*
545  * Check to see if name corresponds to an entry in vfstab, and that the entry
546  * does not have option ro.
547  */
548 writable(char *name)
549 {
550 	int rw = 1;
551 	struct vfstab vfsbuf;
552 	FILE *vfstab;
553 	char *blkname, *unrawname();
554 
555 	vfstab = fopen(VFSTAB, "r");
556 	if (vfstab == NULL) {
557 		(void) printf(gettext("can't open %s\n"), VFSTAB);
558 		return (1);
559 	}
560 	blkname = unrawname(name);
561 	if ((getvfsspec(vfstab, &vfsbuf, blkname) == 0) &&
562 	    (vfsbuf.vfs_fstype != NULL) &&
563 	    (strcmp(vfsbuf.vfs_fstype, MNTTYPE_UDFS) == 0) &&
564 	    (hasvfsopt(&vfsbuf, MNTOPT_RO))) {
565 		rw = 0;
566 	}
567 	(void) fclose(vfstab);
568 	return (rw);
569 }
570 
571 /*
572  * print out clean info
573  */
574 void
575 printclean()
576 {
577 	char	*s;
578 
579 	switch (lvintp->lvid_int_type) {
580 
581 	case LVI_CLOSE:
582 		s = gettext("clean");
583 		break;
584 
585 	case LVI_OPEN:
586 		s = gettext("active");
587 		break;
588 
589 	default:
590 		s = gettext("unknown");
591 	}
592 
593 	if (preen)
594 		pwarn(gettext("is %s.\n"), s);
595 	else
596 		(void) printf("** %s is %s.\n", devname, s);
597 }
598