fstest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * fstest - filesystem test code
  31. *
  32. * Writes a file (in small chunks) and then reads it back again
  33. * (also in small chunks) and complains if what it reads back is
  34. * not the same.
  35. *
  36. * The length of SLOGAN is intentionally a prime number and
  37. * specifically *not* a power of two.
  38. */
  39. #include <types.h>
  40. #include <kern/errno.h>
  41. #include <kern/fcntl.h>
  42. #include <lib.h>
  43. #include <uio.h>
  44. #include <thread.h>
  45. #include <synch.h>
  46. #include <vfs.h>
  47. #include <fs.h>
  48. #include <vnode.h>
  49. #include <test.h>
  50. #define SLOGAN "HODIE MIHI - CRAS TIBI\n"
  51. #define FILENAME "fstest.tmp"
  52. #define NCHUNKS 720
  53. #define NTHREADS 12
  54. #define NCREATES 32
  55. static struct semaphore *threadsem = NULL;
  56. static
  57. void
  58. init_threadsem(void)
  59. {
  60. if (threadsem==NULL) {
  61. threadsem = sem_create("fstestsem", 0);
  62. if (threadsem == NULL) {
  63. panic("fstest: sem_create failed\n");
  64. }
  65. }
  66. }
  67. /*
  68. * Vary each line of the test file in a way that's predictable but
  69. * unlikely to mask bugs in the filesystem.
  70. */
  71. static
  72. void
  73. rotate(char *str, int amt)
  74. {
  75. int i, ch;
  76. amt = (amt+2600)%26;
  77. KASSERT(amt>=0);
  78. for (i=0; str[i]; i++) {
  79. ch = str[i];
  80. if (ch>='A' && ch<='Z') {
  81. ch = ch - 'A';
  82. ch += amt;
  83. ch %= 26;
  84. ch = ch + 'A';
  85. KASSERT(ch>='A' && ch<='Z');
  86. }
  87. str[i] = ch;
  88. }
  89. }
  90. ////////////////////////////////////////////////////////////
  91. static
  92. void
  93. fstest_makename(char *buf, size_t buflen,
  94. const char *fs, const char *namesuffix)
  95. {
  96. snprintf(buf, buflen, "%s:%s%s", fs, FILENAME, namesuffix);
  97. KASSERT(strlen(buf) < buflen);
  98. }
  99. #define MAKENAME() fstest_makename(name, sizeof(name), fs, namesuffix)
  100. static
  101. int
  102. fstest_remove(const char *fs, const char *namesuffix)
  103. {
  104. char name[32];
  105. char buf[32];
  106. int err;
  107. MAKENAME();
  108. strcpy(buf, name);
  109. err = vfs_remove(buf);
  110. if (err) {
  111. kprintf("Could not remove %s: %s\n", name, strerror(err));
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static
  117. int
  118. fstest_write(const char *fs, const char *namesuffix,
  119. int stridesize, int stridepos)
  120. {
  121. struct vnode *vn;
  122. int err;
  123. int i;
  124. size_t shouldbytes=0;
  125. size_t bytes=0;
  126. off_t pos=0;
  127. char name[32];
  128. char buf[32];
  129. struct iovec iov;
  130. struct uio ku;
  131. int flags;
  132. KASSERT(sizeof(buf) > strlen(SLOGAN));
  133. MAKENAME();
  134. flags = O_WRONLY|O_CREAT;
  135. if (stridesize == 1) {
  136. flags |= O_TRUNC;
  137. }
  138. /* vfs_open destroys the string it's passed */
  139. strcpy(buf, name);
  140. err = vfs_open(buf, flags, 0664, &vn);
  141. if (err) {
  142. kprintf("Could not open %s for write: %s\n",
  143. name, strerror(err));
  144. return -1;
  145. }
  146. for (i=0; i<NCHUNKS; i++) {
  147. if (i % stridesize != stridepos) {
  148. pos += strlen(SLOGAN);
  149. continue;
  150. }
  151. strcpy(buf, SLOGAN);
  152. rotate(buf, i);
  153. uio_kinit(&iov, &ku, buf, strlen(SLOGAN), pos, UIO_WRITE);
  154. err = VOP_WRITE(vn, &ku);
  155. if (err) {
  156. kprintf("%s: Write error: %s\n", name, strerror(err));
  157. vfs_close(vn);
  158. vfs_remove(name);
  159. return -1;
  160. }
  161. if (ku.uio_resid > 0) {
  162. kprintf("%s: Short write: %lu bytes left over\n",
  163. name, (unsigned long) ku.uio_resid);
  164. vfs_close(vn);
  165. vfs_remove(name);
  166. return -1;
  167. }
  168. bytes += (ku.uio_offset - pos);
  169. shouldbytes += strlen(SLOGAN);
  170. pos = ku.uio_offset;
  171. }
  172. vfs_close(vn);
  173. if (bytes != shouldbytes) {
  174. kprintf("%s: %lu bytes written, should have been %lu!\n",
  175. name, (unsigned long) bytes,
  176. (unsigned long) (NCHUNKS*strlen(SLOGAN)));
  177. vfs_remove(name);
  178. return -1;
  179. }
  180. kprintf("%s: %lu bytes written\n", name, (unsigned long) bytes);
  181. return 0;
  182. }
  183. static
  184. int
  185. fstest_read(const char *fs, const char *namesuffix)
  186. {
  187. struct vnode *vn;
  188. int err;
  189. int i;
  190. size_t bytes=0;
  191. char name[32];
  192. char buf[32];
  193. struct iovec iov;
  194. struct uio ku;
  195. MAKENAME();
  196. /* vfs_open destroys the string it's passed */
  197. strcpy(buf, name);
  198. err = vfs_open(buf, O_RDONLY, 0664, &vn);
  199. if (err) {
  200. kprintf("Could not open test file for read: %s\n",
  201. strerror(err));
  202. return -1;
  203. }
  204. for (i=0; i<NCHUNKS; i++) {
  205. uio_kinit(&iov, &ku, buf, strlen(SLOGAN), bytes, UIO_READ);
  206. err = VOP_READ(vn, &ku);
  207. if (err) {
  208. kprintf("%s: Read error: %s\n", name, strerror(err));
  209. vfs_close(vn);
  210. return -1;
  211. }
  212. if (ku.uio_resid > 0) {
  213. kprintf("%s: Short read: %lu bytes left over\n", name,
  214. (unsigned long) ku.uio_resid);
  215. vfs_close(vn);
  216. return -1;
  217. }
  218. buf[strlen(SLOGAN)] = 0;
  219. rotate(buf, -i);
  220. if (strcmp(buf, SLOGAN)) {
  221. kprintf("%s: Test failed: line %d mismatched: %s\n",
  222. name, i+1, buf);
  223. vfs_close(vn);
  224. return -1;
  225. }
  226. bytes = ku.uio_offset;
  227. }
  228. vfs_close(vn);
  229. if (bytes != NCHUNKS*strlen(SLOGAN)) {
  230. kprintf("%s: %lu bytes read, should have been %lu!\n",
  231. name, (unsigned long) bytes,
  232. (unsigned long) (NCHUNKS*strlen(SLOGAN)));
  233. return -1;
  234. }
  235. kprintf("%s: %lu bytes read\n", name, (unsigned long) bytes);
  236. return 0;
  237. }
  238. ////////////////////////////////////////////////////////////
  239. static
  240. void
  241. dofstest(const char *filesys)
  242. {
  243. kprintf("*** Starting filesystem test on %s:\n", filesys);
  244. if (fstest_write(filesys, "", 1, 0)) {
  245. kprintf("*** Test failed\n");
  246. return;
  247. }
  248. if (fstest_read(filesys, "")) {
  249. kprintf("*** Test failed\n");
  250. return;
  251. }
  252. if (fstest_remove(filesys, "")) {
  253. kprintf("*** Test failed\n");
  254. return;
  255. }
  256. kprintf("*** Filesystem test done\n");
  257. }
  258. ////////////////////////////////////////////////////////////
  259. static
  260. void
  261. readstress_thread(void *fs, unsigned long num)
  262. {
  263. const char *filesys = fs;
  264. if (fstest_read(filesys, "")) {
  265. kprintf("*** Thread %lu: failed\n", num);
  266. }
  267. V(threadsem);
  268. }
  269. static
  270. void
  271. doreadstress(const char *filesys)
  272. {
  273. int i, err;
  274. init_threadsem();
  275. kprintf("*** Starting fs read stress test on %s:\n", filesys);
  276. if (fstest_write(filesys, "", 1, 0)) {
  277. kprintf("*** Test failed\n");
  278. return;
  279. }
  280. for (i=0; i<NTHREADS; i++) {
  281. err = thread_fork("readstress", NULL,
  282. readstress_thread, (char *)filesys, i);
  283. if (err) {
  284. panic("readstress: thread_fork failed: %s\n",
  285. strerror(err));
  286. }
  287. }
  288. for (i=0; i<NTHREADS; i++) {
  289. P(threadsem);
  290. }
  291. if (fstest_remove(filesys, "")) {
  292. kprintf("*** Test failed\n");
  293. return;
  294. }
  295. kprintf("*** fs read stress test done\n");
  296. }
  297. ////////////////////////////////////////////////////////////
  298. static
  299. void
  300. writestress_thread(void *fs, unsigned long num)
  301. {
  302. const char *filesys = fs;
  303. char numstr[8];
  304. snprintf(numstr, sizeof(numstr), "%lu", num);
  305. if (fstest_write(filesys, numstr, 1, 0)) {
  306. kprintf("*** Thread %lu: failed\n", num);
  307. V(threadsem);
  308. return;
  309. }
  310. if (fstest_read(filesys, numstr)) {
  311. kprintf("*** Thread %lu: failed\n", num);
  312. V(threadsem);
  313. return;
  314. }
  315. if (fstest_remove(filesys, numstr)) {
  316. kprintf("*** Thread %lu: failed\n", num);
  317. }
  318. kprintf("*** Thread %lu: done\n", num);
  319. V(threadsem);
  320. }
  321. static
  322. void
  323. dowritestress(const char *filesys)
  324. {
  325. int i, err;
  326. init_threadsem();
  327. kprintf("*** Starting fs write stress test on %s:\n", filesys);
  328. for (i=0; i<NTHREADS; i++) {
  329. err = thread_fork("writestress", NULL,
  330. writestress_thread, (char *)filesys, i);
  331. if (err) {
  332. panic("thread_fork failed %s\n", strerror(err));
  333. }
  334. }
  335. for (i=0; i<NTHREADS; i++) {
  336. P(threadsem);
  337. }
  338. kprintf("*** fs write stress test done\n");
  339. }
  340. ////////////////////////////////////////////////////////////
  341. static
  342. void
  343. writestress2_thread(void *fs, unsigned long num)
  344. {
  345. const char *filesys = fs;
  346. if (fstest_write(filesys, "", NTHREADS, num)) {
  347. kprintf("*** Thread %lu: failed\n", num);
  348. V(threadsem);
  349. return;
  350. }
  351. V(threadsem);
  352. }
  353. static
  354. void
  355. dowritestress2(const char *filesys)
  356. {
  357. int i, err;
  358. char name[32];
  359. struct vnode *vn;
  360. init_threadsem();
  361. kprintf("*** Starting fs write stress test 2 on %s:\n", filesys);
  362. /* Create and truncate test file */
  363. fstest_makename(name, sizeof(name), filesys, "");
  364. err = vfs_open(name, O_WRONLY|O_CREAT|O_TRUNC, 0664, &vn);
  365. if (err) {
  366. kprintf("Could not create test file: %s\n", strerror(err));
  367. kprintf("*** Test failed\n");
  368. return;
  369. }
  370. vfs_close(vn);
  371. for (i=0; i<NTHREADS; i++) {
  372. err = thread_fork("writestress2", NULL,
  373. writestress2_thread, (char *)filesys, i);
  374. if (err) {
  375. panic("writestress2: thread_fork failed: %s\n",
  376. strerror(err));
  377. }
  378. }
  379. for (i=0; i<NTHREADS; i++) {
  380. P(threadsem);
  381. }
  382. if (fstest_read(filesys, "")) {
  383. kprintf("*** Test failed\n");
  384. return;
  385. }
  386. if (fstest_remove(filesys, "")) {
  387. kprintf("*** Test failed\n");
  388. }
  389. kprintf("*** fs write stress test 2 done\n");
  390. }
  391. ////////////////////////////////////////////////////////////
  392. static
  393. void
  394. createstress_thread(void *fs, unsigned long num)
  395. {
  396. const char *filesys = fs;
  397. int i;
  398. char numstr[16];
  399. for (i=0; i<NCREATES; i++) {
  400. snprintf(numstr, sizeof(numstr), "%lu-%d", num, i);
  401. if (fstest_write(filesys, numstr, 1, 0)) {
  402. kprintf("*** Thread %lu: file %d: failed\n", num, i);
  403. V(threadsem);
  404. return;
  405. }
  406. if (fstest_read(filesys, numstr)) {
  407. kprintf("*** Thread %lu: file %d: failed\n", num, i);
  408. V(threadsem);
  409. return;
  410. }
  411. if (fstest_remove(filesys, numstr)) {
  412. kprintf("*** Thread %lu: file %d: failed\n", num, i);
  413. V(threadsem);
  414. return;
  415. }
  416. }
  417. V(threadsem);
  418. }
  419. static
  420. void
  421. docreatestress(const char *filesys)
  422. {
  423. int i, err;
  424. init_threadsem();
  425. kprintf("*** Starting fs create stress test on %s:\n", filesys);
  426. for (i=0; i<NTHREADS; i++) {
  427. #ifdef UW
  428. err = thread_fork("createstress", NULL,
  429. createstress_thread, (char *)filesys, i);
  430. #else
  431. err = thread_fork("createstress",
  432. createstress_thread, (char *)filesys, i,
  433. NULL);
  434. #endif
  435. if (err) {
  436. panic("createstress: thread_fork failed %s\n",
  437. strerror(err));
  438. }
  439. }
  440. for (i=0; i<NTHREADS; i++) {
  441. P(threadsem);
  442. }
  443. kprintf("*** fs create stress test done\n");
  444. }
  445. ////////////////////////////////////////////////////////////
  446. static
  447. int
  448. checkfilesystem(int nargs, char **args)
  449. {
  450. char *device;
  451. if (nargs != 2) {
  452. kprintf("Usage: fs[12345] filesystem:\n");
  453. return EINVAL;
  454. }
  455. device = args[1];
  456. /* Allow (but do not require) colon after device name */
  457. if (device[strlen(device)-1]==':') {
  458. device[strlen(device)-1] = 0;
  459. }
  460. return 0;
  461. }
  462. #define DEFTEST(testname) \
  463. int \
  464. testname(int nargs, char **args) \
  465. { \
  466. int result; \
  467. result = checkfilesystem(nargs, args); \
  468. if (result) { \
  469. return result; \
  470. } \
  471. do##testname(args[1]); \
  472. return 0; \
  473. }
  474. DEFTEST(fstest);
  475. DEFTEST(readstress);
  476. DEFTEST(writestress);
  477. DEFTEST(writestress2);
  478. DEFTEST(createstress);
  479. ////////////////////////////////////////////////////////////
  480. int
  481. printfile(int nargs, char **args)
  482. {
  483. struct vnode *rv, *wv;
  484. struct iovec iov;
  485. struct uio ku;
  486. off_t rpos=0, wpos=0;
  487. char buf[128];
  488. char outfile[16];
  489. int result;
  490. int done=0;
  491. if (nargs != 2) {
  492. kprintf("Usage: pf filename\n");
  493. return EINVAL;
  494. }
  495. /* vfs_open destroys the string it's passed; make a copy */
  496. strcpy(outfile, "con:");
  497. result = vfs_open(args[1], O_RDONLY, 0664, &rv);
  498. if (result) {
  499. kprintf("printfile: %s\n", strerror(result));
  500. return result;
  501. }
  502. result = vfs_open(outfile, O_WRONLY, 0664, &wv);
  503. if (result) {
  504. kprintf("printfile: output: %s\n", strerror(result));
  505. vfs_close(rv);
  506. return result;
  507. }
  508. while (!done) {
  509. uio_kinit(&iov, &ku, buf, sizeof(buf), rpos, UIO_READ);
  510. result = VOP_READ(rv, &ku);
  511. if (result) {
  512. kprintf("Read error: %s\n", strerror(result));
  513. break;
  514. }
  515. rpos = ku.uio_offset;
  516. if (ku.uio_resid > 0) {
  517. done = 1;
  518. }
  519. uio_kinit(&iov, &ku, buf, sizeof(buf)-ku.uio_resid, wpos,
  520. UIO_WRITE);
  521. result = VOP_WRITE(wv, &ku);
  522. if (result) {
  523. kprintf("Write error: %s\n", strerror(result));
  524. break;
  525. }
  526. wpos = ku.uio_offset;
  527. if (ku.uio_resid > 0) {
  528. kprintf("Warning: short write\n");
  529. }
  530. }
  531. vfs_close(wv);
  532. vfs_close(rv);
  533. return 0;
  534. }