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