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