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