📄 yaffs_fsx.c
字号:
offset -= offset % writebdy; if (size == 0) { if (!quiet && testcalls > simulatedopcount) prt("skipping zero size write\n"); log4(OP_SKIPPED, OP_WRITE, offset, size); return; } log4(OP_WRITE, offset, size, file_size); gendata(original_buf, good_buf, offset, size); if (file_size < offset + size) { if (file_size < offset) memset(good_buf + file_size, '\0', offset - file_size); file_size = offset + size; if (lite) { warn("Lite file size bug in fsx!"); report_failure(149); } } if (testcalls <= simulatedopcount) return; if (!quiet && ((progressinterval && testcalls % progressinterval == 0) || (debug && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend)))))) prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset, offset + size - 1, size); ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET); if (ret == (off_t)-1) { prterr("dowrite: lseek"); report_failure(150); } iret = yaffs_write(fd, good_buf + offset, size); if (iret != size) { if (iret == -1) prterr("dowrite: write"); else prt("short write: 0x%x bytes instead of 0x%x\n", iret, size); report_failure(151); }}voiddotruncate(unsigned size){ int oldsize = file_size; size -= size % truncbdy; if (size > biggest) { biggest = size; if (!quiet && testcalls > simulatedopcount) prt("truncating to largest ever: 0x%x\n", size); } log4(OP_TRUNCATE, size, (unsigned)file_size, 0); if (size > file_size) memset(good_buf + file_size, '\0', size - file_size); file_size = size; if (testcalls <= simulatedopcount) return; if ((progressinterval && testcalls % progressinterval == 0) || (debug && (monitorstart == -1 || monitorend == -1 || size <= monitorend))) prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size); if (yaffs_ftruncate(fd, (off_t)size) == -1) { prt("ftruncate1: %x\n", size); prterr("dotruncate: ftruncate"); report_failure(160); }}voidwritefileimage(){ ssize_t iret; if (yaffs_lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { prterr("writefileimage: lseek"); report_failure(171); } iret = yaffs_write(fd, good_buf, file_size); if ((off_t)iret != file_size) { if (iret == -1) prterr("writefileimage: write"); else prt("short write: 0x%x bytes instead of 0x%llx\n", iret, (unsigned long long)file_size); report_failure(172); } if (lite ? 0 : yaffs_ftruncate(fd, file_size) == -1) { prt("ftruncate2: %llx\n", (unsigned long long)file_size); prterr("writefileimage: ftruncate"); report_failure(173); }}voiddocloseopen(void){ if (testcalls <= simulatedopcount) return; if (debug) prt("%lu close/open\n", testcalls); if (yaffs_close(fd)) { prterr("docloseopen: close"); report_failure(180); } fd = yaffs_open(fname, O_RDWR, 0); if (fd < 0) { prterr("docloseopen: open"); report_failure(181); }}voidtest(void){ unsigned long offset; unsigned long size = maxoplen; unsigned long rv = random(); unsigned long op = rv % (3 + !lite + mapped_writes); /* turn off the map read if necessary */ if (op == 2 && !mapped_reads) op = 0; if (simulatedopcount > 0 && testcalls == simulatedopcount) writefileimage(); testcalls++; if (closeprob) closeopen = (rv >> 3) < (1 << 28) / closeprob; if (debugstart > 0 && testcalls >= debugstart) debug = 1; if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0) prt("%lu...\n", testcalls); /* * READ: op = 0 * WRITE: op = 1 * MAPREAD: op = 2 * TRUNCATE: op = 3 * MAPWRITE: op = 3 or 4 */ if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */ dotruncate(random() % maxfilelen); else { if (randomoplen) size = random() % (maxoplen+1); if (lite ? 0 : op == 3) dotruncate(size); else { offset = random(); if (op == 1 || op == (lite ? 3 : 4)) { offset %= maxfilelen; if (offset + size > maxfilelen) size = maxfilelen - offset; dowrite(offset, size); } else { if (file_size) offset %= file_size; else offset = 0; if (offset + size > file_size) size = file_size - offset; doread(offset, size); } } } if (sizechecks && testcalls > simulatedopcount) check_size(); if (closeopen) docloseopen();}voidcleanup(sig) int sig;{ if (sig) prt("signal %d\n", sig); prt("testcalls = %lu\n", testcalls); exit(sig);}voidusage(void){ fprintf(stdout, "usage: %s", "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\ -b opnum: beginning operation number (default 1)\n\ -c P: 1 in P chance of file close+open at each op (default infinity)\n\ -d: debug output for all operations\n\ -l flen: the upper bound on file size (default 262144)\n\ -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\ -n: no verifications of file size\n\ -o oplen: the upper bound on operation size (default 65536)\n\ -p progressinterval: debug output at specified operation interval\n\ -q: quieter operation\n\ -r readbdy: 4096 would make reads page aligned (default 1)\n\ -s style: 1 gives smaller truncates (default 0)\n\ -t truncbdy: 4096 would make truncates page aligned (default 1)\n\ -w writebdy: 4096 would make writes page aligned (default 1)\n\ -D startingop: debug output starting at specified operation\n\ -L: fsxLite - no file creations & no file size changes\n\ -N numops: total # operations to do (default infinity)\n\ -O: use oplen (see -o flag) for every op (default random)\n\ -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\ -S seed: for random # generator (default 1) 0 gets timestamp\n\ fname: this filename is REQUIRED (no default)\n"); exit(90);}intgetnum(char *s, char **e){ int ret = -1; *e = (char *) 0; ret = strtol(s, e, 0); if (*e) switch (**e) { case 'b': case 'B': ret *= 512; *e = *e + 1; break; case 'k': case 'K': ret *= 1024; *e = *e + 1; break; case 'm': case 'M': ret *= 1024*1024; *e = *e + 1; break; case 'w': case 'W': ret *= 4; *e = *e + 1; break; } return (ret);}#define BASE_NAME "/M18-1"int random_seed = 999;int simulate_power_failure = 0;intmain(int argc, char **argv){ int i, style, ch; char *endp; char goodfile[1024]; char logfile[1024]; goodfile[0] = 0; logfile[0] = 0; page_size = getpagesize(); page_mask = page_size - 1; setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */ while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W")) != EOF) switch (ch) { case 'b': simulatedopcount = getnum(optarg, &endp); if (!quiet) fprintf(stdout, "Will begin at operation %ld\n", simulatedopcount); if (simulatedopcount == 0) usage(); simulatedopcount -= 1; break; case 'c': closeprob = getnum(optarg, &endp); if (!quiet) fprintf(stdout, "Chance of close/open is 1 in %d\n", closeprob); if (closeprob <= 0) usage(); break; case 'd': debug = 1; break; case 'l': maxfilelen = getnum(optarg, &endp); if (maxfilelen <= 0) usage(); break; case 'm': monitorstart = getnum(optarg, &endp); if (monitorstart < 0) usage(); if (!endp || *endp++ != ':') usage(); monitorend = getnum(endp, &endp); if (monitorend < 0) usage(); if (monitorend == 0) monitorend = -1; /* aka infinity */ debug = 1; case 'n': sizechecks = 0; break; case 'o': maxoplen = getnum(optarg, &endp); if (maxoplen <= 0) usage(); break; case 'p': progressinterval = getnum(optarg, &endp); if (progressinterval < 0) usage(); break; case 'q': quiet = 1; break; case 'r': readbdy = getnum(optarg, &endp); if (readbdy <= 0) usage(); break; case 's': style = getnum(optarg, &endp); if (style < 0 || style > 1) usage(); break; case 't': truncbdy = getnum(optarg, &endp); if (truncbdy <= 0) usage(); break; case 'w': writebdy = getnum(optarg, &endp); if (writebdy <= 0) usage(); break; case 'D': debugstart = getnum(optarg, &endp); if (debugstart < 1) usage(); break; case 'L': lite = 1; break; case 'N': numops = getnum(optarg, &endp); if (numops < 0) usage(); break; case 'O': randomoplen = 0; break; case 'P': strncpy(goodfile, optarg, sizeof(goodfile)); strcat(goodfile, "/"); strncpy(logfile, optarg, sizeof(logfile)); strcat(logfile, "/"); break; case 'R': mapped_reads = 0; break; case 'S': seed = getnum(optarg, &endp); if (seed == 0) seed = time(0) % 10000; if (!quiet) fprintf(stdout, "Seed set to %d\n", seed); if (seed < 0) usage(); break; case 'W': mapped_writes = 0; if (!quiet) fprintf(stdout, "mapped writes DISABLED\n"); break; default: usage(); /* NOTREACHED */ } argc -= optind; argv += optind; yaffs_StartUp(); yaffs_mount(BASE_NAME); fname = BASE_NAME "/fsxdata"; signal(SIGHUP, cleanup); signal(SIGINT, cleanup); signal(SIGPIPE, cleanup); signal(SIGALRM, cleanup); signal(SIGTERM, cleanup); signal(SIGXCPU, cleanup); signal(SIGXFSZ, cleanup); signal(SIGVTALRM, cleanup); signal(SIGUSR1, cleanup); signal(SIGUSR2, cleanup); initstate(seed, state, 256); setstate(state); fd = yaffs_open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666); if (fd < 0) { prterr(fname); exit(91); } strncat(goodfile, fname, 256); strcat (goodfile, ".fsxgood"); fsxgoodfd = yaffs_open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666); if (fsxgoodfd < 0) { prterr(goodfile); exit(92); } strncat(logfile, "fsx", 256); strcat (logfile, ".fsxlog"); fsxlogf = fopen(logfile, "w"); if (fsxlogf == NULL) { prterr(logfile); exit(93); } if (lite) { off_t ret; file_size = maxfilelen = yaffs_lseek(fd, (off_t)0, SEEK_END); if (file_size == (off_t)-1) { prterr(fname); warn("main: lseek eof"); exit(94); } ret = yaffs_lseek(fd, (off_t)0, SEEK_SET); if (ret == (off_t)-1) { prterr(fname); warn("main: lseek 0"); exit(95); } } original_buf = (char *) malloc(maxfilelen); for (i = 0; i < maxfilelen; i++) original_buf[i] = random() % 256; good_buf = (char *) malloc(maxfilelen); memset(good_buf, '\0', maxfilelen); temp_buf = (char *) malloc(maxoplen); memset(temp_buf, '\0', maxoplen); if (lite) { /* zero entire existing file */ ssize_t written; written = yaffs_write(fd, good_buf, (size_t)maxfilelen); if (written != maxfilelen) { if (written == -1) { prterr(fname); warn("main: error on write"); } else warn("main: short write, 0x%x bytes instead of 0x%x\n", (unsigned)written, maxfilelen); exit(98); } } else check_trunc_hack(); while (numops == -1 || numops--) test(); if (yaffs_close(fd)) { prterr("close"); report_failure(99); } yaffs_close(fsxgoodfd); yaffs_unmount(BASE_NAME); prt("All operations completed A-OK!\n"); exit(0); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -