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