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