1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)library.c	8.2 (Berkeley) 05/04/95";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/stat.h>
15 #include <sys/mount.h>
16 #include <sys/types.h>
17 #include <sys/mman.h>
18 
19 #include <ufs/ufs/dinode.h>
20 #include <ufs/lfs/lfs.h>
21 
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "clean.h"
29 
30 void	 add_blocks __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t,
31 	     daddr_t, daddr_t));
32 void	 add_inodes __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t,
33 	     daddr_t));
34 int	 bi_compare __P((const void *, const void *));
35 int	 bi_toss __P((const void *, const void *, const void *));
36 void	 get_ifile __P((FS_INFO *, int));
37 int	 get_superblock __P((FS_INFO *, struct lfs *));
38 int	 pseg_valid __P((FS_INFO *, SEGSUM *));
39 
40 /*
41  * This function will get information on a a filesystem which matches
42  * the name and type given.  If a "name" is in a filesystem of the given
43  * type, then buf is filled with that filesystem's info, and the
44  * a non-zero value is returned.
45  */
46 int
47 fs_getmntinfo(buf, name, type)
48 	struct	statfs	**buf;
49 	char	*name;
50 	char	*type;
51 {
52 	/* allocate space for the filesystem info */
53 	*buf = (struct statfs *)malloc(sizeof(struct statfs));
54 	if (*buf == NULL)
55 		return 0;
56 
57 	/* grab the filesystem info */
58 	if (statfs(name, *buf) < 0) {
59 		free(*buf);
60 		return 0;
61 	}
62 
63 	/* check to see if it's the one we want */
64 	if (strcmp((*buf)->f_fstypename, type) ||
65 	    strncmp(name, (*buf)->f_mntonname, MNAMELEN)) {
66 		/* "this is not the filesystem you're looking for */
67 		free(*buf);
68 		return 0;
69 	}
70 
71 	return 1;
72 }
73 
74 /*
75  * Get all the information available on an LFS file system.
76  * Returns an pointer to an FS_INFO structure, NULL on error.
77  */
78 FS_INFO *
79 get_fs_info (lstatfsp, use_mmap)
80 	struct statfs *lstatfsp;	/* IN: pointer to statfs struct */
81 	int use_mmap;			/* IN: mmap or read */
82 {
83 	FS_INFO	*fsp;
84 	int	i;
85 
86 	fsp = (FS_INFO *)malloc(sizeof(FS_INFO));
87 	if (fsp == NULL)
88 		return NULL;
89 	bzero(fsp, sizeof(FS_INFO));
90 
91 	fsp->fi_statfsp = lstatfsp;
92 	if (get_superblock (fsp, &fsp->fi_lfs))
93 		err(1, "get_fs_info: get_superblock failed");
94 	fsp->fi_daddr_shift =
95 	     fsp->fi_lfs.lfs_bshift - fsp->fi_lfs.lfs_fsbtodb;
96 	get_ifile (fsp, use_mmap);
97 	return (fsp);
98 }
99 
100 /*
101  * If we are reading the ifile then we need to refresh it.  Even if
102  * we are mmapping it, it might have grown.  Finally, we need to
103  * refresh the file system information (statfs) info.
104  */
105 void
106 reread_fs_info(fsp, use_mmap)
107 	FS_INFO *fsp;	/* IN: prointer fs_infos to reread */
108 	int use_mmap;
109 {
110 	int i;
111 
112 	if (statfs(fsp->fi_statfsp->f_mntonname, fsp->fi_statfsp))
113 		err(1, "reread_fs_info: statfs failed");
114 	get_ifile (fsp, use_mmap);
115 }
116 
117 /*
118  * Gets the superblock from disk (possibly in face of errors)
119  */
120 int
121 get_superblock (fsp, sbp)
122 	FS_INFO *fsp;		/* local file system info structure */
123 	struct lfs *sbp;
124 {
125 	char mntfromname[MNAMELEN+1];
126         int fid;
127 
128 	strcpy(mntfromname, "/dev/r");
129 	strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5);
130 
131 	if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) {
132 		err(0, "get_superblock: bad open");
133 		return (-1);
134 	}
135 
136 	get(fid, LFS_LABELPAD, sbp, sizeof(struct lfs));
137 	close (fid);
138 
139 	return (0);
140 }
141 
142 /*
143  * This function will map the ifile into memory.  It causes a
144  * fatal error on failure.
145  */
146 void
147 get_ifile (fsp, use_mmap)
148 	FS_INFO	*fsp;
149 	int use_mmap;
150 
151 {
152 	struct stat file_stat;
153 	caddr_t ifp;
154 	char *ifile_name;
155 	int count, fid;
156 
157 	ifp = NULL;
158 	ifile_name = malloc(strlen(fsp->fi_statfsp->f_mntonname) +
159 	    strlen(IFILE_NAME)+2);
160 	strcat(strcat(strcpy(ifile_name, fsp->fi_statfsp->f_mntonname), "/"),
161 	    IFILE_NAME);
162 
163 	if ((fid = open(ifile_name, O_RDWR, (mode_t)0)) < 0)
164 		err(1, "get_ifile: bad open");
165 
166 	if (fstat (fid, &file_stat))
167 		err(1, "get_ifile: fstat failed");
168 
169 	if (use_mmap && file_stat.st_size == fsp->fi_ifile_length) {
170 		(void) close(fid);
171 		return;
172 	}
173 
174 	/* get the ifile */
175 	if (use_mmap) {
176 		if (fsp->fi_cip)
177 			munmap((caddr_t)fsp->fi_cip, fsp->fi_ifile_length);
178 		ifp = mmap ((caddr_t)0, file_stat.st_size,
179 		    PROT_READ|PROT_WRITE, 0, fid, (off_t)0);
180 		if (ifp ==  (caddr_t)(-1))
181 			err(1, "get_ifile: mmap failed");
182 	} else {
183 		if (fsp->fi_cip)
184 			free(fsp->fi_cip);
185 		if (!(ifp = malloc (file_stat.st_size)))
186 			err (1, "get_ifile: malloc failed");
187 redo_read:
188 		count = read (fid, ifp, (size_t) file_stat.st_size);
189 
190 		if (count < 0)
191 			err(1, "get_ifile: bad ifile read");
192 		else if (count < file_stat.st_size) {
193 			err(0, "get_ifile");
194 			if (lseek(fid, 0, SEEK_SET) < 0)
195 				err(1, "get_ifile: bad ifile lseek");
196 			goto redo_read;
197 		}
198 	}
199 	fsp->fi_ifile_length = file_stat.st_size;
200 	close (fid);
201 
202 	fsp->fi_cip = (CLEANERINFO *)ifp;
203 	fsp->fi_segusep = (SEGUSE *)(ifp + CLEANSIZE(fsp));
204 	fsp->fi_ifilep  = (IFILE *)((caddr_t)fsp->fi_segusep + SEGTABSIZE(fsp));
205 
206 	/*
207 	 * The number of ifile entries is equal to the number of blocks
208 	 * blocks in the ifile minus the ones allocated to cleaner info
209 	 * and segment usage table multiplied by the number of ifile
210 	 * entries per page.
211 	 */
212 	fsp->fi_ifile_count = (fsp->fi_ifile_length >> fsp->fi_lfs.lfs_bshift -
213 	    fsp->fi_lfs.lfs_cleansz - fsp->fi_lfs.lfs_segtabsz) *
214 	    fsp->fi_lfs.lfs_ifpb;
215 
216 	free (ifile_name);
217 }
218 
219 /*
220  * This function will scan a segment and return a list of
221  * <inode, blocknum> pairs which indicate which blocks were
222  * contained as live data within the segment when the segment
223  * summary was read (it may have "died" since then).  Any given
224  * pair will be listed at most once.
225  */
226 int
227 lfs_segmapv(fsp, seg, seg_buf, blocks, bcount)
228 	FS_INFO *fsp;		/* pointer to local file system information */
229 	int seg;		/* the segment number */
230 	caddr_t seg_buf;	/* the buffer containing the segment's data */
231 	BLOCK_INFO **blocks;	/* OUT: array of block_info for live blocks */
232 	int *bcount;		/* OUT: number of active blocks in segment */
233 {
234 	BLOCK_INFO *bip;
235 	SEGSUM *sp;
236 	SEGUSE *sup;
237 	FINFO *fip;
238 	struct lfs *lfsp;
239 	caddr_t s, segend;
240 	daddr_t pseg_addr, seg_addr;
241 	int i, nelem, nblocks, sumsize;
242 	time_t timestamp;
243 
244 	lfsp = &fsp->fi_lfs;
245 	nelem = 2 * lfsp->lfs_ssize;
246 	if (!(bip = malloc(nelem * sizeof(BLOCK_INFO))))
247 		goto err0;
248 
249 	sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, seg);
250 	s = seg_buf + (sup->su_flags & SEGUSE_SUPERBLOCK ? LFS_SBPAD : 0);
251 	seg_addr = sntoda(lfsp, seg);
252 	pseg_addr = seg_addr + (sup->su_flags & SEGUSE_SUPERBLOCK ? btodb(LFS_SBPAD) : 0);
253 #ifdef VERBOSE
254 		printf("\tsegment buffer at: 0x%x\tseg_addr 0x%x\n", s, seg_addr);
255 #endif /* VERBOSE */
256 
257 	*bcount = 0;
258 	for (segend = seg_buf + seg_size(lfsp), timestamp = 0; s < segend; ) {
259 		sp = (SEGSUM *)s;
260 
261 #ifdef VERBOSE
262 		printf("\tpartial at: 0x%x\n", pseg_addr);
263 		print_SEGSUM(lfsp, sp);
264 		fflush(stdout);
265 #endif /* VERBOSE */
266 
267 		nblocks = pseg_valid(fsp, sp);
268 		if (nblocks <= 0)
269 			break;
270 
271 		/* Check if we have hit old data */
272 		if (timestamp > ((SEGSUM*)s)->ss_create)
273 			break;
274 		timestamp = ((SEGSUM*)s)->ss_create;
275 
276 #ifdef DIAGNOSTIC
277 		/* Verfiy size of summary block */
278 		sumsize = sizeof(SEGSUM) +
279 		    (sp->ss_ninos + INOPB(lfsp) - 1) / INOPB(lfsp);
280 		for (fip = (FINFO *)(sp + 1); i < sp->ss_nfinfo; ++i) {
281 			sumsize += sizeof(FINFO) +
282 			    (fip->fi_nblocks - 1) * sizeof(daddr_t);
283 			fip = (FINFO *)(&fip->fi_blocks[fip->fi_nblocks]);
284 		}
285 		if (sumsize > LFS_SUMMARY_SIZE) {
286 			fprintf(stderr,
287 			    "Segment %d summary block too big: %d\n",
288 			    seg, sumsize);
289 			exit(1);
290 		}
291 #endif
292 
293 		if (*bcount + nblocks + sp->ss_ninos > nelem) {
294 			nelem = *bcount + nblocks + sp->ss_ninos;
295 			bip = realloc (bip, nelem * sizeof(BLOCK_INFO));
296 			if (!bip)
297 				goto err0;
298 		}
299 		add_blocks(fsp, bip, bcount, sp, seg_buf, seg_addr, pseg_addr);
300 		add_inodes(fsp, bip, bcount, sp, seg_buf, seg_addr);
301 		pseg_addr += fsbtodb(lfsp, nblocks) +
302 		    bytetoda(fsp, LFS_SUMMARY_SIZE);
303 		s += (nblocks << lfsp->lfs_bshift) + LFS_SUMMARY_SIZE;
304 	}
305 	qsort(bip, *bcount, sizeof(BLOCK_INFO), bi_compare);
306 	toss(bip, bcount, sizeof(BLOCK_INFO), bi_toss, NULL);
307 #ifdef VERBOSE
308 	{
309 		BLOCK_INFO *_bip;
310 		int i;
311 
312 		printf("BLOCK INFOS\n");
313 		for (_bip = bip, i=0; i < *bcount; ++_bip, ++i)
314 			PRINT_BINFO(_bip);
315 	}
316 #endif
317 	*blocks = bip;
318 	return (0);
319 
320 err0:	*bcount = 0;
321 	return (-1);
322 
323 }
324 
325 /*
326  * This will parse a partial segment and fill in BLOCK_INFO structures
327  * for each block described in the segment summary.  It will not include
328  * blocks or inodes from files with new version numbers.
329  */
330 void
331 add_blocks (fsp, bip, countp, sp, seg_buf, segaddr, psegaddr)
332 	FS_INFO *fsp;		/* pointer to super block */
333 	BLOCK_INFO *bip;	/* Block info array */
334 	int *countp;		/* IN/OUT: number of blocks in array */
335 	SEGSUM	*sp;		/* segment summmary pointer */
336 	caddr_t seg_buf;	/* buffer containing segment */
337 	daddr_t segaddr;	/* address of this segment */
338 	daddr_t psegaddr;	/* address of this partial segment */
339 {
340 	IFILE	*ifp;
341 	FINFO	*fip;
342 	caddr_t	bp;
343 	daddr_t	*dp, *iaddrp;
344 	int db_per_block, i, j;
345 	u_long page_size;
346 
347 #ifdef VERBOSE
348 	printf("FILE INFOS\n");
349 #endif
350 	db_per_block = fsbtodb(&fsp->fi_lfs, 1);
351 	page_size = fsp->fi_lfs.lfs_bsize;
352 	bp = seg_buf + datobyte(fsp, psegaddr - segaddr) + LFS_SUMMARY_SIZE;
353 	bip += *countp;
354 	psegaddr += bytetoda(fsp, LFS_SUMMARY_SIZE);
355 	iaddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE);
356 	--iaddrp;
357 	for (fip = (FINFO *)(sp + 1), i = 0; i < sp->ss_nfinfo;
358 	    ++i, fip = (FINFO *)(&fip->fi_blocks[fip->fi_nblocks])) {
359 
360 		ifp = IFILE_ENTRY(&fsp->fi_lfs, fsp->fi_ifilep, fip->fi_ino);
361 		PRINT_FINFO(fip, ifp);
362 		if (ifp->if_version > fip->fi_version)
363 			continue;
364 		dp = &(fip->fi_blocks[0]);
365 		for (j = 0; j < fip->fi_nblocks; j++, dp++) {
366 			while (psegaddr == *iaddrp) {
367 				psegaddr += db_per_block;
368 				bp += page_size;
369 				--iaddrp;
370 			}
371 			bip->bi_inode = fip->fi_ino;
372 			bip->bi_lbn = *dp;
373 			bip->bi_daddr = psegaddr;
374 			bip->bi_segcreate = (time_t)(sp->ss_create);
375 			bip->bi_bp = bp;
376 			bip->bi_version = ifp->if_version;
377 			psegaddr += db_per_block;
378 			bp += page_size;
379 			++bip;
380 			++(*countp);
381 		}
382 	}
383 }
384 
385 /*
386  * For a particular segment summary, reads the inode blocks and adds
387  * INODE_INFO structures to the array.  Returns the number of inodes
388  * actually added.
389  */
390 void
391 add_inodes (fsp, bip, countp, sp, seg_buf, seg_addr)
392 	FS_INFO *fsp;		/* pointer to super block */
393 	BLOCK_INFO *bip;	/* block info array */
394 	int *countp;		/* pointer to current number of inodes */
395 	SEGSUM *sp;		/* segsum pointer */
396 	caddr_t	seg_buf;	/* the buffer containing the segment's data */
397 	daddr_t	seg_addr;	/* disk address of seg_buf */
398 {
399 	struct dinode *di;
400 	struct lfs *lfsp;
401 	IFILE *ifp;
402 	BLOCK_INFO *bp;
403 	daddr_t	*daddrp;
404 	ino_t inum;
405 	int i;
406 
407 	if (sp->ss_ninos <= 0)
408 		return;
409 
410 	bp = bip + *countp;
411 	lfsp = &fsp->fi_lfs;
412 #ifdef VERBOSE
413 	(void) printf("INODES:\n");
414 #endif
415 	daddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE);
416 	for (i = 0; i < sp->ss_ninos; ++i) {
417 		if (i % INOPB(lfsp) == 0) {
418 			--daddrp;
419 			di = (struct dinode *)(seg_buf +
420 			    ((*daddrp - seg_addr) << fsp->fi_daddr_shift));
421 		} else
422 			++di;
423 
424 		inum = di->di_inumber;
425 		bp->bi_lbn = LFS_UNUSED_LBN;
426 		bp->bi_inode = inum;
427 		bp->bi_daddr = *daddrp;
428 		bp->bi_bp = di;
429 		bp->bi_segcreate = sp->ss_create;
430 
431 		if (inum == LFS_IFILE_INUM) {
432 			bp->bi_version = 1;	/* Ifile version should be 1 */
433 			bp++;
434 			++(*countp);
435 			PRINT_INODE(1, bp);
436 		} else {
437 			ifp = IFILE_ENTRY(lfsp, fsp->fi_ifilep, inum);
438 			PRINT_INODE(ifp->if_daddr == *daddrp, bp);
439 			bp->bi_version = ifp->if_version;
440 			if (ifp->if_daddr == *daddrp) {
441 				bp++;
442 				++(*countp);
443 			}
444 		}
445 	}
446 }
447 
448 /*
449  * Checks the summary checksum and the data checksum to determine if the
450  * segment is valid or not.  Returns the size of the partial segment if it
451  * is valid, * and 0 otherwise.  Use dump_summary to figure out size of the
452  * the partial as well as whether or not the checksum is valid.
453  */
454 int
455 pseg_valid (fsp, ssp)
456 	FS_INFO *fsp;   /* pointer to file system info */
457 	SEGSUM *ssp;	/* pointer to segment summary block */
458 {
459 	caddr_t	p;
460 	int i, nblocks;
461 	u_long *datap;
462 
463 	if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0 ||
464 	    nblocks > fsp->fi_lfs.lfs_ssize - 1)
465 		return(0);
466 
467 	/* check data/inode block(s) checksum too */
468 	datap = (u_long *)malloc(nblocks * sizeof(u_long));
469 	p = (caddr_t)ssp + LFS_SUMMARY_SIZE;
470 	for (i = 0; i < nblocks; ++i) {
471 		datap[i] = *((u_long *)p);
472 		p += fsp->fi_lfs.lfs_bsize;
473 	}
474 	if (cksum ((void *)datap, nblocks * sizeof(u_long)) != ssp->ss_datasum)
475 		return (0);
476 
477 	return (nblocks);
478 }
479 
480 
481 /* #define MMAP_SEGMENT */
482 /*
483  * read a segment into a memory buffer
484  */
485 int
486 mmap_segment (fsp, segment, segbuf, use_mmap)
487 	FS_INFO *fsp;		/* file system information */
488 	int segment;		/* segment number */
489 	caddr_t *segbuf;	/* pointer to buffer area */
490 	int use_mmap;		/* mmap instead of read */
491 {
492 	struct lfs *lfsp;
493 	int fid;		/* fildes for file system device */
494 	daddr_t seg_daddr;	/* base disk address of segment */
495 	off_t seg_byte;
496 	size_t ssize;
497 	char mntfromname[MNAMELEN+2];
498 
499 	lfsp = &fsp->fi_lfs;
500 
501 	/* get the disk address of the beginning of the segment */
502 	seg_daddr = sntoda(lfsp, segment);
503 	seg_byte = datobyte(fsp, seg_daddr);
504 	ssize = seg_size(lfsp);
505 
506 	strcpy(mntfromname, "/dev/r");
507 	strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5);
508 
509 	if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) {
510 		err(0, "mmap_segment: bad open");
511 		return (-1);
512 	}
513 
514 	if (use_mmap) {
515 		*segbuf = mmap ((caddr_t)0, seg_size(lfsp), PROT_READ,
516 		    0, fid, seg_byte);
517 		if (*(long *)segbuf < 0) {
518 			err(0, "mmap_segment: mmap failed");
519 			return (NULL);
520 		}
521 	} else {
522 #ifdef VERBOSE
523 		printf("mmap_segment\tseg_daddr: %lu\tseg_size: %lu\tseg_offset: %qu\n",
524 		    seg_daddr, ssize, seg_byte);
525 #endif
526 		/* malloc the space for the buffer */
527 		*segbuf = malloc(ssize);
528 		if (!*segbuf) {
529 			err(0, "mmap_segment: malloc failed");
530 			return(NULL);
531 		}
532 
533 		/* read the segment data into the buffer */
534 		if (lseek (fid, seg_byte, SEEK_SET) != seg_byte) {
535 			err (0, "mmap_segment: bad lseek");
536 			free(*segbuf);
537 			return (-1);
538 		}
539 
540 		if (read (fid, *segbuf, ssize) != ssize) {
541 			err (0, "mmap_segment: bad read");
542 			free(*segbuf);
543 			return (-1);
544 		}
545 	}
546 	close (fid);
547 
548 	return (0);
549 }
550 
551 void
552 munmap_segment (fsp, seg_buf, use_mmap)
553 	FS_INFO *fsp;		/* file system information */
554 	caddr_t seg_buf;	/* pointer to buffer area */
555 	int use_mmap;		/* mmap instead of read/write */
556 {
557 	if (use_mmap)
558 		munmap (seg_buf, seg_size(&fsp->fi_lfs));
559 	else
560 		free (seg_buf);
561 }
562 
563 
564 /*
565  * USEFUL DEBUGGING TOOLS:
566  */
567 void
568 print_SEGSUM (lfsp, p)
569 	struct lfs *lfsp;
570 	SEGSUM	*p;
571 {
572 	if (p)
573 		(void) dump_summary(lfsp, p, DUMP_ALL, NULL);
574 	else printf("0x0");
575 	fflush(stdout);
576 }
577 
578 int
579 bi_compare(a, b)
580 	const void *a;
581 	const void *b;
582 {
583 	const BLOCK_INFO *ba, *bb;
584 	int diff;
585 
586 	ba = a;
587 	bb = b;
588 
589 	if (diff = (int)(ba->bi_inode - bb->bi_inode))
590 		return (diff);
591 	if (diff = (int)(ba->bi_lbn - bb->bi_lbn)) {
592 		if (ba->bi_lbn == LFS_UNUSED_LBN)
593 			return(-1);
594 		else if (bb->bi_lbn == LFS_UNUSED_LBN)
595 			return(1);
596 		else if (ba->bi_lbn < 0 && bb->bi_lbn >= 0)
597 			return(1);
598 		else if (bb->bi_lbn < 0 && ba->bi_lbn >= 0)
599 			return(-1);
600 		else
601 			return (diff);
602 	}
603 	if (diff = (int)(ba->bi_segcreate - bb->bi_segcreate))
604 		return (diff);
605 	diff = (int)(ba->bi_daddr - bb->bi_daddr);
606 	return (diff);
607 }
608 
609 int
610 bi_toss(dummy, a, b)
611 	const void *dummy;
612 	const void *a;
613 	const void *b;
614 {
615 	const BLOCK_INFO *ba, *bb;
616 
617 	ba = a;
618 	bb = b;
619 
620 	return(ba->bi_inode == bb->bi_inode && ba->bi_lbn == bb->bi_lbn);
621 }
622 
623 void
624 toss(p, nump, size, dotoss, client)
625 	void *p;
626 	int *nump;
627 	size_t size;
628 	int (*dotoss) __P((const void *, const void *, const void *));
629 	void *client;
630 {
631 	int i;
632 	void *p1;
633 
634 	if (*nump == 0)
635 		return;
636 
637 	for (i = *nump; --i > 0;) {
638 		p1 = p + size;
639 		if (dotoss(client, p, p1)) {
640 			memmove(p, p1, i * size);
641 			--(*nump);
642 		} else
643 			p += size;
644 	}
645 }
646