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.4 (Berkeley) 08/26/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: found %d segments to clean 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 #ifdef VERBOSE 226 printf("Completed cleaning segment %d\n", sp->sl_id); 227 #endif 228 } 229 free(segs); 230 } 231 232 /* 233 * Segment with the highest priority get sorted to the beginning of the 234 * list. This sort assumes that empty segments always have a higher 235 * cost/benefit than any utilized segment. 236 */ 237 int 238 cost_compare(a, b) 239 const void *a; 240 const void *b; 241 { 242 return (((struct seglist *)b)->sl_cost - 243 ((struct seglist *)a)->sl_cost); 244 } 245 246 247 /* 248 * Returns the number of segments to be cleaned with the elements of seglist 249 * filled in. 250 */ 251 int 252 choose_segments(fsp, seglist, cost_func) 253 FS_INFO *fsp; 254 struct seglist *seglist; 255 int (*cost_func) __P((FS_INFO *, SEGUSE *)); 256 { 257 struct lfs *lfsp; 258 struct seglist *sp; 259 SEGUSE *sup; 260 int i, nsegs; 261 262 lfsp = &fsp->fi_lfs; 263 264 #ifdef VERBOSE 265 (void) printf("Entering choose_segments\n"); 266 #endif 267 dump_super(lfsp); 268 dump_cleaner_info(fsp->fi_cip); 269 270 for (sp = seglist, i = 0; i < lfsp->lfs_nseg; ++i) { 271 sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, i); 272 PRINT_SEGUSE(sup, i); 273 if (!(sup->su_flags & SEGUSE_DIRTY) || 274 sup->su_flags & SEGUSE_ACTIVE) 275 continue; 276 #ifdef VERBOSE 277 (void) printf("\tchoosing segment %d\n", i); 278 #endif 279 sp->sl_cost = (*cost_func)(fsp, sup); 280 sp->sl_id = i; 281 sp->sl_empty = sup->su_nbytes ? 0 : 1; 282 ++sp; 283 } 284 nsegs = sp - seglist; 285 qsort(seglist, nsegs, sizeof(struct seglist), cost_compare); 286 #ifdef VERBOSE 287 (void)printf("Returning %d segments\n", nsegs); 288 #endif 289 return (nsegs); 290 } 291 292 293 int 294 clean_segment(fsp, id) 295 FS_INFO *fsp; /* file system information */ 296 int id; /* segment number */ 297 { 298 BLOCK_INFO *block_array; 299 SEGUSE *sp; 300 struct lfs *lfsp; 301 struct tossstruct t; 302 caddr_t seg_buf; 303 int num_blocks; 304 305 lfsp = &fsp->fi_lfs; 306 sp = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, id); 307 308 #ifdef VERBOSE 309 (void) printf("cleaning segment %d: contains %lu bytes\n", id, 310 sp->su_nbytes); 311 fflush(stdout); 312 #endif 313 /* XXX could add debugging to verify that segment is really empty */ 314 if (sp->su_nbytes == sp->su_nsums * LFS_SUMMARY_SIZE) 315 return (0); 316 317 /* map the segment into a buffer */ 318 if (mmap_segment(fsp, id, &seg_buf) < 0) { 319 err(0, "mmap_segment failed"); 320 return (-1); 321 } 322 /* get a list of blocks that are contained by the segment */ 323 if (lfs_segmapv(fsp, id, seg_buf, &block_array, &num_blocks) < 0) { 324 err(0, "clean_segment: lfs_segmapv failed"); 325 return (-1); 326 } 327 328 #ifdef VERBOSE 329 (void) printf("lfs_segmapv returned %d blocks\n", num_blocks); 330 fflush (stdout); 331 #endif 332 333 /* get the current disk address of blocks contained by the segment */ 334 if (lfs_bmapv(fsp->fi_statfsp->f_fsid, block_array, num_blocks) < 0) { 335 perror("clean_segment: lfs_bmapv failed\n"); 336 return -1; 337 } 338 339 /* Now toss any blocks not in the current segment */ 340 t.lfs = lfsp; 341 t.seg = id; 342 toss(block_array, &num_blocks, sizeof(BLOCK_INFO), bi_tossold, &t); 343 344 /* Check if last element should be tossed */ 345 if (num_blocks && bi_tossold(&t, block_array + num_blocks - 1, NULL)) 346 --num_blocks; 347 348 #ifdef VERBOSE 349 { 350 BLOCK_INFO *_bip; 351 u_long *lp; 352 int i; 353 354 (void) printf("after bmapv still have %d blocks\n", num_blocks); 355 fflush (stdout); 356 if (num_blocks) 357 printf("BLOCK INFOS\n"); 358 for (_bip = block_array, i=0; i < num_blocks; ++_bip, ++i) { 359 PRINT_BINFO(_bip); 360 lp = (u_long *)_bip->bi_bp; 361 } 362 } 363 #endif 364 /* rewrite the live data */ 365 if (num_blocks > 0) 366 if (lfs_markv(fsp->fi_statfsp->f_fsid, block_array, num_blocks) 367 < 0 ) { 368 err(0, "clean_segment: lfs_markv failed"); 369 return (-1); 370 } 371 free(block_array); 372 munmap_segment(fsp, seg_buf); 373 374 return (0); 375 } 376 377 378 int 379 bi_tossold(client, a, b) 380 const void *client; 381 const void *a; 382 const void *b; 383 { 384 const struct tossstruct *t; 385 386 t = (struct tossstruct *)client; 387 388 return (((BLOCK_INFO *)a)->bi_daddr == LFS_UNUSED_DADDR || 389 datosn(t->lfs, ((BLOCK_INFO *)a)->bi_daddr) != t->seg); 390 } 391