PageRenderTime 162ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 2ms

/usr/src/tools/diskomizer/diskomizer64mpism.c

https://bitbucket.org/illumos/illumos-stc
C | 5417 lines | 4501 code | 528 blank | 388 comment | 1214 complexity | 83822d2969540770a7df01777bee7815 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. #pragma ident "%Z%%M% %I% %E% SMI"
  22. /*
  23. * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
  24. * Use is subject to license terms.
  25. */
  26. /*
  27. * diskomizer64mpism
  28. *
  29. * Write to and then read from disk partitions and or files.
  30. *
  31. * This is a test program.
  32. *
  33. * To do:
  34. *
  35. * The messages it prints out at the begining are a mess.
  36. * The code should be better commented.
  37. *
  38. * Chris.Gerhard@uk.sun.com - SMCC CTE
  39. */
  40. #include "args.h"
  41. #include "diskomizer64mpism.h"
  42. #include "bufs.h"
  43. #include "buf_init.h"
  44. #include <netdb.h>
  45. #include <sys/systeminfo.h>
  46. #include <tnf/probe.h>
  47. #include <sys/times.h>
  48. #include <diskomizer/log.h>
  49. #include "findap.h"
  50. #include "device_control.h"
  51. #include "timeval.h"
  52. #include "list_ops.h"
  53. #include "bits.h"
  54. #include "locks.h"
  55. #include "shm_ops.h"
  56. #include "signal_catch.h"
  57. #include "limit.h"
  58. #include "time.h"
  59. #include "prompt.h"
  60. #include "errors.h"
  61. #include "utils.h"
  62. #include "shared_device_info.h"
  63. #include "decode_errors.h"
  64. #include <sys/utsname.h>
  65. #include <sys/statvfs.h>
  66. #include <dlfcn.h>
  67. #include <diskomizer/uadmin.h>
  68. #include <usage_tracking/usage_tracking.h>
  69. #include "disko_usage_track.h"
  70. #ifdef __i386
  71. #include <note.h>
  72. #endif
  73. #define OPEN_BRACE '{'
  74. #define CLOSE_BRACE '}'
  75. #define DEFAULT_BLOCK_SIZE 0x200 /* 512 */
  76. static char diskomizer_str[] = "diskomizer";
  77. iolen_index_t max_disk_io_len;
  78. static char *diffs;
  79. static char diffs_str[] = "diffs";
  80. static char write_str[] = "write";
  81. static char read_str[] = "read";
  82. static const char nil[] = "nil";
  83. #define NIL(A) (A == NULL ? nil : A)
  84. static char *random_str;
  85. static hrtime_t stoptime;
  86. static time_t (*secs_till_exit)(void);
  87. static struct timeval start_time;
  88. void *usage_tracking_handle;
  89. enum read_type {
  90. NORMAL_READ,
  91. RETRY_READ,
  92. WRITE_READ,
  93. READ_ONLY_RAND,
  94. READ_ONLY_SEQ
  95. };
  96. typedef enum read_type read_type_t;
  97. typedef uchar_t (*initializer_t)(int buf, int i);
  98. static int exit_status = EXIT_SUCCESS;
  99. /*
  100. * The minimum block size that can be used. Essentially the lowest common
  101. * muliple of the blocksizes available.
  102. */
  103. static int min_block_size;
  104. struct proc_store {
  105. pid_t pid;
  106. };
  107. static struct proc_store *proc_store;
  108. /*
  109. * The daio_ops
  110. */
  111. static struct daio_ops *daio;
  112. /*
  113. * All the functions we have
  114. */
  115. time_t handle_read(struct aio_str *aiop, ullong_t start);
  116. time_t do_new_read(struct aio_str *aiop, ullong_t start, read_type_t read_type);
  117. time_t handle_readonly_rand(struct aio_str *aiop, ullong_t start);
  118. static int proc_no;
  119. long long convert_time(struct timeval tv);
  120. void update_time_stats(char off, struct times *tp, long long tyme,
  121. struct aio_str *aiop);
  122. static int pend_write_with_lock(bitmap_t map[], ullong_t off, int maplen);
  123. static int do_memcmp(ullong_t start, struct aio_str *aiop);
  124. static struct device *open_device(char *name, struct paths *, ullong_t size,
  125. int paths_to_use, int error_paths);
  126. static void check_matching_io(ullong_t start, struct aio_str *aiop);
  127. static int is_master(void);
  128. static void unwritten_block_rand(bitmap_t *map, struct aio_str *aiop,
  129. ullong_t start, ullong_t len, int maplen);
  130. static void unwritten_block_seq(bitmap_t *map, struct aio_str *aiop,
  131. ullong_t start, ullong_t len, int maplen);
  132. extern void close_and_free_paths(struct device *dev);
  133. extern void run_func(uchar_t *buf, size_t size);
  134. static struct bufhdr
  135. build_bufhr(struct device *dev, ullong_t start, ullong_t off);
  136. void newfd(struct aio_str *aiop);
  137. struct fds *open_path(struct device *devp, char *name, ullong_t size);
  138. void cancel_all_io_byfd(struct fds *fd);
  139. struct fds *find_path(struct fds *fdhead, char path_id);
  140. static int check_for_duplicate_paths(struct device *devp);
  141. static void do_start_cancelled_io(struct device *devices, ullong_t start);
  142. static int return_zero(void);
  143. static int return_one(void);
  144. /*
  145. * The error handling functions.
  146. */
  147. static loop_type on_error_reread(ullong_t start, struct aio_str *aiop);
  148. static loop_type on_error_exit(ullong_t start, struct aio_str *aiop);
  149. static loop_type on_error_stop(ullong_t start, struct aio_str *aiop);
  150. static loop_type on_error_nop(ullong_t start, struct aio_str *aiop);
  151. static loop_type on_error_abort(ullong_t start, struct aio_str *aiop);
  152. static loop_type on_error_pause(ullong_t start, struct aio_str *aiop);
  153. static loop_type on_error_retry(ullong_t start, struct aio_str *aiop);
  154. static loop_type on_error_rewrite(ullong_t start, struct aio_str *aiop);
  155. static loop_type on_error_fail_path(ullong_t start, struct aio_str *aiop);
  156. /*
  157. * Error handling init functions.
  158. */
  159. static int init_path_stop_check(void);
  160. static int init_stop_check(void);
  161. /*
  162. * the "globals" that we use
  163. */
  164. struct device *devices; /* all the devices there are */
  165. write_buf_initializer_t init_uchar_func;
  166. static read_buf_initializer_t read_buffer_initializer;
  167. pid_t pgrp;
  168. /*
  169. * statics
  170. */
  171. static pid_t parent_pid;
  172. static pid_t killer_pid;
  173. static on_error_t *on_error_corrupt;
  174. static on_error_t *on_error_short;
  175. static on_error_t *on_write_error;
  176. static int Longest_device_name = 0;
  177. static int Longest_logical_name = 0;
  178. static int write_loops;
  179. static int usr1_exit = 0;
  180. struct shm_ops *shm_ops;
  181. /*
  182. * Count of the total number of io's that are currently cancelled.
  183. */
  184. static int cancelled_count = 0;
  185. /*
  186. * Start cancelled. Only gets unset from nop if there are cancelled ios
  187. * to restart. A rare thing.
  188. */
  189. static void (*start_cancelled_io)(struct device *, ullong_t) =
  190. (void (*)(struct device *, ullong_t)) nop;
  191. static void (*start_deferred)(struct device *dev, ullong_t) =
  192. (void (*)(struct device *, ullong_t)) nop;
  193. static int (*stop_check)(void *handle) = (int (*)(void *))return_zero;
  194. static int (*path_stop_check)(struct fds *fd, struct device *dev) =
  195. (int (*)(struct fds *, struct device *dev))return_zero;
  196. static char nom[] = "no memory";
  197. #define NOT_NULL(A) (A == NULL ? &nom[0] : A)
  198. #define PLURAL(A) (A == 1 ? "" : "s")
  199. #define LEN_BYTES2BLOCKS(A) (A->length / INDEX_TO_DIOLEN(max_disk_io_len))
  200. #define TRUE_OR_FALSE(A) (A ? "true" : "false")
  201. struct error_handlers {
  202. char *name; /* String that describes this error handler */
  203. on_error_t func; /* error handleing function */
  204. int (*setup)(void); /* init routine for the error handler */
  205. uint_t breaker:1; /* Is this the last error handler on the list */
  206. uint_t rw:2; /* Does this hander apply to read or write or both */
  207. };
  208. #define READ_ERR 1
  209. #define WRITE_ERR (READ_ERR << 1)
  210. #define BOTH_ERR (READ_ERR | WRITE_ERR)
  211. struct error_handlers on_error_table[] = {
  212. {"EXIT", on_error_exit, return_one, 1, BOTH_ERR},
  213. {"ABORT", on_error_abort, return_one, 1, BOTH_ERR},
  214. {"CONTINUE", on_error_nop, return_one, 0, BOTH_ERR},
  215. {"NONE", on_error_nop, return_one, 0, BOTH_ERR},
  216. {"STOP", on_error_stop, init_stop_check, 1, BOTH_ERR},
  217. {"PAUSE", on_error_pause, return_one, 0, BOTH_ERR},
  218. {"RETRY", on_error_retry, return_one, 0, BOTH_ERR},
  219. {"FAIL_PATH", on_error_fail_path, init_path_stop_check, 0, BOTH_ERR},
  220. {"UADMIN", on_error_uadmin, uadmin_init, 1, BOTH_ERR},
  221. {"REREAD", on_error_reread, return_one, 0, READ_ERR},
  222. {"REWRITE", on_error_rewrite, return_one, 0, WRITE_ERR}
  223. };
  224. /*
  225. * TNF declarations.
  226. */
  227. /*
  228. * The DEFINE should not have explicit mentions of the daio_ZZZZ
  229. * elements, they should be opaque.
  230. */
  231. TNF_DEFINE_RECORD_5(aio_str_t, aio_tnf_str,
  232. tnf_opaque, buf, tnf_short, iolen, tnf_ulonglong, off,
  233. tnf_longlong, aio_res.result.daio_return,
  234. tnf_uint, aio_res.result.daio_errno)
  235. /*
  236. * locking functions.
  237. */
  238. static char *
  239. hostname(void)
  240. {
  241. static char hostname[MAXHOSTNAMELEN + 1];
  242. (void) sysinfo(SI_HOSTNAME, &hostname[0], MAXHOSTNAMELEN);
  243. return (&hostname[0]);
  244. }
  245. int
  246. this_proc(void)
  247. {
  248. return (proc_no);
  249. }
  250. static int
  251. return_one(void)
  252. {
  253. return (1);
  254. }
  255. static int
  256. return_zero(void)
  257. {
  258. return (0);
  259. }
  260. void
  261. nop(void)
  262. {
  263. }
  264. static void
  265. not_null_free(void *ptr)
  266. {
  267. if (ptr != NULL)
  268. free(ptr);
  269. }
  270. static time_t
  271. inf_secs_till_exit(void)
  272. {
  273. return (LONG_MAX);
  274. }
  275. static time_t
  276. do_secs_till_exit(void)
  277. {
  278. return ((stoptime - gethrtime()) / BILLION);
  279. }
  280. off64_t
  281. start_offset(void)
  282. {
  283. return ((off64_t)(opts.start_offset *
  284. (ullong_t)INDEX_TO_DIOLEN(max_disk_io_len)));
  285. }
  286. static void
  287. return_aio_read_buf(struct aio_str *aiop)
  288. {
  289. if (!(aiop->count % opts.expert_release_read_buffers_after_n_uses) &&
  290. aiop->buf != NULL) {
  291. return_read_buf(aiop->buf);
  292. aiop->buf = NULL;
  293. }
  294. }
  295. static int
  296. do_stop_check(void *handle)
  297. {
  298. return (get_shared_stop_flag(handle, this_proc()));
  299. }
  300. int
  301. is_readonly(void)
  302. {
  303. return (opts.o_rdonly == 1);
  304. }
  305. const char *
  306. rw_string(void)
  307. {
  308. return (is_readonly() ? read_str : write_str);
  309. }
  310. /*
  311. * background. disassociate from controlling tty make session leader
  312. * then fork. The parent exits and the child goes on in
  313. * the back ground.
  314. */
  315. static void
  316. background()
  317. {
  318. pid_t pid;
  319. (void) freopen("/dev/null", "+r", stdin);
  320. pid = opts.use_fork1 == 0 ? fork() : fork1();
  321. if (pid == 0) {
  322. if (setsid() == (pid_t)-1)
  323. pperror("setsid");
  324. return;
  325. }
  326. if (pid < 0) {
  327. FORK_ERROR(opts.use_fork1 == 0 ? "" : "s");
  328. exit(1);
  329. }
  330. exit(0);
  331. }
  332. static struct blks *
  333. aio_attach(struct aio_str *aiop)
  334. {
  335. int error_count = 0;
  336. struct blks *blocks;
  337. while ((blocks = shm_ops->attach(AIO_BLOCK_HANDLE(aiop))) ==
  338. NULL) {
  339. if (error_count++ % 10000 == 0)
  340. ATTACH_ERROR(AIO_BLOCK_HANDLE(aiop));
  341. }
  342. if (error_count > 0)
  343. plog(LOG_WARNING, "attached o.k.\n");
  344. return (blocks);
  345. }
  346. static void
  347. update_aio_time_stats(struct aio_str *aiop, struct times *ts)
  348. {
  349. if (aiop->count > 0) {
  350. ullong_t len = LEN_BYTES2BLOCKS(aiop->dev);
  351. update_time_stats((100 * MIN(aiop->dev->block, len))/
  352. ((aiop->dev->length/
  353. INDEX_TO_DIOLEN(max_disk_io_len))),
  354. ts,
  355. DAIO_GET_TIME_TAKEN(aiop->aio_res), aiop);
  356. }
  357. }
  358. static void
  359. update_aio_read_stats(struct aio_str *aiop)
  360. {
  361. update_aio_time_stats(aiop, &aiop->fd->read_times);
  362. }
  363. static void
  364. update_aio_write_stats(struct aio_str *aiop)
  365. {
  366. update_aio_time_stats(aiop, &aiop->fd->write_times);
  367. }
  368. ullong_t
  369. diskomizer_off2byteoff(ullong_t off)
  370. {
  371. return ((off + opts.start_offset) * INDEX_TO_DIOLEN(max_disk_io_len));
  372. }
  373. static ullong_t
  374. byteoff2diskomizer_off(ullong_t off)
  375. {
  376. return ((off/INDEX_TO_DIOLEN(max_disk_io_len)) - opts.start_offset);
  377. }
  378. /*
  379. * Sanity check.
  380. */
  381. #define ASSERT_OFFSET(X) \
  382. assert(byteoff2diskomizer_off(diskomizer_off2byteoff(X)) == X)
  383. static ullong_t
  384. aio_str2byteoff(struct aio_str *aiop)
  385. {
  386. return (diskomizer_off2byteoff(aiop->off));
  387. }
  388. static int64_t
  389. aio_str2lba(struct aio_str *aiop)
  390. {
  391. long long byteoff;
  392. int64_t lba;
  393. if (aiop->dev->v_part == NULL || aiop->dev->device_block_size == 0) {
  394. return (-1);
  395. }
  396. byteoff = aio_str2byteoff(aiop);
  397. lba = byteoff / (int64_t)aiop->dev->device_block_size;
  398. return (aiop->dev->v_part->p_start + lba);
  399. }
  400. static void
  401. plog_dd(int pri, struct aio_str *aiop)
  402. {
  403. if ((INDEX_TO_DIOLEN(max_disk_io_len) %
  404. INDEX_TO_DIOLEN(aiop->iolen)) == 0) {
  405. daio->plog_dd(pri, aiop->fd->fd, INDEX_TO_DIOLEN(aiop->iolen),
  406. INDEX_TO_DIOLEN(max_disk_io_len),
  407. aio_str2byteoff(aiop));
  408. }
  409. }
  410. static void
  411. report_device(int pri, struct aio_str *aiop)
  412. {
  413. plog(pri, "Requested File %s (%s)\n",
  414. aiop->fd->name, aiop->dev->logicalname);
  415. }
  416. /*
  417. * report block.
  418. *
  419. * Report all the information about the block that was requested to be read
  420. */
  421. static void
  422. report_offset(int pri, struct aio_str *aiop)
  423. {
  424. long long byteoff;
  425. long long lba;
  426. byteoff = aio_str2byteoff(aiop);
  427. lba = aio_str2lba(aiop);
  428. if (lba >= 0) {
  429. plog(pri,
  430. "Requested File offset 0t%lld (0x%llx), block size "
  431. "0t%d (0x%x), LBA 0t%lld (0x%llx)\n",
  432. byteoff, byteoff, INDEX_TO_DIOLEN(aiop->iolen),
  433. INDEX_TO_DIOLEN(aiop->iolen), lba, lba);
  434. } else {
  435. plog(pri, "Requested File offset 0t%lld (0x%llx), block size "
  436. "0t%d (0x%x)\n", byteoff, byteoff,
  437. INDEX_TO_DIOLEN(aiop->iolen),
  438. INDEX_TO_DIOLEN(aiop->iolen));
  439. }
  440. }
  441. static void
  442. report_device_and_offset(int pri, struct aio_str *aiop)
  443. {
  444. report_device(pri, aiop);
  445. report_offset(pri, aiop);
  446. }
  447. /*
  448. * report_error. This is the generic error reporting routine.
  449. * It reports all errors to stderr, giving similar information
  450. * and advise as to other commands that can be tried.
  451. */
  452. void
  453. report_error(struct aio_str *aiop, const union err_info u, err_type error)
  454. {
  455. struct timeval now_tv;
  456. long long disk_block;
  457. int pri;
  458. while (my_gettimeofday(&now_tv, NULL) == -1)
  459. pperror("gettimeofday");
  460. disk_block = aio_str2byteoff(aiop);
  461. mutex->stderr_enter();
  462. if (error == ERR_HUNG) {
  463. pri = LOG_WARNING;
  464. time_log(pri, now_tv.tv_sec, "Time now");
  465. time_log(pri, aiop->tv.tv_sec, "Requested io requested at");
  466. report_device(pri, aiop);
  467. if (is_readonly()) {
  468. plog(pri, "%s has %ld out of %ld read%s\n",
  469. aiop->dev->logicalname,
  470. aiop->fd->number_of_hung_read,
  471. aiop->fd->total_read,
  472. aiop->fd->number_of_hung_read == 1 ? "" : "s");
  473. } else {
  474. plog(pri, "%s has %ld out of %ld read%s and %ld "
  475. "out of %ld write%s\n",
  476. aiop->dev->logicalname,
  477. aiop->fd->number_of_hung_read,
  478. aiop->fd->total_read,
  479. aiop->fd->number_of_hung_read == 1 ? "" : "s",
  480. aiop->fd->number_of_hung_write,
  481. aiop->fd->total_write,
  482. aiop->fd->number_of_hung_write == 1 ? "" : "s");
  483. }
  484. plog(pri, "waiting for more than %ld second%s\n",
  485. u.time, PLURAL(u.time));
  486. if (is_readonly()) {
  487. plog(pri, "Last read took %lld\n",
  488. aiop->fd->last_read_time/BILLION);
  489. } else {
  490. plog(pri, "Last read took %lld, last write took "
  491. "%lld seconds\n",
  492. aiop->fd->last_read_time/BILLION,
  493. aiop->fd->last_write_time/BILLION);
  494. }
  495. plog(pri,
  496. "oldest i/o is a %s waiting for %ld second%s\n",
  497. is_read_io(aiop) ? "read" : "write",
  498. now_tv.tv_sec - aiop->tv.tv_sec,
  499. ((now_tv.tv_sec - aiop->tv.tv_sec) > 1) ? "s" : "");
  500. } else if (error == ERR_CORRUPT) {
  501. time_t request_time;
  502. time_t return_time;
  503. int read_count;
  504. struct blks *blocks;
  505. struct fds *fd;
  506. pri = LOG_ERR;
  507. time_log(pri, now_tv.tv_sec, "Time now");
  508. time_log(pri, aiop->tv.tv_sec, "Requested io requested at");
  509. report_device_and_offset(pri, aiop);
  510. report_error_desc(pri, aiop, u.str);
  511. blocks = aio_attach(aiop);
  512. fd = find_path(aiop->dev->fdhead,
  513. blocks[AIO_BLOCK_INDEX(aiop)].path_id);
  514. assert(fd != NULL);
  515. read_count = blocks[AIO_BLOCK_INDEX(aiop)].read_count;
  516. request_time = blocks[AIO_BLOCK_INDEX(aiop)].last_requested;
  517. return_time = request_time + GET_LAST_RETURN(
  518. blocks[AIO_BLOCK_INDEX(aiop)].last_returned_delta);
  519. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  520. time_log(pri, blocks[AIO_BLOCK_INDEX(aiop)].last_requested,
  521. "Last %s to the requested block submitted", rw_string());
  522. time_log(pri, return_time,
  523. "Last %s to the requested block returned", rw_string());
  524. plog(pri, "Last %s to the requested block used path: %s\n",
  525. rw_string(), fd->name);
  526. if (!is_readonly()) {
  527. plog(pri, "Requested block has been read %d times "
  528. "since last written\n", read_count);
  529. }
  530. decode_errors(pri, aiop, read_count);
  531. } else if (error == ERR_DEFERRED) {
  532. pri = LOG_WARNING;
  533. time_log(pri, now_tv.tv_sec, "Time now");
  534. time_log(pri, aiop->tv.tv_sec, "Requested io requested at");
  535. plog(pri, "%s to device %s deferred\n",
  536. u.str, aiop->dev->logicalname);
  537. } else if (DAIO_RETURN(aiop->aio_res) < 0) {
  538. char *datestr;
  539. pri = LOG_ERR;
  540. time_log(pri, now_tv.tv_sec, "Time now");
  541. time_log(pri, aiop->tv.tv_sec, "Requested io requested at");
  542. (void) plog(pri, "%s %s%s error, errno %d %s\n",
  543. aiop->fd->name, u.str,
  544. aiop->retrycnt > 0 ? " retry" : "",
  545. DAIO_ERROR(aiop->aio_res),
  546. strerror(DAIO_ERROR(aiop->aio_res)));
  547. datestr = alloc_time_str_fmt(aiop->tv.tv_sec, "%b %e %H:%M");
  548. if (datestr != NULL) {
  549. plog(pri, "Try \"egrep '^%s.*%s' "
  550. "/var/adm/messages\"\n",
  551. datestr, hostname());
  552. free(datestr);
  553. }
  554. } else {
  555. pri = LOG_WARNING;
  556. time_log(pri, now_tv.tv_sec, "Time now");
  557. time_log(pri, aiop->tv.tv_sec, "Requested io requested at");
  558. plog(pri, "%s short %s%s, Transferred %ld (%#lx)"
  559. " bytes, requested %d (%#x) bytes.\n",
  560. aiop->fd->name, u.str,
  561. aiop->retrycnt > 0 ? " retry" : "",
  562. (long)DAIO_RETURN(aiop->aio_res),
  563. (long)DAIO_RETURN(aiop->aio_res),
  564. INDEX_TO_DIOLEN(aiop->iolen),
  565. INDEX_TO_DIOLEN(aiop->iolen));
  566. }
  567. dlog(pri, "Block at byte offset 0t%lld (%#llx) block size %d (%#x)\n",
  568. disk_block, disk_block, INDEX_TO_DIOLEN(aiop->iolen),
  569. INDEX_TO_DIOLEN(aiop->iolen));
  570. plog_dd(pri, aiop);
  571. (void) fflush(stderr);
  572. (void) fsync(fileno(stderr));
  573. mutex->stderr_exit();
  574. }
  575. ulong_t
  576. my_lrand(void)
  577. {
  578. union {
  579. ulong_t l;
  580. uint32_t i[sizeof (ulong_t)/sizeof (uint32_t)];
  581. } u;
  582. int i;
  583. #ifdef __lint
  584. ZERO_OBJ(u);
  585. #endif
  586. for (i = 0; i < (sizeof (ulong_t)/sizeof (uint32_t)); i++)
  587. u.i[i] = (uint32_t)lrand48();
  588. return (u.l);
  589. }
  590. static void
  591. remove_from_all_aios(struct aio_str *aiop)
  592. {
  593. remove_from_aio_list(&aiop->fd->all_aios, aiop);
  594. }
  595. static void
  596. infantacide(void)
  597. {
  598. (void) killpg(pgrp, SIGTERM);
  599. }
  600. /*ARGSUSED*/
  601. static loop_type
  602. on_error_exit(ullong_t start, struct aio_str *aiop)
  603. {
  604. union err_info err_info;
  605. err_info.str = "On error exit";
  606. DAIO_SET_RETURN(aiop->aio_res, 0);
  607. report_error(aiop, err_info, ERR_SYS);
  608. remove_from_all_aios(aiop);
  609. (void) sighold(SIGTERM);
  610. if (incr_shared_device_error(aiop->dev->shared_data_handle,
  611. aiop->dev->errors) != -1) {
  612. aiop->dev->errors = 0;
  613. }
  614. exit_status = EXIT_FAILURE;
  615. exit(exit_status);
  616. /*NOTREACHED*/
  617. return (BREAK);
  618. }
  619. /*ARGSUSED*/
  620. static loop_type
  621. on_error_stop(ullong_t start, struct aio_str *aiop)
  622. {
  623. pfprintf(stderr, "%s Set On error stop\n", aiop->fd->name);
  624. if (set_shared_stop_flag(aiop->dev->shared_data_handle) == -1)
  625. aiop->dev->need_to_stop = 1;
  626. aiop->dev->stop_flag = 1;
  627. return (BREAK);
  628. }
  629. /*ARGSUSED*/
  630. static loop_type
  631. on_error_nop(ullong_t start, struct aio_str *aiop)
  632. {
  633. if (is_write_io(aiop)) {
  634. struct blks *blocks;
  635. /*
  636. * Clear the last io as the retries never suceeded
  637. * so we don't read this block which is now in an
  638. * undefined state.
  639. */
  640. blocks = aio_attach(aiop);
  641. blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io = NULL;
  642. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  643. }
  644. pfprintf(stderr, "%s On error continue\n", aiop->fd->name);
  645. return (BREAK);
  646. }
  647. /*ARGSUSED*/
  648. static loop_type
  649. on_error_abort(ullong_t start, struct aio_str *aiop)
  650. {
  651. union err_info err_info;
  652. err_info.str = "On error abort";
  653. report_error(aiop, err_info, ERR_SYS);
  654. /* pfprintf(stderr, "On error abort\n"); */
  655. (void) sighold(SIGTERM);
  656. if (incr_shared_device_error(aiop->dev->shared_data_handle,
  657. aiop->dev->errors) != -1) {
  658. aiop->dev->errors = 0;
  659. }
  660. abort(); /* On error abort. This one is o.k. */
  661. return (BREAK);
  662. }
  663. /*
  664. * report_hangers_fd. counts the number of I/O requests that
  665. * have been waiting for more than hanger_time seconds and then
  666. * calls report_error() with the i/o that has been waiting the
  667. * longest and a count of the number of i/o requests that are
  668. * over time. It only calls report_error() when the number of i/o
  669. * requests or the oldest outstanding i/o change or if the last
  670. * report was more than hanger_time seconds ago and there are some
  671. * i/o hung.
  672. */
  673. static int
  674. report_hangers_fd(struct fds *fd, time_t tyme, time_t hanger_time)
  675. {
  676. int total_hung_read = 0;
  677. int total_hung_write = 0;
  678. #ifdef IO_COUNT_DEBUG
  679. int total_read = 0;
  680. int total_write = 0;
  681. #endif
  682. struct aio_str *aiop;
  683. union err_info err_info;
  684. if (fd->error_path != 0 || fd->stop_flag != 0) {
  685. return (0);
  686. }
  687. /* fd->total_read = fd->total_write = 0; */
  688. err_info.time = hanger_time;
  689. for (aiop = fd->all_aios.head; aiop != NULL; aiop = aiop->next) {
  690. if (tyme - aiop->tv.tv_sec > hanger_time) {
  691. if (is_read_io(aiop)) {
  692. total_hung_read++;
  693. } else {
  694. total_hung_write++;
  695. }
  696. } else {
  697. break;
  698. }
  699. }
  700. #ifdef IO_COUNT_DEBUG
  701. assert(total_read == fd->total_read);
  702. assert(total_write == fd->total_write);
  703. #endif
  704. if (fd->oldest_io == NULL) {
  705. fd->oldest_io = fd->all_aios.head;
  706. }
  707. if (total_hung_read != fd->number_of_hung_read ||
  708. total_hung_write != fd->number_of_hung_write ||
  709. (fd->all_aios.head != fd->oldest_io &&
  710. (total_hung_read || total_hung_write))) {
  711. fd->number_of_hung_read = total_hung_read;
  712. fd->number_of_hung_write = total_hung_write;
  713. report_error(fd->all_aios.head, err_info, ERR_HUNG);
  714. fd->last_report = tyme;
  715. fd->oldest_io = fd->all_aios.head;
  716. } else if (total_hung_read + total_hung_write > 0 &&
  717. fd->all_aios.head != NULL && fd->last_report + hanger_time < tyme) {
  718. report_error(fd->all_aios.head, err_info, ERR_HUNG);
  719. fd->last_report = tyme;
  720. }
  721. return (total_hung_read + total_hung_write);
  722. }
  723. /*
  724. * Search the list of i/o that are currently outstanding and report
  725. * on any that have been outstanding for more than hanger_time.
  726. * Also display howlong the oldest i/o has been Waiting for and when
  727. * it was submitted.
  728. */
  729. static void
  730. report_hangers(struct device *dev, time_t tyme, time_t hanger_time)
  731. {
  732. int total = 0;
  733. struct fds *fd;
  734. for (fd = dev->fdhead; ; fd = fd->next) {
  735. total += report_hangers_fd(fd, tyme, hanger_time);
  736. if (dev->fdhead == fd->next)
  737. break;
  738. }
  739. }
  740. static void
  741. report_all_hangers(struct device *dev, time_t hanger_time)
  742. {
  743. struct timeval tv;
  744. while (my_gettimeofday(&tv, NULL) == -1)
  745. pperror("gettimeofday");
  746. for (; dev != NULL; dev = dev->next) {
  747. report_hangers(dev, tv.tv_sec, hanger_time);
  748. }
  749. }
  750. char *
  751. my_strdup(const char *s)
  752. {
  753. char *x = strdup(s);
  754. if (x == NULL) {
  755. STRDUP_ERROR(s);
  756. }
  757. return (x);
  758. }
  759. void *
  760. my_calloc(long a, long b)
  761. {
  762. void *x;
  763. x = calloc(a, b);
  764. if (x == NULL) {
  765. CALLOC_ERROR(a, b);
  766. return (NULL);
  767. }
  768. return (x);
  769. }
  770. static void
  771. add_to_all_aios(struct aio_str *aiop)
  772. {
  773. add_to_aio_list(&aiop->fd->all_aios, aiop);
  774. }
  775. static int
  776. init_read(struct aio_str aio[], ullong_t start)
  777. {
  778. struct aio_str *aiop;
  779. aiop = my_calloc(1, sizeof (struct aio_str));
  780. if (aiop == NULL) {
  781. pfprintf(stderr, "init_read, can't allocate memory\n");
  782. return (0);
  783. }
  784. aiop->buf = NULL;
  785. aiop->off = 0;
  786. aiop->handler = is_readonly() ? handle_readonly_rand : handle_read;
  787. aiop->dev = aio->dev;
  788. aiop->fd = aio->dev->fdhead;
  789. aio->dev->fdhead = aio->dev->fdhead->next;
  790. add_to_all_aios(aiop);
  791. (void) do_new_read(aiop, start,
  792. is_readonly() ? READ_ONLY_RAND : NORMAL_READ);
  793. return (1);
  794. }
  795. static int
  796. has_no_unwritten(struct aio_str *aiop)
  797. {
  798. return (aiop->dev->unwritten == NULL);
  799. }
  800. static void
  801. push_unwritten(struct aio_str *aiop)
  802. {
  803. struct offset_list *new;
  804. if ((new = calloc(1, sizeof (struct offset_list))) == NULL) {
  805. CALLOC_ERROR(1, sizeof (struct offset_list));
  806. aiop->dev->failed_to_push_unwritten = 1;
  807. } else {
  808. plog(LOG_DEBUG,
  809. "Block %#llx (0t%lld) %s pushed onto unwritten queue\n",
  810. aiop->off, aiop->off, aiop->dev->logicalname);
  811. new->offset = aiop->off;
  812. new->next = aiop->dev->unwritten;
  813. aiop->dev->unwritten = new;
  814. aiop->dev->choose_block = unwritten_block_seq;
  815. }
  816. }
  817. static int
  818. find_unwritten(bitmap_t *map, struct aio_str *aiop, int maplen)
  819. {
  820. struct offset_list *u, *p;
  821. int status = 0;
  822. p = NULL;
  823. for (u = aiop->dev->unwritten; u != NULL; u = u->next) {
  824. if (pend_write_with_lock(map, u->offset, maplen) == 0) {
  825. if (p == NULL)
  826. aiop->dev->unwritten = u->next;
  827. else
  828. p->next = u->next;
  829. aiop->off = u->offset;
  830. plog(LOG_DEBUG,
  831. "Block %#llx (0t%lld) %s locked and removed "
  832. "from unwritten queue\n",
  833. aiop->off, aiop->off, aiop->dev->logicalname);
  834. free(u);
  835. status = 1;
  836. break;
  837. }
  838. p = u;
  839. }
  840. return (status);
  841. }
  842. static int
  843. is_unwritten(struct aio_str *aiop)
  844. {
  845. struct offset_list *u;
  846. for (u = aiop->dev->unwritten; u != NULL; u = u->next) {
  847. if (u->offset == aiop->off)
  848. return (1);
  849. }
  850. return (0);
  851. }
  852. /*
  853. * randomish_block
  854. * return a random block to try to do io too or from. If we
  855. * are short of memory the block is less random to try and
  856. * decrease the number of attach/detach pairs that actually
  857. * result in system calls, and therefore reduce the number of
  858. * faults.
  859. *
  860. * In particular when short of memory the next io will tend to
  861. * be in the same block ob blks structures or the next block
  862. * for odd numbered processes and the previous block for even
  863. * numbered processes. The overall effect is still close to
  864. * random at the device, but individual processes thrash less.
  865. */
  866. ulong_t
  867. randomish_block(struct aio_str *aiop)
  868. {
  869. ulong_t t;
  870. if (shm_ops->is_short_of_mem()) {
  871. t = aiop->off + ((my_lrand() % (shm_ops->max_size() /
  872. sizeof (struct blks))) * this_proc() % 2 ? -1 : 1);
  873. } else {
  874. t = my_lrand();
  875. }
  876. return (t);
  877. }
  878. /*ARGSUSED2*/
  879. void
  880. rand_block(bitmap_t *map, struct aio_str *aiop,
  881. ullong_t start, ullong_t len, int maplen)
  882. {
  883. ulong_t t;
  884. ullong_t *next_io_blk_ptr;
  885. if (is_write_io(aiop)) {
  886. next_io_blk_ptr = &aiop->dev->next_write_blk;
  887. if (!(aiop->dev->next_write_blk %
  888. opts.expert_write_cluster_length)) {
  889. t = randomish_block(aiop);
  890. t = t - (t % opts.expert_write_cluster_length);
  891. } else {
  892. TNF_PROBE_1(cluster_write, "rand_block",
  893. "sunw%cte%diskomizer%blocks write cluster",
  894. tnf_ulonglong, next_read_blk,
  895. aiop->dev->next_read_blk);
  896. t = *next_io_blk_ptr;
  897. }
  898. } else {
  899. next_io_blk_ptr = &aiop->dev->next_read_blk;
  900. if (!(aiop->dev->next_read_blk %
  901. opts.expert_read_cluster_length)) {
  902. t = randomish_block(aiop);
  903. t = t - (t % opts.expert_read_cluster_length);
  904. } else {
  905. TNF_PROBE_1(cluster_read, "rand_block",
  906. "sunw%cte%diskomizer%blocks read cluster",
  907. tnf_ulonglong, next_read_blk,
  908. aiop->dev->next_read_blk);
  909. t = *next_io_blk_ptr;
  910. }
  911. }
  912. aiop->off = (t)%(len);
  913. aiop->off = find_next_free(map, aiop->off, len, maplen);
  914. if (next_io_blk_ptr != NULL)
  915. *next_io_blk_ptr = aiop->off + 1;
  916. assert(aiop->off < len);
  917. }
  918. static void
  919. unwritten_block(bitmap_t *map, struct aio_str *aiop,
  920. ullong_t start, ullong_t len, int maplen, choose_block_t chooser)
  921. {
  922. plog(LOG_DEBUG, "in unwritten_block %llx\n",
  923. aiop->dev->unwritten == NULL ?
  924. 0LL : aiop->dev->unwritten->offset);
  925. if (find_unwritten(map, aiop, maplen)) {
  926. struct blks *blocks;
  927. if (has_no_unwritten(aiop))
  928. aiop->dev->choose_block = chooser;
  929. blocks = aio_attach(aiop);
  930. blocks[AIO_BLOCK_INDEX(aiop)].u.was_unwritten = 1;
  931. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  932. } else {
  933. chooser(map, aiop, start, len, maplen);
  934. }
  935. }
  936. /*
  937. * find the "next" block to read for this aio_str. Increment by
  938. * by the number of processes, so that when initializing the disk
  939. * each block only gets written once.
  940. */
  941. static void
  942. seq_block(bitmap_t *map, struct aio_str *aiop,
  943. ullong_t start, ullong_t len, int maplen)
  944. {
  945. int does_not_have_lock;
  946. assert((aiop->dev->block % opts.nprocs) == this_proc() ||
  947. aiop->dev->block == len);
  948. aiop->off = aiop->dev->block;
  949. if (aiop->dev->block >= len) {
  950. does_not_have_lock = 1;
  951. } else while ((does_not_have_lock =
  952. pend_write_with_lock(map, aiop->off, maplen)) != 0) {
  953. /*
  954. * Only push blocks that this process would have to write
  955. * onto the unwritten queue. This only effects the last
  956. * block on the device.
  957. */
  958. if ((aiop->off % opts.nprocs) == this_proc()) {
  959. push_unwritten(aiop);
  960. }
  961. aiop->dev->block += opts.nprocs;
  962. aiop->off = aiop->dev->block % len;
  963. if (aiop->dev->block >= len) {
  964. aiop->dev->block = len;
  965. break;
  966. }
  967. }
  968. if (does_not_have_lock != 0) {
  969. if (aiop->dev->seq_passes == 0 ||
  970. --aiop->dev->seq_passes == 0) {
  971. time_now_log(LOG_NOTICE,
  972. "Finished sequential %ss on %s count %d",
  973. is_readonly() ? read_str : write_str,
  974. aiop->dev->logicalname,
  975. aiop->count);
  976. if (has_no_unwritten(aiop)) {
  977. aiop->dev->choose_block = rand_block;
  978. } else {
  979. aiop->dev->choose_block = unwritten_block_rand;
  980. }
  981. } else {
  982. aiop->off = aiop->dev->block = this_proc();
  983. time_now_log(LOG_NOTICE,
  984. "Starting sequential series again on %s counts %d",
  985. aiop->dev->logicalname, aiop->count);
  986. }
  987. aiop->dev->choose_block(map, aiop, start, len, maplen);
  988. } else {
  989. assert(!does_not_have_lock);
  990. ASSERT_OFFSET(aiop->dev->block);
  991. aiop->dev->block += opts.nprocs;
  992. if (aiop->dev->block >= len) {
  993. aiop->dev->block = len;
  994. }
  995. }
  996. }
  997. static void
  998. unwritten_block_seq(bitmap_t *map, struct aio_str *aiop,
  999. ullong_t start, ullong_t len, int maplen)
  1000. {
  1001. unwritten_block(map, aiop, start, len, maplen, seq_block);
  1002. }
  1003. static void
  1004. unwritten_block_rand(bitmap_t *map, struct aio_str *aiop,
  1005. ullong_t start, ullong_t len, int maplen)
  1006. {
  1007. unwritten_block(map, aiop, start, len, maplen, rand_block);
  1008. }
  1009. /*
  1010. * I leave the source as this _may_ be useful in the future.
  1011. */
  1012. #ifdef NOT_USED_CODE
  1013. static char
  1014. set_write(bitmap_t map[], ullong_t off, int maplen)
  1015. {
  1016. ulong_t tmp = GET_OFF(off) % maplen;
  1017. char status;
  1018. mutex->enter(tmp);
  1019. if (map[tmp] & GET_BIT(off)) {
  1020. /* we are already locked */
  1021. status = 0;
  1022. } else {
  1023. map[tmp] |= GET_BIT(off);
  1024. status = 1;
  1025. }
  1026. mutex->exit(tmp);
  1027. TNF_PROBE_3(set_write, "set_write", "sunw%cte%diskomizer",
  1028. tnf_opaque, off, off,
  1029. tnf_opaque, map, map,
  1030. tnf_char, status, status);
  1031. return (status);
  1032. }
  1033. #endif
  1034. void
  1035. clear_write(bitmap_t map[], ullong_t off, ulong_t maplen)
  1036. {
  1037. ulong_t tmp = (GET_OFF(off) % maplen);
  1038. ulong_t x;
  1039. bitmap_t bit = ~(GET_BIT(off));
  1040. mutex->enter(tmp);
  1041. x = map[tmp];
  1042. map[tmp] &= bit;
  1043. assert(~bit != (ulong_t)0);
  1044. if (x == map[tmp]) {
  1045. plog(LOG_ALERT, "Ooops block %#llx (0t%lld) was not locked\n",
  1046. diskomizer_off2byteoff(off), diskomizer_off2byteoff(off));
  1047. TNF_PROBE_2(clear_write, "clear_write failed",
  1048. "sunw%cte%diskomizer",
  1049. tnf_opaque, off, off, tnf_opaque, map, map);
  1050. } else {
  1051. TNF_PROBE_2(clear_write, "clear_write ok",
  1052. "sunw%cte%diskomizer",
  1053. tnf_opaque, off, off,
  1054. tnf_opaque, map, map);
  1055. }
  1056. mutex->exit(tmp);
  1057. }
  1058. #ifdef NOT_USED_CODE
  1059. static void
  1060. print_bitmap(bitmap_t map[], int maplen)
  1061. {
  1062. int i;
  1063. for (i = 0; i < maplen; i++)
  1064. pprintf("%#8.8X %#8.8X\n", i, map[i]);
  1065. (void) fflush(stdout);
  1066. }
  1067. #endif
  1068. /*
  1069. * find_next_free finds the "next" block that is not locked starting from
  1070. * offset.
  1071. */
  1072. ullong_t
  1073. find_next_free(bitmap_t map[], ullong_t off, int len, int maplen)
  1074. {
  1075. ulong_t tmp = (GET_OFF(off) % maplen);
  1076. ulong_t i = 0;
  1077. bitmap_t bit = GET_BIT(off);
  1078. mutex->enter(tmp);
  1079. while ((map[tmp] & bit) != 0) {
  1080. ulong_t newtmp;
  1081. off = off + 1;
  1082. off %= len;
  1083. newtmp = (GET_OFF(off) % maplen);
  1084. bit = GET_BIT(off);
  1085. mutex->getnext(tmp, newtmp);
  1086. tmp = newtmp;
  1087. TNF_PROBE_2(find_next_free_trying, "find_next_free trying",
  1088. "sunw%cte%diskomizer", tnf_longlong, off, off,
  1089. tnf_opaque, map, map);
  1090. if (!(i < (4 * len))) {
  1091. (void) plog(LOG_ALERT,
  1092. "Unable to find free entry in map %#lx"
  1093. " of length %d\n",
  1094. (ulong_t)&map[0], maplen);
  1095. (void) fflush(stderr);
  1096. /* print_bitmap(map, maplen); */
  1097. mutex->exit(tmp);
  1098. (void) sleep(1);
  1099. mutex->enter(tmp);
  1100. i = 0;
  1101. }
  1102. i++;
  1103. }
  1104. map[tmp] |= GET_BIT(off);
  1105. mutex->exit(tmp);
  1106. TNF_PROBE_2(find_next_free_found, "find_next_free found",
  1107. "sunw%cte%diskomizer", tnf_longlong, off, off,
  1108. tnf_opaque, map, map);
  1109. return (off);
  1110. }
  1111. /*
  1112. * Test to see if the write bit is set for this offset. The lock MUST
  1113. * already be held
  1114. */
  1115. static int
  1116. test_write(bitmap_t map[], ullong_t off, int maplen)
  1117. {
  1118. ulong_t tmp = GET_OFF(off) % maplen;
  1119. return (map[tmp] & GET_BIT(off) ? 1 : 0);
  1120. }
  1121. /*
  1122. * If this block is being read from or written to return true
  1123. * Otherwise return lock it and return.
  1124. */
  1125. static int
  1126. pend_write_with_lock(bitmap_t map[], ullong_t off, int maplen)
  1127. {
  1128. ulong_t tmp = GET_OFF(off) % maplen;
  1129. int status;
  1130. mutex->enter(tmp);
  1131. plog(LOG_DEBUG, "Disk Block %lld\n", diskomizer_off2byteoff(off));
  1132. if (map[tmp] & GET_BIT(off)) {
  1133. status = 1;
  1134. } else {
  1135. map[tmp] |= GET_BIT(off);
  1136. status = 0;
  1137. }
  1138. mutex->exit(tmp);
  1139. return (status);
  1140. }
  1141. static uchar_t
  1142. choose_iolen(struct aio_str *aiop)
  1143. {
  1144. if (is_executable(aiop->buf)) {
  1145. return (max_disk_io_len);
  1146. }
  1147. return (opts.disk_io_sizes.weightings[lrand48() %
  1148. opts.disk_io_sizes.wlen]);
  1149. }
  1150. static void
  1151. init_read_buf(uchar_t *buf, ulong_t len, const uchar_t * const write_buf)
  1152. {
  1153. void *sig = expect_signal(SIGBUS, "memset", buf, len);
  1154. read_buffer_initializer(buf, len, write_buf);
  1155. cancel_expected_signal(SIGBUS, sig);
  1156. }
  1157. static bitmap_t *
  1158. attach_dev_writemap(struct device *dev)
  1159. {
  1160. bitmap_t *map;
  1161. int error_count = 0;
  1162. while ((map = (bitmap_t *)
  1163. shm_ops->attach(dev->writemap_handle)) == NULL) {
  1164. if ((error_count++ % 10000) == 0)
  1165. ATTACH_ERROR(dev->writemap_handle);
  1166. }
  1167. if (error_count > 0)
  1168. plog(LOG_WARNING, "attached o.k.\n");
  1169. return (map);
  1170. }
  1171. static bitmap_t *
  1172. attach_aio_writemap(struct aio_str *aiop)
  1173. {
  1174. return (attach_dev_writemap(aiop->dev));
  1175. }
  1176. static void
  1177. clear_writemap(struct aio_str *aiop)
  1178. {
  1179. bitmap_t *map = attach_aio_writemap(aiop);
  1180. clear_write(map, aiop->off, aiop->dev->writemap_size);
  1181. shm_ops->detach(aiop->dev->writemap_handle);
  1182. }
  1183. static void
  1184. clear_writemap_success(struct aio_str *aiop)
  1185. {
  1186. aiop->off = push_recent(aiop->dev->recent, aiop->off);
  1187. if (aiop->off != -1) {
  1188. clear_writemap(aiop);
  1189. }
  1190. }
  1191. static struct blks *
  1192. choose_new_random_read(struct aio_str *aiop, ullong_t start, ullong_t len)
  1193. {
  1194. struct blks *blocks;
  1195. bitmap_t *map;
  1196. if ((aiop->off = pop_recent(aiop->dev->recent)) != -1) {
  1197. return (aio_attach(aiop));
  1198. }
  1199. map = attach_aio_writemap(aiop);
  1200. aiop->retrycnt = 0;
  1201. for (;;) {
  1202. rand_block(map, aiop, start, len,
  1203. aiop->dev->writemap_size);
  1204. blocks = aio_attach(aiop);
  1205. if (is_readonly()) {
  1206. if (0x1 & (uint_t)
  1207. blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_io)
  1208. break;
  1209. } else {
  1210. if (blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io != NULL)
  1211. break;
  1212. }
  1213. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  1214. blocks = NULL;
  1215. clear_write(map, aiop->off, aiop->dev->writemap_size);
  1216. }
  1217. shm_ops->detach(aiop->dev->writemap_handle);
  1218. return (blocks);
  1219. }
  1220. /*
  1221. * do a new read.
  1222. */
  1223. time_t
  1224. do_new_read(struct aio_str *aiop, ullong_t start, read_type_t read_type)
  1225. {
  1226. ullong_t offset;
  1227. int fd = aiop->fd->fd;
  1228. ullong_t len;
  1229. struct blks *blocks = NULL;
  1230. /*
  1231. * if opts.sequential_passes is equal to seq_passes then we are on the
  1232. * first pass or opts.sequential_passes was zero to start with. In
  1233. * the second case once the disk is fill aip->dev->block will contain
  1234. * the address of the last block anyway.
  1235. */
  1236. if (aiop->dev->seq_passes == opts.sequential_passes) {
  1237. len = aiop->dev->block;
  1238. } else {
  1239. len = LEN_BYTES2BLOCKS(aiop->dev);
  1240. }
  1241. if (read_type != RETRY_READ &&
  1242. OPTION(nloops) != 0 && aiop->dev->countdown != 0) {
  1243. if (--aiop->dev->countdown == 0) {
  1244. time_now_log(LOG_INFO, "countdown on device %s is zero",
  1245. aiop->dev->logicalname);
  1246. }
  1247. }
  1248. if (read_type == NORMAL_READ) {
  1249. struct shadow_hdr const *shadow;
  1250. blocks = choose_new_random_read(aiop, start, len);
  1251. if (aiop->buf == NULL)
  1252. aiop->buf = get_read_buf();
  1253. aiop->iolen = blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_iolen;
  1254. aiop->daio_id.bufs = INDEX_TO_DIOLEN(aiop->iolen);
  1255. aiop->daio_id.buf = blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io;
  1256. aiop->hdr = build_bufhr(aiop->dev, start, aiop->off);
  1257. shadow = get_shadow_hdr(aiop->daio_id.buf);
  1258. aiop->daio_id.chksum = shadow->chksums[aiop->iolen];
  1259. aiop->daio_id.buf_id = get_write_buf_id(
  1260. blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io);
  1261. aiop->daio_id.hdr_len = sizeof (aiop->hdr);
  1262. aiop->daio_id.hdr = (uchar_t *)&aiop->hdr;
  1263. } else if (read_type == RETRY_READ) {
  1264. aiop->retrycnt++;
  1265. } else if (read_type == WRITE_READ) {
  1266. struct shadow_hdr const *shadow;
  1267. blocks = aio_attach(aiop);
  1268. aiop->daio_id.bufs = INDEX_TO_DIOLEN(aiop->iolen);
  1269. aiop->daio_id.buf = blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io;
  1270. aiop->daio_id.buf_id = get_write_buf_id(
  1271. blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io);
  1272. shadow = get_shadow_hdr(aiop->daio_id.buf);
  1273. aiop->daio_id.chksum = shadow->chksums[aiop->iolen];
  1274. aiop->daio_id.hdr_len = sizeof (aiop->hdr);
  1275. aiop->daio_id.hdr = (uchar_t *)&aiop->hdr;
  1276. } else if (read_type == READ_ONLY_RAND) {
  1277. if (aiop->buf == NULL)
  1278. aiop->buf = get_read_buf();
  1279. blocks = choose_new_random_read(aiop, start, len);
  1280. aiop->daio_id.buf =
  1281. BIT2CHARSTAR(blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_io);
  1282. aiop->daio_id.chksum =
  1283. blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_chksum;
  1284. aiop->iolen = max_disk_io_len;
  1285. aiop->daio_id.bufs = INDEX_TO_DIOLEN(aiop->iolen);
  1286. } else if (read_type == READ_ONLY_SEQ) {
  1287. bitmap_t *map;
  1288. if (aiop->buf == NULL)
  1289. aiop->buf = get_read_buf();
  1290. map = attach_aio_writemap(aiop);
  1291. len = LEN_BYTES2BLOCKS(aiop->dev);
  1292. aiop->dev->choose_block(map, aiop, start, len,
  1293. aiop->dev->writemap_size);
  1294. shm_ops->detach(aiop->dev->writemap_handle);
  1295. blocks = aio_attach(aiop);
  1296. aiop->daio_id.buf =
  1297. BIT2CHARSTAR(blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_io);
  1298. aiop->daio_id.chksum =
  1299. blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_chksum;
  1300. aiop->iolen = max_disk_io_len;
  1301. aiop->daio_id.bufs = INDEX_TO_DIOLEN(aiop->iolen);
  1302. }
  1303. offset = aio_str2byteoff(aiop);
  1304. while (my_gettimeofday(&aiop->tv, NULL) == -1)
  1305. pperror("gettimeofday");
  1306. if (blocks == NULL)
  1307. blocks = aio_attach(aiop);
  1308. init_read_buf(aiop->buf, INDEX_TO_DIOLEN(max_disk_io_len),
  1309. blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io);
  1310. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  1311. for (;;) {
  1312. if (aiop->dev->stop_flag ||
  1313. stop_check(aiop->dev->shared_data_handle)) {
  1314. if (aiop->dev->stop_flag == 0) {
  1315. plog(LOG_NOTICE, "Stopping %s\n",
  1316. aiop->dev->logicalname);
  1317. aiop->dev->stop_flag = 1;
  1318. } else if (aiop->dev->need_to_stop &&
  1319. set_shared_stop_flag(
  1320. aiop->dev->shared_data_handle) != -1) {
  1321. aiop->dev->need_to_stop = 0;
  1322. }
  1323. clear_writemap(aiop);
  1324. return_read_buf(aiop->buf);
  1325. aiop->buf = NULL;
  1326. remove_from_aio_list(&aiop->fd->all_aios, aiop);
  1327. break;
  1328. }
  1329. ZERO_OBJ(aiop->error.desc);
  1330. /* Move to the begining of the all_aios list */
  1331. remove_from_aio_list(&aiop->fd->all_aios, aiop);
  1332. add_to_aio_list(&aiop->fd->all_aios, aiop);
  1333. TNF_PROBE_4(aioread, "aioread",
  1334. "sunw%cte%diskomizer%aio read",
  1335. tnf_long, fd, aiop->fd->fd,
  1336. tnf_opaque, offset, offset,
  1337. tnf_opaque, aiop, aiop,
  1338. aio_tnf_str, *aiop, aiop);
  1339. if (daio->aread(fd, aiop->buf,
  1340. INDEX_TO_DIOLEN(aiop->iolen), offset,
  1341. &aiop->aio_res, &aiop->daio_id) < 0) {
  1342. if (errno == EAGAIN) {
  1343. AIOREAD_ERROR(fd, aiop->fd->name,
  1344. aiop->buf,
  1345. INDEX_TO_DIOLEN(aiop->iolen), offset,
  1346. SEEK_SET, &aiop->aio_res);
  1347. continue;
  1348. } else {
  1349. AIOREAD_ERROR(fd, aiop->fd->name, aiop->buf,
  1350. INDEX_TO_DIOLEN(aiop->iolen), offset,
  1351. SEEK_SET, &aiop->aio_res);
  1352. clear_writemap(aiop);
  1353. }
  1354. }
  1355. aiop->fd->total_read++;
  1356. break;
  1357. }
  1358. return (aiop->tv.tv_sec);
  1359. }
  1360. static struct shadow_hdr const *
  1361. set_io_len(struct aio_str *aiop)
  1362. {
  1363. struct shadow_hdr const *shadow_hdr = get_shadow_hdr(aiop->buf);
  1364. if (!shadow_hdr->type.BUF_READY) {
  1365. struct shadow_hdr *shadow;
  1366. int j;
  1367. shadow = (struct shadow_hdr *)shadow_hdr;
  1368. init_buf(aiop->buf);
  1369. for (j = 0; j <= opts.disk_io_sizes.weightings[
  1370. opts.disk_io_sizes.wlen - 1]; j++) {
  1371. shadow->chksums[j] =
  1372. check_bufbody(aiop->buf,
  1373. INDEX_TO_DIOLEN(j));
  1374. }
  1375. shadow->type = get_bufhdr_a(aiop->buf).type;
  1376. shadow->type.BUF_READY = 1;
  1377. if (opts.obscure_execute && is_executable(aiop->buf)) {
  1378. run_func(aiop->buf,
  1379. opts.disk_io_sizes.vals[aiop->iolen] -
  1380. SIZEOF_BUFHDR);
  1381. }
  1382. aiop->iolen = choose_iolen(aiop);
  1383. } else if (shadow_hdr->type.BUF_READ_ONLY) {
  1384. aiop->iolen = max_disk_io_len;
  1385. } else {
  1386. assert(*aiop->buf == 0xAA || *aiop->buf == 0x55);
  1387. aiop->iolen = choose_iolen(aiop);
  1388. }
  1389. return (shadow_hdr);
  1390. }
  1391. static int
  1392. is_sequential(struct aio_str *aiop)
  1393. {
  1394. return (aiop->dev->choose_block == seq_block ||
  1395. aiop->dev->choose_block == unwritten_block_seq);
  1396. }
  1397. static struct aio_str *
  1398. get_deferred_io(struct device *dev)
  1399. {
  1400. struct device *devp;
  1401. struct aio_str *aiop;
  1402. for (devp = dev; devp != NULL; devp = devp->next) {
  1403. if ((aiop = pop_from_aio_list(&devp->deferred_ios)) != NULL) {
  1404. return (aiop);
  1405. }
  1406. }
  1407. return (NULL);
  1408. }
  1409. static void
  1410. deferred_starter(struct device *dev, ullong_t start)
  1411. {
  1412. int all_going = 1;
  1413. struct device *devp;
  1414. for (devp = dev; devp != NULL; devp = devp->next) {
  1415. struct aio_str *aiop;
  1416. check_exit_flag();
  1417. aiop = pop_from_aio_list(&devp->deferred_ios);
  1418. if (aiop != NULL) {
  1419. cancelled_count--;
  1420. aiop->handler(aiop, start);
  1421. }
  1422. if (is_aio_on_list(&devp->deferred_ios)) {
  1423. all_going = 0;
  1424. }
  1425. }
  1426. if (all_going == 1) {
  1427. start_deferred = (void (*)(struct device *, ullong_t)) nop;
  1428. }
  1429. }
  1430. static int
  1431. number_of_writes(struct device *dev)
  1432. {
  1433. struct fds *fd = dev->fdhead;
  1434. int count = 0;
  1435. do {
  1436. count += fd->total_write;
  1437. fd = fd->next;
  1438. } while (fd != dev->fdhead);
  1439. return (count);
  1440. }
  1441. /* static void */
  1442. void
  1443. do_new_write(struct aio_str *aiop, ullong_t start, int retry)
  1444. {
  1445. ullong_t len;
  1446. ullong_t offset;
  1447. long writemap_size;
  1448. struct shadow_hdr const *shadow_hdr;
  1449. struct blks *blocks;
  1450. struct blks *block;
  1451. char deferred;
  1452. if (aiop->fd == NULL) {
  1453. aiop->fd = aiop->dev->fdhead;
  1454. add_to_aio_list(&aiop->fd->all_aios, aiop);
  1455. deferred = 1;
  1456. } else {
  1457. deferred = 0;
  1458. }
  1459. len = LEN_BYTES2BLOCKS(aiop->dev);
  1460. writemap_size = aiop->dev->writemap_size;
  1461. if (!retry) {
  1462. bitmap_t *map;
  1463. int i;
  1464. if (write_loops) {
  1465. if (--aiop->dev->countdown == 0) {
  1466. time_now_log(LOG_INFO,
  1467. "countdown on device %s is zero",
  1468. aiop->dev->logicalname);
  1469. }
  1470. }
  1471. if (aiop->buf == NULL) {
  1472. aiop->buf = get_write_buf();
  1473. }
  1474. shadow_hdr = set_io_len(aiop);
  1475. map = attach_aio_writemap(aiop);
  1476. for (i = 0; /* cstyle */; i++) {
  1477. aiop->dev->choose_block(map, aiop, start, len,
  1478. writemap_size);
  1479. blocks = aio_attach(aiop);
  1480. block = &blocks[AIO_BLOCK_INDEX(aiop)];
  1481. if (block->r.w.last_io == NULL ||
  1482. (block->read_count >= OPTION(read_minimum) &&
  1483. block->r.w.last_io != aiop->buf)) {
  1484. break;
  1485. } else {
  1486. if (block->r.w.last_io == aiop->buf) {
  1487. uchar_t *buf;
  1488. /*
  1489. * Get the new write buf first so that
  1490. * You definitely get a new buffer.
  1491. */
  1492. if ((buf = get_write_buf()) != NULL) {
  1493. return_write_buf(aiop->buf);
  1494. aiop->buf = buf;
  1495. shadow_hdr = set_io_len(aiop);
  1496. break;
  1497. }
  1498. }
  1499. clear_write(map, aiop->off, writemap_size);
  1500. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  1501. block = blocks = NULL;
  1502. if (i * OPTION(obscure_search_multiplier) >=
  1503. len || deferred) {
  1504. if (!deferred) {
  1505. union err_info err_info;
  1506. err_info.str = "write";
  1507. report_error(aiop, err_info,
  1508. ERR_DEFERRED);
  1509. }
  1510. remove_from_aio_list(
  1511. &aiop->fd->all_aios, aiop);
  1512. aiop->fd = NULL;
  1513. add_to_aio_list(
  1514. &aiop->dev->deferred_ios, aiop);
  1515. return_write_buf(aiop->buf);
  1516. aiop->buf = NULL;
  1517. shm_ops->detach(
  1518. aiop->dev->writemap_handle);
  1519. if (number_of_writes(aiop->dev) == 0) {
  1520. start_deferred =
  1521. deferred_starter;
  1522. }
  1523. return;
  1524. }
  1525. }
  1526. }
  1527. aiop->retrycnt = 0;
  1528. shm_ops->detach(aiop->dev->writemap_handle);
  1529. } else {
  1530. /* if we are retrying then we already have the lock. */
  1531. ullong_t blockno = aio_str2byteoff(aiop);
  1532. shadow_hdr = get_shadow_hdr(aiop->buf);
  1533. if (retry == 1) {
  1534. pfprintf(stderr,
  1535. "%s Block 0t%lld (%#llx) retry count %d\n",
  1536. aiop->fd->name, blockno, blockno,
  1537. ++aiop->retrycnt);
  1538. }
  1539. blocks = aio_attach(aiop);
  1540. block = &blocks[AIO_BLOCK_INDEX(aiop)];
  1541. }
  1542. offset = (ullong_t)start + (INDEX_TO_DIOLEN(max_disk_io_len)*aiop->off);
  1543. assert((ullong_t)offset >= (ullong_t)start);
  1544. assert((ullong_t)offset <= (ullong_t)(start + aiop->dev->length -
  1545. INDEX_TO_DIOLEN(max_disk_io_len)));
  1546. while (my_gettimeofday(&aiop->tv, NULL) == -1)
  1547. pperror("gettimeofday");
  1548. if (shadow_hdr->type.BUF_READ_ONLY == 0) {
  1549. /*
  1550. * Set up the buffer header and store away the path_id of the
  1551. * path we are using, and the header checksum.
  1552. */
  1553. unprotect_buf(aiop->buf);
  1554. toggle_bufhdr(aiop->buf);
  1555. set_bufhdr_all(aiop->buf, shadow_hdr->chksums[aiop->iolen],
  1556. INDEX_TO_DIOLEN(aiop->iolen),
  1557. aiop->fd->devid, offset, shadow_hdr->type,
  1558. ++block->sequence,
  1559. aiop->tv.tv_sec);
  1560. block->path_id = aiop->fd->path_id;
  1561. block->hdrchksum =
  1562. set_buf_hdrchksum(aiop->buf);
  1563. protect_buf(aiop->buf);
  1564. } else {
  1565. block->hdrchksum = check_bufhdr(aiop->buf,
  1566. get_bufhdr_hdrchksum(aiop->buf));
  1567. }
  1568. if (get_bufhdr_hdrchksum(aiop->buf) != block->hdrchksum) {
  1569. pfprintf(stderr, "writing bad checksum buf %#lx\n",
  1570. (ulong_t)aiop->buf);
  1571. }
  1572. if (opts.obscure_execute && is_executable(aiop->buf)) {
  1573. plog(LOG_DEBUG, "Writing executable buffer\n");
  1574. }
  1575. /*
  1576. * This if is saying that this process should have initialized
  1577. * this block, during the sequential part of the run. So last_io
  1578. * should be set. If not then something went wrong.
  1579. *
  1580. * The block could also been skipped as it was busy and put on
  1581. * the unwritten list, so only check if the unwritten list is
  1582. * empty.
  1583. */
  1584. if (!is_sequential(aiop) && block->r.w.last_io == NULL &&
  1585. (aiop->off % opts.nprocs) == this_proc() &&
  1586. aiop->dev->failed_to_push_unwritten == 0 &&
  1587. blocks[AIO_BLOCK_INDEX(aiop)].u.was_unwritten == 1 &&
  1588. !is_unwritten(aiop)) {
  1589. ullong_t blockno = aio_str2byteoff(aiop);
  1590. pfprintf(stderr, "Device %s\n", aiop->fd->name);
  1591. pfprintf(stderr, "Device len %#llx\n", aiop->dev->length);
  1592. pfprintf(stderr, "This proc %d nprocs %ld\n", this_proc(),
  1593. opts.nprocs);
  1594. pfprintf(stderr, "Block %#llx (0t%lld) byte off %llx error\n",
  1595. aiop->off, aiop->off, blockno);
  1596. pfprintf(stderr, "Last Requested %ld\n",
  1597. block->last_requested);
  1598. pfprintf(stderr, "Last return delta %d\n",
  1599. block->last_returned_delta);
  1600. pfprintf(stderr, "Read Count %d\n", block->read_count);
  1601. pfprintf(stderr, "Last Io Len %d\n",
  1602. INDEX_TO_DIOLEN(block->r.w.last_iolen));
  1603. assert(block->r.w.last_io != NULL);
  1604. }
  1605. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  1606. for (;;) {
  1607. if (aiop->dev->stop_flag ||
  1608. stop_check(aiop->dev->shared_data_handle)) {
  1609. if (aiop->dev->stop_flag == 0) {
  1610. plog(LOG_NOTICE, "Stopping %s\n",
  1611. aiop->dev->logicalname);
  1612. aiop->dev->stop_flag = 1;
  1613. } else if (aiop->dev->need_to_stop &&
  1614. set_shared_stop_flag(
  1615. aiop->dev->shared_data_handle) != -1) {
  1616. aiop->dev->need_to_stop = 0;
  1617. }
  1618. clear_writemap(aiop);
  1619. return_write_buf(aiop->buf);
  1620. aiop->buf = NULL;
  1621. remove_from_aio_list(&aiop->fd->all_aios, aiop);
  1622. break;
  1623. }
  1624. assert(*aiop->buf == 0xAA || *aiop->buf == 0x55);
  1625. ZERO_OBJ(aiop->error.desc);
  1626. TNF_PROBE_4(daiowrite, "aiowrite",
  1627. "sunw%cte%diskomizer%aio write",
  1628. tnf_long, fd, aiop->fd->fd,
  1629. tnf_opaque, offset, offset,
  1630. tnf_opaque, aiop, aiop,
  1631. aio_tnf_str, *aiop, aiop);
  1632. aiop->daio_id.buf = aiop->buf;
  1633. aiop->daio_id.buf_id = get_write_buf_id(aiop->buf);
  1634. aiop->daio_id.hdr_len = sizeof (aiop->hdr);
  1635. aiop->daio_id.hdr = (uchar_t *)&aiop->hdr;
  1636. (void) memcpy(&aiop->hdr, aiop->buf, sizeof (aiop->hdr));
  1637. aiop->daio_id.footer_len = 0;
  1638. /* Move to the begining of the all_aios list */
  1639. remove_from_aio_list(&aiop->fd->all_aios, aiop);
  1640. add_to_aio_list(&aiop->fd->all_aios, aiop);
  1641. if (daio->awrite(aiop->fd->fd, aiop->buf,
  1642. INDEX_TO_DIOLEN(aiop->iolen),
  1643. offset, &aiop->aio_res, &aiop->daio_id) == -1) {
  1644. int serrno = errno;
  1645. AIOWRITE_ERROR(aiop->fd->fd, aiop->fd->name,
  1646. (ulong_t)aiop->buf,
  1647. INDEX_TO_DIOLEN(aiop->iolen),
  1648. offset,
  1649. SEEK_SET,
  1650. (ulong_t)&aiop->aio_res);
  1651. if (serrno == EAGAIN) {
  1652. continue;
  1653. } else {
  1654. clear_writemap(aiop);
  1655. }
  1656. } else if (!deferred) {
  1657. aiop->fd->total_write++;
  1658. if (is_aio_on_list(&aiop->dev->deferred_ios)) {
  1659. aiop = pop_from_aio_list(
  1660. &aiop->dev->deferred_ios);
  1661. do_new_write(aiop, start, 0);
  1662. }
  1663. } else {
  1664. aiop->fd->total_write++;
  1665. plog(LOG_NOTICE, "Started deferred io to %s\n",
  1666. aiop->dev->logicalname);
  1667. }
  1668. break;
  1669. }
  1670. }
  1671. /*ARGSUSED1*/
  1672. void
  1673. run_func(uchar_t *buf, size_t size)
  1674. {
  1675. uchar_t *cptr;
  1676. #ifdef SPARC
  1677. uint32_t *last, *ptr;
  1678. #else
  1679. uint32_t *ptr;
  1680. #endif
  1681. void (*func)(void);
  1682. cptr = get_buf_data(buf);
  1683. #ifdef SPARC
  1684. /* check alignment for SPARC */
  1685. if ((ulong_t)cptr % 4) {
  1686. return;
  1687. }
  1688. #endif
  1689. /*LINTED*/
  1690. ptr = (uint32_t *)cptr;
  1691. func = (void (*)(void))(ptr);
  1692. #ifdef SPARC
  1693. last = ptr + size / sizeof (uint32_t);
  1694. for (; ptr < last; ptr++)
  1695. flush((int32_t *)ptr);
  1696. #endif
  1697. plog(LOG_DEBUG, "Running func %#lx in buf %#lx, type %llx\n",
  1698. func, (ulong_t)buf, get_bufhdr(buf).start);
  1699. TNF_PROBE_1(run_func, "run_func",
  1700. "sunw%cte%diskomizer%aio execute run",
  1701. tnf_opaque, buf, buf);
  1702. func();
  1703. }
  1704. struct fds *
  1705. find_path(struct fds *fdhead, char path_id)
  1706. {
  1707. struct fds *fd;
  1708. for (fd = fdhead->next; ; fd = fd->next) {
  1709. if (fd->path_id == path_id)
  1710. return (fd);
  1711. if (fd == fdhead)
  1712. return (NULL);
  1713. }
  1714. }
  1715. static struct bufhdr
  1716. build_bufhr(struct device *dev, ullong_t start, ullong_t off)
  1717. {
  1718. struct bufhdr hdr;
  1719. struct shadow_hdr const *shadow_hdr;
  1720. struct blks *block;
  1721. struct blks *blocks;
  1722. struct fds *fd;
  1723. ushort16_t hdrchksum;
  1724. int error_count = 0;
  1725. ullong_t offset = (ullong_t)start +
  1726. (ullong_t)(INDEX_TO_DIOLEN(max_disk_io_len)*off);
  1727. while ((blocks = shm_ops->attach(DEV_BLOCK_HANDLE(dev, off))) == NULL) {
  1728. if (error_count++ % 10000 == 0)
  1729. ATTACH_ERROR(DEV_BLOCK_HANDLE(dev, off));
  1730. }
  1731. if (error_count > 0)
  1732. plog(LOG_WARNING, "attached o.k.\n");
  1733. block = &blocks[DEV_BLOCK_INDEX(dev, off)];
  1734. ZERO_OBJ(hdr);
  1735. fd = find_path(dev->fdhead, block->path_id);
  1736. assert(fd != NULL);
  1737. if (block->bad_hdr) {
  1738. (void) memcpy(&hdr, block->r.w.last_io, SIZEOF_BUFHDR);
  1739. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  1740. return (hdr);
  1741. }
  1742. shadow_hdr = get_shadow_hdr(block->r.w.last_io);
  1743. if (block->ab == 1) {
  1744. hdr.start = hdr.end = BUF_TYPE_A;
  1745. hdr.ab.a.chksum = shadow_hdr->chksums[block->r.w.last_iolen];
  1746. hdr.ab.a.type = shadow_hdr->type;
  1747. hdr.ab.a.type.sequence = block->sequence;
  1748. hdr.ab.a.devid = fd->devid;
  1749. hdr.ab.a.off = offset;
  1750. hdr.ab.a.time = block->last_requested;
  1751. hdr.ab.a.did = master_pid();
  1752. hdr.ab.a.len = INDEX_TO_DIOLEN(block->r.w.last_iolen);
  1753. get_serial_and_provider(hdr.ab.a.serial_and_provider,
  1754. SIZEOF_SERIAL_AND_PROVIDER);
  1755. } else {
  1756. hdr.start = hdr.end = BUF_TYPE_B;
  1757. hdr.ab.b.time = block->last_requested;
  1758. hdr.ab.b.chksum = shadow_hdr->chksums[block->r.w.last_iolen];
  1759. hdr.ab.b.type = shadow_hdr->type;
  1760. hdr.ab.b.type.sequence = block->sequence;
  1761. hdr.ab.b.devid = fd->devid;
  1762. hdr.ab.b.off = offset;
  1763. hdr.ab.b.did = master_pid();
  1764. hdr.ab.b.len = INDEX_TO_DIOLEN(block->r.w.last_iolen);
  1765. get_serial_and_provider(hdr.ab.b.serial_and_provider,
  1766. SIZEOF_SERIAL_AND_PROVIDER);
  1767. }
  1768. if ((hdrchksum = set_hdrchksum(&hdr)) != block->hdrchksum) {
  1769. pfprintf(stderr,
  1770. "Bad rebuilt buf header is %#x should be %#x\n",
  1771. block->hdrchksum, hdrchksum);
  1772. }
  1773. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  1774. return (hdr);
  1775. }
  1776. struct bufhdr
  1777. build_prevbufhr(struct device *dev, ullong_t start, ullong_t off)
  1778. {
  1779. struct bufhdr hdr;
  1780. struct shadow_hdr const *shadow_hdr;
  1781. struct blks *block;
  1782. struct blks *blocks;
  1783. struct fds *fd;
  1784. ushort16_t hdrchksum;
  1785. int error_count = 0;
  1786. ullong_t offset = (ullong_t)start +
  1787. (ullong_t)(INDEX_TO_DIOLEN(max_disk_io_len)*off);
  1788. while ((blocks = shm_ops->attach(DEV_BLOCK_HANDLE(dev, off))) == NULL) {
  1789. if (error_count++ % 10000 == 0)
  1790. ATTACH_ERROR(DEV_BLOCK_HANDLE(dev, off));
  1791. }
  1792. if (error_count > 0)
  1793. plog(LOG_WARNING, "attached o.k.\n");
  1794. block = &blocks[DEV_BLOCK_INDEX(dev, off)];
  1795. ZERO_OBJ(hdr);
  1796. fd = find_path(dev->fdhead, block->path_id);
  1797. assert(fd != NULL);
  1798. if (block->bad_hdr) {
  1799. (void) memcpy(&hdr, block->r.w.last_io, SIZEOF_BUFHDR);
  1800. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  1801. return (hdr);
  1802. }
  1803. shadow_hdr = get_shadow_hdr(block->r.w.prev_io);
  1804. if (block->ab != 1) {
  1805. hdr.start = hdr.end = BUF_TYPE_A;
  1806. hdr.ab.a.time = block->u.prev_requested;
  1807. hdr.ab.a.chksum = shadow_hdr->chksums[block->r.w.prev_iolen];
  1808. hdr.ab.a.type = shadow_hdr->type;
  1809. hdr.ab.a.type.sequence = block->sequence - 1;
  1810. hdr.ab.a.devid = fd->devid;
  1811. hdr.ab.a.off = offset;
  1812. get_serial_and_provider(hdr.ab.a.serial_and_provider,
  1813. SIZEOF_SERIAL_AND_PROVIDER);
  1814. hdr.ab.a.len = INDEX_TO_DIOLEN(block->r.w.prev_iolen);
  1815. hdr.ab.a.did = master_pid();
  1816. } else {
  1817. hdr.start = hdr.end = BUF_TYPE_B;
  1818. hdr.ab.b.chksum = shadow_hdr->chksums[block->r.w.prev_iolen];
  1819. hdr.ab.b.type = shadow_hdr->type;
  1820. hdr.ab.b.type.sequence = block->sequence - 1;
  1821. hdr.ab.b.devid = fd->devid;
  1822. hdr.ab.b.off = offset;
  1823. hdr.ab.b.len = INDEX_TO_DIOLEN(block->r.w.prev_iolen);
  1824. hdr.ab.b.time = block->u.prev_requested;
  1825. hdr.ab.b.did = master_pid();
  1826. get_serial_and_provider(hdr.ab.b.serial_and_provider,
  1827. SIZEOF_SERIAL_AND_PROVIDER);
  1828. }
  1829. if ((hdrchksum = set_hdrchksum(&hdr)) != block->hdrchksum) {
  1830. pfprintf(stderr,
  1831. "Bad rebuilt buf header is %#x should be %#x\n",
  1832. block->hdrchksum, hdrchksum);
  1833. }
  1834. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  1835. return (hdr);
  1836. }
  1837. static struct diff_return
  1838. memdiff_data(FILE *err, uchar_t *goodptr, uchar_t *badptr,
  1839. int offset, int len)
  1840. {
  1841. int i;
  1842. struct diff_return dr;
  1843. union {
  1844. uchar_t c[sizeof (uint64_t) / sizeof (uchar_t)];
  1845. uint32_t i[sizeof (uint64_t) / sizeof (uint32_t)];
  1846. uint64_t l;
  1847. } good, bad, diff;
  1848. dr.bits = 0LL;
  1849. dr.count = 0LL;
  1850. for (i = 0; i < len; i += sizeof (uint64_t)) {
  1851. (void) memcpy(&good.c[0], goodptr, sizeof (uint64_t));
  1852. (void) memcpy(&bad.c[0], badptr, sizeof (uint64_t));
  1853. diff.i[0] = good.i[0] ^ bad.i[0];
  1854. diff.i[1] = good.i[1] ^ bad.i[1];
  1855. if (!opts.expert_small_diffs || diff.l) {
  1856. int bc = count_uint32_bits(diff.i[0]) +
  1857. count_uint32_bits(diff.i[1]);
  1858. dr.count += bc;
  1859. dr.bits |= diff.l;
  1860. #ifdef _BIG_ENDIAN
  1861. (void) fprintf(err,
  1862. "0x%8.8x %8.8x%8.8x %8.8x%8.8x "
  1863. "%8.8x%8.8x %2.2d\n", i + offset,
  1864. good.i[0], good.i[1], bad.i[0], bad.i[1],
  1865. diff.i[0], diff.i[1], bc);
  1866. #elif defined(_LITTLE_ENDIAN)
  1867. (void) fprintf(err,
  1868. "0x%8.8x "
  1869. "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x "
  1870. "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x "
  1871. "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x "
  1872. "%2.2d\n", i + offset,
  1873. good.c[0], good.c[1], good.c[2], good.c[3],
  1874. good.c[4], good.c[5], good.c[6], good.c[7],
  1875. bad.c[0], bad.c[1], bad.c[2], bad.c[3],
  1876. bad.c[4], bad.c[5], bad.c[6], bad.c[7],
  1877. diff.c[0], diff.c[1], diff.c[2], diff.c[3],
  1878. diff.c[4], diff.c[5], diff.c[6], diff.c[7],
  1879. bc);
  1880. #else
  1881. #error "niether _BIG_ENDIAN or _LITTLE_ENDIAN defined"
  1882. #endif
  1883. }
  1884. badptr += sizeof (uint64_t);
  1885. goodptr += sizeof (uint64_t);
  1886. }
  1887. return (dr);
  1888. }
  1889. static struct diff_return
  1890. memdiff_bufhdr(FILE *err, uchar_t *buf, uchar_t *good_hdr)
  1891. {
  1892. union {
  1893. struct bufhdr hdr;
  1894. uchar_t c[SIZEOF_BUFHDR];
  1895. } bad;
  1896. bad.hdr = get_bufhdr(buf);
  1897. return (memdiff_data(err, good_hdr, &bad.c[0], 0, SIZEOF_BUFHDR));
  1898. }
  1899. char *
  1900. diff_file(void)
  1901. {
  1902. char *wd;
  1903. static char *diffs_file;
  1904. if (NULL == diffs_file) {
  1905. if (diffs[0] != '/' && (wd = getcwd(NULL, 128)) != NULL) {
  1906. int x = strlen(diffs) + strlen(wd) + 2;
  1907. if ((diffs_file = malloc(x)) != NULL) {
  1908. snprintf(diffs_file, x, "%s/%s", wd, diffs);
  1909. } else {
  1910. diffs_file = diffs;
  1911. }
  1912. free(wd);
  1913. } else {
  1914. diffs_file = diffs;
  1915. }
  1916. }
  1917. return (diffs_file);
  1918. }
  1919. struct diff_return
  1920. memdiff_buf(uint64_t off, struct device *dev, uchar_t *buf, uint32_t iolen,
  1921. struct fds *fd, const char *str, struct error *error)
  1922. {
  1923. static const char zero2seven[] = "0 1 2 3 4 5 6 7";
  1924. uchar_t *badptr;
  1925. uchar_t *goodptr;
  1926. uchar_t *prevptr; /* pointer to the previous buffer that was written */
  1927. FILE *err;
  1928. time_t now;
  1929. sigset_t nset; /* new set */
  1930. sigset_t oset; /* old set */
  1931. int sigprocmask_status;
  1932. int error_count = 0;
  1933. struct diff_return dr;
  1934. struct diff_return dr2;
  1935. struct blks *blocks, *block;
  1936. union {
  1937. struct bufhdr hdr;
  1938. uchar_t c[SIZEOF_BUFHDR];
  1939. } good, prev;
  1940. while ((blocks = shm_ops->attach(DEV_BLOCK_HANDLE(dev, off))) == NULL) {
  1941. if (error_count++ % 10000 == 0)
  1942. ATTACH_ERROR(DEV_BLOCK_HANDLE(dev, off));
  1943. }
  1944. if (error_count > 0)
  1945. plog(LOG_WARNING, "attached o.k.\n");
  1946. block = &blocks[DEV_BLOCK_INDEX(dev, off)];
  1947. if (block->r.w.last_io == NULL) {
  1948. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  1949. dr.count = -1;
  1950. return (dr);
  1951. }
  1952. goodptr = get_buf_data(block->r.w.last_io);
  1953. if (block->r.w.prev_io != NULL) {
  1954. prevptr = get_buf_data(block->r.w.prev_io);
  1955. } else {
  1956. prevptr = NULL;
  1957. }
  1958. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  1959. badptr = get_buf_data(buf);
  1960. if ((err = fopen(diff_file(), "a+")) == NULL) {
  1961. err = stderr;
  1962. FOPEN_ERROR(diff_file(), "a+");
  1963. (void) sigemptyset(&nset);
  1964. (void) sigaddset(&nset, SIGINT);
  1965. (void) sigaddset(&nset, SIGTERM);
  1966. sigprocmask_status =
  1967. sigprocmask(SIG_BLOCK, &nset, &oset);
  1968. mutex->stderr_enter();
  1969. }
  1970. now = time(NULL);
  1971. (void) fprintf(err, "diskomizer %s\n", VERSION);
  1972. print_bufhdr_offsets(err);
  1973. (void) fprintf(err, "Error Instance %d\n", get_error_instance_number());
  1974. (void) fprintf(err, "Diffs dumped %s", ctime(&now));
  1975. (void) fprintf(err, "Diffs from %s for block 0x%llx\n",
  1976. str, diskomizer_off2byteoff(off));
  1977. (void) fprintf(err,
  1978. "use \""
  1979. "dd if=%s bs=%d iseek=%lld count=1\" to read the block\n",
  1980. fd->longname, iolen,
  1981. (opts.start_offset) + off);
  1982. good.hdr = build_bufhr(dev, start_offset(), off);
  1983. decode_header(err, &good.c[0], buf);
  1984. (void) fprintf(err, "%10.10s %16.16s %16.16s %16.16s %s\n",
  1985. "", "Written", "Read", "Diffs", "Bit count");
  1986. (void) fprintf(err, "%10.10s %16.16s %16.16s %16.16s\n",
  1987. "Offset", zero2seven, zero2seven, zero2seven);
  1988. dr = memdiff_bufhdr(err, buf, &good.c[0]);
  1989. dr2 = memdiff_data(err, goodptr, badptr, SIZEOF_BUFHDR,
  1990. iolen - SIZEOF_BUFHDR);
  1991. dr.count += dr2.count;
  1992. dr.bits |= dr2.bits;
  1993. (void) fprintf(err, "End of diffs for block 0x%llx\n",
  1994. diskomizer_off2byteoff(off));
  1995. if (prevptr != NULL && opts.display_prev_diffs) {
  1996. prev.hdr = build_prevbufhr(dev, start_offset(), off);
  1997. (void) fprintf(err, "Diffs from %s for previous io to block "
  1998. "0x%llx\n", str, diskomizer_off2byteoff(off));
  1999. (void) fprintf(err, "%10.10s %16.16s %16.16s %16.16s %s\n",
  2000. "", "Written", "Read", "Diffs", "Bit count");
  2001. (void) fprintf(err, "%10.10s %16.16s %16.16s %16.16s\n",
  2002. "Offset", zero2seven, zero2seven, zero2seven);
  2003. (void) memdiff_bufhdr(err, buf, &prev.c[0]);
  2004. (void) memdiff_data(err, prevptr, badptr, SIZEOF_BUFHDR,
  2005. iolen - SIZEOF_BUFHDR);
  2006. }
  2007. (void) fflush(err);
  2008. if (fsync(fileno(err)) == -1) {
  2009. FSYNC_ERROR(fileno(err), diffs);
  2010. }
  2011. if (err != stderr) {
  2012. (void) fclose(err);
  2013. error->diff_file = diff_file();
  2014. dlog(LOG_ERR, "Diffs file dumped to %s\n", diff_file());
  2015. } else {
  2016. mutex->stderr_exit();
  2017. error->diff_file = NULL;
  2018. if (sigprocmask_status == 0)
  2019. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  2020. }
  2021. return (dr);
  2022. }
  2023. struct diff_return
  2024. memdiff(struct aio_str *aiop, char *str)
  2025. {
  2026. struct diff_return dr;
  2027. if (!is_readonly()) {
  2028. aiop->error.dr = memdiff_buf(aiop->off, aiop->dev, aiop->buf,
  2029. INDEX_TO_DIOLEN(aiop->iolen), aiop->fd, str, &aiop->error);
  2030. return (aiop->error.dr);
  2031. }
  2032. dr.count = dr.bits = 0;
  2033. return (dr);
  2034. }
  2035. /*
  2036. * Check to see if the buffer that has been read matches the previous
  2037. * buffer that was written. This would spot if an write never got to
  2038. * the disk.
  2039. */
  2040. int
  2041. check_previous_buffer(check_t check_sum, struct aio_str *aiop)
  2042. {
  2043. uchar_t *previous_buf_written;
  2044. struct blks *blocks;
  2045. time_t prev_time;
  2046. blocks = aio_attach(aiop);
  2047. previous_buf_written = blocks[AIO_BLOCK_INDEX(aiop)].r.w.prev_io;
  2048. prev_time = blocks[AIO_BLOCK_INDEX(aiop)].u.prev_requested;
  2049. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  2050. if (previous_buf_written != NULL) {
  2051. struct bufhdr_a hdr_a;
  2052. struct bufhdr hdr;
  2053. hdr = get_bufhdr(previous_buf_written);
  2054. hdr_a = conv_bufhdr(&hdr);
  2055. if (check_sum == hdr_a.chksum) {
  2056. char *time_str;
  2057. time_str = alloc_time_str(prev_time);
  2058. pfprintf(stderr, "block %llx checksum matches"
  2059. " the previous block written at %s\n",
  2060. aio_str2byteoff(aiop), NOT_NULL(time_str));
  2061. not_null_free(time_str);
  2062. return (1);
  2063. }
  2064. }
  2065. return (0);
  2066. }
  2067. static int
  2068. check_old_data(struct aio_str *aiop)
  2069. {
  2070. const char *x;
  2071. time_t tyme;
  2072. pid_t did;
  2073. if (((x = get_buf_serial_and_provider(aiop->buf)) == NULL) ||
  2074. cmp_serial_and_provider(x) != 0) {
  2075. char y[SIZEOF_SERIAL_AND_PROVIDER];
  2076. get_serial_and_provider(y, SIZEOF_SERIAL_AND_PROVIDER);
  2077. if (x == NULL) {
  2078. plog(LOG_NOTICE, "block %llx contains data that "
  2079. "could not be recognized.\n",
  2080. aio_str2byteoff(aiop));
  2081. } else {
  2082. plog(LOG_NOTICE, "block %llx contains data written "
  2083. "by host %.*s not %.*s\n", aio_str2byteoff(aiop),
  2084. SIZEOF_SERIAL_AND_PROVIDER, x,
  2085. SIZEOF_SERIAL_AND_PROVIDER, y);
  2086. }
  2087. return (1);
  2088. } else if ((tyme = get_buf_time(aiop->buf)) < start_time.tv_sec) {
  2089. char *t = alloc_time_str(tyme);
  2090. plog(LOG_NOTICE, "block %llx contains data written before "
  2091. "this instance started. It was written at %s\n",
  2092. aio_str2byteoff(aiop), NOT_NULL(t));
  2093. not_null_free(t);
  2094. return (1);
  2095. } else if ((did = get_buf_did(aiop->buf)) != master_pid()) {
  2096. plog(LOG_NOTICE, "block at byte offset %llx not written by "
  2097. "this instance, but by %ld\n", aio_str2byteoff(aiop), did);
  2098. return (1);
  2099. }
  2100. return (0);
  2101. }
  2102. int
  2103. check_header(ullong_t start, struct aio_str *aiop)
  2104. {
  2105. if (!is_readonly()) {
  2106. ushort16_t bufhdrchksum;
  2107. ushort16_t hdrchksum;
  2108. bufhdrchksum = get_bufhdr_hdrchksum(aiop->buf);
  2109. hdrchksum = check_bufhdr(aiop->buf, bufhdrchksum);
  2110. if (bufhdrchksum != hdrchksum || hdrchksum == 0) {
  2111. plog(LOG_ERR, "block %llx bad header checksum\n",
  2112. aio_str2byteoff(aiop));
  2113. return (0);
  2114. } else {
  2115. struct bufhdr_a hdr_a;
  2116. ullong_t off;
  2117. struct bufhdr hdr;
  2118. hdr = get_bufhdr(aiop->buf);
  2119. (void) check_old_data(aiop);
  2120. hdr_a = conv_bufhdr(&hdr);
  2121. off = byteoff2diskomizer_off(hdr_a.off);
  2122. if (off != aiop->off) {
  2123. plog(LOG_ERR,
  2124. "On disk header says device byte offset "
  2125. "%llx (0t%lld), which calculates "
  2126. "diskomizer block %#llx (0t%lld), I "
  2127. "requested diskomizer block "
  2128. "%#llx (0t%lld)\n",
  2129. hdr_a.off, hdr_a.off, off, off,
  2130. aiop->off, aiop->off);
  2131. return (0);
  2132. }
  2133. }
  2134. }
  2135. return (1);
  2136. }
  2137. int
  2138. do_memcmp(ullong_t start, struct aio_str *aiop)
  2139. {
  2140. check_t check_sum;
  2141. uchar_t *last;
  2142. struct blks *blocks;
  2143. int status = 0;
  2144. if (check_header(start, aiop) == 0)
  2145. return (0);
  2146. blocks = aio_attach(aiop);
  2147. if ((last = blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io) != NULL) {
  2148. if (memcmp(get_buf_data(last), get_buf_data(aiop->buf),
  2149. INDEX_TO_DIOLEN(aiop->iolen) - SIZEOF_BUFHDR)) {
  2150. struct bufhdr_a hdr_a;
  2151. struct bufhdr hdr = get_bufhdr(last);
  2152. hdr_a = conv_bufhdr(&hdr);
  2153. /* the memcmp failed */
  2154. check_sum = check_aiobuf(aiop);
  2155. dfprintf(stderr, "block %llx buf %#lx does not match "
  2156. "what was written, what was read %#lx,"
  2157. " written %#lx\n", aio_str2byteoff(aiop),
  2158. (ulong_t)last, check_sum, hdr_a.chksum);
  2159. if (check_previous_buffer(check_sum, aiop) == 0) {
  2160. check_old_data(aiop);
  2161. }
  2162. } else {
  2163. status = 1;
  2164. }
  2165. } else {
  2166. status = 1;
  2167. }
  2168. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  2169. return (status);
  2170. }
  2171. /*
  2172. * print number of bytes will print the given number in full and then
  2173. * convert it to a human readable form and print it to 2 decimal places.
  2174. */
  2175. void
  2176. print_number_of_bytes(unsigned long long x, char *singular, char *plural)
  2177. {
  2178. char *str = x != 1 ? plural : singular;
  2179. const char *units;
  2180. int j;
  2181. int y;
  2182. /*
  2183. * All the units that fit in 64 bits:
  2184. * kilo, mega, giga, tera, peta, exa
  2185. */
  2186. static const char *all_units[] = { "K", "M", "G", "T", "P", "E" };
  2187. (void) printf("\t%#llx, %lld, %s ", x, x, str);
  2188. units = NULL;
  2189. for (j = 0; j < (sizeof (all_units) / sizeof (all_units[0])); j++) {
  2190. if (x / 1024) {
  2191. y = ((x * 1000) / 1024) % 1000;
  2192. x = x / 1024;
  2193. units = all_units[j];
  2194. } else {
  2195. break;
  2196. }
  2197. }
  2198. if (units) {
  2199. /* Round up it necessary */
  2200. if (y % 10 >= 5) {
  2201. y = y + 10;
  2202. }
  2203. /* loose the least significant digit */
  2204. y = y/10;
  2205. if (y >= 100) {
  2206. y -= 100;
  2207. x++;
  2208. }
  2209. (void) printf("(%lld.%.2d %s)\n", x, y, units);
  2210. } else {
  2211. (void) printf("\n");
  2212. }
  2213. }
  2214. void
  2215. print_number(unsigned long long i, char *singular, char *plural)
  2216. {
  2217. char *str = i != 1 ? plural : singular;
  2218. (void) printf("\t%#llx, %lld, %s\n", i, i, str);
  2219. }
  2220. /*
  2221. * given that the bufhdr for the io has a good check sum but is not
  2222. * for this device find the correct device and offset for the io
  2223. * and report this.
  2224. */
  2225. struct fds *
  2226. check_matching_path_io(struct bufhdr_a *hdr, struct fds *fd)
  2227. {
  2228. struct fds *x, *fdh;
  2229. fdh = fd;
  2230. for (x = fd->next; /* make cstyle happy */; x = x->next) {
  2231. if (memcmp(&hdr->devid, &fd->devid,
  2232. sizeof (struct device_id)) == 0) {
  2233. return (fd);
  2234. }
  2235. if (x == fdh) {
  2236. return (NULL);
  2237. } else {
  2238. fd = x;
  2239. }
  2240. }
  2241. /*NOTREACHED*/
  2242. }
  2243. void
  2244. read_and_check(ullong_t start, struct device *dev, ullong_t off,
  2245. struct aio_str *aiop)
  2246. {
  2247. uchar_t *buf;
  2248. ullong_t status;
  2249. ullong_t diskoff = diskomizer_off2byteoff(off);
  2250. struct blks *blocks;
  2251. struct blks *block;
  2252. struct shadow_hdr const *shadow;
  2253. int error_count = 0;
  2254. buf = calloc(1, INDEX_TO_DIOLEN(max_disk_io_len));
  2255. if (buf == NULL) {
  2256. CALLOC_ERROR(1L, (ulong_t)INDEX_TO_DIOLEN(max_disk_io_len));
  2257. return;
  2258. }
  2259. while ((blocks = shm_ops->attach(DEV_BLOCK_HANDLE(dev, off))) == NULL) {
  2260. if (error_count++ % 10000 == 0)
  2261. ATTACH_ERROR(DEV_BLOCK_HANDLE(dev, off));
  2262. }
  2263. if (error_count > 0)
  2264. plog(LOG_WARNING, "attached o.k.\n");
  2265. block = &blocks[DEV_BLOCK_INDEX(dev, off)];
  2266. aiop->daio_id.buf = block->r.w.last_io;
  2267. aiop->daio_id.bufs =
  2268. INDEX_TO_DIOLEN(opts.disk_io_sizes.vals[block->r.w.last_iolen]);
  2269. aiop->daio_id.buf_id = get_write_buf_id(block->r.w.last_io);
  2270. aiop->hdr = build_bufhr(dev, start, off);
  2271. aiop->daio_id.hdr = (uchar_t *)&aiop->hdr;
  2272. aiop->daio_id.hdr_len = sizeof (aiop->hdr);
  2273. aiop->daio_id.footer_len = 0;
  2274. shadow = get_shadow_hdr(aiop->daio_id.buf);
  2275. aiop->daio_id.chksum = shadow->chksums[aiop->iolen];
  2276. status = daio->pread(dev->fdhead->fd, buf,
  2277. opts.disk_io_sizes.vals[block->r.w.last_iolen], diskoff,
  2278. &aiop->daio_id);
  2279. if (status == DAIO_CORRUPT) {
  2280. int check_sum;
  2281. struct error error;
  2282. ulong_t shadow_chksum = check_bufbody(block->r.w.last_io,
  2283. opts.disk_io_sizes.vals[block->r.w.last_iolen]);
  2284. ZERO_OBJ(error);
  2285. check_sum = check_buf(buf,
  2286. INDEX_TO_DIOLEN(block->r.w.last_iolen), &error);
  2287. if (check_sum != shadow_chksum) {
  2288. pfprintf(stderr,
  2289. "Off %#llx (%lld) header differs "
  2290. "on disk\n", diskoff, diskoff);
  2291. aiop->error.dr = memdiff_buf(off, dev, buf,
  2292. opts.disk_io_sizes.vals[block->r.w.last_iolen],
  2293. dev->fdhead, "read and check", &aiop->error);
  2294. } else {
  2295. if (memcmp(get_buf_data(buf),
  2296. get_buf_data(block->r.w.last_io),
  2297. INDEX_TO_DIOLEN(block->r.w.last_iolen) -
  2298. SIZEOF_BUFHDR) != 0) {
  2299. pfprintf(stderr,
  2300. "Off %#llx (%lld) body differs "
  2301. "on disk\n", diskoff, diskoff);
  2302. aiop->error.dr = memdiff_buf(off, dev, buf,
  2303. opts.disk_io_sizes.vals[
  2304. block->r.w.last_iolen],
  2305. dev->fdhead, "read and check",
  2306. &aiop->error);
  2307. } else {
  2308. ullong_t off = aio_str2byteoff(aiop);
  2309. pfprintf(stderr,
  2310. "Data at byte offset %#llx (%lld) "
  2311. "on disk, matches the data just read "
  2312. "from %#llx (%lld)\n", diskoff, diskoff,
  2313. off, off);
  2314. }
  2315. }
  2316. } else if (status != opts.disk_io_sizes.vals[block->r.w.last_iolen]) {
  2317. PREAD_ERROR(dev->fdhead->fd, dev->fdhead->name, (ulong_t)buf,
  2318. opts.disk_io_sizes.vals[block->r.w.last_iolen], diskoff);
  2319. }
  2320. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  2321. free(buf);
  2322. }
  2323. /*
  2324. * Check the buffer contents matches an io which it internally thinks it
  2325. * is. This error path is used when we have read a block X but the contents
  2326. * of the block is not for block X but for block Y. So this routine gives
  2327. * information regarding the last movements of block Y.
  2328. *
  2329. * This routine must be called with all the mutexs held, see
  2330. * mutex->grab_all().
  2331. *
  2332. * To Do:
  2333. * bounds checking must be done on off before using it!
  2334. */
  2335. void
  2336. check_by_buffer(ullong_t start, struct device *dev, struct aio_str *aiop)
  2337. {
  2338. struct bufhdr_a read_hdr;
  2339. struct bufhdr hdr;
  2340. ullong_t off;
  2341. struct blks *block;
  2342. struct blks *blocks;
  2343. ulong_t shadow_chksum;
  2344. int error_count = 0;
  2345. bitmap_t *map;
  2346. hdr = get_bufhdr(aiop->buf);
  2347. read_hdr = conv_bufhdr(&hdr);
  2348. off = byteoff2diskomizer_off(read_hdr.off);
  2349. if ((long long)off < 0 || off >= LEN_BYTES2BLOCKS(dev)) {
  2350. return;
  2351. }
  2352. aiop->error.doff = off;
  2353. aiop->error.dev = dev;
  2354. if (aiop->dev == dev && off == aiop->off) {
  2355. return;
  2356. }
  2357. map = attach_dev_writemap(dev);
  2358. if (test_write(map, off, LEN_BYTES2BLOCKS(dev))) {
  2359. /*
  2360. * Grr. There is an IO outstanding on this block on this device,
  2361. * I don't know whether it is a read or a write
  2362. */
  2363. shm_ops->detach(dev->writemap_handle);
  2364. aiop->error.desc.UNABLE_TO_LOCK = 1;
  2365. dfprintf(stderr,
  2366. "Block %#llx 0t%lld is currently locked for dev %s\n",
  2367. diskomizer_off2byteoff(off),
  2368. diskomizer_off2byteoff(off), dev->logicalname);
  2369. return;
  2370. }
  2371. shm_ops->detach(dev->writemap_handle);
  2372. while ((blocks = shm_ops->attach(DEV_BLOCK_HANDLE(dev, off))) == NULL) {
  2373. if (error_count++ % 10000 == 0)
  2374. ATTACH_ERROR(DEV_BLOCK_HANDLE(dev, off));
  2375. }
  2376. if (error_count > 0)
  2377. plog(LOG_WARNING, "attached o.k.\n");
  2378. block = &blocks[DEV_BLOCK_INDEX(dev, off)];
  2379. if (block->r.w.last_io != NULL &&
  2380. (shadow_chksum = check_bufbody(block->r.w.last_io,
  2381. opts.disk_io_sizes.vals[block->r.w.last_iolen])) ==
  2382. read_hdr.chksum &&
  2383. memcmp(get_buf_data(aiop->buf),
  2384. get_buf_data(block->r.w.last_io),
  2385. opts.disk_io_sizes.vals[aiop->iolen] - SIZEOF_BUFHDR) == 0) {
  2386. struct fds *fd;
  2387. fd = find_path(dev->fdhead, block->path_id);
  2388. assert(fd != NULL);
  2389. aiop->error.desc.MATCHING_LAST = 1;
  2390. aiop->error.last_requested = block->last_requested;
  2391. aiop->error.delta = block->last_returned_delta;
  2392. aiop->error.doff = off;
  2393. aiop->error.dev = dev;
  2394. aiop->error.path_id = block->path_id;
  2395. dlog(LOG_ERR, "Buffer matches last write to block %#llx "
  2396. "(0t%lld) (block %#llx 0t%lld) on dev %s path %s\n",
  2397. off, off, diskomizer_off2byteoff(off),
  2398. diskomizer_off2byteoff(off), dev->logicalname,
  2399. fd->name);
  2400. dtime_log(LOG_ERR, block->last_requested,
  2401. "Last write to %s block %#llx (0t%lld) requested",
  2402. dev->logicalname, diskomizer_off2byteoff(off),
  2403. diskomizer_off2byteoff(off));
  2404. dtime_log(LOG_ERR,
  2405. block->last_requested +
  2406. block->last_returned_delta,
  2407. "Last write to %s block %#llx (0t%lld)"
  2408. " returned ", dev->logicalname,
  2409. diskomizer_off2byteoff(off),
  2410. diskomizer_off2byteoff(off));
  2411. read_and_check(start, dev, off, aiop);
  2412. } else if (block->r.w.prev_io != NULL) {
  2413. shadow_chksum = check_bufbody(block->r.w.prev_io,
  2414. MIN(opts.disk_io_sizes.vals[block->r.w.last_iolen],
  2415. opts.disk_io_sizes.vals[block->r.w.prev_iolen]));
  2416. if (shadow_chksum == read_hdr.chksum &&
  2417. memcmp(get_buf_data(aiop->buf),
  2418. get_buf_data(block->r.w.prev_io),
  2419. opts.disk_io_sizes.vals[aiop->iolen] -
  2420. SIZEOF_BUFHDR)) {
  2421. aiop->error.desc.MATCHING_PREV = 1;
  2422. aiop->error.doff = off;
  2423. aiop->error.last_requested = block->last_requested;
  2424. dlog(LOG_ERR,
  2425. "Buffer matches block %#llx (block %lld) dev "
  2426. "%s prev io\n",
  2427. (ullong_t)off, diskomizer_off2byteoff(off),
  2428. dev->logicalname);
  2429. dtime_log(LOG_ERR, block->u.prev_requested,
  2430. "Prev write to %s block %#llx (0t%lld)"
  2431. " requested %s\n",
  2432. dev->logicalname,
  2433. diskomizer_off2byteoff(off),
  2434. diskomizer_off2byteoff(off));
  2435. } else {
  2436. dfprintf(stderr, "Buffer claiming to be from block "
  2437. "%#llx dev %s does not match either of the "
  2438. "last two ios.\n",
  2439. (ullong_t)diskomizer_off2byteoff(off),
  2440. dev->logicalname);
  2441. }
  2442. }
  2443. shm_ops->detach(DEV_BLOCK_HANDLE(dev, off));
  2444. }
  2445. /*ARGSUSED*/
  2446. void
  2447. check_matching_io(ullong_t start, struct aio_str *aiop)
  2448. {
  2449. struct device *device;
  2450. struct bufhdr_a hdr_a;
  2451. struct bufhdr hdr = get_bufhdr(aiop->buf);
  2452. hdr_a = conv_bufhdr(&hdr);
  2453. if (hdr_a.hdrchksum != check_bufhdr(aiop->buf, hdr_a.hdrchksum)) {
  2454. return;
  2455. }
  2456. mutex->grab_all();
  2457. for (device = devices; device != NULL; device = device->next) {
  2458. struct fds *fd;
  2459. if ((fd = check_matching_path_io(&hdr_a,
  2460. device->fdhead)) != NULL) {
  2461. /*
  2462. * Now we have the device to which this io was sent
  2463. */
  2464. aiop->error.desc.MATCHING_DEVICE = 1;
  2465. aiop->error.fd = fd;
  2466. aiop->error.doff = byteoff2diskomizer_off(hdr_a.off);
  2467. aiop->error.last_requested = hdr_a.time;
  2468. check_by_buffer(start, device, aiop);
  2469. dfprintf(stderr, "Block read from %s matches block "
  2470. "written to %s\n", aiop->fd->name,
  2471. fd->name);
  2472. }
  2473. }
  2474. mutex->drop_all();
  2475. }
  2476. static loop_type
  2477. on_error_pause(ullong_t start, struct aio_str *aiop)
  2478. {
  2479. int isread = is_read_io(aiop);
  2480. pfprintf(stderr, "On %s error pause %d seconds\n",
  2481. isread ? "read" : "write", OPTION(pause_time));
  2482. (void) sleep(opts.pause_time);
  2483. if (!isread) {
  2484. return (CONTINUE);
  2485. }
  2486. if (!do_memcmp(start, aiop)) {
  2487. check_matching_io(start, aiop);
  2488. memdiff(aiop, "pause");
  2489. return (CONTINUE);
  2490. } else {
  2491. return (BREAK);
  2492. }
  2493. }
  2494. static void
  2495. bring_error_path_online(struct fds *fd)
  2496. {
  2497. struct fds *start = fd;
  2498. do {
  2499. if (fd->error_path == 1) {
  2500. pfprintf(stderr,
  2501. "Path %s brought on line\n", fd->name);
  2502. fd->error_path = 0;
  2503. break;
  2504. }
  2505. fd = fd->next;
  2506. } while (fd != start);
  2507. }
  2508. /*ARGSUSED*/
  2509. static loop_type
  2510. on_error_fail_path(ullong_t start, struct aio_str *aiop)
  2511. {
  2512. char *name = aiop->fd->name;
  2513. pfprintf(stderr,
  2514. "On error fail path %s failed\n", aiop->fd->name);
  2515. if (set_shared_stop_flag(aiop->fd->shared_data_handle) == -1) {
  2516. aiop->fd->need_to_stop = 1;
  2517. } else {
  2518. aiop->fd->stop_flag = 1;
  2519. cancel_all_io_byfd(aiop->fd);
  2520. snapshot_recent(aiop->dev->recent);
  2521. bring_error_path_online(aiop->fd);
  2522. }
  2523. newfd(aiop);
  2524. if (aiop->fd->stop_flag == 1 || aiop->fd->need_to_stop == 1) {
  2525. /*
  2526. * All the paths have failed, we muddle on to complete any furhter
  2527. * error action down this failed path.
  2528. */
  2529. pfprintf(stderr, "On error fail path %s continuing\n", name);
  2530. return (BREAK);
  2531. } else {
  2532. aiop->retrycnt = 0;
  2533. pfprintf(stderr, "On error fail path %s retrying\n", name);
  2534. return (RETRY);
  2535. }
  2536. }
  2537. /*ARGSUSED*/
  2538. static loop_type
  2539. on_error_retry(ullong_t start, struct aio_str *aiop)
  2540. {
  2541. int isread = is_read_io(aiop);
  2542. short max = (isread ?
  2543. OPTION(max_read_retries) : OPTION(max_write_retries));
  2544. if (aiop->retrycnt < max) {
  2545. pfprintf(stderr,
  2546. "On %s error retry %d, %d remaining %s blk %#llx\n",
  2547. isread ? "read": "write",
  2548. 1+aiop->retrycnt, max-(1+aiop->retrycnt),
  2549. aiop->fd->name, aio_str2byteoff(aiop));
  2550. return (RETRY);
  2551. } else {
  2552. return (CONTINUE);
  2553. }
  2554. }
  2555. static loop_type
  2556. on_error_rewrite(ullong_t start, struct aio_str *aiop)
  2557. {
  2558. ssize_t status;
  2559. union err_info err_info;
  2560. struct shadow_hdr const *shadow_hdr = get_shadow_hdr(aiop->buf);
  2561. ullong_t offset = (ullong_t)start +
  2562. (ullong_t)(INDEX_TO_DIOLEN(max_disk_io_len)*aiop->off);
  2563. err_info.str = "pwrite";
  2564. pfprintf(stderr, "%s On error rewrite\n", aiop->fd->name);
  2565. aiop->daio_id.buf = aiop->buf;
  2566. aiop->daio_id.buf_id = get_write_buf_id(aiop->buf);
  2567. aiop->daio_id.chksum = shadow_hdr->chksums[aiop->iolen];
  2568. aiop->daio_id.bufs = opts.disk_io_sizes.vals[aiop->iolen];
  2569. aiop->daio_id.hdr = (uchar_t *)&aiop->hdr;
  2570. aiop->daio_id.footer_len = 0;
  2571. aiop->daio_id.hdr_len = sizeof (aiop->hdr);
  2572. (void) memcpy(aiop->daio_id.hdr, aiop->daio_id.buf,
  2573. aiop->daio_id.hdr_len);
  2574. (void) my_gettimeofday(&aiop->tv, NULL);
  2575. status = daio->pwrite(aiop->fd->fd, aiop->buf,
  2576. opts.disk_io_sizes.vals[aiop->iolen], offset, &aiop->daio_id);
  2577. DAIO_SET_RETURN(aiop->aio_res, status);
  2578. DAIO_SET_ERROR(aiop->aio_res, errno);
  2579. if (status != opts.disk_io_sizes.vals[aiop->iolen]) {
  2580. report_error(aiop, err_info, ERR_SYS);
  2581. aiop->dev->errors += 1;
  2582. return (CONTINUE);
  2583. }
  2584. return (BREAK);
  2585. }
  2586. static loop_type
  2587. on_error_reread(ullong_t start, struct aio_str *aiop)
  2588. {
  2589. ssize_t status;
  2590. union err_info err_info;
  2591. ullong_t offset = (ullong_t)start +
  2592. (ullong_t)(INDEX_TO_DIOLEN(max_disk_io_len)*aiop->off);
  2593. err_info.str = "pread";
  2594. pfprintf(stderr, "%s On error re-read\n", aiop->fd->name);
  2595. (void) my_gettimeofday(&aiop->tv, NULL);
  2596. status = daio->pread(aiop->fd->fd, aiop->buf,
  2597. opts.disk_io_sizes.vals[aiop->iolen], offset, &aiop->daio_id);
  2598. DAIO_SET_RETURN(aiop->aio_res, status);
  2599. DAIO_SET_ERROR(aiop->aio_res, errno);
  2600. if (status == DAIO_CORRUPT) {
  2601. if (is_readonly() || !do_memcmp(start, aiop)) {
  2602. report_error(aiop, err_info, ERR_CORRUPT);
  2603. aiop->dev->errors += 1;
  2604. memdiff(aiop, err_info.str);
  2605. return (CONTINUE);
  2606. }
  2607. /* There should be an assert here */
  2608. } else if (status != opts.disk_io_sizes.vals[aiop->iolen]) {
  2609. report_error(aiop, err_info, ERR_SYS);
  2610. aiop->dev->errors += 1;
  2611. return (CONTINUE);
  2612. }
  2613. return (BREAK);
  2614. }
  2615. static int
  2616. do_path_stop_check(struct fds *fd, struct device *dev)
  2617. {
  2618. if (fd->error_path == 0 && fd->stop_flag == 0) {
  2619. if (fd->need_to_stop == 1 &&
  2620. set_shared_stop_flag(fd->shared_data_handle) != -1) {
  2621. fd->need_to_stop = 0;
  2622. /*
  2623. * need to cancel all the io outstanding for this
  2624. * path
  2625. */
  2626. fd->stop_flag = 1;
  2627. cancel_all_io_byfd(fd);
  2628. snapshot_recent(dev->recent);
  2629. bring_error_path_online(fd);
  2630. } else {
  2631. if (do_stop_check(fd->shared_data_handle) == 1) {
  2632. fd->stop_flag = 1;
  2633. cancel_all_io_byfd(fd);
  2634. bring_error_path_online(fd);
  2635. } else {
  2636. return (0);
  2637. }
  2638. }
  2639. }
  2640. return (1);
  2641. }
  2642. static int
  2643. init_stop_check(void)
  2644. {
  2645. stop_check = do_stop_check;
  2646. return (1);
  2647. }
  2648. static int
  2649. init_path_stop_check(void)
  2650. {
  2651. path_stop_check = do_path_stop_check;
  2652. return (1);
  2653. }
  2654. void
  2655. newfd(struct aio_str *aiop)
  2656. {
  2657. struct fds *fd = aiop->fd;
  2658. if (aiop->fd != aiop->fd->next) {
  2659. while (aiop->fd->next != fd) {
  2660. if (path_stop_check(aiop->fd->next, aiop->dev) == 0) {
  2661. aiop->fd = aiop->fd->next;
  2662. remove_from_aio_list(&fd->all_aios, aiop);
  2663. add_to_all_aios(aiop);
  2664. return;
  2665. }
  2666. aiop->fd = aiop->fd->next;
  2667. }
  2668. /*
  2669. * To get here we searched them all and found none that
  2670. * were not error paths or had been stopped. Reset the path
  2671. * back to the original.
  2672. */
  2673. aiop->fd = fd;
  2674. }
  2675. }
  2676. loop_type
  2677. handle_err_generic(struct aio_str *aiop, ullong_t start,
  2678. on_error_t *on_error_func)
  2679. {
  2680. struct blks *blocks = NULL;
  2681. loop_type status = BREAK;
  2682. blocks = aio_attach(aiop);
  2683. aiop->dev->errors++;
  2684. if (blocks[AIO_BLOCK_INDEX(aiop)].r.w.last_io !=
  2685. NULL && does_check(daio->what_checker())) {
  2686. char i;
  2687. union err_info err_info;
  2688. err_info.str = "aioread";
  2689. if (on_error_func == on_error_corrupt) {
  2690. memdiff(aiop, (aiop->retrycnt == 0) ?
  2691. "aioread" : "aioread RETRY");
  2692. check_matching_io(start, aiop);
  2693. report_error(aiop, err_info, ERR_CORRUPT);
  2694. } else {
  2695. report_error(aiop, err_info, ERR_SYS);
  2696. }
  2697. for (i = 0; on_error_func[i] != NULL; i++) {
  2698. loop_type l;
  2699. if ((l = on_error_func[i](start, aiop)) == BREAK)
  2700. break;
  2701. else if (l == RETRY) {
  2702. status = RETRY;
  2703. break;
  2704. }
  2705. }
  2706. }
  2707. if (incr_shared_device_error(aiop->dev->shared_data_handle,
  2708. aiop->dev->errors) != -1) {
  2709. aiop->dev->errors = 0;
  2710. }
  2711. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  2712. return (status);
  2713. }
  2714. loop_type
  2715. handle_write_error(struct aio_str *aiop, ullong_t start)
  2716. {
  2717. loop_type status = BREAK;
  2718. int i;
  2719. aiop->dev->errors++;
  2720. for (i = 0; on_write_error[i] != NULL; i++) {
  2721. loop_type l;
  2722. if ((l = on_write_error[i](start, aiop)) == BREAK)
  2723. break;
  2724. else if (l == RETRY) {
  2725. status = RETRY;
  2726. break;
  2727. }
  2728. }
  2729. if (incr_shared_device_error(aiop->dev->shared_data_handle,
  2730. aiop->dev->errors) != -1) {
  2731. aiop->dev->errors = 0;
  2732. }
  2733. return (status);
  2734. }
  2735. loop_type
  2736. handle_read_corrupt(struct aio_str *aiop, ullong_t start)
  2737. {
  2738. return (handle_err_generic(aiop, start, on_error_corrupt));
  2739. }
  2740. loop_type
  2741. handle_read_short(struct aio_str *aiop, ullong_t start)
  2742. {
  2743. return (handle_err_generic(aiop, start, on_error_short));
  2744. }
  2745. static time_t
  2746. handle_readonly(struct aio_str *aiop, ullong_t start, read_type_t read_type)
  2747. {
  2748. struct blks *blocks = NULL;
  2749. struct timeval tv;
  2750. hrtime_t delta = DAIO_GET_TIME_TAKEN(aiop->aio_res);
  2751. TNF_PROBE_2(handle_read, "handle_readonly",
  2752. "sunw%cte%diskomizer%aio readonly wait",
  2753. tnf_opaque, aiop, aiop,
  2754. aio_tnf_str, *aiop, aiop);
  2755. while (my_gettimeofday(&tv, NULL) == -1)
  2756. pperror("gettimeofday");
  2757. if (aiop->fd == NULL) {
  2758. aiop->fd = aiop->dev->fdhead;
  2759. add_to_aio_list(&aiop->fd->all_aios, aiop);
  2760. } else {
  2761. if (DAIO_RETURN(aiop->aio_res) == DAIO_CORRUPT) {
  2762. if (handle_read_corrupt(aiop, start) == RETRY) {
  2763. return (do_new_read(aiop, start, RETRY_READ));
  2764. }
  2765. } else if (DAIO_RETURN(aiop->aio_res) !=
  2766. opts.disk_io_sizes.vals[aiop->iolen]) {
  2767. if (handle_read_short(aiop, start) == RETRY) {
  2768. return (do_new_read(aiop, start, RETRY_READ));
  2769. }
  2770. } else {
  2771. struct blks *block;
  2772. aiop->fd->last_read_time = delta;
  2773. if (aiop->retrycnt != 0) {
  2774. char *now_str;
  2775. now_str = alloc_time_str(tv.tv_sec);
  2776. pprintf("Read retry %d of block 0x%llx "
  2777. "on %s o.k. %s\n",
  2778. aiop->retrycnt,
  2779. aio_str2byteoff(aiop),
  2780. aiop->fd->name,
  2781. NIL(now_str));
  2782. not_null_free(now_str);
  2783. }
  2784. blocks = aio_attach(aiop);
  2785. block = &blocks[AIO_BLOCK_INDEX(aiop)];
  2786. block->r.o.prev_io =
  2787. blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_io;
  2788. block->r.o.prev_chksum =
  2789. blocks[AIO_BLOCK_INDEX(aiop)].r.o.last_chksum;
  2790. block->r.o.last_io =
  2791. (((ulong_t)aiop->daio_id.buf) & 0x1);
  2792. block->r.o.last_chksum = aiop->daio_id.chksum;
  2793. block->u.prev_requested = block->last_requested;
  2794. block->last_requested = aiop->tv.tv_sec;
  2795. block->last_returned_delta =
  2796. tv.tv_sec - aiop->tv.tv_sec;
  2797. }
  2798. if (blocks == NULL) {
  2799. blocks = aio_attach(aiop);
  2800. }
  2801. blocks[AIO_BLOCK_INDEX(aiop)].read_count += 1;
  2802. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  2803. clear_writemap(aiop);
  2804. newfd(aiop);
  2805. }
  2806. update_aio_read_stats(aiop);
  2807. aiop->count++;
  2808. return_aio_read_buf(aiop);
  2809. return (do_new_read(aiop, start, read_type));
  2810. }
  2811. time_t
  2812. handle_readonly_rand(struct aio_str *aiop, ullong_t start)
  2813. {
  2814. return (handle_readonly(aiop, start, READ_ONLY_RAND));
  2815. }
  2816. time_t
  2817. handle_readonly_seq(struct aio_str *aiop, ullong_t start)
  2818. {
  2819. return (handle_readonly(aiop, start, READ_ONLY_SEQ));
  2820. }
  2821. time_t
  2822. handle_read(struct aio_str *aiop, ullong_t start)
  2823. {
  2824. struct blks *blocks = NULL;
  2825. hrtime_t delta = DAIO_GET_TIME_TAKEN(aiop->aio_res);
  2826. TNF_PROBE_2(handle_read, "handle_read",
  2827. "sunw%cte%diskomizer%aio read wait",
  2828. tnf_opaque, aiop, aiop,
  2829. aio_tnf_str, *aiop, aiop);
  2830. aiop->fd->total_read--;
  2831. if (DAIO_RETURN(aiop->aio_res) == DAIO_CORRUPT) {
  2832. if (!do_memcmp(start, aiop) &&
  2833. handle_read_corrupt(aiop, start) == RETRY) {
  2834. return (do_new_read(aiop, start, RETRY_READ));
  2835. }
  2836. } else if (DAIO_RETURN(aiop->aio_res) !=
  2837. opts.disk_io_sizes.vals[aiop->iolen]) {
  2838. if (handle_read_short(aiop, start) == RETRY) {
  2839. return (do_new_read(aiop, start, RETRY_READ));
  2840. }
  2841. } else {
  2842. aiop->fd->last_read_time = delta;
  2843. if (aiop->retrycnt != 0) {
  2844. pprintf("Read retry %d of block 0x%llx on %s o.k.\n",
  2845. aiop->retrycnt, aio_str2byteoff(aiop),
  2846. aiop->fd->name);
  2847. }
  2848. if (opts.obscure_execute && is_executable(aiop->buf)) {
  2849. run_func(aiop->buf,
  2850. opts.disk_io_sizes.vals[aiop->iolen] -
  2851. SIZEOF_BUFHDR);
  2852. }
  2853. }
  2854. if (blocks == NULL) {
  2855. blocks = aio_attach(aiop);
  2856. }
  2857. blocks[AIO_BLOCK_INDEX(aiop)].read_count += 1;
  2858. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  2859. clear_writemap(aiop);
  2860. update_aio_read_stats(aiop);
  2861. aiop->count++;
  2862. return_aio_read_buf(aiop);
  2863. newfd(aiop);
  2864. return (do_new_read(aiop, start, NORMAL_READ));
  2865. }
  2866. time_t
  2867. handle_read_then_write(struct aio_str *aiop, ullong_t start)
  2868. {
  2869. hrtime_t delta = DAIO_GET_TIME_TAKEN(aiop->aio_res);
  2870. struct timeval tv;
  2871. while (my_gettimeofday(&tv, NULL) == -1)
  2872. pperror("gettimeofday");
  2873. update_aio_read_stats(aiop);
  2874. if (aiop->fd == NULL) {
  2875. /* This is the first write so no read to check */
  2876. aiop->count++;
  2877. aiop->buf = get_write_buf();
  2878. aiop->fd = aiop->dev->fdhead;
  2879. add_to_aio_list(&aiop->fd->all_aios, aiop);
  2880. } else {
  2881. aiop->fd->total_read--;
  2882. if (DAIO_RETURN(aiop->aio_res) == DAIO_CORRUPT) {
  2883. /* handle read error */
  2884. if (!do_memcmp(start, aiop) &&
  2885. handle_read_corrupt(aiop, start) == RETRY) {
  2886. return (do_new_read(aiop, start, RETRY_READ));
  2887. }
  2888. } else if (DAIO_RETURN(aiop->aio_res) !=
  2889. opts.disk_io_sizes.vals[aiop->iolen]) {
  2890. /* handle read error */
  2891. if (handle_read_short(aiop, start) == RETRY) {
  2892. return (do_new_read(aiop, start, RETRY_READ));
  2893. }
  2894. } else {
  2895. if (opts.obscure_execute && is_executable(aiop->buf)) {
  2896. run_func(aiop->buf,
  2897. opts.disk_io_sizes.vals[aiop->iolen] -
  2898. SIZEOF_BUFHDR);
  2899. }
  2900. aiop->fd->last_read_time = delta;
  2901. }
  2902. return_read_buf(aiop->buf);
  2903. /*
  2904. * Need to return the disk block to the free list
  2905. *
  2906. * the use of clear_writemap_success() reflects the fact that
  2907. * to get here the write to this block must have succeeded.
  2908. */
  2909. clear_writemap_success(aiop);
  2910. aiop->buf = get_write_buf();
  2911. (void) set_io_len(aiop);
  2912. newfd(aiop);
  2913. aiop->count++;
  2914. }
  2915. aiop->handler = handle_write_then_read;
  2916. do_new_write(aiop, start, 0);
  2917. return (tv.tv_sec);
  2918. }
  2919. time_t
  2920. handle_write_then_read(struct aio_str *aiop, ullong_t start)
  2921. {
  2922. struct timeval tv;
  2923. struct blks *block;
  2924. struct blks *blocks;
  2925. struct bufhdr hdr;
  2926. while (my_gettimeofday(&tv, NULL) == -1)
  2927. pperror("gettimeofday");
  2928. assert(aiop->buf == NULL || *aiop->buf == 0xAA || *aiop->buf == 0x55);
  2929. aiop->fd->total_write--;
  2930. if (DAIO_RETURN(aiop->aio_res) !=
  2931. opts.disk_io_sizes.vals[aiop->iolen]) {
  2932. union err_info err_info;
  2933. err_info.str = "aiowrite";
  2934. report_error(aiop, err_info, ERR_SYS);
  2935. if (handle_write_error(aiop, start) == RETRY) {
  2936. do_new_write(aiop, start, 1);
  2937. } else {
  2938. if (is_sequential(aiop) &&
  2939. (aiop->off % opts.nprocs) == this_proc()) {
  2940. push_unwritten(aiop);
  2941. }
  2942. do_new_write(aiop, start, 0);
  2943. }
  2944. return (tv.tv_sec);
  2945. }
  2946. update_aio_write_stats(aiop);
  2947. aiop->count++;
  2948. aiop->fd->last_write_time = DAIO_GET_TIME_TAKEN(aiop->aio_res);
  2949. hdr = get_bufhdr(aiop->buf);
  2950. blocks = aio_attach(aiop);
  2951. block = &blocks[AIO_BLOCK_INDEX(aiop)];
  2952. block->r.w.prev_io = block->r.w.last_io;
  2953. block->r.w.prev_iolen = block->r.w.last_iolen;
  2954. block->u.prev_requested = block->last_requested;
  2955. block->last_requested = aiop->tv.tv_sec;
  2956. block->last_returned_delta = tv.tv_sec - aiop->tv.tv_sec;
  2957. block->r.w.last_io = aiop->buf;
  2958. block->r.w.last_iolen = aiop->iolen;
  2959. if (hdr.start == BUF_TYPE_A)
  2960. block->ab = 1;
  2961. else
  2962. block->ab = 0;
  2963. block->read_count = 0;
  2964. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  2965. return_write_buf(aiop->buf);
  2966. aiop->buf = get_read_buf();
  2967. aiop->handler = handle_read_then_write;
  2968. return (do_new_read(aiop, start, WRITE_READ));
  2969. }
  2970. time_t
  2971. handle_write(struct aio_str *aiop, ullong_t start)
  2972. {
  2973. struct timeval tv;
  2974. union err_info err_info;
  2975. struct bufhdr hdr;
  2976. struct blks *block;
  2977. struct blks *blocks;
  2978. err_info.str = "aiowrite";
  2979. if (aiop->buf != NULL) {
  2980. if (*aiop->buf != 0xAA && *aiop->buf != 0x55) {
  2981. char tmp = *aiop->buf;
  2982. void *sig = expect_signal(SIGSEGV,
  2983. "Buffer not mapped writable but was updated!",
  2984. aiop->buf, sizeof (*aiop->buf));
  2985. *aiop->buf = 0;
  2986. *aiop->buf = tmp;
  2987. cancel_expected_signal(SIGSEGV, sig);
  2988. exit(1);
  2989. }
  2990. if (aiop->fd != NULL) {
  2991. aiop->fd->total_write--;
  2992. }
  2993. } else {
  2994. if (aiop->fd != NULL) {
  2995. aiop->fd->total_write--;
  2996. plog(LOG_WARNING, "buf == NULL, off %#llx (0t%lld)\n",
  2997. (ullong_t)aiop->off, (ullong_t)aiop->off);
  2998. }
  2999. }
  3000. TNF_PROBE_2(handle_write, "handle_write",
  3001. "sunw%cte%diskomizer%aio write wait",
  3002. tnf_opaque, aiop, aiop,
  3003. aio_tnf_str, *aiop, aiop);
  3004. while (my_gettimeofday(&tv, NULL) == -1)
  3005. pperror("gettimeofday");
  3006. if (DAIO_RETURN(aiop->aio_res) !=
  3007. opts.disk_io_sizes.vals[aiop->iolen]) {
  3008. /* retry the write */
  3009. if (aiop->fd != NULL) {
  3010. report_error(aiop, err_info, ERR_SYS);
  3011. if (handle_write_error(aiop, start) == RETRY) {
  3012. do_new_write(aiop, start, 1);
  3013. } else {
  3014. if (is_sequential(aiop) &&
  3015. (aiop->off % opts.nprocs) ==
  3016. this_proc()) {
  3017. push_unwritten(aiop);
  3018. }
  3019. do_new_write(aiop, start, 0);
  3020. }
  3021. return (tv.tv_sec);
  3022. } else {
  3023. aiop->fd = aiop->dev->fdhead;
  3024. add_to_aio_list(&aiop->fd->all_aios, aiop);
  3025. do_new_write(aiop, start, 0);
  3026. return (tv.tv_sec);
  3027. }
  3028. }
  3029. aiop->fd->last_write_time = DAIO_GET_TIME_TAKEN(aiop->aio_res);
  3030. update_aio_write_stats(aiop);
  3031. aiop->count++;
  3032. hdr = get_bufhdr(aiop->buf);
  3033. if (aiop->retrycnt) {
  3034. ullong_t block = aio_str2byteoff(aiop);
  3035. pfprintf(stderr,
  3036. "%s Block 0t%lld (%#llx) retry %d succeeded\n",
  3037. aiop->fd->name, (ullong_t)block,
  3038. (ullong_t)block, ++aiop->retrycnt);
  3039. }
  3040. blocks = aio_attach(aiop);
  3041. block = &blocks[AIO_BLOCK_INDEX(aiop)];
  3042. block->r.w.prev_io = block->r.w.last_io;
  3043. block->r.w.prev_iolen = block->r.w.last_iolen;
  3044. block->u.prev_requested = block->last_requested;
  3045. block->last_requested = aiop->tv.tv_sec;
  3046. block->last_returned_delta = tv.tv_sec - aiop->tv.tv_sec;
  3047. block->r.w.last_io = aiop->buf;
  3048. block->r.w.last_iolen = aiop->iolen;
  3049. if (hdr.start == BUF_TYPE_A)
  3050. block->ab = 1;
  3051. else
  3052. block->ab = 0;
  3053. block->read_count = 0;
  3054. shm_ops->detach(AIO_BLOCK_HANDLE(aiop));
  3055. clear_writemap_success(aiop);
  3056. assert(aiop->buf != NULL);
  3057. if (aiop->count %
  3058. opts.expert_release_write_buffers_after_n_uses == 0) {
  3059. return_write_buf(aiop->buf);
  3060. aiop->buf = NULL;
  3061. }
  3062. newfd(aiop);
  3063. do_new_write(aiop, start, 0);
  3064. return (tv.tv_sec);
  3065. }
  3066. void
  3067. init_all_aio(struct device *devices, struct aio_str *aio,
  3068. int count)
  3069. {
  3070. int i, j;
  3071. struct device *device;
  3072. for (j = i = 0; i < count; i++) {
  3073. for (device = devices; device != NULL; device = device->next) {
  3074. if (i == 0)
  3075. device->block = this_proc();
  3076. aio[j].dev = device;
  3077. aio[j].fd = NULL;
  3078. aio[j].iolen = 0;
  3079. add_to_aio_list(&device->stopped_ios, &aio[j]);
  3080. while (my_gettimeofday(&device->state_ttl, NULL) == -1)
  3081. pperror("gettimeofday");
  3082. j++;
  3083. }
  3084. }
  3085. }
  3086. void
  3087. cancel_all_io_byfd(struct fds *fd)
  3088. {
  3089. struct aioqtop not_cancelled;
  3090. struct aioqtop cancelled;
  3091. struct aio_str *io;
  3092. struct device *devp;
  3093. int count = 0;
  3094. ZERO_OBJ(not_cancelled);
  3095. ZERO_OBJ(cancelled);
  3096. while ((io = pop_from_aio_list(&fd->all_aios)) != NULL) {
  3097. devp = io->dev;
  3098. if (daio->cancel(&io->aio_res) == -1) {
  3099. io->count = errno;
  3100. add_to_aio_list(&not_cancelled, io);
  3101. } else {
  3102. count++;
  3103. add_to_aio_list(&cancelled, io);
  3104. }
  3105. }
  3106. plog(LOG_WARNING, "%d io's cancelled to path %s\n", count,
  3107. fd->name);
  3108. while ((io = pop_from_aio_list(&cancelled)) != NULL) {
  3109. DAIO_SET_ERROR(io->aio_res, ECANCELED);
  3110. DAIO_SET_RETURN(io->aio_res, -1);
  3111. add_to_aio_list(&devp->cancelled, io);
  3112. cancelled_count++;
  3113. }
  3114. if (cancelled_count) {
  3115. start_cancelled_io = do_start_cancelled_io;
  3116. }
  3117. fd->all_aios = not_cancelled;
  3118. }
  3119. void
  3120. cancel_all_io(void)
  3121. {
  3122. int i = 0;
  3123. int errors = 0;
  3124. int total = 0;
  3125. int reaped = 0;
  3126. struct aio_str *io;
  3127. struct aio_str *first_error_io = NULL;
  3128. struct fds *fd;
  3129. struct aioqtop not_cancelled;
  3130. struct device *dev;
  3131. /*
  3132. * If daio is NULL, then no paths can be open so nothing more to do.
  3133. */
  3134. if (daio == NULL)
  3135. return;
  3136. time_now_log(LOG_NOTICE, gettext("cancelling all aios\n"));
  3137. ZERO_OBJ(not_cancelled);
  3138. for (dev = devices; dev != NULL; dev = dev->next) {
  3139. for (fd = dev->fdhead; ; fd = fd->next) {
  3140. while ((io = pop_from_aio_list(
  3141. &fd->all_aios)) != NULL) {
  3142. total++;
  3143. if (daio->cancel(&io->aio_res) == -1) {
  3144. io->count = errno;
  3145. add_to_aio_list(&not_cancelled, io);
  3146. } else {
  3147. i++;
  3148. }
  3149. }
  3150. if (fd->next == dev->fdhead)
  3151. break;
  3152. }
  3153. }
  3154. /*
  3155. * Now reap all the remaining ios, popping them off the list of
  3156. * ios that could not be cancelled.
  3157. */
  3158. while ((io = (aio_str_t *)daio->wait(NULL)) != (aio_str_t *)-1 ||
  3159. errno != EINVAL) {
  3160. if (io != (aio_str_t *)-1 && io != (aio_str_t *)0) {
  3161. reaped++;
  3162. remove_from_aio_list(&not_cancelled, io);
  3163. }
  3164. }
  3165. /*
  3166. * If the list contains more than one entry there was a problem,
  3167. * probably in the internal logic of diskomizer.
  3168. */
  3169. while ((io = pop_from_aio_list(&not_cancelled)) != NULL) {
  3170. errno = io->count;
  3171. /*
  3172. * If we were interupted the signal might have come in while
  3173. * we were handling an io so we could have just one io
  3174. * that is not in the aio system. So only report errors
  3175. * if there are more then one. If there are more than one
  3176. * report them all.
  3177. *
  3178. */
  3179. if (errors++ > 0) {
  3180. if (first_error_io != NULL) {
  3181. AIOCANCEL_ERROR(first_error_io);
  3182. first_error_io = NULL;
  3183. }
  3184. AIOCANCEL_ERROR(io);
  3185. } else {
  3186. first_error_io = io;
  3187. }
  3188. }
  3189. for (dev = devices; dev != NULL; dev = dev->next) {
  3190. close_and_free_paths(dev);
  3191. }
  3192. time_now_log(LOG_NOTICE,
  3193. "%d/%d aios cancelled successfully, %d reaped\n",
  3194. i, total, reaped);
  3195. }
  3196. pid_t
  3197. master_pid()
  3198. {
  3199. return (parent_pid);
  3200. }
  3201. static int
  3202. is_master()
  3203. {
  3204. return (parent_pid == getpid());
  3205. }
  3206. static void
  3207. register_death(pid_t pid)
  3208. {
  3209. int i;
  3210. for (i = 0; i < opts.nprocs; i++) {
  3211. if (proc_store[i].pid != pid) {
  3212. proc_store[i].pid = 0;
  3213. break;
  3214. }
  3215. }
  3216. }
  3217. static int
  3218. ischildless()
  3219. {
  3220. int i;
  3221. if (proc_store != NULL) {
  3222. for (i = 0; i < opts.nprocs; i++) {
  3223. if (proc_store[i].pid != 0)
  3224. return (0);
  3225. }
  3226. }
  3227. return (1);
  3228. }
  3229. static int
  3230. haskids()
  3231. {
  3232. return (!ischildless());
  3233. }
  3234. static void
  3235. mourning(pid_t pid, int stat)
  3236. {
  3237. union {
  3238. char dir[PATH_MAX];
  3239. char buf[SIG2STR_MAX];
  3240. } u;
  3241. if (pid == -1)
  3242. return;
  3243. if (!WIFEXITED(stat) && !WIFSIGNALED(stat))
  3244. return;
  3245. register_death(pid);
  3246. if (WIFSIGNALED(stat)) {
  3247. char *x = strsignal(WTERMSIG(stat));
  3248. if (sig2str(WTERMSIG(stat), u.buf) == -1) {
  3249. (void) strcpy(u.buf, "(Unknown)");
  3250. }
  3251. plog(LOG_ERR,
  3252. "Process %ld killed by signal %d %s,%s%s%s.\n",
  3253. (ulong_t)pid, WTERMSIG(stat), u.buf,
  3254. x == NULL ? "" : " ", x == NULL ? "" : x,
  3255. WCOREDUMP(stat) ? " core dumped" : "");
  3256. }
  3257. if (pid) {
  3258. (void) snprintf(u.dir, sizeof (u.dir), "%s/%ld",
  3259. opts.workingdir, (ulong_t)pid);
  3260. plog(LOG_DEBUG, "removing %s\n", u.dir);
  3261. if (rmdir(u.dir) == -1)
  3262. pperror("rmdir(%s)", u.dir);
  3263. }
  3264. }
  3265. static int
  3266. all_countdowns_zero(struct device *devp)
  3267. {
  3268. while (devp) {
  3269. if (devp->countdown > 0) {
  3270. return (0);
  3271. }
  3272. devp = devp->next;
  3273. }
  3274. return (1);
  3275. }
  3276. static int
  3277. stoptime_reached(void)
  3278. {
  3279. return (stoptime > 0 && stoptime < gethrtime());
  3280. }
  3281. static void
  3282. report_exit_reason(void)
  3283. {
  3284. if (opts.nloops && all_countdowns_zero(devices)) {
  3285. time_now_log(LOG_NOTICE,
  3286. "All devices have completed %ld loops; exiting",
  3287. opts.nloops);
  3288. } else if (stoptime_reached()) {
  3289. time_now_log(LOG_NOTICE, "stop time reached; exiting");
  3290. }
  3291. }
  3292. void
  3293. cleanup(void)
  3294. {
  3295. (void) sigignore(SIGTERM);
  3296. (void) sigignore(SIGINT);
  3297. new_log_transaction(stderr);
  3298. if (is_master()) {
  3299. int stat;
  3300. pid_t pid;
  3301. struct device *dev;
  3302. infantacide();
  3303. while (haskids() &&
  3304. (pid = waitpid((pid_t)-1, &stat, WNOHANG)) != -1 &&
  3305. errno != ECHILD) {
  3306. if (pid == 0) {
  3307. sleep(1);
  3308. infantacide();
  3309. } else {
  3310. mourning(pid, stat);
  3311. }
  3312. }
  3313. save_data_bufs();
  3314. for (dev = devices; dev != NULL; dev = dev->next) {
  3315. close_and_free_paths(dev);
  3316. }
  3317. if (rmdir(opts.workingdir) == -1) {
  3318. pperror("rmdir(%s)", opts.workingdir);
  3319. }
  3320. shm_ops->fini();
  3321. } else {
  3322. report_exit_reason();
  3323. cancel_all_io();
  3324. }
  3325. time_now_log(LOG_NOTICE, "exiting");
  3326. }
  3327. /*
  3328. * change_dir change into our own directory.
  3329. */
  3330. void
  3331. change_dir()
  3332. {
  3333. char dir[PATH_MAX];
  3334. (void) snprintf(dir, sizeof (dir),
  3335. "%s/%ld", opts.workingdir, (ulong_t)getpid());
  3336. if (mkdir(opts.workingdir, 0755) == -1 && errno != EEXIST)
  3337. pperror("mkdir(%s, 0755)", opts.workingdir);
  3338. if (mkdir(dir, 0755) == -1)
  3339. pperror("mkdir(%s, 0755)", dir);
  3340. if (chdir(dir) == -1) {
  3341. pperror("chdir(%s)", dir);
  3342. (void) snprintf(dir, sizeof (dir),
  3343. "%s.%ld", diffs_str, (ulong_t)getpid());
  3344. diffs = strdup(dir);
  3345. if (diffs == NULL)
  3346. diffs = diffs_str;
  3347. } else {
  3348. diffs = diffs_str;
  3349. }
  3350. }
  3351. /*
  3352. * aios_outstanding_or_on_hold:
  3353. * return 1 if there are aios outstanding.
  3354. * return 0 if there are none.
  3355. */
  3356. int
  3357. aios_queued_to_fd(struct device *dev)
  3358. {
  3359. struct fds *fd;
  3360. for (fd = dev->fdhead; ; fd = fd->next) {
  3361. if (is_aio_on_list(&fd->all_aios))
  3362. return (1);
  3363. if (fd->next == dev->fdhead)
  3364. break;
  3365. }
  3366. return (0);
  3367. }
  3368. struct device *
  3369. first_to_restart(struct device *devices)
  3370. {
  3371. struct device *dev;
  3372. struct device *first_to_start;
  3373. struct timeval now_tv;
  3374. do {
  3375. while (my_gettimeofday(&now_tv, NULL) == -1)
  3376. pperror("gettimeofday");
  3377. for (first_to_start = devices;
  3378. first_to_start != NULL && (
  3379. is_aio_on_list(&first_to_start->stopped_ios) == 0 ||
  3380. get_dev_state(first_to_start, &now_tv) ==
  3381. DEV_NOT_READY);
  3382. first_to_start = first_to_start->next) {
  3383. /*LINTED*/
  3384. }
  3385. if (first_to_start == NULL)
  3386. break;
  3387. for (dev = first_to_start->next; dev != NULL;
  3388. dev = dev->next) {
  3389. if (get_dev_state(dev, &now_tv) == DEV_NOT_READY) {
  3390. continue;
  3391. }
  3392. if (is_aio_on_list(&first_to_start->stopped_ios) !=
  3393. 0 && dev->state_ttl.tv_sec != -1 &&
  3394. (first_to_start->state_ttl.tv_sec == -1 ||
  3395. timeval_lt(dev->state_ttl,
  3396. first_to_start->state_ttl))) {
  3397. first_to_start = dev;
  3398. }
  3399. }
  3400. } while (first_to_start != NULL &&
  3401. first_to_start->state_ttl.tv_sec == -1 && sleep(1) != 2);
  3402. return (first_to_start);
  3403. }
  3404. struct aio_str *
  3405. wait_to_restart(struct device *devices)
  3406. {
  3407. struct device *first_to_start;
  3408. struct aio_str *aiop;
  3409. struct timeval tv;
  3410. first_to_start = first_to_restart(devices);
  3411. if (first_to_start == NULL)
  3412. return (NULL);
  3413. while (my_gettimeofday(&tv, NULL) == -1)
  3414. pperror("gettimeofday");
  3415. tv = timeval_timeval_sub(first_to_start->state_ttl, tv);
  3416. if (tv.tv_sec) {
  3417. char buf[128];
  3418. (void) strftime(buf, 128, TIME_FORMAT,
  3419. localtime(&first_to_start->state_ttl.tv_sec));
  3420. if (tv.tv_sec > secs_till_exit()) {
  3421. pprintf("All IO on hold until after our exit time.\n");
  3422. exit(0);
  3423. }
  3424. pprintf("Sleeping for %ld seconds, until %s\n", tv.tv_sec, buf);
  3425. (void) sleep(tv.tv_sec);
  3426. check_exit_flag();
  3427. }
  3428. if (tv.tv_usec) {
  3429. (void) usleep(tv.tv_usec);
  3430. check_exit_flag();
  3431. }
  3432. aiop = pop_from_aio_list(&first_to_start->stopped_ios);
  3433. if (aiop != NULL && aiop->fd != NULL)
  3434. add_to_aio_list(&aiop->fd->all_aios, aiop);
  3435. return (aiop);
  3436. }
  3437. int
  3438. aios_outstanding(struct device *devices)
  3439. {
  3440. struct device *dev;
  3441. for (dev = devices; dev != NULL; dev = dev->next) {
  3442. if (aios_queued_to_fd(dev)) {
  3443. return (1);
  3444. }
  3445. }
  3446. return (0);
  3447. }
  3448. #ifdef NOT_USED
  3449. int
  3450. aios_on_hold(struct device *devices)
  3451. {
  3452. struct device *dev;
  3453. for (dev = devices; dev != NULL; dev = dev->next) {
  3454. if (is_aio_on_list(&dev->stopped_ios)) {
  3455. return (1);
  3456. }
  3457. }
  3458. return (0);
  3459. }
  3460. #endif
  3461. int
  3462. aios_outstanding_or_on_hold(struct device *devices)
  3463. {
  3464. struct device *dev;
  3465. for (dev = devices; dev != NULL; dev = dev->next) {
  3466. if (is_aio_on_list(&dev->cancelled))
  3467. return (1);
  3468. if (is_aio_on_list(&dev->stopped_ios))
  3469. return (1);
  3470. if (aios_queued_to_fd(dev)) {
  3471. return (1);
  3472. }
  3473. }
  3474. return (0);
  3475. }
  3476. /*
  3477. * Return true if diskomizer would exit before the time supplied is reached.
  3478. */
  3479. int
  3480. would_stop_before(time_t secs)
  3481. {
  3482. time_t xit;
  3483. if (secs == -1 || (xit = secs_till_exit()) < 0) {
  3484. return (0);
  3485. } else {
  3486. struct timeval tv;
  3487. while (my_gettimeofday(&tv, NULL) == -1)
  3488. pperror("gettimeofday");
  3489. return (secs >= tv.tv_sec + xit);
  3490. }
  3491. }
  3492. /*ARGSUSED*/
  3493. static int
  3494. has_cancelled(struct device *devices)
  3495. {
  3496. return (cancelled_count == 0 ? 0 : 1);
  3497. #ifdef SLOW_BUT_SURE
  3498. while (device != NULL) {
  3499. if (is_aio_on_list(&device->cancelled)) {
  3500. return (1);
  3501. }
  3502. device = device->next;
  3503. }
  3504. return (0);
  3505. #endif
  3506. }
  3507. struct timeval
  3508. get_timeout(struct device *devices, int report_time)
  3509. {
  3510. struct device *first_to_start;
  3511. struct timeval tv;
  3512. time_t secs_til_xit;
  3513. if (has_cancelled(devices)) {
  3514. tv.tv_sec = tv.tv_usec = 0;
  3515. return (tv);
  3516. }
  3517. secs_til_xit = secs_till_exit();
  3518. if (secs_til_xit < 0) {
  3519. secs_til_xit = report_time;
  3520. }
  3521. if (!all_running()) {
  3522. first_to_start = first_to_restart(devices);
  3523. } else {
  3524. first_to_start = NULL;
  3525. }
  3526. while (my_gettimeofday(&tv, NULL) == -1)
  3527. pperror("gettimeofday");
  3528. if (first_to_start == NULL) {
  3529. tv.tv_sec = MIN(secs_til_xit, report_time);
  3530. tv.tv_usec = 0;
  3531. return (tv);
  3532. }
  3533. tv = timeval_timeval_sub(first_to_start->state_ttl, tv);
  3534. if (tv.tv_sec > report_time || tv.tv_sec > secs_til_xit) {
  3535. tv.tv_sec = MIN(secs_til_xit, report_time);
  3536. tv.tv_usec = 0;
  3537. } else if (tv.tv_sec < 0) {
  3538. tv.tv_sec = tv.tv_usec = 0;
  3539. }
  3540. return (tv);
  3541. }
  3542. void
  3543. init_all_blk_str(struct device *dev, int proc_no)
  3544. {
  3545. int i;
  3546. struct timeval now_tv;
  3547. for (i = 0; dev != NULL; dev = dev->next, i++) {
  3548. if ((i % opts.nprocs) == proc_no) {
  3549. /* init_block_str(dev); */
  3550. while (my_gettimeofday(&now_tv, NULL) == -1)
  3551. pperror("gettimeofday");
  3552. (void) set_dev_state(dev, DEV_NOT_READY, DEV_STOPPED,
  3553. &now_tv);
  3554. }
  3555. }
  3556. }
  3557. /*
  3558. * Start the first cancelled io for each device.
  3559. */
  3560. void
  3561. do_start_cancelled_io(struct device *devices, ullong_t start)
  3562. {
  3563. struct device *devp;
  3564. for (devp = devices; devp != NULL; devp = devp->next) {
  3565. struct aio_str *aiop;
  3566. check_exit_flag();
  3567. aiop = pop_from_aio_list(&devp->cancelled);
  3568. if (aiop != NULL) {
  3569. cancelled_count--;
  3570. aiop->handler(aiop, start);
  3571. }
  3572. }
  3573. if (!has_cancelled(devices)) {
  3574. start_cancelled_io =
  3575. (void (*)(struct device *devices, ullong_t start))nop;
  3576. }
  3577. }
  3578. static void
  3579. report_times(void)
  3580. {
  3581. struct tms tms;
  3582. if (times(&tms) != (clock_t)-1) {
  3583. plog(LOG_NOTICE, "User time %d seconds\n",
  3584. tms.tms_cutime/CLK_TCK);
  3585. plog(LOG_NOTICE, "System time %d seconds\n",
  3586. tms.tms_cstime/CLK_TCK);
  3587. }
  3588. }
  3589. void
  3590. do_aio(struct device *devices, ullong_t start, int report_time)
  3591. {
  3592. struct aio_str *aio_writes;
  3593. int i;
  3594. int rdflag = 0;
  3595. pid_t pid;
  3596. int ndevices = how_many_devices(devices);
  3597. dev_state dev_state;
  3598. if (opts.wthreads + opts.wrthreads == 0) {
  3599. plog(LOG_ERR, "WTHREADS and WRTHREADS can not both be zero\n");
  3600. exit(1);
  3601. }
  3602. aio_writes = my_calloc((opts.wthreads + opts.wrthreads) * ndevices,
  3603. sizeof (struct aio_str));
  3604. if (aio_writes == NULL) {
  3605. pfprintf(stderr, "Can't allocate write structures\n");
  3606. exit(1);
  3607. }
  3608. proc_store = my_calloc(opts.nprocs, sizeof (struct proc_store));
  3609. if (proc_store == NULL) {
  3610. pfprintf(stderr, "Can't allocate process store\n");
  3611. exit(1);
  3612. }
  3613. if (opts.seconds_to_run > 0) {
  3614. secs_till_exit = do_secs_till_exit;
  3615. stoptime = gethrtime() + (opts.seconds_to_run * 1000 * MILLION);
  3616. } else {
  3617. secs_till_exit = inf_secs_till_exit;
  3618. stoptime = -1;
  3619. }
  3620. (void) printf("\tPID = %ld\n", (ulong_t)getpid());
  3621. (void) printf("\t%s\n", gettext(checker_string(daio->what_checker())));
  3622. if (opts_fini() != 0) {
  3623. exit(EXIT_FAILURE);
  3624. }
  3625. save_usage_tracking(usage_tracking_handle, opts.obscure_usage_file);
  3626. send_usage_tracking(usage_tracking_handle);
  3627. close_usage_tracking(usage_tracking_handle);
  3628. init_read_bufs(devices);
  3629. init_all_write_bufs(aio_writes, devices);
  3630. shm_ops->complete(NULL);
  3631. report_uadmin();
  3632. if (opts.debug_no_action) {
  3633. exit(EXIT_SUCCESS);
  3634. }
  3635. if (!is_readonly() && opts.expert_do_path_check &&
  3636. check_for_duplicate_paths(devices) == 0) {
  3637. exit(EXIT_FAILURE);
  3638. }
  3639. (void) sighold(SIGTERM);
  3640. (void) sighold(SIGINT);
  3641. proc_no = 0;
  3642. NOTE(COMPETING_THREADS_NOW)
  3643. for (i = 0; i < opts.nprocs; i++) {
  3644. int forkcount = 0;
  3645. (void) fflush(stdout);
  3646. (void) fflush(stderr);
  3647. do {
  3648. pid = opts.use_fork1 == 0 ? fork() : fork1();
  3649. if (pid == -1) {
  3650. FORK_ERROR(opts.use_fork1 == 0 ?
  3651. "" : "1");
  3652. if (forkcount >= opts.max_fork_failure)
  3653. break;
  3654. forkcount++;
  3655. (void) sleep(opts.fork_failure_wait_time);
  3656. }
  3657. } while (pid == -1);
  3658. if (pid == -1)
  3659. FORK_ERROR(opts.use_fork1 == 0 ? "" : "1");
  3660. else if (pid == 0) {
  3661. proc_no = i;
  3662. break;
  3663. }
  3664. proc_store[i].pid = pid;
  3665. plog(LOG_DEBUG, "fork%s %ld\n", opts.use_fork1 == 0 ? "" : "1",
  3666. (ulong_t)pid);
  3667. }
  3668. if (pid != 0) { /* We are the parent */
  3669. int status;
  3670. (void) sigrelse(SIGINT);
  3671. (void) sigrelse(SIGTERM);
  3672. while ((pid = waitpid((pid_t)-1, &status, 0)) != -1 &&
  3673. errno != ECHILD) {
  3674. if (WIFEXITED(status) || WIFSIGNALED(status)) {
  3675. mourning(pid, status);
  3676. if (WEXITSTATUS(status) != 0 ||
  3677. WIFSIGNALED(status)) {
  3678. exit_status = EXIT_FAILURE;
  3679. }
  3680. }
  3681. }
  3682. report_times();
  3683. exit(exit_status);
  3684. }
  3685. free(proc_store);
  3686. daio->init((opts.wthreads + opts.rthreads + opts.wrthreads) * ndevices);
  3687. (void) sigrelse(SIGINT);
  3688. (void) sigrelse(SIGTERM);
  3689. change_dir();
  3690. /* init_all_blk_str(devices, proc_no); */
  3691. if (usr1_exit)
  3692. exit(0);
  3693. init_all_aio(devices, aio_writes, opts.wthreads + opts.wrthreads);
  3694. assert(devices->block == this_proc());
  3695. new_log_transaction(stderr);
  3696. /* This is the main loop for the diskomizer. */
  3697. for (i = 0; aios_outstanding_or_on_hold(devices) != 0; i++) {
  3698. struct aio_str *aiop;
  3699. struct timeval timeout, now_tv;
  3700. time_t tyme;
  3701. int x;
  3702. check_exit_flag();
  3703. if (aios_outstanding(devices) != 0) {
  3704. timeout = get_timeout(devices, report_time);
  3705. new_log_transaction(stderr);
  3706. aiop = (struct aio_str *)daio->wait(&timeout);
  3707. x = errno;
  3708. check_exit_flag();
  3709. while (my_gettimeofday(&now_tv, NULL) == -1)
  3710. pperror("gettimeofday");
  3711. if ((long)aiop == -1) {
  3712. errno = x;
  3713. if (errno == EINVAL &&
  3714. aios_outstanding_or_on_hold(
  3715. devices) == 0) {
  3716. AIOWAIT_ERROR(timeout);
  3717. exit(1);
  3718. }
  3719. AIOWAIT_ERROR(timeout);
  3720. continue;
  3721. } else if ((long)aiop == 0) {
  3722. /* the aiowait timed out */
  3723. report_all_hangers(devices, report_time);
  3724. restart_stopped_devices(start, devices,
  3725. &now_tv);
  3726. continue;
  3727. }
  3728. } else {
  3729. /*
  3730. * If all the io requests have been stopped then we call
  3731. * wait_to_restart which will return the first io to
  3732. * restart from all the devices and it will sleep until
  3733. * that io is due to be queued. It will return NULL if the
  3734. * next io to start would be started after the process should
  3735. * have exited or if there are no stopped devices or if all
  3736. * the ios have been deferred.
  3737. */
  3738. if ((aiop = wait_to_restart(devices)) != NULL) {
  3739. while (my_gettimeofday(&now_tv, NULL) == -1)
  3740. pperror("gettimeofday");
  3741. dev_state = set_dev_state(aiop->dev,
  3742. DEV_STOPPED, DEV_STARTING, &now_tv);
  3743. assert(dev_state == DEV_STOPPED ||
  3744. dev_state == DEV_STARTING);
  3745. } else if ((aiop = get_deferred_io(devices)) == NULL) {
  3746. exit(exit_status);
  3747. }
  3748. }
  3749. if (aiop == NULL ||
  3750. (opts.nloops != 0 && aiop->dev->countdown == 0) ||
  3751. stoptime_reached()) {
  3752. /* this stops the other processes being killed */
  3753. usr1_exit++;
  3754. if (aiop != NULL) {
  3755. if (aiop->fd != NULL)
  3756. remove_from_aio_list(
  3757. &aiop->fd->all_aios, aiop);
  3758. if (aiop->next != NULL)
  3759. aiop->next->prev = aiop->prev;
  3760. }
  3761. continue;
  3762. }
  3763. /*
  3764. * Controls.
  3765. */
  3766. dev_state = get_dev_state(aiop->dev, &now_tv);
  3767. if (dev_state == DEV_RUNNING || dev_state == DEV_STARTING) {
  3768. tyme = aiop->handler(aiop, start);
  3769. } else {
  3770. if (aiop->fd != NULL) {
  3771. remove_from_aio_list(&aiop->fd->all_aios,
  3772. aiop);
  3773. }
  3774. add_to_aio_list(&aiop->dev->stopped_ios, aiop);
  3775. if (aios_queued_to_fd(aiop->dev) == 0) {
  3776. (void) set_dev_state(aiop->dev,
  3777. DEV_STOPPING, DEV_STOPPED, &now_tv);
  3778. }
  3779. }
  3780. start_cancelled_io(devices, start);
  3781. start_deferred(devices, start);
  3782. report_hangers(aiop->dev, tyme, report_time);
  3783. restart_stopped_devices(start, devices, &now_tv);
  3784. if (aiop->off >= aiop->dev->read_start_block &&
  3785. aiop->handler != handle_read &&
  3786. i > (2*(opts.rthreads + opts.wthreads))) {
  3787. if (opts.rthreads > aiop->dev->running_rthreads) {
  3788. if (aiop->dev->running_rthreads == 0 &&
  3789. rdflag == 0) {
  3790. rdflag = 1;
  3791. time_now_log(LOG_NOTICE,
  3792. "Starting first %s reader %d",
  3793. random_str, i);
  3794. }
  3795. if (init_read(aiop, start))
  3796. aiop->dev->running_rthreads++;
  3797. } else if (rdflag == 1) {
  3798. time_now_log(LOG_NOTICE,
  3799. "All %sreaders started %d",
  3800. random_str, i);
  3801. rdflag = 2;
  3802. }
  3803. }
  3804. }
  3805. exit(exit_status);
  3806. }
  3807. /*
  3808. * select_error_func:
  3809. * search the handlers array for an entry whose name matches the name
  3810. * passed in. If the name passed in is NULL then default to using the
  3811. * first handler in the list.
  3812. */
  3813. static int
  3814. select_error_func(const char *name,
  3815. struct error_handlers *handlers,
  3816. int nhandlers,
  3817. on_error_t *oef,
  3818. int rw)
  3819. {
  3820. int i;
  3821. struct error_handlers *h;
  3822. if (name == NULL) {
  3823. h = &handlers[0];
  3824. } else for (i = 0; i < nhandlers; i++) {
  3825. h = &handlers[i];
  3826. if (((h->rw & rw) != 0) && strcasecmp(h->name, name) == 0) {
  3827. break;
  3828. } else {
  3829. h = NULL;
  3830. }
  3831. }
  3832. if (h != NULL) {
  3833. *oef = h->func;
  3834. if (h->setup() != 1) {
  3835. fprintf(stderr, "Unable to init %s\n", h->name);
  3836. }
  3837. return (h->breaker == 0 ? 0 : 1);
  3838. }
  3839. return (-1);
  3840. }
  3841. on_error_t *
  3842. setup_onerror(char *prog, const char *str, int rw)
  3843. {
  3844. char *tmp;
  3845. char *opaque;
  3846. char *toogo;
  3847. char i;
  3848. on_error_t *oef = NULL;
  3849. if ((toogo = strdup(str)) == NULL) {
  3850. (void) fprintf(stderr, "strdup(%s) failed: %s\n",
  3851. str, strerror(errno));
  3852. return (NULL);
  3853. }
  3854. for (i = 0, tmp = toogo; ; i++) {
  3855. on_error_t *noef;
  3856. int n;
  3857. if ((tmp = strtok_r(tmp, ",", &opaque)) == NULL) {
  3858. break;
  3859. }
  3860. noef = realloc(oef, (i+2) * sizeof (on_error_t));
  3861. if (noef == NULL) {
  3862. free(toogo);
  3863. free(oef);
  3864. return (NULL);
  3865. }
  3866. oef = noef;
  3867. oef[i+1] = NULL;
  3868. if ((n = select_error_func(tmp, on_error_table,
  3869. ARRAY_LEN(on_error_table), &oef[i], rw)) != 0) {
  3870. if (n == -1) {
  3871. (void) fprintf(stderr,
  3872. "bad on error option %s in %s\n",
  3873. NIL(tmp), str);
  3874. }
  3875. break;
  3876. }
  3877. tmp = NULL;
  3878. }
  3879. free(toogo);
  3880. if (oef == NULL) {
  3881. if ((oef = malloc(sizeof (on_error_t))) != NULL) {
  3882. oef[0] = NULL;
  3883. }
  3884. }
  3885. return (oef);
  3886. }
  3887. int
  3888. how_many_devices(struct device *devices)
  3889. {
  3890. int i = 0;
  3891. while (devices != NULL) {
  3892. i++;
  3893. devices = devices->next;
  3894. }
  3895. return (i);
  3896. }
  3897. ullong_t
  3898. set_file_size(const char *dir)
  3899. {
  3900. struct statvfs buf;
  3901. if (opts.expert_amount_to_leave_unused && opts.number_of_files &&
  3902. statvfs(dir, &buf) != -1) {
  3903. ullong_t count;
  3904. count = (buf.f_bavail * buf.f_frsize) -
  3905. opts.expert_amount_to_leave_unused;
  3906. count = (count/opts.number_of_files);
  3907. return (count);
  3908. }
  3909. return (opts.file_size);
  3910. }
  3911. int
  3912. set_number_of_files(const char *dir)
  3913. {
  3914. struct statvfs buf;
  3915. if (opts.expert_amount_to_leave_unused && opts.file_size &&
  3916. statvfs(dir, &buf) != -1) {
  3917. ullong_t count;
  3918. longlong_t n;
  3919. int i;
  3920. count = (ullong_t)buf.f_bavail * (ullong_t)buf.f_frsize;
  3921. count -= opts.expert_amount_to_leave_unused;
  3922. n = count / opts.file_size;
  3923. i = (int)(MIN(n, INT_MAX));
  3924. if (opts.number_of_files)
  3925. return (MIN(i, opts.number_of_files));
  3926. else
  3927. return (i);
  3928. }
  3929. return (opts.number_of_files);
  3930. }
  3931. /*
  3932. * read the path as if it is a symbolic link and process that.
  3933. */
  3934. static char *
  3935. do_link(char *path)
  3936. {
  3937. char buf[PATH_MAX+1];
  3938. char *res;
  3939. int x;
  3940. if ((x = readlink(path, &buf[0], sizeof (buf))) > 0) {
  3941. buf[x] = NULL;
  3942. res = full_path(path, &buf[0]);
  3943. } else {
  3944. res = my_strdup(path);
  3945. }
  3946. if (res == NULL) {
  3947. exit(EXIT_FAILURE);
  3948. }
  3949. return (res);
  3950. }
  3951. static struct fds *
  3952. open_path_count(struct device *devp, char *name, ullong_t size)
  3953. {
  3954. struct fds *fd;
  3955. int i = 0;
  3956. do {
  3957. if ((fd = open_path(devp, name, size)) != NULL)
  3958. break;
  3959. } while (i++ < opts.open_retries);
  3960. return (fd);
  3961. }
  3962. struct device *
  3963. open_path_group(struct paths *paths, int paths_to_use, int error_paths)
  3964. {
  3965. int count;
  3966. struct other_paths *opath;
  3967. struct device *devp;
  3968. struct fds *fd;
  3969. int total_paths = paths_to_use + error_paths;
  3970. if ((devp = (struct device *)my_calloc(1,
  3971. sizeof (struct device))) == NULL) {
  3972. return (NULL);
  3973. }
  3974. for (count = 0, opath = paths->op;
  3975. count < total_paths && opath != NULL; /* */) {
  3976. if ((fd = open_path_count(devp,
  3977. opath->path, 0)) != NULL) {
  3978. fd->error_path =
  3979. count >= paths_to_use ? 1 : 0;
  3980. fd->path_id = count++;
  3981. }
  3982. opath = opath->next;
  3983. }
  3984. return (devp);
  3985. }
  3986. /*
  3987. * Open_devices
  3988. *
  3989. * ARGUMENT: char *name
  3990. * A space seperated list of devices. Devices may be grouped by
  3991. * putting curly brackets around them to sepficy multiple paths to
  3992. * the same device.
  3993. */
  3994. struct device *
  3995. open_devices(char *name)
  3996. {
  3997. struct device *devp;
  3998. struct device *newone;
  3999. int brace_count = 0;
  4000. int error_paths = opts.error_paths;
  4001. int paths_to_use = opts.paths_to_use;
  4002. struct paths *path_group = NULL;
  4003. struct other_paths *op;
  4004. char *tmp;
  4005. char *toogo;
  4006. char *opaque;
  4007. if ((toogo = strdup(name)) == NULL) {
  4008. (void) fprintf(stderr, "strdup(%s) failed: %s\n", name,
  4009. strerror(errno));
  4010. exit(1);
  4011. }
  4012. tmp = toogo;
  4013. devp = NULL;
  4014. while ((tmp = strtok_r(tmp, "\t ", &opaque)) != NULL) {
  4015. struct stat64 sbuf;
  4016. if (usr1_exit)
  4017. exit(0);
  4018. if (*tmp == OPEN_BRACE) {
  4019. if (brace_count++ == 0) {
  4020. error_paths = paths_to_use = 0;
  4021. }
  4022. if (path_group == NULL) {
  4023. path_group = my_calloc(
  4024. sizeof (struct paths), 1);
  4025. if (path_group == NULL) {
  4026. exit(EXIT_FAILURE);
  4027. }
  4028. }
  4029. } else if (*tmp == CLOSE_BRACE) {
  4030. if (--brace_count == 0) {
  4031. if (path_group->logicalpath == NULL) {
  4032. plog(LOG_WARNING, gettext(
  4033. "Empty path device list "
  4034. "found"));
  4035. free(path_group);
  4036. path_group = NULL;
  4037. continue;
  4038. }
  4039. newone = open_device(NULL,
  4040. path_group, opts.file_size,
  4041. paths_to_use, error_paths);
  4042. if (newone != NULL) {
  4043. newone->next = devp;
  4044. devp = newone;
  4045. }
  4046. free_paths(path_group);
  4047. path_group = NULL;
  4048. error_paths = opts.error_paths;
  4049. paths_to_use = opts.paths_to_use;
  4050. }
  4051. if (brace_count < 0)
  4052. plog(LOG_WARNING,
  4053. "Unbalanced braces in device list\n");
  4054. } else if (*tmp == '-') {
  4055. /* PATH options */
  4056. plog(LOG_WARNING, "Path options are not currently "
  4057. "supported: \"%s\" ignored\n", tmp);
  4058. } else if (path_group != NULL) {
  4059. if (path_group->op == NULL) {
  4060. op = my_calloc(
  4061. sizeof (struct other_paths), 1);
  4062. if (op == NULL) {
  4063. exit(EXIT_FAILURE);
  4064. }
  4065. path_group->op = op;
  4066. path_group->logicalpath = my_strdup(tmp);
  4067. } else {
  4068. /* lint does not like empty loops */
  4069. for (op = path_group->op; op->next != NULL; ) {
  4070. op = op->next;
  4071. }
  4072. op->next = my_calloc(
  4073. sizeof (struct other_paths), 1);
  4074. if (op->next == NULL) {
  4075. exit(EXIT_FAILURE);
  4076. }
  4077. op = op->next;
  4078. }
  4079. if (brace_count > 1) {
  4080. if (path_stop_check == do_path_stop_check) {
  4081. error_paths++;
  4082. }
  4083. } else {
  4084. paths_to_use++;
  4085. }
  4086. op->path = do_link(tmp);
  4087. } else if ((opts.number_of_files ||
  4088. opts.expert_amount_to_leave_unused) &&
  4089. daio->stat(tmp, &sbuf) != -1 && S_ISDIR(sbuf.st_mode)) {
  4090. int len = strlen(tmp) +
  4091. strlen(opts.obscure_data_file_basename) + 16;
  4092. int i;
  4093. char *x;
  4094. int nf = set_number_of_files(tmp);
  4095. ullong_t size = set_file_size(tmp);
  4096. pprintf("%s %d files of %lld bytes\n", tmp, nf,
  4097. (ullong_t)size);
  4098. for (i = 0; i < nf; i++) {
  4099. /*
  4100. * If the open succeds then we just have to
  4101. * "leak" this memory here as it is in use
  4102. * in the device structures.
  4103. */
  4104. if ((x = malloc(len)) == NULL) {
  4105. MALLOC_ERROR(len);
  4106. exit(1);
  4107. }
  4108. (void) sprintf(x, "%s/%s%d", tmp,
  4109. opts.obscure_data_file_basename, i);
  4110. newone = open_device(x, NULL, size,
  4111. paths_to_use, error_paths);
  4112. if (newone != NULL) {
  4113. newone->next = devp;
  4114. devp = newone;
  4115. } else {
  4116. free(x);
  4117. }
  4118. }
  4119. } else {
  4120. newone = open_device(tmp, NULL, opts.file_size,
  4121. paths_to_use, error_paths);
  4122. if (newone != NULL) {
  4123. newone->next = devp;
  4124. devp = newone;
  4125. }
  4126. }
  4127. tmp = NULL;
  4128. }
  4129. if (brace_count != 0) {
  4130. plog(LOG_WARNING, "Unbalanced braces in device list\n");
  4131. }
  4132. /* don't free toogo as it is being used in the devices structures. */
  4133. /* free(toogo); */
  4134. if (usr1_exit) {
  4135. exit(0);
  4136. }
  4137. if (devp != NULL) {
  4138. init_device_control(devp);
  4139. }
  4140. return (devp);
  4141. }
  4142. void
  4143. print_dev(struct device *dev)
  4144. {
  4145. struct fds *fds;
  4146. static const char device_str[] = "device";
  4147. (void) printf("Logical Device: %s\n", dev->logicalname);
  4148. USAGE_TRACKING_OPEN_KEY(device_str, NULL, dev->logicalname);
  4149. fds = dev->fdhead;
  4150. (void) printf("Physical device%s:\n", fds->next == fds ? "" : "s");
  4151. USAGE_TRACKING_OPEN_KEY("paths", NULL, NULL);
  4152. for (;;) {
  4153. USAGE_TRACKING_STORE_KEY_VALUE("longname", fds->longname);
  4154. USAGE_TRACKING_STORE_KEY_VALUE("created", TRUE_OR_FALSE(
  4155. fds->created));
  4156. (void) printf("\t%s%s%s%s%s%s\n", fds->longname,
  4157. fds->error_path || fds->created ? " (" : "",
  4158. fds->error_path ? "error path" : "",
  4159. fds->error_path && fds->created ? ", " : "",
  4160. fds->created ? "created" : "",
  4161. fds->error_path || fds->created ? ")" : "");
  4162. if (fds->longname != fds->shortname) {
  4163. (void) printf("\t\t(%s)\n", fds->shortname);
  4164. }
  4165. if (fds->next != dev->fdhead) {
  4166. fds = fds->next;
  4167. } else {
  4168. break;
  4169. }
  4170. }
  4171. USAGE_TRACKING_CLOSE_KEY();
  4172. print_number_of_bytes(dev->device_block_size,
  4173. "Device block size", "Device block size");
  4174. print_number_of_bytes(dev->length, "length", "length");
  4175. print_number(LEN_BYTES2BLOCKS(dev), "block", "blocks");
  4176. (void) fflush(stdout);
  4177. if (write_loops) {
  4178. print_number(dev->countdown, "write", "writes");
  4179. } else if (opts.nloops) {
  4180. print_number(dev->countdown, "read", "reads");
  4181. }
  4182. USAGE_TRACKING_STORE_KEY_VALUE_INT("length", dev->length);
  4183. USAGE_TRACKING_STORE_KEY_VALUE_INT("blocks", LEN_BYTES2BLOCKS(dev));
  4184. USAGE_TRACKING_CLOSE_KEY();
  4185. }
  4186. /*
  4187. * close all the fds and free the data associated with all the paths
  4188. * for a device.
  4189. */
  4190. void
  4191. close_and_free_paths(struct device *dev)
  4192. {
  4193. struct fds *fd, *next;
  4194. for (fd = dev->fdhead, next = fd->next; ; fd = next, next = fd->next) {
  4195. (void) daio->close(fd->fd);
  4196. if (opts.expert_cleanup_created_files && fd->created &&
  4197. is_master() && exit_status == EXIT_SUCCESS &&
  4198. get_shared_device_error(dev->shared_data_handle) == 0) {
  4199. pprintf(gettext("Removing %s\n"), fd->longname);
  4200. if (daio->unlink(fd->longname) == -1) {
  4201. pperror(gettext("unlink(%s)"), fd->longname);
  4202. }
  4203. }
  4204. if (fd->shortname != fd->longname) {
  4205. free(fd->longname);
  4206. }
  4207. free(fd->shortname);
  4208. free(fd);
  4209. if (dev->fdhead == next) {
  4210. break;
  4211. }
  4212. }
  4213. dev->fdhead = NULL;
  4214. }
  4215. struct fds *
  4216. open_path(struct device *devp, char *name, ullong_t size)
  4217. {
  4218. struct fds *fd;
  4219. struct dk_cinfo dk_cinfo;
  4220. struct stat64 sbuf;
  4221. char create;
  4222. check_exit_flag();
  4223. if (daio->stat(name, &sbuf) == -1) {
  4224. if (size == 0) {
  4225. pfprintf(stderr, "stat(%s) == -1 errno = %d (%s)\n",
  4226. name, errno, strerror(errno));
  4227. return (NULL);
  4228. } else {
  4229. create = 1;
  4230. }
  4231. } else {
  4232. create = 0;
  4233. }
  4234. if ((fd = (struct fds *)calloc(1, sizeof (struct fds))) == NULL) {
  4235. return (NULL);
  4236. }
  4237. if (opts.debug_no_action == 0) {
  4238. if ((fd->fd = daio->open(name,
  4239. (is_readonly() ? O_RDONLY : O_RDWR)|
  4240. (opts.o_sync ? O_SYNC : 0)|
  4241. (opts.o_excl ? O_EXCL : 0) |
  4242. (opts.o_ndelay ? O_NDELAY : 0) |
  4243. (create ? O_CREAT : 0) |
  4244. (opts.o_trunc ? O_TRUNC : 0), 0600)) == -1) {
  4245. pperror("open(%s, %s%s%s%s%s%s)",
  4246. name, (is_readonly() ? "O_RDONLY" : "O_RDWR"),
  4247. (opts.o_excl ? "|O_EXCL": ""),
  4248. (opts.o_sync ? "|O_SYNC": ""),
  4249. (opts.o_ndelay ? "|O_NDELAY": ""),
  4250. (opts.o_trunc ? "|O_TRUNC": ""),
  4251. (create ? "|O_CREAT, 0600" : ""));
  4252. free(fd);
  4253. return (NULL);
  4254. }
  4255. if (daio->directio(fd->fd, opts.directio == 1 ?
  4256. DIRECTIO_ON : DIRECTIO_OFF) == -1) {
  4257. if (errno != ENOTTY || opts.directio == 1) {
  4258. pperror("directio(\"%s\") failed", name);
  4259. }
  4260. }
  4261. fd->created = create;
  4262. if (create) {
  4263. if (daio->fstat(fd->fd, &sbuf) == -1) {
  4264. FSTAT_ERROR(fd->fd, name);
  4265. (void) daio->close(fd->fd);
  4266. free(fd);
  4267. return (NULL);
  4268. }
  4269. if (S_ISREG(sbuf.st_mode) &&
  4270. daio->ftruncate(fd->fd, size) == -1) {
  4271. (void) daio->close(fd->fd);
  4272. free(fd);
  4273. return (NULL);
  4274. }
  4275. }
  4276. }
  4277. if (sbuf.st_mode & (S_IFCHR|S_IFBLK)) {
  4278. fd->devid.dev = sbuf.st_rdev;
  4279. } else {
  4280. fd->devid.dev = sbuf.st_dev;
  4281. }
  4282. fd->devid.ino = sbuf.st_ino;
  4283. fd->read_times.str = read_str;
  4284. fd->read_times.best = 0xffffffff;
  4285. fd->write_times.str = write_str;
  4286. fd->write_times.best = 0xffffffff;
  4287. fd->last_read_time = fd->last_write_time = ~0;
  4288. if ((fd->longname = strdup(name)) == NULL) {
  4289. (void) daio->close(fd->fd);
  4290. free(fd);
  4291. return (NULL);
  4292. }
  4293. fd->stop_flag = 0;
  4294. fd->shared_data_handle = init_shared_device_info(opts.nprocs);
  4295. if (fd->shared_data_handle == NULL) {
  4296. plog(LOG_ERR, gettext("Unable to allocate shared data "
  4297. "handle for %s\n"), name);
  4298. }
  4299. if (daio->ioctl(fd->fd, DKIOCINFO, &dk_cinfo) == -1) {
  4300. fd->shortname = fd->longname;
  4301. } else {
  4302. fd->shortname = calloc(1,
  4303. strlen(dk_cinfo.dki_dname) + (3 * 10));
  4304. if (fd->shortname == NULL) {
  4305. fd->shortname = fd->longname;
  4306. } else {
  4307. (void) sprintf(fd->shortname, "%s%d:%c",
  4308. dk_cinfo.dki_dname, dk_cinfo.dki_unit,
  4309. dk_cinfo.dki_partition + 'a');
  4310. }
  4311. }
  4312. if (opts.use_long_names) {
  4313. fd->name = fd->longname;
  4314. } else {
  4315. fd->name = fd->shortname;
  4316. }
  4317. Longest_device_name = MAX(Longest_device_name, strlen(fd->name));
  4318. if (devp->fdhead == NULL) {
  4319. devp->fdhead = fd;
  4320. fd->next = fd;
  4321. } else {
  4322. fd->next = devp->fdhead->next;
  4323. devp->fdhead->next = fd;
  4324. }
  4325. return (fd);
  4326. }
  4327. void *
  4328. read_vtoc_all_paths(struct fds *fdhead)
  4329. {
  4330. struct fds *fd;
  4331. void *handle = NULL;
  4332. fd = fdhead;
  4333. do {
  4334. if ((handle = daio->read_vtoc(fd->fd)) != NULL) {
  4335. break;
  4336. }
  4337. fd = fd->next;
  4338. } while (fd->next != fdhead); /* do loop! */
  4339. return (handle);
  4340. }
  4341. struct paths *
  4342. do_ap(const char *inpath)
  4343. {
  4344. return (daio->findap(inpath, opts.dev_tree));
  4345. }
  4346. /*
  4347. * Set the minimum possible block size that can be used for all the devices
  4348. * in this test set. Typically the block sizes seen are 512 bytes, 2048 bytes
  4349. * or 4096 bytes. It will choose the smallest common multiple of the block
  4350. * sizes available. Typically this will just be the largest block size of
  4351. * all the devices but if you had a 3K and 4K block sized device this will
  4352. * return the smallest block size possible is 12k.
  4353. */
  4354. static void
  4355. set_minimum_block_size(int block_size)
  4356. {
  4357. if (min_block_size == 0) {
  4358. min_block_size = block_size;
  4359. } else {
  4360. min_block_size = min_block_size * block_size /
  4361. gcd(min_block_size, block_size);
  4362. }
  4363. }
  4364. struct device *
  4365. open_device(char *name, struct paths *paths, ullong_t size,
  4366. int paths_to_use, int error_paths)
  4367. {
  4368. struct stat64 sbuf;
  4369. ullong_t nsize;
  4370. void *vtoc_handle;
  4371. struct device *devp;
  4372. struct fds *fd;
  4373. int total_paths = paths_to_use + error_paths;
  4374. if (paths != NULL) {
  4375. name = my_strdup(paths->logicalpath);
  4376. if (name == NULL) {
  4377. return (NULL);
  4378. }
  4379. devp = open_path_group(paths, paths_to_use, error_paths);
  4380. } else if (total_paths > 1 &&
  4381. (paths = do_ap(name)) != NULL) {
  4382. devp = open_path_group(paths, paths_to_use, error_paths);
  4383. free_paths(paths);
  4384. } else {
  4385. if ((devp = (struct device *)calloc(1,
  4386. sizeof (struct device))) == NULL) {
  4387. return (NULL);
  4388. }
  4389. if ((fd = open_path_count(devp, name, size)) != NULL) {
  4390. fd->path_id = 0;
  4391. fd->error_path = 0;
  4392. }
  4393. }
  4394. Longest_logical_name = MAX(Longest_logical_name, strlen(name));
  4395. if (devp->fdhead == NULL) {
  4396. free(devp);
  4397. return (NULL);
  4398. } else {
  4399. devp->logicalname = name;
  4400. }
  4401. if (opts.debug_no_action) {
  4402. return (devp);
  4403. }
  4404. /*
  4405. * this is a mess.
  4406. */
  4407. if (daio->fstat(devp->fdhead->fd, &sbuf) == -1) {
  4408. FSTAT_ERROR(devp->fdhead->fd, devp->fdhead->name);
  4409. close_and_free_paths(devp);
  4410. free(devp);
  4411. return (NULL);
  4412. }
  4413. devp->next = NULL;
  4414. devp->choose_block = seq_block;
  4415. if (!(sbuf.st_mode & S_IFCHR)) {
  4416. plog(LOG_DEBUG, "Not a character device\n");
  4417. nsize = (ullong_t)sbuf.st_size;
  4418. devp->device_block_size = SIZEOF_BUF;
  4419. } else if ((vtoc_handle = read_vtoc_all_paths(devp->fdhead)) == NULL) {
  4420. nsize = (ullong_t)SIZEOF_BUF*(ullong_t)sbuf.st_blocks;
  4421. devp->device_block_size = SIZEOF_BUF;
  4422. } else {
  4423. const struct disko_partition *part;
  4424. devp->device_block_size = disko_vtoc_sectorsz(vtoc_handle);
  4425. if (devp->device_block_size == 0) {
  4426. devp->device_block_size = DEFAULT_BLOCK_SIZE;
  4427. }
  4428. part = disko_vtoc_this_partition(vtoc_handle);
  4429. devp->v_part = malloc(sizeof (struct disko_partition));
  4430. if (devp->v_part != NULL) {
  4431. *devp->v_part = *part;
  4432. }
  4433. nsize = (ullong_t)devp->device_block_size * part->p_size;
  4434. disko_vtoc_free(vtoc_handle);
  4435. }
  4436. set_minimum_block_size(devp->device_block_size);
  4437. if (size == 0 || (nsize > 0 && nsize < size)) {
  4438. size = nsize;
  4439. }
  4440. if (size == 0) {
  4441. (void) fprintf(stderr, gettext("File size is zero on %s\n"),
  4442. name);
  4443. (void) fflush(stderr);
  4444. close_and_free_paths(devp);
  4445. free(devp);
  4446. return (NULL);
  4447. }
  4448. devp->length = size-(opts.start_offset *
  4449. INDEX_TO_DIOLEN(max_disk_io_len));
  4450. devp->read_start_block = opts.expert_recent_log_size +
  4451. ((devp->length/INDEX_TO_DIOLEN(max_disk_io_len)) *
  4452. opts.start_reads_percentage)/100;
  4453. if (devp->read_start_block < (opts.nprocs * (opts.rthreads +
  4454. opts.wthreads))) {
  4455. devp->read_start_block = (opts.nprocs * (opts.rthreads +
  4456. opts.wthreads));
  4457. }
  4458. if (devp->read_start_block > LEN_BYTES2BLOCKS(devp)) {
  4459. devp->read_start_block = LEN_BYTES2BLOCKS(devp);
  4460. }
  4461. if (opts.nloops) {
  4462. devp->countdown = (opts.nloops * devp->length) /
  4463. (opts.nprocs * INDEX_TO_DIOLEN(max_disk_io_len));
  4464. } else {
  4465. devp->countdown = ~(uint64_t)0;
  4466. }
  4467. print_dev(devp);
  4468. print_number_of_bytes(size, "size", "size");
  4469. if (opts.start_offset * INDEX_TO_DIOLEN(max_disk_io_len) > size) {
  4470. (void) printf("starting offset is greater than disk size! ");
  4471. (void) printf("%llx > %llx\n", (ullong_t)(opts.start_offset *
  4472. INDEX_TO_DIOLEN(max_disk_io_len)), (ullong_t)size);
  4473. close_and_free_paths(devp);
  4474. free(devp);
  4475. return (NULL);
  4476. }
  4477. if (LEN_BYTES2BLOCKS(devp) < (opts.nprocs *
  4478. (opts.wthreads + opts.rthreads + opts.wrthreads))) {
  4479. (void) printf("There are not enough blocks (%#llx) to support "
  4480. "this many I/O's (%#lx) on device %s, device closed\n",
  4481. (ullong_t)LEN_BYTES2BLOCKS(devp),
  4482. (ulong_t)(opts.nprocs * (opts.wthreads +
  4483. opts.rthreads + opts.wrthreads)), name);
  4484. close_and_free_paths(devp);
  4485. free(devp);
  4486. return (NULL);
  4487. }
  4488. if (opts.expert_max_active_time == 0) {
  4489. devp->state_ttl.tv_sec = 0;
  4490. devp->state_ttl.tv_usec = 0;
  4491. } else {
  4492. while (my_gettimeofday(&devp->state_ttl, NULL) == -1)
  4493. pperror("gettimeofday");
  4494. devp->state_ttl = set_ttl(devp->state_ttl,
  4495. opts.expert_max_active_time,
  4496. opts.expert_min_active_time);
  4497. }
  4498. devp->shared_data_handle = init_shared_device_info(opts.nprocs);
  4499. if (devp->shared_data_handle == NULL) {
  4500. plog(LOG_ERR, gettext("Unable to allocate shared data "
  4501. "handle for %s\n"), name);
  4502. }
  4503. devp->seq_passes = opts.sequential_passes;
  4504. devp->recent = init_recent(opts.expert_recent_log_size);
  4505. return (devp);
  4506. }
  4507. static int
  4508. check_for_duplicate_paths(struct device *devp)
  4509. {
  4510. uchar_t *buf;
  4511. int buflen = min_block_size;
  4512. struct device *d;
  4513. struct fds *fd;
  4514. int status = 1;
  4515. if ((buf = malloc(buflen)) == NULL) {
  4516. return (0);
  4517. }
  4518. memset(buf, NULL, buflen);
  4519. /*
  4520. * first zero all the target blocks
  4521. */
  4522. for (d = devp; d != NULL; d = d->next) {
  4523. fd = d->fdhead;
  4524. do {
  4525. check_exit_flag();
  4526. if (daio->pwrite(fd->fd, buf, buflen,
  4527. INDEX_TO_DIOLEN(max_disk_io_len) *
  4528. OPTION(start_offset), NULL) != buflen) {
  4529. PWRITE_ERROR(fd->fd, fd->name,
  4530. (ulong_t)buf,
  4531. buflen,
  4532. INDEX_TO_DIOLEN(
  4533. max_disk_io_len) *
  4534. OPTION(start_offset));
  4535. status = 0;
  4536. }
  4537. fd = fd->next;
  4538. } while (fd != d->fdhead);
  4539. }
  4540. /* Now write the dev structure to the first path only */
  4541. for (d = devp; d != NULL; d = d->next) {
  4542. check_exit_flag();
  4543. fd = d->fdhead;
  4544. (void) memcpy(&buf[0], d, sizeof (struct device));
  4545. if (daio->pwrite(fd->fd, buf, buflen,
  4546. INDEX_TO_DIOLEN(max_disk_io_len) *
  4547. OPTION(start_offset), NULL) != buflen) {
  4548. PWRITE_ERROR(fd->fd, fd->name, (ulong_t)buf,
  4549. buflen, INDEX_TO_DIOLEN(max_disk_io_len) *
  4550. OPTION(start_offset));
  4551. status = 0;
  4552. }
  4553. }
  4554. /*
  4555. * Now read all the blocks via each path and verify that they
  4556. * are ok.
  4557. */
  4558. for (d = devp; d != NULL; d = d->next) {
  4559. fd = d->fdhead;
  4560. do {
  4561. check_exit_flag();
  4562. memset(buf, NULL, buflen);
  4563. if (daio->pread(fd->fd, buf, buflen,
  4564. INDEX_TO_DIOLEN(max_disk_io_len) *
  4565. OPTION(start_offset), NULL) != buflen) {
  4566. PREAD_ERROR(fd->fd, fd->name,
  4567. (ulong_t)buf, buflen,
  4568. INDEX_TO_DIOLEN(max_disk_io_len) *
  4569. OPTION(start_offset));
  4570. status = 0;
  4571. } else if (memcmp(buf, d, sizeof (struct device)) !=
  4572. 0) {
  4573. status = 0;
  4574. pfprintf(stderr,
  4575. "dev %s path %s failed path check\n",
  4576. d->logicalname, fd->name);
  4577. }
  4578. fd = fd->next;
  4579. } while (fd != d->fdhead);
  4580. }
  4581. free(buf);
  4582. return (status);
  4583. }
  4584. void
  4585. print_uname(FILE *out)
  4586. {
  4587. static char uname_str[] = "uname";
  4588. struct utsname name;
  4589. char platform[255], hw_prov[255], domain[255];
  4590. (void) sysinfo(SI_PLATFORM, &platform[0], sizeof (platform));
  4591. (void) sysinfo(SI_HW_PROVIDER, &hw_prov[0], sizeof (hw_prov));
  4592. (void) sysinfo(SI_SRPC_DOMAIN, &domain[0], sizeof (domain));
  4593. if (uname(&name) == -1) {
  4594. pperror(uname_str);
  4595. }
  4596. (void) fprintf(out, "System info:\n\t%s %s %s %s %s %s %s\n",
  4597. name.sysname, name.nodename,
  4598. name.release, name.version, name.machine, platform,
  4599. hw_prov);
  4600. }
  4601. void
  4602. set_max_blocks(void)
  4603. {
  4604. int i;
  4605. for (i = 0; i < opts.disk_io_sizes.wlen; i++) {
  4606. if (opts.disk_io_sizes.vals[max_disk_io_len] <
  4607. opts.disk_io_sizes.vals[opts.disk_io_sizes.weightings[i]]) {
  4608. max_disk_io_len = opts.disk_io_sizes.weightings[i];
  4609. }
  4610. }
  4611. }
  4612. int
  4613. check_block_sizes(void)
  4614. {
  4615. int i;
  4616. int bs;
  4617. int ret = 0;
  4618. for (i = 0; i < opts.disk_io_sizes.wlen; i++) {
  4619. bs = opts.disk_io_sizes.vals[opts.disk_io_sizes.weightings[i]];
  4620. if ((bs % min_block_size) != 0) {
  4621. plog(LOG_ERR, "Disk IO size 0x%x (%d) is not a "
  4622. "multiple of the minimum block size, 0x%x (%d)\n",
  4623. bs, bs, min_block_size, min_block_size);
  4624. ret = 1;
  4625. }
  4626. }
  4627. return (ret);
  4628. }
  4629. static void
  4630. usr1(int sig, siginfo_t *info, void *v)
  4631. {
  4632. plog(LOG_DEBUG, "USR1 caught\n");
  4633. usr1_exit++;
  4634. }
  4635. static int exit_flag;
  4636. /*ARGSUSED*/
  4637. static void
  4638. set_exit_flag(int sig, siginfo_t *info, void *v)
  4639. {
  4640. plog(LOG_DEBUG, "Sig %d\n", sig);
  4641. if (info == NULL) {
  4642. /*
  4643. * Keyboard generated SIGINT has no info pointer.
  4644. */
  4645. if (sig == SIGINT)
  4646. killer_pid = master_pid();
  4647. } else if (killer_pid == 0)
  4648. killer_pid = info->si_pid;
  4649. exit_flag++;
  4650. }
  4651. void
  4652. check_exit_flag()
  4653. {
  4654. if (exit_flag) {
  4655. (void) sighold(SIGTERM);
  4656. exit(killer_pid == master_pid() ? exit_status : EXIT_FAILURE);
  4657. }
  4658. }
  4659. static void
  4660. print_startup_info(void)
  4661. {
  4662. (void) printf("Setting up to do:\n");
  4663. print_number(LONG_BIT, "Bit mode", "Bit mode");
  4664. print_number_of_bytes(min_block_size,
  4665. "Common block size", "Common block size");
  4666. (void) printf("\tRead %s mode\n", is_readonly() ? "only" : write_str);
  4667. random_str = is_readonly() ? "random " : "";
  4668. print_number(opts.wthreads, "write", "writes");
  4669. print_number(opts.wrthreads, "Write - read", "Write - reads");
  4670. print_number(opts.rthreads, "read", "reads");
  4671. print_number_of_bytes(INDEX_TO_DIOLEN(max_disk_io_len),
  4672. "Max block size", "Max block size");
  4673. print_number(opts.nprocs, "proc", "procs");
  4674. print_number(opts.nlocks, "lock", "locks");
  4675. (void) printf("\t%d%% of disk written before reads start\n",
  4676. opts.start_reads_percentage);
  4677. if (!is_readonly() && opts.obscure_execute &&
  4678. does_check(daio->what_checker())) {
  4679. (void) printf("\tWill execute code read into buffer\n");
  4680. }
  4681. (void) printf("\tUsing %s as buffer allocator\n",
  4682. shm_ops->longname(NULL));
  4683. USAGE_TRACKING_STORE_KEY_VALUE("allocator", shm_ops->longname(NULL));
  4684. #define UT_KVS(A) USAGE_TRACKING_STORE_KEY_VALUE_INT(#A, opts.A);
  4685. UT_KVS(nprocs);
  4686. UT_KVS(wthreads);
  4687. UT_KVS(wrthreads);
  4688. UT_KVS(rthreads);
  4689. #undef UT_KVS
  4690. }
  4691. static void
  4692. setup_signals(void)
  4693. {
  4694. setup_signal_catcher(SIGTERM, set_exit_flag, SA_SIGINFO);
  4695. setup_signal_catcher(SIGHUP, set_exit_flag, SA_SIGINFO);
  4696. setup_signal_catcher(SIGINT, set_exit_flag, SA_SIGINFO);
  4697. setup_signal_catcher(SIGUSR1, usr1, 0);
  4698. }
  4699. int
  4700. main(int argc, char **argv)
  4701. {
  4702. const char *path;
  4703. srand48(getpid());
  4704. path = set_diskomizer_path();
  4705. if (do_args(argc, argv, pprintf, path) == 0) {
  4706. usage(*argv);
  4707. }
  4708. /*
  4709. * Usage tracking has to open after argument checking as we need
  4710. * the values from the configuration files.
  4711. */
  4712. usage_tracking_handle = open_usage_tracking(
  4713. opts.obscure_usagetracking_domain,
  4714. opts.obscure_sendmail,
  4715. opts.obscure_usage_email, /* from */
  4716. "diskomizer", /* to */
  4717. diskomizer_str, /* tool */
  4718. VERSION);
  4719. setup_signal((int (*)(void *, const char *, ...))pfprintf, stderr);
  4720. set_limits();
  4721. set_max_blocks();
  4722. if (opts.STDOUT != NULL)
  4723. if (freopen(opts.STDOUT, "a+", stdout) == NULL) {
  4724. pperror("Unable to open %s for stdout\n",
  4725. opts.STDOUT);
  4726. exit(1);
  4727. }
  4728. if (opts.STDERR != NULL)
  4729. if (freopen(opts.STDERR, "a+", stderr) == NULL) {
  4730. pperror("Unable to open %s for stderr\n",
  4731. opts.STDERR);
  4732. exit(1);
  4733. }
  4734. popenlog("diskomizer");
  4735. if (my_gettimeofday(&start_time, NULL) == -1) {
  4736. plog(LOG_ERR, "Unable to get time of day\n");
  4737. exit(EXIT_FAILURE);
  4738. }
  4739. set_serial_and_provider();
  4740. if (opts.expert_write_cluster_length == 0)
  4741. opts.expert_write_cluster_length = 1;
  4742. if (opts.expert_read_cluster_length == 0)
  4743. opts.expert_read_cluster_length = 1;
  4744. if (opts.STDERR != NULL && opts.STDOUT != NULL && opts.background)
  4745. background();
  4746. (void) printf("\tCopyright %s Sun Microsystems, Inc."
  4747. " All Rights Reserved\n\tUse is subject to license terms.\n\t"
  4748. "Version %s\n", THIS_YEAR, VERSION);
  4749. print_args(argc, argv, (void (*)(const char *, ...))printf);
  4750. /* Check for values which mean we do nothing */
  4751. if (opts.nprocs < 1 || opts.start_reads_percentage > 100) {
  4752. exit(1);
  4753. }
  4754. if (opts.read_minimum > 0 &&
  4755. opts.rthreads < opts.wthreads * opts.read_minimum) {
  4756. (void) printf("WARNING: The ratio of readers to writers with "
  4757. "read_minimum set to %d\ncould lead to thrashing "
  4758. "or deadlock\n", opts.read_minimum);
  4759. }
  4760. /*
  4761. * Set up all the functions to use.
  4762. */
  4763. /* First what to do on error. */
  4764. if ((on_error_short = setup_onerror(*argv, opts.on_error_short,
  4765. READ_ERR)) == NULL) {
  4766. exit_status = EXIT_FAILURE;
  4767. exit(exit_status);
  4768. }
  4769. if ((on_error_corrupt = setup_onerror(*argv, opts.on_error_corrupt,
  4770. READ_ERR)) == NULL) {
  4771. exit_status = EXIT_FAILURE;
  4772. exit(exit_status);
  4773. }
  4774. if ((on_write_error = setup_onerror(*argv, opts.on_write_error,
  4775. WRITE_ERR)) == NULL) {
  4776. exit_status = EXIT_FAILURE;
  4777. exit(exit_status);
  4778. }
  4779. /* Now the type of allocator to be used */
  4780. if ((init_uchar_func = setup_write_buf_initializer()) == NULL ||
  4781. (read_buffer_initializer = setup_read_buf_initializer()) == NULL) {
  4782. usage(*argv);
  4783. }
  4784. /* Choose a shared memmory allocator */
  4785. shm_ops = choose_shm_ops(opts.allocator);
  4786. if (opts.device == NULL) {
  4787. (void) prompt();
  4788. }
  4789. if (opts.device == NULL) {
  4790. pfprintf(stderr, "No devices specified.\n");
  4791. exit(1);
  4792. }
  4793. print_uname(stdout);
  4794. USAGE_TRACKING_STORE_KEY_VALUE_INT("pid", getpid());
  4795. print_bufhdr_offsets(stdout);
  4796. print_serial_and_provider(stdout);
  4797. if (opts.nlocks == 0)
  4798. opts.nlocks = (opts.nprocs * 2) + 1; /* should be prime */
  4799. pgrp = setpgrp();
  4800. parent_pid = getpid();
  4801. setup_signals();
  4802. /* Now setup the locking primitives to use to protect the bit maps */
  4803. init_locks();
  4804. /* register a clean up routine. */
  4805. (void) atexit(cleanup);
  4806. /* get our daio */
  4807. if ((daio = daio_choose_ops(opts.aio_routines)) == NULL) {
  4808. char *reason = dlerror();
  4809. (void) pfprintf(stderr,
  4810. "Unable to load daio routines(%s): %s\n",
  4811. opts.aio_routines,
  4812. reason == NULL ? "Unknown" : reason);
  4813. exit(1);
  4814. }
  4815. daio->init_master(opts.checker, INDEX_TO_DIOLEN(max_disk_io_len));
  4816. if (opts.nloops && opts.rthreads == 0 && opts.wrthreads == 0) {
  4817. write_loops = 1;
  4818. }
  4819. USAGE_TRACKING_OPEN_KEY("devices", NULL, NULL);
  4820. /* now open the devices */
  4821. if ((devices = open_devices(opts.device)) == NULL) {
  4822. (void) pfprintf(stderr, "No devices opened\n");
  4823. exit(1);
  4824. }
  4825. USAGE_TRACKING_CLOSE_KEY();
  4826. findap_fini(); /* free up any data that was cached */
  4827. if (check_block_sizes()) {
  4828. exit(1);
  4829. }
  4830. print_startup_info();
  4831. /* and go! */
  4832. do_aio(devices, start_offset(), opts.report_time);
  4833. /*NOTREACHED*/
  4834. return (1);
  4835. }
  4836. long long
  4837. convert_time(struct timeval tv)
  4838. {
  4839. long long tyme;
  4840. long long mill = MILLION;
  4841. tyme = (long long)tv.tv_sec;
  4842. assert(tyme >= 0);
  4843. tyme *= mill;
  4844. assert(tyme >= 0);
  4845. tyme += tv.tv_usec;
  4846. assert(tyme >= 0);
  4847. return (tyme);
  4848. }
  4849. int
  4850. longest_logical_name(void)
  4851. {
  4852. return (Longest_logical_name);
  4853. }
  4854. int
  4855. longest_device_name(void)
  4856. {
  4857. return (Longest_device_name);
  4858. }
  4859. void
  4860. update_time_stats(char off, struct times *tp, hrtime_t hrtyme,
  4861. struct aio_str *aiop)
  4862. {
  4863. if (hrtyme < 0) {
  4864. pfprintf(stderr, "Warning time appears to go backwards\n");
  4865. return;
  4866. }
  4867. if (hrtyme > tp->worst) {
  4868. tp->worst = hrtyme;
  4869. }
  4870. if (hrtyme < tp->best) {
  4871. tp->best = hrtyme;
  4872. }
  4873. tp->ave -= tp->last_few[tp->count % ARRAY_LEN(tp->last_few)];
  4874. tp->last_few[tp->count++ % ARRAY_LEN(tp->last_few)] = hrtyme;
  4875. tp->ave += hrtyme;
  4876. if (opts.how_often_to_report &&
  4877. (tp->count % opts.how_often_to_report) == 0) {
  4878. plog(LOG_INFO, "%-*s (%-*s) %s times (%.*f,%.*f,%.*f) %3d%%\n",
  4879. longest_logical_name(), aiop->dev->logicalname,
  4880. longest_device_name(), aiop->fd->name,
  4881. tp->str,
  4882. opts.expert_decimal_places, (double)tp->best/ACCURACY,
  4883. opts.expert_decimal_places,
  4884. (double)(tp->ave/MIN(tp->count,
  4885. ARRAY_LEN(tp->last_few)))/ACCURACY,
  4886. opts.expert_decimal_places, (double)tp->worst/ACCURACY,
  4887. off);
  4888. }
  4889. }