📄 store_dir_ufs.c
字号:
*/ ufs_dir_index = xcalloc(n_ufs_dirs, sizeof(*ufs_dir_index)); for (i = 0, n = 0; i < Config.cacheSwap.n_configured; i++) { sd = &Config.cacheSwap.swapDirs[i]; if (!storeUfsDirIs(sd)) continue; ufs_dir_index[n++] = i; ufsinfo = (ufsinfo_t *) sd->fsdata; j += (ufsinfo->l1 * ufsinfo->l2); } assert(n == n_ufs_dirs); /* * Start the storeUfsDirClean() swap_index with a random * value. j equals the total number of UFS level 2 * swap directories */ swap_index = (int) (squid_random() % j); } if (0 == store_dirs_rebuilding) { n = storeUfsDirClean(swap_index); swap_index++; } eventAdd("storeDirClean", storeUfsDirCleanEvent, NULL, 15.0 * exp(-0.25 * n), 1);}static intstoreUfsDirIs(SwapDir * sd){ if (strncmp(sd->type, "ufs", 3) == 0) return 1; return 0;}/* * Does swapfile number 'fn' belong in cachedir #F0, * level1 dir #F1, level2 dir #F2? */static intstoreUfsFilenoBelongsHere(int fn, int F0, int F1, int F2){ int D1, D2; int L1, L2; int filn = fn; ufsinfo_t *ufsinfo; assert(F0 < Config.cacheSwap.n_configured); ufsinfo = (ufsinfo_t *) Config.cacheSwap.swapDirs[F0].fsdata; L1 = ufsinfo->l1; L2 = ufsinfo->l2; D1 = ((filn / L2) / L2) % L1; if (F1 != D1) return 0; D2 = (filn / L2) % L2; if (F2 != D2) return 0; return 1;}intstoreUfsDirValidFileno(SwapDir * SD, sfileno filn, int flag){ ufsinfo_t *ufsinfo = (ufsinfo_t *) SD->fsdata; if (filn < 0) return 0; /* * If flag is set it means out-of-range file number should * be considered invalid. */ if (flag) if (filn > ufsinfo->map->max_n_files) return 0; return 1;}voidstoreUfsDirMaintain(SwapDir * SD){ ufsinfo_t *ufsinfo = SD->fsdata; StoreEntry *e = NULL; int removed = 0; int max_scan; int max_remove; double f; RemovalPurgeWalker *walker; /* We can't delete objects while rebuilding swap */ if (store_dirs_rebuilding) { return; } else { f = (double) (SD->cur_size - SD->low_size) / (SD->max_size - SD->low_size); f = f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f; max_scan = (int) (f * 400.0 + 100.0); max_remove = (int) (f * 70.0 + 10.0); /* * This is kinda cheap, but so we need this priority hack? */ } debug(47, 3) ("storeMaintainSwapSpace: f=%f, max_scan=%d, max_remove=%d\n", f, max_scan, max_remove); walker = SD->repl->PurgeInit(SD->repl, max_scan); while (1) { if (SD->cur_size < SD->low_size && ufsinfo->map->n_files_in_map < FILEMAP_MAX) break; if (removed >= max_remove) break; e = walker->Next(walker); if (!e) break; /* no more objects */ removed++; storeRelease(e); } walker->Done(walker); debug(47, (removed ? 2 : 3)) ("storeUfsDirMaintain: %s removed %d/%d f=%.03f max_scan=%d\n", SD->path, removed, max_remove, f, max_scan);}/* * storeUfsDirCheckObj * * This routine is called by storeDirSelectSwapDir to see if the given * object is able to be stored on this filesystem. UFS filesystems will * happily store anything as long as the LRU time isn't too small. */intstoreUfsDirCheckObj(SwapDir * SD, const StoreEntry * e){ return 1;}/* * storeUfsDirCheckLoadAv * * Return load average from 0 to 1000. */intstoreUfsDirCheckLoadAv(SwapDir * SD, store_op_t op){ ufsinfo_t *ufsinfo = SD->fsdata; return UFS_LOAD_BASE + ufsinfo->open_files / 2;}/* * storeUfsDirRefObj * * This routine is called whenever an object is referenced, so we can * maintain replacement information within the storage fs. */voidstoreUfsDirRefObj(SwapDir * SD, StoreEntry * e){ debug(47, 3) ("storeUfsDirRefObj: referencing %p %d/%d\n", e, e->swap_dirn, e->swap_filen); if (SD->repl->Referenced) SD->repl->Referenced(SD->repl, e, &e->repl);}/* * storeUfsDirUnrefObj * This routine is called whenever the last reference to an object is * removed, to maintain replacement information within the storage fs. */voidstoreUfsDirUnrefObj(SwapDir * SD, StoreEntry * e){ debug(47, 3) ("storeUfsDirUnrefObj: referencing %p %d/%d\n", e, e->swap_dirn, e->swap_filen); if (SD->repl->Dereferenced) SD->repl->Dereferenced(SD->repl, e, &e->repl);}/* * storeUfsDirUnlinkFile * * This routine unlinks a file and pulls it out of the bitmap. * It used to be in storeUfsUnlink(), however an interface change * forced this bit of code here. Eeek. */voidstoreUfsDirUnlinkFile(SwapDir * SD, sfileno f){ debug(79, 3) ("storeUfsDirUnlinkFile: unlinking fileno %08X\n", f); /* storeUfsDirMapBitReset(SD, f); */#if USE_UNLINKD unlinkdUnlink(storeUfsDirFullPath(SD, f, NULL));#elif USE_TRUNCATE truncate(storeUfsDirFullPath(SD, f, NULL), 0);#else unlink(storeUfsDirFullPath(SD, f, NULL));#endif}/* * Add and remove the given StoreEntry from the replacement policy in * use. */voidstoreUfsDirReplAdd(SwapDir * SD, StoreEntry * e){ debug(47, 4) ("storeUfsDirReplAdd: added node %p to dir %d\n", e, SD->index); SD->repl->Add(SD->repl, e, &e->repl);}voidstoreUfsDirReplRemove(StoreEntry * e){ SwapDir *SD = INDEXSD(e->swap_dirn); debug(47, 4) ("storeUfsDirReplRemove: remove node %p from dir %d\n", e, SD->index); SD->repl->Remove(SD->repl, e, &e->repl);}/* ========== LOCAL FUNCTIONS ABOVE, GLOBAL FUNCTIONS BELOW ========== */voidstoreUfsDirStats(SwapDir * SD, StoreEntry * sentry){ ufsinfo_t *ufsinfo = SD->fsdata;#ifdef HAVE_STATVFS fsblkcnt_t totl_kb = 0; fsblkcnt_t free_kb = 0; fsfilcnt_t totl_in = 0; fsfilcnt_t free_in = 0;#else int totl_kb = 0; int free_kb = 0; int totl_in = 0; int free_in = 0;#endif int x; storeAppendPrintf(sentry, "First level subdirectories: %d\n", ufsinfo->l1); storeAppendPrintf(sentry, "Second level subdirectories: %d\n", ufsinfo->l2); storeAppendPrintf(sentry, "Maximum Size: %d KB\n", SD->max_size); storeAppendPrintf(sentry, "Current Size: %d KB\n", SD->cur_size); storeAppendPrintf(sentry, "Percent Used: %0.2f%%\n", 100.0 * SD->cur_size / SD->max_size); storeAppendPrintf(sentry, "Current load metric: %d / %d\n", storeUfsDirCheckLoadAv(SD, ST_OP_CREATE), MAX_LOAD_VALUE); storeAppendPrintf(sentry, "Filemap bits in use: %d of %d (%d%%)\n", ufsinfo->map->n_files_in_map, ufsinfo->map->max_n_files, percent(ufsinfo->map->n_files_in_map, ufsinfo->map->max_n_files)); x = storeDirGetUFSStats(SD->path, &totl_kb, &free_kb, &totl_in, &free_in); if (0 == x) {#ifdef HAVE_STATVFS storeAppendPrintf(sentry, "Filesystem Space in use: %" PRIu64 "/%" PRIu64 " KB (%.0f%%)\n", (uint64_t) (totl_kb - free_kb), (uint64_t) totl_kb, dpercent(totl_kb - free_kb, totl_kb)); storeAppendPrintf(sentry, "Filesystem Inodes in use: %" PRIu64 "/%" PRIu64 " (%.0f%%)\n", (uint64_t) (totl_in - free_in), (uint64_t) totl_in, dpercent(totl_in - free_in, totl_in));#else storeAppendPrintf(sentry, "Filesystem Space in use: %d/%d KB (%d%%)\n", totl_kb - free_kb, totl_kb, percent(totl_kb - free_kb, totl_kb)); storeAppendPrintf(sentry, "Filesystem Inodes in use: %d/%d (%d%%)\n", totl_in - free_in, totl_in, percent(totl_in - free_in, totl_in));#endif } storeAppendPrintf(sentry, "Flags:"); if (SD->flags.selected) storeAppendPrintf(sentry, " SELECTED"); if (SD->flags.read_only) storeAppendPrintf(sentry, " READ-ONLY"); storeAppendPrintf(sentry, "\n");}static struct cache_dir_option options[] ={#if NOT_YET_DONE {"L1", storeUfsDirParseL1, storeUfsDirDumpL1}, {"L2", storeUfsDirParseL2, storeUfsDirDumpL2},#endif {NULL, NULL}};/* * storeUfsDirReconfigure * * This routine is called when the given swapdir needs reconfiguring */static voidstoreUfsDirReconfigure(SwapDir * sd, int index, char *path){ int i; int size; int l1; int l2; i = GetInteger(); size = i << 10; /* Mbytes to kbytes */ if (size <= 0) fatal("storeUfsDirReconfigure: invalid size value"); i = GetInteger(); l1 = i; if (l1 <= 0) fatal("storeUfsDirReconfigure: invalid level 1 directories value"); i = GetInteger(); l2 = i; if (l2 <= 0) fatal("storeUfsDirReconfigure: invalid level 2 directories value"); /* just reconfigure it */ if (size == sd->max_size) debug(3, 1) ("Cache dir '%s' size remains unchanged at %d KB\n", path, size); else debug(3, 1) ("Cache dir '%s' size changed to %d KB\n", path, size); sd->max_size = size; parse_cachedir_options(sd, options, 1);}voidstoreUfsDirDump(StoreEntry * entry, SwapDir * s){ ufsinfo_t *ufsinfo = (ufsinfo_t *) s->fsdata; storeAppendPrintf(entry, " %d %d %d", s->max_size >> 10, ufsinfo->l1, ufsinfo->l2); dump_cachedir_options(entry, options, s);}/* * Only "free" the filesystem specific stuff here */static voidstoreUfsDirFree(SwapDir * s){ ufsinfo_t *ufsinfo = (ufsinfo_t *) s->fsdata; if (ufsinfo->swaplog_fd > -1) { file_close(ufsinfo->swaplog_fd); ufsinfo->swaplog_fd = -1; } filemapFreeMemory(ufsinfo->map); xfree(ufsinfo); s->fsdata = NULL; /* Will aid debugging... */}char *storeUfsDirFullPath(SwapDir * SD, sfileno filn, char *fullpath){ LOCAL_ARRAY(char, fullfilename, SQUID_MAXPATHLEN); ufsinfo_t *ufsinfo = (ufsinfo_t *) SD->fsdata; int L1 = ufsinfo->l1; int L2 = ufsinfo->l2; if (!fullpath) fullpath = fullfilename; fullpath[0] = '\0'; snprintf(fullpath, SQUID_MAXPATHLEN, "%s/%02X/%02X/%08X", SD->path, ((filn / L2) / L2) % L1, (filn / L2) % L2, filn); return fullpath;}/* * storeUfsCleanupDoubleCheck * * This is called by storeCleanup() if -S was given on the command line. */static intstoreUfsCleanupDoubleCheck(SwapDir * sd, StoreEntry * e){ struct stat sb; if (stat(storeUfsDirFullPath(sd, e->swap_filen, NULL), &sb) < 0) { debug(47, 0) ("storeUfsCleanupDoubleCheck: MISSING SWAP FILE\n"); debug(47, 0) ("storeUfsCleanupDoubleCheck: FILENO %08X\n", e->swap_filen); debug(47, 0) ("storeUfsCleanupDoubleCheck: PATH %s\n", storeUfsDirFullPath(sd, e->swap_filen, NULL)); storeEntryDump(e, 0); return -1; } if (e->swap_file_sz != sb.st_size) { debug(47, 0) ("storeUfsCleanupDoubleCheck: SIZE MISMATCH\n"); debug(47, 0) ("storeUfsCleanupDoubleCheck: FILENO %08X\n", e->swap_filen); debug(47, 0) ("storeUfsCleanupDoubleCheck: PATH %s\n", storeUfsDirFullPath(sd, e->swap_filen, NULL)); debug(47, 0) ("storeUfsCleanupDoubleCheck: ENTRY SIZE: %ld, FILE SIZE: %ld\n", (long int) e->swap_file_sz, (long int) sb.st_size); storeEntryDump(e, 0); return -1; } return 0;}/* * storeUfsDirParse * * Called when a *new* fs is being setup. */static voidstoreUfsDirParse(SwapDir * sd, int index, char *path){ int i; int size; int l1; int l2; ufsinfo_t *ufsinfo; i = GetInteger(); size = i << 10; /* Mbytes to kbytes */ if (size <= 0) fatal("storeUfsDirParse: invalid size value"); i = GetInteger(); l1 = i; if (l1 <= 0) fatal("storeUfsDirParse: invalid level 1 directories value"); i = GetInteger(); l2 = i; if (l2 <= 0) fatal("storeUfsDirParse: invalid level 2 directories value"); ufsinfo = xmalloc(sizeof(ufsinfo_t)); if (ufsinfo == NULL) fatal("storeUfsDirParse: couldn't xmalloc() ufsinfo_t!\n"); sd->index = index; sd->path = xstrdup(path); sd->max_size = size; sd->fsdata = ufsinfo; ufsinfo->l1 = l1; ufsinfo->l2 = l2; ufsinfo->swaplog_fd = -1; ufsinfo->map = NULL; /* Debugging purposes */ ufsinfo->suggest = 0; ufsinfo->open_files = 0; sd->checkconfig = storeUfsCheckConfig; sd->init = storeUfsDirInit; sd->newfs = storeUfsDirNewfs; sd->dump = storeUfsDirDump; sd->freefs = storeUfsDirFree; sd->dblcheck = storeUfsCleanupDoubleCheck; sd->statfs = storeUfsDirStats; sd->maintainfs = storeUfsDirMaintain; sd->checkobj = storeUfsDirCheckObj; sd->checkload = storeUfsDirCheckLoadAv; sd->refobj = storeUfsDirRefObj; sd->unrefobj = storeUfsDirUnrefObj; sd->callback = NULL; sd->sync = NULL; sd->obj.create = storeUfsCreate; sd->obj.open = storeUfsOpen; sd->obj.close = storeUfsClose; sd->obj.read = storeUfsRead; sd->obj.write = storeUfsWrite; sd->obj.unlink = storeUfsUnlink; sd->obj.recycle = storeUfsRecycle; sd->log.open = storeUfsDirOpenSwapLog; sd->log.close = storeUfsDirCloseSwapLog; sd->log.write = storeUfsDirSwapLog; sd->log.clean.start = storeUfsDirWriteCleanStart; sd->log.clean.nextentry = storeUfsDirCleanLogNextEntry; sd->log.clean.done = storeUfsDirWriteCleanDone; parse_cachedir_options(sd, options, 1); /* Initialise replacement policy stuff */ sd->repl = createRemovalPolicy(Config.replPolicy);}/* * Initial setup / end destruction */static voidstoreUfsDirDone(void){ memPoolDestroy(ufs_state_pool); ufs_initialised = 0;}voidstoreFsSetup_ufs(storefs_entry_t * storefs){ assert(!ufs_initialised); storefs->parsefunc = storeUfsDirParse; storefs->reconfigurefunc = storeUfsDirReconfigure; storefs->donefunc = storeUfsDirDone; ufs_state_pool = memPoolCreate("UFS IO State data", sizeof(ufsstate_t)); ufs_initialised = 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -