xref: /netbsd-src/libexec/lfs_cleanerd/lfs_cleanerd.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: lfs_cleanerd.c,v 1.37 2013/10/19 17:19:30 christos Exp $	 */
2 
3 /*-
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Konrad E. Schroder <perseant@hhhh.org>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * The cleaner daemon for the NetBSD Log-structured File System.
34  * Only tested for use with version 2 LFSs.
35  */
36 
37 #include <sys/syslog.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <ufs/lfs/lfs.h>
42 
43 #include <assert.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <semaphore.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <time.h>
53 #include <util.h>
54 
55 #include "bufcache.h"
56 #include "vnode.h"
57 #include "lfs_user.h"
58 #include "fdfs.h"
59 #include "cleaner.h"
60 #include "kernelops.h"
61 #include "mount_lfs.h"
62 
63 /*
64  * Global variables.
65  */
66 /* XXX these top few should really be fs-specific */
67 int use_fs_idle;	/* Use fs idle rather than cpu idle time */
68 int use_bytes;		/* Use bytes written rather than segments cleaned */
69 double load_threshold;	/* How idle is idle (CPU idle) */
70 int atatime;		/* How many segments (bytes) to clean at a time */
71 
72 int nfss;		/* Number of filesystems monitored by this cleanerd */
73 struct clfs **fsp;	/* Array of extended filesystem structures */
74 int segwait_timeout;	/* Time to wait in lfs_segwait() */
75 int do_quit;		/* Quit after one cleaning loop */
76 int do_coalesce;	/* Coalesce filesystem */
77 int do_small;		/* Use small writes through markv */
78 char *copylog_filename; /* File to use for fs debugging analysis */
79 int inval_segment;	/* Segment to invalidate */
80 int stat_report;	/* Report statistics for this period of cycles */
81 int debug;		/* Turn on debugging */
82 struct cleaner_stats {
83 	double	util_tot;
84 	double	util_sos;
85 	off_t	bytes_read;
86 	off_t	bytes_written;
87 	off_t	segs_cleaned;
88 	off_t	segs_empty;
89 	off_t	segs_error;
90 } cleaner_stats;
91 
92 extern u_int32_t cksum(void *, size_t);
93 extern u_int32_t lfs_sb_cksum(struct dlfs *);
94 extern u_int32_t lfs_cksum_part(void *, size_t, u_int32_t);
95 extern int ulfs_getlbns(struct lfs *, struct uvnode *, daddr_t, struct indir *, int *);
96 
97 /* Compat */
98 void pwarn(const char *unused, ...) { /* Does nothing */ };
99 
100 /*
101  * Log a message if debugging is turned on.
102  */
103 void
104 dlog(const char *fmt, ...)
105 {
106 	va_list ap;
107 
108 	if (debug == 0)
109 		return;
110 
111 	va_start(ap, fmt);
112 	vsyslog(LOG_DEBUG, fmt, ap);
113 	va_end(ap);
114 }
115 
116 /*
117  * Remove the specified filesystem from the list, due to its having
118  * become unmounted or other error condition.
119  */
120 void
121 handle_error(struct clfs **cfsp, int n)
122 {
123 	syslog(LOG_NOTICE, "%s: detaching cleaner", cfsp[n]->lfs_fsmnt);
124 	free(cfsp[n]);
125 	if (n != nfss - 1)
126 		cfsp[n] = cfsp[nfss - 1];
127 	--nfss;
128 }
129 
130 /*
131  * Reinitialize a filesystem if, e.g., its size changed.
132  */
133 int
134 reinit_fs(struct clfs *fs)
135 {
136 	char fsname[MNAMELEN];
137 
138 	strncpy(fsname, (char *)fs->lfs_fsmnt, MNAMELEN);
139 	kops.ko_close(fs->clfs_ifilefd);
140 	kops.ko_close(fs->clfs_devfd);
141 	fd_reclaim(fs->clfs_devvp);
142 	fd_reclaim(fs->lfs_ivnode);
143 	free(fs->clfs_dev);
144 	free(fs->clfs_segtab);
145 	free(fs->clfs_segtabp);
146 
147 	return init_fs(fs, fsname);
148 }
149 
150 #ifdef REPAIR_ZERO_FINFO
151 /*
152  * Use fsck's lfs routines to load the Ifile from an unmounted fs.
153  * We interpret "fsname" as the name of the raw disk device.
154  */
155 int
156 init_unmounted_fs(struct clfs *fs, char *fsname)
157 {
158 	struct lfs *disc_fs;
159 	int i;
160 
161 	fs->clfs_dev = fsname;
162 	if ((fs->clfs_devfd = kops.ko_open(fs->clfs_dev, O_RDWR)) < 0) {
163 		syslog(LOG_ERR, "couldn't open device %s read/write",
164 		       fs->clfs_dev);
165 		return -1;
166 	}
167 
168 	disc_fs = lfs_init(fs->clfs_devfd, 0, 0, 0, 0);
169 
170 	fs->lfs_dlfs = disc_fs->lfs_dlfs; /* Structure copy */
171 	strncpy(fs->lfs_fsmnt, fsname, MNAMELEN);
172 	fs->lfs_ivnode = (struct uvnode *)disc_fs->lfs_ivnode;
173 	fs->clfs_devvp = fd_vget(fs->clfs_devfd, fs->lfs_fsize, fs->lfs_ssize,
174 				 atatime);
175 
176 	/* Allocate and clear segtab */
177 	fs->clfs_segtab = (struct clfs_seguse *)malloc(fs->lfs_nseg *
178 						sizeof(*fs->clfs_segtab));
179 	fs->clfs_segtabp = (struct clfs_seguse **)malloc(fs->lfs_nseg *
180 						sizeof(*fs->clfs_segtabp));
181 	for (i = 0; i < fs->lfs_nseg; i++) {
182 		fs->clfs_segtabp[i] = &(fs->clfs_segtab[i]);
183 		fs->clfs_segtab[i].flags = 0x0;
184 	}
185 	syslog(LOG_NOTICE, "%s: unmounted cleaner starting", fsname);
186 
187 	return 0;
188 }
189 #endif
190 
191 /*
192  * Set up the file descriptors, including the Ifile descriptor.
193  * If we can't get the Ifile, this is not an LFS (or the kernel is
194  * too old to support the fcntl).
195  * XXX Merge this and init_unmounted_fs, switching on whether
196  * XXX "fsname" is a dir or a char special device.  Should
197  * XXX also be able to read unmounted devices out of fstab, the way
198  * XXX fsck does.
199  */
200 int
201 init_fs(struct clfs *fs, char *fsname)
202 {
203 	struct statvfs sf;
204 	int rootfd;
205 	int i;
206 	void *sbuf;
207 	char *bn;
208 
209 	/*
210 	 * Get the raw device from the block device.
211 	 * XXX this is ugly.  Is there a way to discover the raw device
212 	 * XXX for a given mount point?
213 	 */
214 	if (kops.ko_statvfs(fsname, &sf, ST_WAIT) < 0)
215 		return -1;
216 	fs->clfs_dev = malloc(strlen(sf.f_mntfromname) + 2);
217 	if (fs->clfs_dev == NULL) {
218 		syslog(LOG_ERR, "couldn't malloc device name string: %m");
219 		return -1;
220 	}
221 	bn = strrchr(sf.f_mntfromname, '/');
222 	bn = bn ? bn+1 : sf.f_mntfromname;
223 	strlcpy(fs->clfs_dev, sf.f_mntfromname, bn - sf.f_mntfromname + 1);
224 	strcat(fs->clfs_dev, "r");
225 	strcat(fs->clfs_dev, bn);
226 	if ((fs->clfs_devfd = kops.ko_open(fs->clfs_dev, O_RDONLY, 0)) < 0) {
227 		syslog(LOG_ERR, "couldn't open device %s for reading",
228 			fs->clfs_dev);
229 		return -1;
230 	}
231 
232 	/* Find the Ifile and open it */
233 	if ((rootfd = kops.ko_open(fsname, O_RDONLY, 0)) < 0)
234 		return -2;
235 	if (kops.ko_fcntl(rootfd, LFCNIFILEFH, &fs->clfs_ifilefh) < 0)
236 		return -3;
237 	if ((fs->clfs_ifilefd = kops.ko_fhopen(&fs->clfs_ifilefh,
238 	    sizeof(fs->clfs_ifilefh), O_RDONLY)) < 0)
239 		return -4;
240 	kops.ko_close(rootfd);
241 
242 	sbuf = malloc(LFS_SBPAD);
243 	if (sbuf == NULL) {
244 		syslog(LOG_ERR, "couldn't malloc superblock buffer");
245 		return -1;
246 	}
247 
248 	/* Load in the superblock */
249 	if (kops.ko_pread(fs->clfs_devfd, sbuf, LFS_SBPAD, LFS_LABELPAD) < 0) {
250 		free(sbuf);
251 		return -1;
252 	}
253 
254 	memcpy(&(fs->lfs_dlfs), sbuf, sizeof(struct dlfs));
255 	free(sbuf);
256 
257 	/* If this is not a version 2 filesystem, complain and exit */
258 	if (fs->lfs_version != 2) {
259 		syslog(LOG_ERR, "%s: not a version 2 LFS", fsname);
260 		return -1;
261 	}
262 
263 	/* Assume fsname is the mounted name */
264 	strncpy((char *)fs->lfs_fsmnt, fsname, MNAMELEN);
265 
266 	/* Set up vnodes for Ifile and raw device */
267 	fs->lfs_ivnode = fd_vget(fs->clfs_ifilefd, fs->lfs_bsize, 0, 0);
268 	fs->clfs_devvp = fd_vget(fs->clfs_devfd, fs->lfs_fsize, fs->lfs_ssize,
269 				 atatime);
270 
271 	/* Allocate and clear segtab */
272 	fs->clfs_segtab = (struct clfs_seguse *)malloc(fs->lfs_nseg *
273 						sizeof(*fs->clfs_segtab));
274 	fs->clfs_segtabp = (struct clfs_seguse **)malloc(fs->lfs_nseg *
275 						sizeof(*fs->clfs_segtabp));
276 	if (fs->clfs_segtab == NULL || fs->clfs_segtabp == NULL) {
277 		syslog(LOG_ERR, "%s: couldn't malloc segment table: %m",
278 			fs->clfs_dev);
279 		return -1;
280 	}
281 
282 	for (i = 0; i < fs->lfs_nseg; i++) {
283 		fs->clfs_segtabp[i] = &(fs->clfs_segtab[i]);
284 		fs->clfs_segtab[i].flags = 0x0;
285 	}
286 
287 	syslog(LOG_NOTICE, "%s: attaching cleaner", fsname);
288 	return 0;
289 }
290 
291 /*
292  * Invalidate all the currently held Ifile blocks so they will be
293  * reread when we clean.  Check the size while we're at it, and
294  * resize the buffer cache if necessary.
295  */
296 void
297 reload_ifile(struct clfs *fs)
298 {
299 	struct ubuf *bp;
300 	struct stat st;
301 	int ohashmax;
302 	extern int hashmax;
303 
304 	while ((bp = LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd)) != NULL) {
305 		bremfree(bp);
306 		buf_destroy(bp);
307 	}
308 	while ((bp = LIST_FIRST(&fs->lfs_ivnode->v_cleanblkhd)) != NULL) {
309 		bremfree(bp);
310 		buf_destroy(bp);
311 	}
312 
313 	/* If Ifile is larger than buffer cache, rehash */
314 	fstat(fs->clfs_ifilefd, &st);
315 	if (st.st_size / fs->lfs_bsize > hashmax) {
316 		ohashmax = hashmax;
317 		bufrehash(st.st_size / fs->lfs_bsize);
318 		dlog("%s: resized buffer hash from %d to %d",
319 		     fs->lfs_fsmnt, ohashmax, hashmax);
320 	}
321 }
322 
323 /*
324  * Get IFILE entry for the given inode, store in ifpp.	The buffer
325  * which contains that data is returned in bpp, and must be brelse()d
326  * by the caller.
327  */
328 void
329 lfs_ientry(IFILE **ifpp, struct clfs *fs, ino_t ino, struct ubuf **bpp)
330 {
331 	int error;
332 
333 	error = bread(fs->lfs_ivnode, ino / fs->lfs_ifpb + fs->lfs_cleansz +
334 		      fs->lfs_segtabsz, fs->lfs_bsize, NOCRED, 0, bpp);
335 	if (error)
336 		syslog(LOG_ERR, "%s: ientry failed for ino %d",
337 			fs->lfs_fsmnt, (int)ino);
338 	*ifpp = (IFILE *)(*bpp)->b_data + ino % fs->lfs_ifpb;
339 	return;
340 }
341 
342 #ifdef TEST_PATTERN
343 /*
344  * Check ULFS_ROOTINO for file data.  The assumption is that we are running
345  * the "twofiles" test with the rest of the filesystem empty.  Files
346  * created by "twofiles" match the test pattern, but ULFS_ROOTINO and the
347  * executable itself (assumed to be inode 3) should not match.
348  */
349 static void
350 check_test_pattern(BLOCK_INFO *bip)
351 {
352 	int j;
353 	unsigned char *cp = bip->bi_bp;
354 
355 	/* Check inode sanity */
356 	if (bip->bi_lbn == LFS_UNUSED_LBN) {
357 		assert(((struct ulfs1_dinode *)bip->bi_bp)->di_inumber ==
358 			bip->bi_inode);
359 	}
360 
361 	/* These can have the test pattern and it's all good */
362 	if (bip->bi_inode > 3)
363 		return;
364 
365 	for (j = 0; j < bip->bi_size; j++) {
366 		if (cp[j] != (j & 0xff))
367 			break;
368 	}
369 	assert(j < bip->bi_size);
370 }
371 #endif /* TEST_PATTERN */
372 
373 /*
374  * Parse the partial segment at daddr, adding its information to
375  * bip.	 Return the address of the next partial segment to read.
376  */
377 int32_t
378 parse_pseg(struct clfs *fs, daddr_t daddr, BLOCK_INFO **bipp, int *bic)
379 {
380 	SEGSUM *ssp;
381 	IFILE *ifp;
382 	BLOCK_INFO *bip, *nbip;
383 	int32_t *iaddrp, idaddr, odaddr;
384 	FINFO *fip;
385 	struct ubuf *ifbp;
386 	struct ulfs1_dinode *dip;
387 	u_int32_t ck, vers;
388 	int fic, inoc, obic;
389 	int i;
390 	char *cp;
391 
392 	odaddr = daddr;
393 	obic = *bic;
394 	bip = *bipp;
395 
396 	/*
397 	 * Retrieve the segment header, set up the SEGSUM pointer
398 	 * as well as the first FINFO and inode address pointer.
399 	 */
400 	cp = fd_ptrget(fs->clfs_devvp, daddr);
401 	ssp = (SEGSUM *)cp;
402 	iaddrp = ((int32_t *)(cp + fs->lfs_ibsize)) - 1;
403 	fip = (FINFO *)(cp + sizeof(SEGSUM));
404 
405 	/*
406 	 * Check segment header magic and checksum
407 	 */
408 	if (ssp->ss_magic != SS_MAGIC) {
409 		syslog(LOG_WARNING, "%s: sumsum magic number bad at 0x%x:"
410 		       " read 0x%x, expected 0x%x", fs->lfs_fsmnt,
411 		       (int32_t)daddr, ssp->ss_magic, SS_MAGIC);
412 		return 0x0;
413 	}
414 	ck = cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
415 	if (ck != ssp->ss_sumsum) {
416 		syslog(LOG_WARNING, "%s: sumsum checksum mismatch at 0x%x:"
417 		       " read 0x%x, computed 0x%x", fs->lfs_fsmnt,
418 		       (int32_t)daddr, ssp->ss_sumsum, ck);
419 		return 0x0;
420 	}
421 
422 	/* Initialize data sum */
423 	ck = 0;
424 
425 	/* Point daddr at next block after segment summary */
426 	++daddr;
427 
428 	/*
429 	 * Loop over file info and inode pointers.  We always move daddr
430 	 * forward here because we are also computing the data checksum
431 	 * as we go.
432 	 */
433 	fic = inoc = 0;
434 	while (fic < ssp->ss_nfinfo || inoc < ssp->ss_ninos) {
435 		/*
436 		 * We must have either a file block or an inode block.
437 		 * If we don't have either one, it's an error.
438 		 */
439 		if (fic >= ssp->ss_nfinfo && *iaddrp != daddr) {
440 			syslog(LOG_WARNING, "%s: bad pseg at %x (seg %d)",
441 			       fs->lfs_fsmnt, odaddr, lfs_dtosn(fs, odaddr));
442 			*bipp = bip;
443 			return 0x0;
444 		}
445 
446 		/*
447 		 * Note each inode from the inode blocks
448 		 */
449 		if (inoc < ssp->ss_ninos && *iaddrp == daddr) {
450 			cp = fd_ptrget(fs->clfs_devvp, daddr);
451 			ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
452 			dip = (struct ulfs1_dinode *)cp;
453 			for (i = 0; i < fs->lfs_inopb; i++) {
454 				if (dip[i].di_inumber == 0)
455 					break;
456 
457 				/*
458 				 * Check currency before adding it
459 				 */
460 #ifndef REPAIR_ZERO_FINFO
461 				lfs_ientry(&ifp, fs, dip[i].di_inumber, &ifbp);
462 				idaddr = ifp->if_daddr;
463 				brelse(ifbp, 0);
464 				if (idaddr != daddr)
465 #endif
466 					continue;
467 
468 				/*
469 				 * A current inode.  Add it.
470 				 */
471 				++*bic;
472 				nbip = (BLOCK_INFO *)realloc(bip, *bic *
473 							     sizeof(*bip));
474 				if (nbip)
475 					bip = nbip;
476 				else {
477 					--*bic;
478 					*bipp = bip;
479 					return 0x0;
480 				}
481 				bip[*bic - 1].bi_inode = dip[i].di_inumber;
482 				bip[*bic - 1].bi_lbn = LFS_UNUSED_LBN;
483 				bip[*bic - 1].bi_daddr = daddr;
484 				bip[*bic - 1].bi_segcreate = ssp->ss_create;
485 				bip[*bic - 1].bi_version = dip[i].di_gen;
486 				bip[*bic - 1].bi_bp = &(dip[i]);
487 				bip[*bic - 1].bi_size = LFS_DINODE1_SIZE;
488 			}
489 			inoc += i;
490 			daddr += lfs_btofsb(fs, fs->lfs_ibsize);
491 			--iaddrp;
492 			continue;
493 		}
494 
495 		/*
496 		 * Note each file block from the finfo blocks
497 		 */
498 		if (fic >= ssp->ss_nfinfo)
499 			continue;
500 
501 		/* Count this finfo, whether or not we use it */
502 		++fic;
503 
504 		/*
505 		 * If this finfo has nblocks==0, it was written wrong.
506 		 * Kernels with this problem always wrote this zero-sized
507 		 * finfo last, so just ignore it.
508 		 */
509 		if (fip->fi_nblocks == 0) {
510 #ifdef REPAIR_ZERO_FINFO
511 			struct ubuf *nbp;
512 			SEGSUM *nssp;
513 
514 			syslog(LOG_WARNING, "fixing short FINFO at %x (seg %d)",
515 			       odaddr, lfs_dtosn(fs, odaddr));
516 			bread(fs->clfs_devvp, odaddr, fs->lfs_fsize,
517 			    NOCRED, 0, &nbp);
518 			nssp = (SEGSUM *)nbp->b_data;
519 			--nssp->ss_nfinfo;
520 			nssp->ss_sumsum = cksum(&nssp->ss_datasum,
521 				fs->lfs_sumsize - sizeof(nssp->ss_sumsum));
522 			bwrite(nbp);
523 #endif
524 			syslog(LOG_WARNING, "zero-length FINFO at %x (seg %d)",
525 			       odaddr, lfs_dtosn(fs, odaddr));
526 			continue;
527 		}
528 
529 		/*
530 		 * Check currency before adding blocks
531 		 */
532 #ifdef REPAIR_ZERO_FINFO
533 		vers = -1;
534 #else
535 		lfs_ientry(&ifp, fs, fip->fi_ino, &ifbp);
536 		vers = ifp->if_version;
537 		brelse(ifbp, 0);
538 #endif
539 		if (vers != fip->fi_version) {
540 			size_t size;
541 
542 			/* Read all the blocks from the data summary */
543 			for (i = 0; i < fip->fi_nblocks; i++) {
544 				size = (i == fip->fi_nblocks - 1) ?
545 					fip->fi_lastlength : fs->lfs_bsize;
546 				cp = fd_ptrget(fs->clfs_devvp, daddr);
547 				ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
548 				daddr += lfs_btofsb(fs, size);
549 			}
550 			fip = (FINFO *)(fip->fi_blocks + fip->fi_nblocks);
551 			continue;
552 		}
553 
554 		/* Add all the blocks from the finfos (current or not) */
555 		nbip = (BLOCK_INFO *)realloc(bip, (*bic + fip->fi_nblocks) *
556 					     sizeof(*bip));
557 		if (nbip)
558 			bip = nbip;
559 		else {
560 			*bipp = bip;
561 			return 0x0;
562 		}
563 
564 		for (i = 0; i < fip->fi_nblocks; i++) {
565 			bip[*bic + i].bi_inode = fip->fi_ino;
566 			bip[*bic + i].bi_lbn = fip->fi_blocks[i];
567 			bip[*bic + i].bi_daddr = daddr;
568 			bip[*bic + i].bi_segcreate = ssp->ss_create;
569 			bip[*bic + i].bi_version = fip->fi_version;
570 			bip[*bic + i].bi_size = (i == fip->fi_nblocks - 1) ?
571 				fip->fi_lastlength : fs->lfs_bsize;
572 			cp = fd_ptrget(fs->clfs_devvp, daddr);
573 			ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
574 			bip[*bic + i].bi_bp = cp;
575 			daddr += lfs_btofsb(fs, bip[*bic + i].bi_size);
576 
577 #ifdef TEST_PATTERN
578 			check_test_pattern(bip + *bic + i); /* XXXDEBUG */
579 #endif
580 		}
581 		*bic += fip->fi_nblocks;
582 		fip = (FINFO *)(fip->fi_blocks + fip->fi_nblocks);
583 	}
584 
585 #ifndef REPAIR_ZERO_FINFO
586 	if (ssp->ss_datasum != ck) {
587 		syslog(LOG_WARNING, "%s: data checksum bad at 0x%x:"
588 		       " read 0x%x, computed 0x%x", fs->lfs_fsmnt, odaddr,
589 		       ssp->ss_datasum, ck);
590 		*bic = obic;
591 		return 0x0;
592 	}
593 #endif
594 
595 	*bipp = bip;
596 	return daddr;
597 }
598 
599 static void
600 log_segment_read(struct clfs *fs, int sn)
601 {
602         FILE *fp;
603 	char *cp;
604 
605         /*
606          * Write the segment read, and its contents, into a log file in
607          * the current directory.  We don't need to log the location of
608          * the segment, since that can be inferred from the segments up
609 	 * to this point (ss_nextseg field of the previously written segment).
610 	 *
611 	 * We can use this info later to reconstruct the filesystem at any
612 	 * given point in time for analysis, by replaying the log forward
613 	 * indexed by the segment serial numbers; but it is not suitable
614 	 * for everyday use since the copylog will be simply enormous.
615          */
616 	cp = fd_ptrget(fs->clfs_devvp, lfs_sntod(fs, sn));
617 
618         fp = fopen(copylog_filename, "ab");
619         if (fp != NULL) {
620                 if (fwrite(cp, (size_t)fs->lfs_ssize, 1, fp) != 1) {
621                         perror("writing segment to copy log");
622                 }
623         }
624         fclose(fp);
625 }
626 
627 /*
628  * Read a segment to populate the BLOCK_INFO structures.
629  * Return the number of partial segments read and parsed.
630  */
631 int
632 load_segment(struct clfs *fs, int sn, BLOCK_INFO **bipp, int *bic)
633 {
634 	int32_t daddr;
635 	int i, npseg;
636 
637 	daddr = lfs_sntod(fs, sn);
638 	if (daddr < lfs_btofsb(fs, LFS_LABELPAD))
639 		daddr = lfs_btofsb(fs, LFS_LABELPAD);
640 	for (i = 0; i < LFS_MAXNUMSB; i++) {
641 		if (fs->lfs_sboffs[i] == daddr) {
642 			daddr += lfs_btofsb(fs, LFS_SBPAD);
643 			break;
644 		}
645 	}
646 
647 	/* Preload the segment buffer */
648 	if (fd_preload(fs->clfs_devvp, lfs_sntod(fs, sn)) < 0)
649 		return -1;
650 
651 	if (copylog_filename)
652 		log_segment_read(fs, sn);
653 
654 	/* Note bytes read for stats */
655 	cleaner_stats.segs_cleaned++;
656 	cleaner_stats.bytes_read += fs->lfs_ssize;
657 	++fs->clfs_nactive;
658 
659 	npseg = 0;
660 	while(lfs_dtosn(fs, daddr) == sn &&
661 	      lfs_dtosn(fs, daddr + lfs_btofsb(fs, fs->lfs_bsize)) == sn) {
662 		daddr = parse_pseg(fs, daddr, bipp, bic);
663 		if (daddr == 0x0) {
664 			++cleaner_stats.segs_error;
665 			break;
666 		}
667 		++npseg;
668 	}
669 
670 	return npseg;
671 }
672 
673 void
674 calc_cb(struct clfs *fs, int sn, struct clfs_seguse *t)
675 {
676 	time_t now;
677 	int64_t age, benefit, cost;
678 
679 	time(&now);
680 	age = (now < t->lastmod ? 0 : now - t->lastmod);
681 
682 	/* Under no circumstances clean active or already-clean segments */
683 	if ((t->flags & SEGUSE_ACTIVE) || !(t->flags & SEGUSE_DIRTY)) {
684 		t->priority = 0;
685 		return;
686 	}
687 
688 	/*
689 	 * If the segment is empty, there is no reason to clean it.
690 	 * Clear its error condition, if any, since we are never going to
691 	 * try to parse this one.
692 	 */
693 	if (t->nbytes == 0) {
694 		t->flags &= ~SEGUSE_ERROR; /* Strip error once empty */
695 		t->priority = 0;
696 		return;
697 	}
698 
699 	if (t->flags & SEGUSE_ERROR) {	/* No good if not already empty */
700 		/* No benefit */
701 		t->priority = 0;
702 		return;
703 	}
704 
705 	if (t->nbytes > fs->lfs_ssize) {
706 		/* Another type of error */
707 		syslog(LOG_WARNING, "segment %d: bad seguse count %d",
708 		       sn, t->nbytes);
709 		t->flags |= SEGUSE_ERROR;
710 		t->priority = 0;
711 		return;
712 	}
713 
714 	/*
715 	 * The non-degenerate case.  Use Rosenblum's cost-benefit algorithm.
716 	 * Calculate the benefit from cleaning this segment (one segment,
717 	 * minus fragmentation, dirty blocks and a segment summary block)
718 	 * and weigh that against the cost (bytes read plus bytes written).
719 	 * We count the summary headers as "dirty" to avoid cleaning very
720 	 * old and very full segments.
721 	 */
722 	benefit = (int64_t)fs->lfs_ssize - t->nbytes -
723 		  (t->nsums + 1) * fs->lfs_fsize;
724 	if (fs->lfs_bsize > fs->lfs_fsize) /* fragmentation */
725 		benefit -= (fs->lfs_bsize / 2);
726 	if (benefit <= 0) {
727 		t->priority = 0;
728 		return;
729 	}
730 
731 	cost = fs->lfs_ssize + t->nbytes;
732 	t->priority = (256 * benefit * age) / cost;
733 
734 	return;
735 }
736 
737 /*
738  * Comparator for BLOCK_INFO structures.  Anything not in one of the segments
739  * we're looking at sorts higher; after that we sort first by inode number
740  * and then by block number (unsigned, i.e., negative sorts higher) *but*
741  * sort inodes before data blocks.
742  */
743 static int
744 bi_comparator(const void *va, const void *vb)
745 {
746 	const BLOCK_INFO *a, *b;
747 
748 	a = (const BLOCK_INFO *)va;
749 	b = (const BLOCK_INFO *)vb;
750 
751 	/* Check for out-of-place block */
752 	if (a->bi_segcreate == a->bi_daddr &&
753 	    b->bi_segcreate != b->bi_daddr)
754 		return -1;
755 	if (a->bi_segcreate != a->bi_daddr &&
756 	    b->bi_segcreate == b->bi_daddr)
757 		return 1;
758 	if (a->bi_size <= 0 && b->bi_size > 0)
759 		return 1;
760 	if (b->bi_size <= 0 && a->bi_size > 0)
761 		return -1;
762 
763 	/* Check inode number */
764 	if (a->bi_inode != b->bi_inode)
765 		return a->bi_inode - b->bi_inode;
766 
767 	/* Check lbn */
768 	if (a->bi_lbn == LFS_UNUSED_LBN) /* Inodes sort lower than blocks */
769 		return -1;
770 	if (b->bi_lbn == LFS_UNUSED_LBN)
771 		return 1;
772 	if ((u_int32_t)a->bi_lbn > (u_int32_t)b->bi_lbn)
773 		return 1;
774 	else
775 		return -1;
776 
777 	return 0;
778 }
779 
780 /*
781  * Comparator for sort_segments: cost-benefit equation.
782  */
783 static int
784 cb_comparator(const void *va, const void *vb)
785 {
786 	const struct clfs_seguse *a, *b;
787 
788 	a = *(const struct clfs_seguse * const *)va;
789 	b = *(const struct clfs_seguse * const *)vb;
790 	return a->priority > b->priority ? -1 : 1;
791 }
792 
793 void
794 toss_old_blocks(struct clfs *fs, BLOCK_INFO **bipp, int *bic, int *sizep)
795 {
796 	int i, r;
797 	BLOCK_INFO *bip = *bipp;
798 	struct lfs_fcntl_markv /* {
799 		BLOCK_INFO *blkiov;
800 		int blkcnt;
801 	} */ lim;
802 
803 	if (bic == 0 || bip == NULL)
804 		return;
805 
806 	/*
807 	 * Kludge: Store the disk address in segcreate so we know which
808 	 * ones to toss.
809 	 */
810 	for (i = 0; i < *bic; i++)
811 		bip[i].bi_segcreate = bip[i].bi_daddr;
812 
813 	/* Sort the blocks */
814 	heapsort(bip, *bic, sizeof(BLOCK_INFO), bi_comparator);
815 
816 	/* Use bmapv to locate the blocks */
817 	lim.blkiov = bip;
818 	lim.blkcnt = *bic;
819 	if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNBMAPV, &lim)) < 0) {
820 		syslog(LOG_WARNING, "%s: bmapv returned %d (%m)",
821 		       fs->lfs_fsmnt, r);
822 		return;
823 	}
824 
825 	/* Toss blocks not in this segment */
826 	heapsort(bip, *bic, sizeof(BLOCK_INFO), bi_comparator);
827 
828 	/* Get rid of stale blocks */
829 	if (sizep)
830 		*sizep = 0;
831 	for (i = 0; i < *bic; i++) {
832 		if (bip[i].bi_segcreate != bip[i].bi_daddr)
833 			break;
834 		if (sizep)
835 			*sizep += bip[i].bi_size;
836 	}
837 	*bic = i; /* XXX should we shrink bip? */
838 	*bipp = bip;
839 
840 	return;
841 }
842 
843 /*
844  * Clean a segment and mark it invalid.
845  */
846 int
847 invalidate_segment(struct clfs *fs, int sn)
848 {
849 	BLOCK_INFO *bip;
850 	int i, r, bic;
851 	off_t nb;
852 	double util;
853 	struct lfs_fcntl_markv /* {
854 		BLOCK_INFO *blkiov;
855 		int blkcnt;
856 	} */ lim;
857 
858 	dlog("%s: inval seg %d", fs->lfs_fsmnt, sn);
859 
860 	bip = NULL;
861 	bic = 0;
862 	fs->clfs_nactive = 0;
863 	if (load_segment(fs, sn, &bip, &bic) <= 0)
864 		return -1;
865 	toss_old_blocks(fs, &bip, &bic, NULL);
866 
867 	/* Record statistics */
868 	for (i = nb = 0; i < bic; i++)
869 		nb += bip[i].bi_size;
870 	util = ((double)nb) / (fs->clfs_nactive * fs->lfs_ssize);
871 	cleaner_stats.util_tot += util;
872 	cleaner_stats.util_sos += util * util;
873 	cleaner_stats.bytes_written += nb;
874 
875 	/*
876 	 * Use markv to move the blocks.
877 	 */
878 	lim.blkiov = bip;
879 	lim.blkcnt = bic;
880 	if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim)) < 0) {
881 		syslog(LOG_WARNING, "%s: markv returned %d (%m) "
882 		       "for seg %d", fs->lfs_fsmnt, r, sn);
883 		return r;
884 	}
885 
886 	/*
887 	 * Finally call invalidate to invalidate the segment.
888 	 */
889 	if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNINVAL, &sn)) < 0) {
890 		syslog(LOG_WARNING, "%s: inval returned %d (%m) "
891 		       "for seg %d", fs->lfs_fsmnt, r, sn);
892 		return r;
893 	}
894 
895 	return 0;
896 }
897 
898 /*
899  * Check to see if the given ino/lbn pair is represented in the BLOCK_INFO
900  * array we are sending to the kernel, or if the kernel will have to add it.
901  * The kernel will only add each such pair once, though, so keep track of
902  * previous requests in a separate "extra" BLOCK_INFO array.  Returns 1
903  * if the block needs to be added, 0 if it is already represented.
904  */
905 static int
906 check_or_add(ino_t ino, int32_t lbn, BLOCK_INFO *bip, int bic, BLOCK_INFO **ebipp, int *ebicp)
907 {
908 	BLOCK_INFO *t, *ebip = *ebipp;
909 	int ebic = *ebicp;
910 	int k;
911 
912 	for (k = 0; k < bic; k++) {
913 		if (bip[k].bi_inode != ino)
914 			break;
915 		if (bip[k].bi_lbn == lbn) {
916 			return 0;
917 		}
918 	}
919 
920 	/* Look on the list of extra blocks, too */
921 	for (k = 0; k < ebic; k++) {
922 		if (ebip[k].bi_inode == ino && ebip[k].bi_lbn == lbn) {
923 			return 0;
924 		}
925 	}
926 
927 	++ebic;
928 	t = realloc(ebip, ebic * sizeof(BLOCK_INFO));
929 	if (t == NULL)
930 		return 1; /* Note *ebicp is unchanged */
931 
932 	ebip = t;
933 	ebip[ebic - 1].bi_inode = ino;
934 	ebip[ebic - 1].bi_lbn = lbn;
935 
936 	*ebipp = ebip;
937 	*ebicp = ebic;
938 	return 1;
939 }
940 
941 /*
942  * Look for indirect blocks we will have to write which are not
943  * contained in this collection of blocks.  This constitutes
944  * a hidden cleaning cost, since we are unaware of it until we
945  * have already read the segments.  Return the total cost, and fill
946  * in *ifc with the part of that cost due to rewriting the Ifile.
947  */
948 static off_t
949 check_hidden_cost(struct clfs *fs, BLOCK_INFO *bip, int bic, off_t *ifc)
950 {
951 	int start;
952 	struct indir in[ULFS_NIADDR + 1];
953 	int num;
954 	int i, j, ebic;
955 	BLOCK_INFO *ebip;
956 	int32_t lbn;
957 
958 	start = 0;
959 	ebip = NULL;
960 	ebic = 0;
961 	for (i = 0; i < bic; i++) {
962 		if (i == 0 || bip[i].bi_inode != bip[start].bi_inode) {
963 			start = i;
964 			/*
965 			 * Look for IFILE blocks, unless this is the Ifile.
966 			 */
967 			if (bip[i].bi_inode != fs->lfs_ifile) {
968 				lbn = fs->lfs_cleansz + bip[i].bi_inode /
969 							fs->lfs_ifpb;
970 				*ifc += check_or_add(fs->lfs_ifile, lbn,
971 						     bip, bic, &ebip, &ebic);
972 			}
973 		}
974 		if (bip[i].bi_lbn == LFS_UNUSED_LBN)
975 			continue;
976 		if (bip[i].bi_lbn < ULFS_NDADDR)
977 			continue;
978 
979 		ulfs_getlbns((struct lfs *)fs, NULL, (daddr_t)bip[i].bi_lbn, in, &num);
980 		for (j = 0; j < num; j++) {
981 			check_or_add(bip[i].bi_inode, in[j].in_lbn,
982 				     bip + start, bic - start, &ebip, &ebic);
983 		}
984 	}
985 	return ebic;
986 }
987 
988 /*
989  * Select segments to clean, add blocks from these segments to a cleaning
990  * list, and send this list through lfs_markv() to move them to new
991  * locations on disk.
992  */
993 int
994 clean_fs(struct clfs *fs, CLEANERINFO *cip)
995 {
996 	int i, j, ngood, sn, bic, r, npos;
997 	int bytes, totbytes;
998 	struct ubuf *bp;
999 	SEGUSE *sup;
1000 	static BLOCK_INFO *bip;
1001 	struct lfs_fcntl_markv /* {
1002 		BLOCK_INFO *blkiov;
1003 		int blkcnt;
1004 	} */ lim;
1005 	int mc;
1006 	BLOCK_INFO *mbip;
1007 	int inc;
1008 	off_t nb;
1009 	off_t goal;
1010 	off_t extra, if_extra;
1011 	double util;
1012 
1013 	/* Read the segment table into our private structure */
1014 	npos = 0;
1015 	for (i = 0; i < fs->lfs_nseg; i+= fs->lfs_sepb) {
1016 		bread(fs->lfs_ivnode, fs->lfs_cleansz + i / fs->lfs_sepb,
1017 		      fs->lfs_bsize, NOCRED, 0, &bp);
1018 		for (j = 0; j < fs->lfs_sepb && i + j < fs->lfs_nseg; j++) {
1019 			sup = ((SEGUSE *)bp->b_data) + j;
1020 			fs->clfs_segtab[i + j].nbytes  = sup->su_nbytes;
1021 			fs->clfs_segtab[i + j].nsums = sup->su_nsums;
1022 			fs->clfs_segtab[i + j].lastmod = sup->su_lastmod;
1023 			/* Keep error status but renew other flags */
1024 			fs->clfs_segtab[i + j].flags  &= SEGUSE_ERROR;
1025 			fs->clfs_segtab[i + j].flags  |= sup->su_flags;
1026 
1027 			/* Compute cost-benefit coefficient */
1028 			calc_cb(fs, i + j, fs->clfs_segtab + i + j);
1029 			if (fs->clfs_segtab[i + j].priority > 0)
1030 				++npos;
1031 		}
1032 		brelse(bp, 0);
1033 	}
1034 
1035 	/* Sort segments based on cleanliness, fulness, and condition */
1036 	heapsort(fs->clfs_segtabp, fs->lfs_nseg, sizeof(struct clfs_seguse *),
1037 		 cb_comparator);
1038 
1039 	/* If no segment is cleanable, just return */
1040 	if (fs->clfs_segtabp[0]->priority == 0) {
1041 		dlog("%s: no segment cleanable", fs->lfs_fsmnt);
1042 		return 0;
1043 	}
1044 
1045 	/* Load some segments' blocks into bip */
1046 	bic = 0;
1047 	fs->clfs_nactive = 0;
1048 	ngood = 0;
1049 	if (use_bytes) {
1050 		/* Set attainable goal */
1051 		goal = fs->lfs_ssize * atatime;
1052 		if (goal > (cip->clean - 1) * fs->lfs_ssize / 2)
1053 			goal = MAX((cip->clean - 1) * fs->lfs_ssize,
1054 				   fs->lfs_ssize) / 2;
1055 
1056 		dlog("%s: cleaning with goal %" PRId64
1057 		     " bytes (%d segs clean, %d cleanable)",
1058 		     fs->lfs_fsmnt, goal, cip->clean, npos);
1059 		syslog(LOG_INFO, "%s: cleaning with goal %" PRId64
1060 		       " bytes (%d segs clean, %d cleanable)",
1061 		       fs->lfs_fsmnt, goal, cip->clean, npos);
1062 		totbytes = 0;
1063 		for (i = 0; i < fs->lfs_nseg && totbytes < goal; i++) {
1064 			if (fs->clfs_segtabp[i]->priority == 0)
1065 				break;
1066 			/* Upper bound on number of segments at once */
1067 			if (ngood * fs->lfs_ssize > 4 * goal)
1068 				break;
1069 			sn = (fs->clfs_segtabp[i] - fs->clfs_segtab);
1070 			dlog("%s: add seg %d prio %" PRIu64
1071 			     " containing %ld bytes",
1072 			     fs->lfs_fsmnt, sn, fs->clfs_segtabp[i]->priority,
1073 			     fs->clfs_segtabp[i]->nbytes);
1074 			if ((r = load_segment(fs, sn, &bip, &bic)) > 0) {
1075 				++ngood;
1076 				toss_old_blocks(fs, &bip, &bic, &bytes);
1077 				totbytes += bytes;
1078 			} else if (r == 0)
1079 				fd_release(fs->clfs_devvp);
1080 			else
1081 				break;
1082 		}
1083 	} else {
1084 		/* Set attainable goal */
1085 		goal = atatime;
1086 		if (goal > cip->clean - 1)
1087 			goal = MAX(cip->clean - 1, 1);
1088 
1089 		dlog("%s: cleaning with goal %d segments (%d clean, %d cleanable)",
1090 		       fs->lfs_fsmnt, (int)goal, cip->clean, npos);
1091 		for (i = 0; i < fs->lfs_nseg && ngood < goal; i++) {
1092 			if (fs->clfs_segtabp[i]->priority == 0)
1093 				break;
1094 			sn = (fs->clfs_segtabp[i] - fs->clfs_segtab);
1095 			dlog("%s: add seg %d prio %" PRIu64,
1096 			     fs->lfs_fsmnt, sn, fs->clfs_segtabp[i]->priority);
1097 			if ((r = load_segment(fs, sn, &bip, &bic)) > 0)
1098 				++ngood;
1099 			else if (r == 0)
1100 				fd_release(fs->clfs_devvp);
1101 			else
1102 				break;
1103 		}
1104 		toss_old_blocks(fs, &bip, &bic, NULL);
1105 	}
1106 
1107 	/* If there is nothing to do, try again later. */
1108 	if (bic == 0) {
1109 		dlog("%s: no blocks to clean in %d cleanable segments",
1110 		       fs->lfs_fsmnt, (int)ngood);
1111 		fd_release_all(fs->clfs_devvp);
1112 		return 0;
1113 	}
1114 
1115 	/* Record statistics */
1116 	for (i = nb = 0; i < bic; i++)
1117 		nb += bip[i].bi_size;
1118 	util = ((double)nb) / (fs->clfs_nactive * fs->lfs_ssize);
1119 	cleaner_stats.util_tot += util;
1120 	cleaner_stats.util_sos += util * util;
1121 	cleaner_stats.bytes_written += nb;
1122 
1123 	/*
1124 	 * Check out our blocks to see if there are hidden cleaning costs.
1125 	 * If there are, we might be cleaning ourselves deeper into a hole
1126 	 * rather than doing anything useful.
1127 	 * XXX do something about this.
1128 	 */
1129 	if_extra = 0;
1130 	extra = fs->lfs_bsize * (off_t)check_hidden_cost(fs, bip, bic, &if_extra);
1131 	if_extra *= fs->lfs_bsize;
1132 
1133 	/*
1134 	 * Use markv to move the blocks.
1135 	 */
1136 	if (do_small)
1137 		inc = MAXPHYS / fs->lfs_bsize - 1;
1138 	else
1139 		inc = LFS_MARKV_MAXBLKCNT / 2;
1140 	for (mc = 0, mbip = bip; mc < bic; mc += inc, mbip += inc) {
1141 		lim.blkiov = mbip;
1142 		lim.blkcnt = (bic - mc > inc ? inc : bic - mc);
1143 #ifdef TEST_PATTERN
1144 		dlog("checking blocks %d-%d", mc, mc + lim.blkcnt - 1);
1145 		for (i = 0; i < lim.blkcnt; i++) {
1146 			check_test_pattern(mbip + i);
1147 		}
1148 #endif /* TEST_PATTERN */
1149 		dlog("sending blocks %d-%d", mc, mc + lim.blkcnt - 1);
1150 		if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim))<0) {
1151 			int oerrno = errno;
1152 			syslog(LOG_WARNING, "%s: markv returned %d (errno %d, %m)",
1153 			       fs->lfs_fsmnt, r, errno);
1154 			if (oerrno != EAGAIN && oerrno != ESHUTDOWN) {
1155 				syslog(LOG_DEBUG, "%s: errno %d, returning",
1156 				       fs->lfs_fsmnt, oerrno);
1157 				fd_release_all(fs->clfs_devvp);
1158 				return r;
1159 			}
1160 			if (oerrno == ESHUTDOWN) {
1161 				syslog(LOG_NOTICE, "%s: filesystem unmounted",
1162 				       fs->lfs_fsmnt);
1163 				fd_release_all(fs->clfs_devvp);
1164 				return r;
1165 			}
1166 		}
1167 	}
1168 
1169 	/*
1170 	 * Report progress (or lack thereof)
1171 	 */
1172 	syslog(LOG_INFO, "%s: wrote %" PRId64 " dirty + %"
1173 	       PRId64 " supporting indirect + %"
1174 	       PRId64 " supporting Ifile = %"
1175 	       PRId64 " bytes to clean %d segs (%" PRId64 "%% recovery)",
1176 	       fs->lfs_fsmnt, (int64_t)nb, (int64_t)(extra - if_extra),
1177 	       (int64_t)if_extra, (int64_t)(nb + extra), ngood,
1178 	       (ngood ? (int64_t)(100 - (100 * (nb + extra)) /
1179 					 (ngood * fs->lfs_ssize)) :
1180 		(int64_t)0));
1181 	if (nb + extra >= ngood * fs->lfs_ssize)
1182 		syslog(LOG_WARNING, "%s: cleaner not making forward progress",
1183 		       fs->lfs_fsmnt);
1184 
1185 	/*
1186 	 * Finally call reclaim to prompt cleaning of the segments.
1187 	 */
1188 	kops.ko_fcntl(fs->clfs_ifilefd, LFCNRECLAIM, NULL);
1189 
1190 	fd_release_all(fs->clfs_devvp);
1191 	return 0;
1192 }
1193 
1194 /*
1195  * Read the cleanerinfo block and apply cleaning policy to determine whether
1196  * the given filesystem needs to be cleaned.  Returns 1 if it does, 0 if it
1197  * does not, or -1 on error.
1198  */
1199 int
1200 needs_cleaning(struct clfs *fs, CLEANERINFO *cip)
1201 {
1202 	struct ubuf *bp;
1203 	struct stat st;
1204 	daddr_t fsb_per_seg, max_free_segs;
1205 	time_t now;
1206 	double loadavg;
1207 
1208 	/* If this fs is "on hold", don't clean it. */
1209 	if (fs->clfs_onhold)
1210 		return 0;
1211 
1212 	/*
1213 	 * Read the cleanerinfo block from the Ifile.  We don't want
1214 	 * the cached information, so invalidate the buffer before
1215 	 * handing it back.
1216 	 */
1217 	if (bread(fs->lfs_ivnode, 0, fs->lfs_bsize, NOCRED, 0, &bp)) {
1218 		syslog(LOG_ERR, "%s: can't read inode", fs->lfs_fsmnt);
1219 		return -1;
1220 	}
1221 	*cip = *(CLEANERINFO *)bp->b_data; /* Structure copy */
1222 	brelse(bp, B_INVAL);
1223 	cleaner_stats.bytes_read += fs->lfs_bsize;
1224 
1225 	/*
1226 	 * If the number of segments changed under us, reinit.
1227 	 * We don't have to start over from scratch, however,
1228 	 * since we don't hold any buffers.
1229 	 */
1230 	if (fs->lfs_nseg != cip->clean + cip->dirty) {
1231 		if (reinit_fs(fs) < 0) {
1232 			/* The normal case for unmount */
1233 			syslog(LOG_NOTICE, "%s: filesystem unmounted", fs->lfs_fsmnt);
1234 			return -1;
1235 		}
1236 		syslog(LOG_NOTICE, "%s: nsegs changed", fs->lfs_fsmnt);
1237 	}
1238 
1239 	/* Compute theoretical "free segments" maximum based on usage */
1240 	fsb_per_seg = lfs_segtod(fs, 1);
1241 	max_free_segs = MAX(cip->bfree, 0) / fsb_per_seg + fs->lfs_minfreeseg;
1242 
1243 	dlog("%s: bfree = %d, avail = %d, clean = %d/%d",
1244 	     fs->lfs_fsmnt, cip->bfree, cip->avail, cip->clean, fs->lfs_nseg);
1245 
1246 	/* If the writer is waiting on us, clean it */
1247 	if (cip->clean <= fs->lfs_minfreeseg ||
1248 	    (cip->flags & LFS_CLEANER_MUST_CLEAN))
1249 		return 1;
1250 
1251 	/* If there are enough segments, don't clean it */
1252 	if (cip->bfree - cip->avail <= fsb_per_seg &&
1253 	    cip->avail > fsb_per_seg)
1254 		return 0;
1255 
1256 	/* If we are in dire straits, clean it */
1257 	if (cip->bfree - cip->avail > fsb_per_seg &&
1258 	    cip->avail <= fsb_per_seg)
1259 		return 1;
1260 
1261 	/* If under busy threshold, clean regardless of load */
1262 	if (cip->clean < max_free_segs * BUSY_LIM)
1263 		return 1;
1264 
1265 	/* Check busy status; clean if idle and under idle limit */
1266 	if (use_fs_idle) {
1267 		/* Filesystem idle */
1268 		time(&now);
1269 		if (fstat(fs->clfs_ifilefd, &st) < 0) {
1270 			syslog(LOG_ERR, "%s: failed to stat ifile",
1271 			       fs->lfs_fsmnt);
1272 			return -1;
1273 		}
1274 		if (now - st.st_mtime > segwait_timeout &&
1275 		    cip->clean < max_free_segs * IDLE_LIM)
1276 			return 1;
1277 	} else {
1278 		/* CPU idle - use one-minute load avg */
1279 		if (getloadavg(&loadavg, 1) == -1) {
1280 			syslog(LOG_ERR, "%s: failed to get load avg",
1281 			       fs->lfs_fsmnt);
1282 			return -1;
1283 		}
1284 		if (loadavg < load_threshold &&
1285 		    cip->clean < max_free_segs * IDLE_LIM)
1286 			return 1;
1287 	}
1288 
1289 	return 0;
1290 }
1291 
1292 /*
1293  * Report statistics.  If the signal was SIGUSR2, clear the statistics too.
1294  * If the signal was SIGINT, exit.
1295  */
1296 static void
1297 sig_report(int sig)
1298 {
1299 	double avg = 0.0, stddev;
1300 
1301 	avg = cleaner_stats.util_tot / MAX(cleaner_stats.segs_cleaned, 1.0);
1302 	stddev = cleaner_stats.util_sos / MAX(cleaner_stats.segs_cleaned -
1303 					      avg * avg, 1.0);
1304 	syslog(LOG_INFO, "bytes read:	     %" PRId64, cleaner_stats.bytes_read);
1305 	syslog(LOG_INFO, "bytes written:     %" PRId64, cleaner_stats.bytes_written);
1306 	syslog(LOG_INFO, "segments cleaned:  %" PRId64, cleaner_stats.segs_cleaned);
1307 #if 0
1308 	/* "Empty segments" is meaningless, since the kernel handles those */
1309 	syslog(LOG_INFO, "empty segments:    %" PRId64, cleaner_stats.segs_empty);
1310 #endif
1311 	syslog(LOG_INFO, "error segments:    %" PRId64, cleaner_stats.segs_error);
1312 	syslog(LOG_INFO, "utilization total: %g", cleaner_stats.util_tot);
1313 	syslog(LOG_INFO, "utilization sos:   %g", cleaner_stats.util_sos);
1314 	syslog(LOG_INFO, "utilization avg:   %4.2f", avg);
1315 	syslog(LOG_INFO, "utilization sdev:  %9.6f", stddev);
1316 
1317 	if (debug)
1318 		bufstats();
1319 
1320 	if (sig == SIGUSR2)
1321 		memset(&cleaner_stats, 0, sizeof(cleaner_stats));
1322 	if (sig == SIGINT)
1323 		exit(0);
1324 }
1325 
1326 static void
1327 sig_exit(int sig)
1328 {
1329 	exit(0);
1330 }
1331 
1332 static void
1333 usage(void)
1334 {
1335 	errx(1, "usage: lfs_cleanerd [-bcdfmqs] [-i segnum] [-l load] "
1336 	     "[-n nsegs] [-r report_freq] [-t timeout] fs_name ...");
1337 }
1338 
1339 #ifndef LFS_CLEANER_AS_LIB
1340 /*
1341  * Main.
1342  */
1343 int
1344 main(int argc, char **argv)
1345 {
1346 
1347 	return lfs_cleaner_main(argc, argv);
1348 }
1349 #endif
1350 
1351 int
1352 lfs_cleaner_main(int argc, char **argv)
1353 {
1354 	int i, opt, error, r, loopcount, nodetach;
1355 	struct timeval tv;
1356 #ifdef LFS_CLEANER_AS_LIB
1357 	sem_t *semaddr = NULL;
1358 #endif
1359 	CLEANERINFO ci;
1360 #ifndef USE_CLIENT_SERVER
1361 	char *cp, *pidname;
1362 #endif
1363 
1364 	/*
1365 	 * Set up defaults
1366 	 */
1367 	atatime	 = 1;
1368 	segwait_timeout = 300; /* Five minutes */
1369 	load_threshold	= 0.2;
1370 	stat_report	= 0;
1371 	inval_segment	= -1;
1372 	copylog_filename = NULL;
1373 	nodetach        = 0;
1374 
1375 	/*
1376 	 * Parse command-line arguments
1377 	 */
1378 	while ((opt = getopt(argc, argv, "bC:cdDfi:l:mn:qr:sS:t:")) != -1) {
1379 		switch (opt) {
1380 		    case 'b':	/* Use bytes written, not segments read */
1381 			    use_bytes = 1;
1382 			    break;
1383 		    case 'C':	/* copy log */
1384 			    copylog_filename = optarg;
1385 			    break;
1386 		    case 'c':	/* Coalesce files */
1387 			    do_coalesce++;
1388 			    break;
1389 		    case 'd':	/* Debug mode. */
1390 			    nodetach++;
1391 			    debug++;
1392 			    break;
1393 		    case 'D':	/* stay-on-foreground */
1394 			    nodetach++;
1395 			    break;
1396 		    case 'f':	/* Use fs idle time rather than cpu idle */
1397 			    use_fs_idle = 1;
1398 			    break;
1399 		    case 'i':	/* Invalidate this segment */
1400 			    inval_segment = atoi(optarg);
1401 			    break;
1402 		    case 'l':	/* Load below which to clean */
1403 			    load_threshold = atof(optarg);
1404 			    break;
1405 		    case 'm':	/* [compat only] */
1406 			    break;
1407 		    case 'n':	/* How many segs to clean at once */
1408 			    atatime = atoi(optarg);
1409 			    break;
1410 		    case 'q':	/* Quit after one run */
1411 			    do_quit = 1;
1412 			    break;
1413 		    case 'r':	/* Report every stat_report segments */
1414 			    stat_report = atoi(optarg);
1415 			    break;
1416 		    case 's':	/* Small writes */
1417 			    do_small = 1;
1418 			    break;
1419 #ifdef LFS_CLEANER_AS_LIB
1420 		    case 'S':	/* semaphore */
1421 			    semaddr = (void*)(uintptr_t)strtoull(optarg,NULL,0);
1422 			    break;
1423 #endif
1424 		    case 't':	/* timeout */
1425 			    segwait_timeout = atoi(optarg);
1426 			    break;
1427 		    default:
1428 			    usage();
1429 			    /* NOTREACHED */
1430 		}
1431 	}
1432 	argc -= optind;
1433 	argv += optind;
1434 
1435 	if (argc < 1)
1436 		usage();
1437 	if (inval_segment >= 0 && argc != 1) {
1438 		errx(1, "lfs_cleanerd: may only specify one filesystem when "
1439 		     "using -i flag");
1440 	}
1441 
1442 	if (do_coalesce) {
1443 		errx(1, "lfs_cleanerd: -c disabled due to reports of file "
1444 		     "corruption; you may re-enable it by rebuilding the "
1445 		     "cleaner");
1446 	}
1447 
1448 	/*
1449 	 * Set up daemon mode or foreground mode
1450 	 */
1451 	if (nodetach) {
1452 		openlog("lfs_cleanerd", LOG_NDELAY | LOG_PID | LOG_PERROR,
1453 			LOG_DAEMON);
1454 		signal(SIGINT, sig_report);
1455 	} else {
1456 		if (daemon(0, 0) == -1)
1457 			err(1, "lfs_cleanerd: couldn't become a daemon!");
1458 		openlog("lfs_cleanerd", LOG_NDELAY | LOG_PID, LOG_DAEMON);
1459 		signal(SIGINT, sig_exit);
1460 	}
1461 
1462 	/*
1463 	 * Look for an already-running master daemon.  If there is one,
1464 	 * send it our filesystems to add to its list and exit.
1465 	 * If there is none, become the master.
1466 	 */
1467 #ifdef USE_CLIENT_SERVER
1468 	try_to_become_master(argc, argv);
1469 #else
1470 	/* XXX think about this */
1471 	asprintf(&pidname, "lfs_cleanerd:m:%s", argv[0]);
1472 	if (pidname == NULL) {
1473 		syslog(LOG_ERR, "malloc failed: %m");
1474 		exit(1);
1475 	}
1476 	for (cp = pidname; cp != NULL; cp = strchr(cp, '/'))
1477 		*cp = '|';
1478 	pidfile(pidname);
1479 #endif
1480 
1481 	/*
1482 	 * Signals mean daemon should report its statistics
1483 	 */
1484 	memset(&cleaner_stats, 0, sizeof(cleaner_stats));
1485 	signal(SIGUSR1, sig_report);
1486 	signal(SIGUSR2, sig_report);
1487 
1488 	/*
1489 	 * Start up buffer cache.  We only use this for the Ifile,
1490 	 * and we will resize it if necessary, so it can start small.
1491 	 */
1492 	bufinit(4);
1493 
1494 #ifdef REPAIR_ZERO_FINFO
1495 	{
1496 		BLOCK_INFO *bip = NULL;
1497 		int bic = 0;
1498 
1499 		nfss = 1;
1500 		fsp = (struct clfs **)malloc(sizeof(*fsp));
1501 		fsp[0] = (struct clfs *)calloc(1, sizeof(**fsp));
1502 
1503 		if (init_unmounted_fs(fsp[0], argv[0]) < 0) {
1504 			err(1, "init_unmounted_fs");
1505 		}
1506 		dlog("Filesystem has %d segments", fsp[0]->lfs_nseg);
1507 		for (i = 0; i < fsp[0]->lfs_nseg; i++) {
1508 			load_segment(fsp[0], i, &bip, &bic);
1509 			bic = 0;
1510 		}
1511 		exit(0);
1512 	}
1513 #endif
1514 
1515 	/*
1516 	 * Initialize cleaning structures, open devices, etc.
1517 	 */
1518 	nfss = argc;
1519 	fsp = (struct clfs **)malloc(nfss * sizeof(*fsp));
1520 	if (fsp == NULL) {
1521 		syslog(LOG_ERR, "couldn't allocate fs table: %m");
1522 		exit(1);
1523 	}
1524 	for (i = 0; i < nfss; i++) {
1525 		fsp[i] = (struct clfs *)calloc(1, sizeof(**fsp));
1526 		if ((r = init_fs(fsp[i], argv[i])) < 0) {
1527 			syslog(LOG_ERR, "%s: couldn't init: error code %d",
1528 			       argv[i], r);
1529 			handle_error(fsp, i);
1530 			--i; /* Do the new #i over again */
1531 		}
1532 	}
1533 
1534 	/*
1535 	 * If asked to coalesce, do so and exit.
1536 	 */
1537 	if (do_coalesce) {
1538 		for (i = 0; i < nfss; i++)
1539 			clean_all_inodes(fsp[i]);
1540 		exit(0);
1541 	}
1542 
1543 	/*
1544 	 * If asked to invalidate a segment, do that and exit.
1545 	 */
1546 	if (inval_segment >= 0) {
1547 		invalidate_segment(fsp[0], inval_segment);
1548 		exit(0);
1549 	}
1550 
1551 	/*
1552 	 * Main cleaning loop.
1553 	 */
1554 	loopcount = 0;
1555 #ifdef LFS_CLEANER_AS_LIB
1556 	if (semaddr)
1557 		sem_post(semaddr);
1558 #endif
1559 	error = 0;
1560 	while (nfss > 0) {
1561 		int cleaned_one;
1562 		do {
1563 #ifdef USE_CLIENT_SERVER
1564 			check_control_socket();
1565 #endif
1566 			cleaned_one = 0;
1567 			for (i = 0; i < nfss; i++) {
1568 				if ((error = needs_cleaning(fsp[i], &ci)) < 0) {
1569 					syslog(LOG_DEBUG, "%s: needs_cleaning returned %d",
1570 					       getprogname(), error);
1571 					handle_error(fsp, i);
1572 					continue;
1573 				}
1574 				if (error == 0) /* No need to clean */
1575 					continue;
1576 
1577 				reload_ifile(fsp[i]);
1578 				if ((error = clean_fs(fsp[i], &ci)) < 0) {
1579 					syslog(LOG_DEBUG, "%s: clean_fs returned %d",
1580 					       getprogname(), error);
1581 					handle_error(fsp, i);
1582 					continue;
1583 				}
1584 				++cleaned_one;
1585 			}
1586 			++loopcount;
1587 			if (stat_report && loopcount % stat_report == 0)
1588 				sig_report(0);
1589 			if (do_quit)
1590 				exit(0);
1591 		} while(cleaned_one);
1592 		tv.tv_sec = segwait_timeout;
1593 		tv.tv_usec = 0;
1594 		/* XXX: why couldn't others work if fsp socket is shutdown? */
1595 		error = kops.ko_fcntl(fsp[0]->clfs_ifilefd,LFCNSEGWAITALL,&tv);
1596 		if (error) {
1597 			if (errno == ESHUTDOWN) {
1598 				for (i = 0; i < nfss; i++) {
1599 					syslog(LOG_INFO, "%s: shutdown",
1600 					       getprogname());
1601 					handle_error(fsp, i);
1602 					assert(nfss == 0);
1603 				}
1604 			} else {
1605 #ifdef LFS_CLEANER_AS_LIB
1606 				error = ESHUTDOWN;
1607 				break;
1608 #else
1609 				err(1, "LFCNSEGWAITALL");
1610 #endif
1611 			}
1612 		}
1613 	}
1614 
1615 	/* NOTREACHED */
1616 	return error;
1617 }
1618