1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1992 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)cleanerd.c 5.3 (Berkeley) 08/25/92"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/mount.h> 20 #include <sys/time.h> 21 22 #include <ufs/ufs/dinode.h> 23 #include <ufs/lfs/lfs.h> 24 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <unistd.h> 28 29 #include "clean.h" 30 char *special = "cleanerd"; 31 32 struct seglist { 33 int sl_id; /* segment number */ 34 int sl_cost; /* cleaning cost */ 35 char sl_empty; /* is segment empty */ 36 }; 37 38 struct tossstruct { 39 struct lfs *lfs; 40 int seg; 41 }; 42 43 /* function prototypes for system calls; not sure where they should go */ 44 int lfs_segwait __P((fsid_t, struct timeval *)); 45 int lfs_segclean __P((fsid_t, u_long)); 46 int lfs_bmapv __P((fsid_t, BLOCK_INFO *, int)); 47 int lfs_markv __P((fsid_t, BLOCK_INFO *, int)); 48 49 /* function prototypes */ 50 int bi_tossold __P((const void *, const void *, const void *)); 51 int choose_segments __P((FS_INFO *, struct seglist *, 52 int (*)(FS_INFO *, SEGUSE *))); 53 void clean_fs __P((FS_INFO *, int (*)(FS_INFO *, SEGUSE *))); 54 int clean_loop __P((FS_INFO *)); 55 int clean_segment __P((FS_INFO *, int)); 56 int cost_benefit __P((FS_INFO *, SEGUSE *)); 57 int cost_compare __P((const void *, const void *)); 58 59 /* 60 * Cleaning Cost Functions: 61 * 62 * These return the cost of cleaning a segment. The higher the cost value 63 * the better it is to clean the segment, so empty segments have the highest 64 * cost. (It is probably better to think of this as a priority value 65 * instead). 66 * 67 * This is the cost-benefit policy simulated and described in Rosenblum's 68 * 1991 SOSP paper. 69 */ 70 71 int 72 cost_benefit(fsp, su) 73 FS_INFO *fsp; /* file system information */ 74 SEGUSE *su; 75 { 76 struct lfs *lfsp; 77 struct timeval t; 78 int age; 79 int live; 80 81 gettimeofday(&t, NULL); 82 83 live = su->su_nbytes; 84 age = t.tv_sec - su->su_lastmod < 0 ? 0 : t.tv_sec - su->su_lastmod; 85 86 lfsp = &fsp->fi_lfs; 87 if (live == 0) 88 return (t.tv_sec * lblkno(lfsp, seg_size(lfsp))); 89 else { 90 /* 91 * from lfsSegUsage.c (Mendel's code). 92 * priority calculation is done using INTEGER arithmetic. 93 * sizes are in BLOCKS (that is why we use lblkno below). 94 * age is in seconds. 95 * 96 * priority = ((seg_size - live) * age) / (seg_size + live) 97 */ 98 #ifdef VERBOSE 99 if (live < 0 || live > seg_size(lfsp)) { 100 err(0, "Bad segusage count: %d", live); 101 live = 0; 102 } 103 #endif 104 return (lblkno(lfsp, seg_size(lfsp) - live) * age) 105 / lblkno(lfsp, seg_size(lfsp) + live); 106 } 107 } 108 109 int 110 main(argc, argv) 111 int argc; 112 char *argv[]; 113 { 114 FS_INFO *lfp, *fsp; 115 struct statfs *lstatfsp; /* file system stats */ 116 struct timeval timeout; /* sleep timeout */ 117 fsid_t fsid; 118 int count; /* number of file systems */ 119 int i, nclean; 120 121 count = fs_getmntinfo(&lstatfsp, MOUNT_LFS); 122 123 timeout.tv_sec = 5*60; /* five minutes */ 124 timeout.tv_usec = 0; 125 fsid.val[0] = 0; 126 fsid.val[1] = 0; 127 128 for (fsp = get_fs_info(lstatfsp, count); ; reread_fs_info(fsp, count)) { 129 for (nclean = 0, lfp = fsp, i = 0; i < count; ++lfp, ++i) 130 nclean += clean_loop(lfp); 131 /* 132 * If some file systems were actually cleaned, run again 133 * to make sure that some nasty process hasn't just 134 * filled the disk system up. 135 */ 136 if (nclean) 137 continue; 138 139 #ifdef VERBOSE 140 (void)printf("Cleaner going to sleep.\n"); 141 #endif 142 if (lfs_segwait(fsid, &timeout) < 0) 143 err(0, "lfs_segwait: returned error\n"); 144 #ifdef VERBOSE 145 (void)printf("Cleaner waking up.\n"); 146 #endif 147 } 148 } 149 150 /* return the number of segments cleaned */ 151 int 152 clean_loop(fsp) 153 FS_INFO *fsp; /* file system information */ 154 { 155 double loadavg[MAXLOADS]; 156 time_t now; 157 u_long max_free_segs; 158 159 /* 160 * Compute the maximum possible number of free segments, given the 161 * number of free blocks. 162 */ 163 max_free_segs = fsp->fi_statfsp->f_bfree / fsp->fi_lfs.lfs_ssize; 164 165 /* 166 * We will clean if there are not enough free blocks or total clean 167 * space is less than BUSY_LIM % of possible clean space. 168 */ 169 now = time((time_t *)NULL); 170 if (fsp->fi_cip->clean <= MIN_SEGS(&fsp->fi_lfs) || 171 fsp->fi_cip->clean < max_free_segs * BUSY_LIM) { 172 printf("Cleaner Running at %s (need space)\n", 173 ctime(&now)); 174 clean_fs(fsp, cost_benefit); 175 return (1); 176 } else { 177 /* 178 * We will also clean if the system is reasonably idle and 179 * the total clean space is less then IDLE_LIM % of possible 180 * clean space. 181 */ 182 if (getloadavg(loadavg, MAXLOADS) == -1) { 183 perror("getloadavg: failed\n"); 184 return (-1); 185 } 186 if (loadavg[ONE_MIN] == 0.0 && loadavg[FIVE_MIN] && 187 fsp->fi_cip->clean < max_free_segs * IDLE_LIM) { 188 clean_fs(fsp, cost_benefit); 189 printf("Cleaner Running at %s (system idle)\n", 190 ctime(&now)); 191 return (1); 192 } 193 } 194 printf("Cleaner Not Running at %s\n", ctime(&now)); 195 return (0); 196 } 197 198 199 void 200 clean_fs(fsp, cost_func) 201 FS_INFO *fsp; /* file system information */ 202 int (*cost_func) __P((FS_INFO *, SEGUSE *)); 203 { 204 struct seglist *segs, *sp; 205 int i; 206 207 if ((segs = malloc(fsp->fi_lfs.lfs_nseg * sizeof(struct seglist))) 208 == NULL) { 209 err(0, "malloc failed"); 210 return; 211 } 212 i = choose_segments(fsp, segs, cost_func); 213 #ifdef VERBOSE 214 printf("clean_fs: cleaning %d segments in file system %s\n", 215 i, fsp->fi_statfsp->f_mntonname); 216 fflush(stdout); 217 #endif 218 if (i) 219 for (i = MIN(i, NUM_TO_CLEAN(fsp)), sp = segs; i-- ; ++sp) 220 if (clean_segment(fsp, sp->sl_id) < 0) 221 perror("clean_segment failed"); 222 else if (lfs_segclean (fsp->fi_statfsp->f_fsid, 223 sp->sl_id) < 0) 224 perror("lfs_segclean failed"); 225 free(segs); 226 } 227 228 /* 229 * Segment with the highest priority get sorted to the beginning of the 230 * list. This sort assumes that empty segments always have a higher 231 * cost/benefit than any utilized segment. 232 */ 233 int 234 cost_compare(a, b) 235 const void *a; 236 const void *b; 237 { 238 return (((struct seglist *)b)->sl_cost - 239 ((struct seglist *)a)->sl_cost); 240 } 241 242 243 /* 244 * Returns the number of segments to be cleaned with the elements of seglist 245 * filled in. 246 */ 247 int 248 choose_segments(fsp, seglist, cost_func) 249 FS_INFO *fsp; 250 struct seglist *seglist; 251 int (*cost_func) __P((FS_INFO *, SEGUSE *)); 252 { 253 struct lfs *lfsp; 254 struct seglist *sp; 255 SEGUSE *sup; 256 int i, nsegs; 257 258 lfsp = &fsp->fi_lfs; 259 260 #ifdef VERBOSE 261 (void) printf("Entering choose_segments\n"); 262 #endif 263 dump_super(lfsp); 264 dump_cleaner_info(fsp->fi_cip); 265 266 for (sp = seglist, i = 0; i < lfsp->lfs_nseg; ++i) { 267 sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, i); 268 PRINT_SEGUSE(sup, i); 269 if (!(sup->su_flags & SEGUSE_DIRTY) || 270 sup->su_flags & SEGUSE_ACTIVE) 271 continue; 272 #ifdef VERBOSE 273 (void) printf("\tchoosing segment %d\n", i); 274 #endif 275 sp->sl_cost = (*cost_func)(fsp, sup); 276 sp->sl_id = i; 277 sp->sl_empty = sup->su_nbytes ? 0 : 1; 278 ++sp; 279 } 280 nsegs = sp - seglist; 281 qsort(seglist, nsegs, sizeof(struct seglist), cost_compare); 282 #ifdef VERBOSE 283 (void)printf("Returning %d segments\n", nsegs); 284 #endif 285 return (nsegs); 286 } 287 288 289 int 290 clean_segment(fsp, id) 291 FS_INFO *fsp; /* file system information */ 292 int id; /* segment number */ 293 { 294 BLOCK_INFO *block_array; 295 SEGUSE *sp; 296 struct lfs *lfsp; 297 struct tossstruct t; 298 caddr_t seg_buf; 299 int num_blocks; 300 301 lfsp = &fsp->fi_lfs; 302 sp = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, id); 303 304 #ifdef VERBOSE 305 (void) printf("cleaning segment %d: contains %lu bytes\n", id, 306 sp->su_nbytes); 307 fflush(stdout); 308 #endif 309 /* XXX could add debugging to verify that segment is really empty */ 310 if (sp->su_nbytes == sp->su_nsums * LFS_SUMMARY_SIZE) 311 return (0); 312 313 /* map the segment into a buffer */ 314 if (mmap_segment(fsp, id, &seg_buf) < 0) { 315 err(0, "mmap_segment failed"); 316 return (-1); 317 } 318 /* get a list of blocks that are contained by the segment */ 319 if (lfs_segmapv(fsp, id, seg_buf, &block_array, &num_blocks) < 0) { 320 err(0, "clean_segment: lfs_segmapv failed"); 321 return (-1); 322 } 323 324 #ifdef VERBOSE 325 (void) printf("lfs_segmapv returned %d blocks\n", num_blocks); 326 fflush (stdout); 327 #endif 328 329 /* get the current disk address of blocks contained by the segment */ 330 if (lfs_bmapv(fsp->fi_statfsp->f_fsid, block_array, num_blocks) < 0) { 331 perror("clean_segment: lfs_bmapv failed\n"); 332 return -1; 333 } 334 335 /* Now toss any blocks not in the current segment */ 336 t.lfs = lfsp; 337 t.seg = id; 338 toss(block_array, &num_blocks, sizeof(BLOCK_INFO), bi_tossold, &t); 339 340 /* Check if last element should be tossed */ 341 if (num_blocks && bi_tossold(&t, block_array + num_blocks - 1, NULL)) 342 --num_blocks; 343 344 #ifdef VERBOSE 345 { 346 BLOCK_INFO *_bip; 347 u_long *lp; 348 int i; 349 350 (void) printf("after bmapv still have %d blocks\n", num_blocks); 351 fflush (stdout); 352 if (num_blocks) 353 printf("BLOCK INFOS\n"); 354 for (_bip = block_array, i=0; i < num_blocks; ++_bip, ++i) { 355 PRINT_BINFO(_bip); 356 lp = (u_long *)_bip->bi_bp; 357 } 358 } 359 #endif 360 /* rewrite the live data */ 361 if (num_blocks > 0) 362 if (lfs_markv(fsp->fi_statfsp->f_fsid, block_array, num_blocks) 363 < 0 ) { 364 err(0, "clean_segment: lfs_bmapv failed"); 365 return (-1); 366 } 367 free(block_array); 368 munmap_segment(fsp, seg_buf); 369 370 return (0); 371 } 372 373 374 int 375 bi_tossold(client, a, b) 376 const void *client; 377 const void *a; 378 const void *b; 379 { 380 const struct tossstruct *t; 381 382 t = (struct tossstruct *)client; 383 384 return (((BLOCK_INFO *)a)->bi_daddr == LFS_UNUSED_DADDR || 385 datosn(t->lfs, ((BLOCK_INFO *)a)->bi_daddr) != t->seg); 386 } 387