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