155860Sbostic /*- 255860Sbostic * Copyright (c) 1992 The Regents of the University of California. 355860Sbostic * All rights reserved. 455860Sbostic * 555860Sbostic * %sccs.include.redist.c% 655860Sbostic */ 755860Sbostic 855860Sbostic #ifndef lint 955860Sbostic char copyright[] = 1055860Sbostic "@(#) Copyright (c) 1992 The Regents of the University of California.\n\ 1155860Sbostic All rights reserved.\n"; 1255860Sbostic #endif /* not lint */ 1355860Sbostic 1455860Sbostic #ifndef lint 15*56656Sbostic static char sccsid[] = "@(#)cleanerd.c 5.7 (Berkeley) 11/01/92"; 1655860Sbostic #endif /* not lint */ 1755860Sbostic 1855857Sbostic #include <sys/param.h> 1955857Sbostic #include <sys/mount.h> 2055857Sbostic #include <sys/time.h> 2155860Sbostic 2255857Sbostic #include <ufs/ufs/dinode.h> 2355857Sbostic #include <ufs/lfs/lfs.h> 2455857Sbostic 2555857Sbostic #include <stdio.h> 2655857Sbostic #include <stdlib.h> 2755857Sbostic #include <unistd.h> 2855857Sbostic 2955857Sbostic #include "clean.h" 3055857Sbostic char *special = "cleanerd"; 3155857Sbostic 3255857Sbostic struct seglist { 3355857Sbostic int sl_id; /* segment number */ 3455857Sbostic int sl_cost; /* cleaning cost */ 3555857Sbostic char sl_empty; /* is segment empty */ 3655857Sbostic }; 3755857Sbostic 3855857Sbostic struct tossstruct { 3955857Sbostic struct lfs *lfs; 4055857Sbostic int seg; 4155857Sbostic }; 4255857Sbostic 4355857Sbostic /* function prototypes for system calls; not sure where they should go */ 4455857Sbostic int lfs_segwait __P((fsid_t, struct timeval *)); 4555857Sbostic int lfs_segclean __P((fsid_t, u_long)); 4655857Sbostic int lfs_bmapv __P((fsid_t, BLOCK_INFO *, int)); 4755931Sbostic int lfs_markv __P((fsid_t, BLOCK_INFO *, int)); 4855857Sbostic 4955857Sbostic /* function prototypes */ 5055857Sbostic int bi_tossold __P((const void *, const void *, const void *)); 5155857Sbostic int choose_segments __P((FS_INFO *, struct seglist *, 5255857Sbostic int (*)(FS_INFO *, SEGUSE *))); 5355857Sbostic void clean_fs __P((FS_INFO *, int (*)(FS_INFO *, SEGUSE *))); 5455857Sbostic int clean_loop __P((FS_INFO *)); 5555857Sbostic int clean_segment __P((FS_INFO *, int)); 5655857Sbostic int cost_benefit __P((FS_INFO *, SEGUSE *)); 5755857Sbostic int cost_compare __P((const void *, const void *)); 5855857Sbostic 5955857Sbostic /* 6055857Sbostic * Cleaning Cost Functions: 6155857Sbostic * 6255857Sbostic * These return the cost of cleaning a segment. The higher the cost value 6355857Sbostic * the better it is to clean the segment, so empty segments have the highest 6455857Sbostic * cost. (It is probably better to think of this as a priority value 6555857Sbostic * instead). 6655857Sbostic * 6755857Sbostic * This is the cost-benefit policy simulated and described in Rosenblum's 6855857Sbostic * 1991 SOSP paper. 6955857Sbostic */ 7055857Sbostic 7155857Sbostic int 7255857Sbostic cost_benefit(fsp, su) 7355857Sbostic FS_INFO *fsp; /* file system information */ 7455857Sbostic SEGUSE *su; 7555857Sbostic { 7655857Sbostic struct lfs *lfsp; 7755857Sbostic struct timeval t; 7855857Sbostic int age; 7955857Sbostic int live; 8055857Sbostic 8155857Sbostic gettimeofday(&t, NULL); 8255857Sbostic 8355857Sbostic live = su->su_nbytes; 8455857Sbostic age = t.tv_sec - su->su_lastmod < 0 ? 0 : t.tv_sec - su->su_lastmod; 8555857Sbostic 8655857Sbostic lfsp = &fsp->fi_lfs; 8755857Sbostic if (live == 0) 8855857Sbostic return (t.tv_sec * lblkno(lfsp, seg_size(lfsp))); 8955857Sbostic else { 9055857Sbostic /* 9155857Sbostic * from lfsSegUsage.c (Mendel's code). 9255857Sbostic * priority calculation is done using INTEGER arithmetic. 9355857Sbostic * sizes are in BLOCKS (that is why we use lblkno below). 9455857Sbostic * age is in seconds. 9555857Sbostic * 9655857Sbostic * priority = ((seg_size - live) * age) / (seg_size + live) 9755857Sbostic */ 9855857Sbostic #ifdef VERBOSE 9955857Sbostic if (live < 0 || live > seg_size(lfsp)) { 10055857Sbostic err(0, "Bad segusage count: %d", live); 10155857Sbostic live = 0; 10255857Sbostic } 10355857Sbostic #endif 10455857Sbostic return (lblkno(lfsp, seg_size(lfsp) - live) * age) 10555857Sbostic / lblkno(lfsp, seg_size(lfsp) + live); 10655857Sbostic } 10755857Sbostic } 10855857Sbostic 10955860Sbostic int 11055857Sbostic main(argc, argv) 11155857Sbostic int argc; 11255857Sbostic char *argv[]; 11355857Sbostic { 11455857Sbostic FS_INFO *lfp, *fsp; 11555857Sbostic struct statfs *lstatfsp; /* file system stats */ 11655857Sbostic struct timeval timeout; /* sleep timeout */ 11755857Sbostic fsid_t fsid; 11855857Sbostic int count; /* number of file systems */ 11956030Sbostic int i, nclean; 12055857Sbostic 12155857Sbostic count = fs_getmntinfo(&lstatfsp, MOUNT_LFS); 12255857Sbostic 12355857Sbostic timeout.tv_sec = 5*60; /* five minutes */ 12455857Sbostic timeout.tv_usec = 0; 12555857Sbostic fsid.val[0] = 0; 12655857Sbostic fsid.val[1] = 0; 12755857Sbostic 12855857Sbostic for (fsp = get_fs_info(lstatfsp, count); ; reread_fs_info(fsp, count)) { 12956030Sbostic for (nclean = 0, lfp = fsp, i = 0; i < count; ++lfp, ++i) 13056030Sbostic nclean += clean_loop(lfp); 13156030Sbostic /* 13256030Sbostic * If some file systems were actually cleaned, run again 13356030Sbostic * to make sure that some nasty process hasn't just 13456030Sbostic * filled the disk system up. 13556030Sbostic */ 13656030Sbostic if (nclean) 13756030Sbostic continue; 13855857Sbostic 13955857Sbostic #ifdef VERBOSE 14055857Sbostic (void)printf("Cleaner going to sleep.\n"); 14155857Sbostic #endif 14255857Sbostic if (lfs_segwait(fsid, &timeout) < 0) 14355857Sbostic err(0, "lfs_segwait: returned error\n"); 14455857Sbostic #ifdef VERBOSE 14555857Sbostic (void)printf("Cleaner waking up.\n"); 14655857Sbostic #endif 14755857Sbostic } 14855857Sbostic } 14955857Sbostic 15055857Sbostic /* return the number of segments cleaned */ 15155857Sbostic int 15255857Sbostic clean_loop(fsp) 15355857Sbostic FS_INFO *fsp; /* file system information */ 15455857Sbostic { 15555857Sbostic double loadavg[MAXLOADS]; 15655857Sbostic time_t now; 15755857Sbostic u_long max_free_segs; 15855857Sbostic 15955857Sbostic /* 16055857Sbostic * Compute the maximum possible number of free segments, given the 16155857Sbostic * number of free blocks. 16255857Sbostic */ 16355857Sbostic max_free_segs = fsp->fi_statfsp->f_bfree / fsp->fi_lfs.lfs_ssize; 16455857Sbostic 16555857Sbostic /* 16655857Sbostic * We will clean if there are not enough free blocks or total clean 16755857Sbostic * space is less than BUSY_LIM % of possible clean space. 16855857Sbostic */ 16955857Sbostic now = time((time_t *)NULL); 17056181Smargo if (fsp->fi_cip->clean < max_free_segs && 17156181Smargo (fsp->fi_cip->clean <= MIN_SEGS(&fsp->fi_lfs) || 17256181Smargo fsp->fi_cip->clean < max_free_segs * BUSY_LIM)) { 173*56656Sbostic printf("Cleaner Running at %s (%d of %d segments available)\n", 174*56656Sbostic ctime(&now), fsp->fi_cip->clean, max_free_segs); 17556030Sbostic clean_fs(fsp, cost_benefit); 17655857Sbostic return (1); 17755857Sbostic } else { 17855857Sbostic /* 17955857Sbostic * We will also clean if the system is reasonably idle and 18055857Sbostic * the total clean space is less then IDLE_LIM % of possible 18155857Sbostic * clean space. 18255857Sbostic */ 18355857Sbostic if (getloadavg(loadavg, MAXLOADS) == -1) { 18455857Sbostic perror("getloadavg: failed\n"); 18555857Sbostic return (-1); 18655857Sbostic } 18755857Sbostic if (loadavg[ONE_MIN] == 0.0 && loadavg[FIVE_MIN] && 18855857Sbostic fsp->fi_cip->clean < max_free_segs * IDLE_LIM) { 18955857Sbostic clean_fs(fsp, cost_benefit); 19055857Sbostic printf("Cleaner Running at %s (system idle)\n", 19155857Sbostic ctime(&now)); 19255857Sbostic return (1); 19355857Sbostic } 19455857Sbostic } 19555857Sbostic printf("Cleaner Not Running at %s\n", ctime(&now)); 19655857Sbostic return (0); 19755857Sbostic } 19855857Sbostic 19955857Sbostic 20055857Sbostic void 20155857Sbostic clean_fs(fsp, cost_func) 20255857Sbostic FS_INFO *fsp; /* file system information */ 20355857Sbostic int (*cost_func) __P((FS_INFO *, SEGUSE *)); 20455857Sbostic { 20555857Sbostic struct seglist *segs, *sp; 20655857Sbostic int i; 20755857Sbostic 20855857Sbostic if ((segs = malloc(fsp->fi_lfs.lfs_nseg * sizeof(struct seglist))) 20955857Sbostic == NULL) { 21055857Sbostic err(0, "malloc failed"); 21155857Sbostic return; 21255857Sbostic } 21355857Sbostic i = choose_segments(fsp, segs, cost_func); 21455857Sbostic #ifdef VERBOSE 21556051Sbostic printf("clean_fs: found %d segments to clean in file system %s\n", 21655857Sbostic i, fsp->fi_statfsp->f_mntonname); 21755857Sbostic fflush(stdout); 21855857Sbostic #endif 21955857Sbostic if (i) 22056051Sbostic for (i = MIN(i, NUM_TO_CLEAN(fsp)), sp = segs; i-- ; ++sp) { 22155857Sbostic if (clean_segment(fsp, sp->sl_id) < 0) 22255857Sbostic perror("clean_segment failed"); 22355857Sbostic else if (lfs_segclean (fsp->fi_statfsp->f_fsid, 22455857Sbostic sp->sl_id) < 0) 22555857Sbostic perror("lfs_segclean failed"); 22656051Sbostic printf("Completed cleaning segment %d\n", sp->sl_id); 22756051Sbostic } 22855857Sbostic free(segs); 22955857Sbostic } 23055857Sbostic 23155857Sbostic /* 23255857Sbostic * Segment with the highest priority get sorted to the beginning of the 23355857Sbostic * list. This sort assumes that empty segments always have a higher 23455857Sbostic * cost/benefit than any utilized segment. 23555857Sbostic */ 23655857Sbostic int 23755857Sbostic cost_compare(a, b) 23855857Sbostic const void *a; 23955857Sbostic const void *b; 24055857Sbostic { 24155857Sbostic return (((struct seglist *)b)->sl_cost - 24255857Sbostic ((struct seglist *)a)->sl_cost); 24355857Sbostic } 24455857Sbostic 24555857Sbostic 24655857Sbostic /* 24755857Sbostic * Returns the number of segments to be cleaned with the elements of seglist 24855857Sbostic * filled in. 24955857Sbostic */ 25055857Sbostic int 25155857Sbostic choose_segments(fsp, seglist, cost_func) 25255857Sbostic FS_INFO *fsp; 25355857Sbostic struct seglist *seglist; 25455857Sbostic int (*cost_func) __P((FS_INFO *, SEGUSE *)); 25555857Sbostic { 25655857Sbostic struct lfs *lfsp; 25755857Sbostic struct seglist *sp; 25855857Sbostic SEGUSE *sup; 25955857Sbostic int i, nsegs; 26055857Sbostic 26155857Sbostic lfsp = &fsp->fi_lfs; 26255857Sbostic 26355857Sbostic #ifdef VERBOSE 26455857Sbostic (void) printf("Entering choose_segments\n"); 26555857Sbostic #endif 26655857Sbostic dump_super(lfsp); 26755857Sbostic dump_cleaner_info(fsp->fi_cip); 26855857Sbostic 26955857Sbostic for (sp = seglist, i = 0; i < lfsp->lfs_nseg; ++i) { 27055857Sbostic sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, i); 27155857Sbostic PRINT_SEGUSE(sup, i); 27255857Sbostic if (!(sup->su_flags & SEGUSE_DIRTY) || 27355857Sbostic sup->su_flags & SEGUSE_ACTIVE) 27455857Sbostic continue; 27555857Sbostic #ifdef VERBOSE 27655857Sbostic (void) printf("\tchoosing segment %d\n", i); 27755857Sbostic #endif 27855857Sbostic sp->sl_cost = (*cost_func)(fsp, sup); 27955857Sbostic sp->sl_id = i; 28055857Sbostic sp->sl_empty = sup->su_nbytes ? 0 : 1; 28155857Sbostic ++sp; 28255857Sbostic } 28355857Sbostic nsegs = sp - seglist; 28455857Sbostic qsort(seglist, nsegs, sizeof(struct seglist), cost_compare); 28555857Sbostic #ifdef VERBOSE 28655857Sbostic (void)printf("Returning %d segments\n", nsegs); 28755857Sbostic #endif 28855857Sbostic return (nsegs); 28955857Sbostic } 29055857Sbostic 29155857Sbostic 29255857Sbostic int 29355857Sbostic clean_segment(fsp, id) 29455857Sbostic FS_INFO *fsp; /* file system information */ 29555857Sbostic int id; /* segment number */ 29655857Sbostic { 29755857Sbostic BLOCK_INFO *block_array; 29855857Sbostic SEGUSE *sp; 29955857Sbostic struct lfs *lfsp; 30055857Sbostic struct tossstruct t; 30155857Sbostic caddr_t seg_buf; 30255931Sbostic int num_blocks; 30355857Sbostic 30455857Sbostic lfsp = &fsp->fi_lfs; 30555857Sbostic sp = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, id); 30655857Sbostic 30755857Sbostic #ifdef VERBOSE 30855857Sbostic (void) printf("cleaning segment %d: contains %lu bytes\n", id, 30955857Sbostic sp->su_nbytes); 31055857Sbostic fflush(stdout); 31155857Sbostic #endif 31255857Sbostic /* XXX could add debugging to verify that segment is really empty */ 31355857Sbostic if (sp->su_nbytes == sp->su_nsums * LFS_SUMMARY_SIZE) 31455857Sbostic return (0); 31555857Sbostic 31655857Sbostic /* map the segment into a buffer */ 31755857Sbostic if (mmap_segment(fsp, id, &seg_buf) < 0) { 31855857Sbostic err(0, "mmap_segment failed"); 31955857Sbostic return (-1); 32055857Sbostic } 32155857Sbostic /* get a list of blocks that are contained by the segment */ 32255931Sbostic if (lfs_segmapv(fsp, id, seg_buf, &block_array, &num_blocks) < 0) { 32355857Sbostic err(0, "clean_segment: lfs_segmapv failed"); 32455857Sbostic return (-1); 32555857Sbostic } 32655857Sbostic 32755857Sbostic #ifdef VERBOSE 32855931Sbostic (void) printf("lfs_segmapv returned %d blocks\n", num_blocks); 32955857Sbostic fflush (stdout); 33055857Sbostic #endif 33155857Sbostic 33255857Sbostic /* get the current disk address of blocks contained by the segment */ 33355857Sbostic if (lfs_bmapv(fsp->fi_statfsp->f_fsid, block_array, num_blocks) < 0) { 33455857Sbostic perror("clean_segment: lfs_bmapv failed\n"); 33555857Sbostic return -1; 33655857Sbostic } 33755857Sbostic 33855857Sbostic /* Now toss any blocks not in the current segment */ 33955857Sbostic t.lfs = lfsp; 34055857Sbostic t.seg = id; 34155857Sbostic toss(block_array, &num_blocks, sizeof(BLOCK_INFO), bi_tossold, &t); 34255857Sbostic 34355857Sbostic /* Check if last element should be tossed */ 34455857Sbostic if (num_blocks && bi_tossold(&t, block_array + num_blocks - 1, NULL)) 34555857Sbostic --num_blocks; 34655857Sbostic 34755857Sbostic #ifdef VERBOSE 34855857Sbostic { 34955857Sbostic BLOCK_INFO *_bip; 35055857Sbostic u_long *lp; 35155857Sbostic int i; 35255857Sbostic 35355857Sbostic (void) printf("after bmapv still have %d blocks\n", num_blocks); 35455857Sbostic fflush (stdout); 35555857Sbostic if (num_blocks) 35655857Sbostic printf("BLOCK INFOS\n"); 35755857Sbostic for (_bip = block_array, i=0; i < num_blocks; ++_bip, ++i) { 35855857Sbostic PRINT_BINFO(_bip); 35955857Sbostic lp = (u_long *)_bip->bi_bp; 36055857Sbostic } 36155857Sbostic } 36255857Sbostic #endif 36355857Sbostic /* rewrite the live data */ 36455931Sbostic if (num_blocks > 0) 36555931Sbostic if (lfs_markv(fsp->fi_statfsp->f_fsid, block_array, num_blocks) 36655931Sbostic < 0 ) { 36756051Sbostic err(0, "clean_segment: lfs_markv failed"); 36855857Sbostic return (-1); 36955857Sbostic } 37055857Sbostic free(block_array); 37155857Sbostic munmap_segment(fsp, seg_buf); 37255857Sbostic 37355857Sbostic return (0); 37455857Sbostic } 37555857Sbostic 37655857Sbostic 37755857Sbostic int 37855857Sbostic bi_tossold(client, a, b) 37955857Sbostic const void *client; 38055857Sbostic const void *a; 38155857Sbostic const void *b; 38255857Sbostic { 38355857Sbostic const struct tossstruct *t; 38455857Sbostic 38555857Sbostic t = (struct tossstruct *)client; 38655857Sbostic 38755857Sbostic return (((BLOCK_INFO *)a)->bi_daddr == LFS_UNUSED_DADDR || 38855857Sbostic datosn(t->lfs, ((BLOCK_INFO *)a)->bi_daddr) != t->seg); 38955857Sbostic } 390