sm_smart.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2008 Tito Dal Canton
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/ioctl.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <strings.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <dev/ata/atareg.h>
  39. #include <sys/ataio.h>
  40. #include <util.h>
  41. #include "conf.h"
  42. #include "data.h"
  43. #include "error.h"
  44. #include "xmalloc.h"
  45. #include "smart.h"
  46. /* ata command register set for requesting smart values */
  47. static struct atareq smart_cmd = {
  48. ATACMD_READ, /* flags */
  49. WDCC_SMART, /* command */
  50. WDSM_RD_DATA, /* features */
  51. 1, /* sector count */
  52. 0, /* sector number */
  53. 0, /* head */
  54. WDSMART_CYL, /* cylinder */
  55. NULL, /* data buffer */
  56. sizeof(struct smart_values), /* sizeof data buffer */
  57. SMART_TIMEOUT, /* timeout */
  58. 0, /* retsts */
  59. 0 /* error bits */
  60. };
  61. /* per drive storage structure */
  62. static struct smart_device {
  63. char name[MAX_PATH_LEN];
  64. int fd;
  65. int type;
  66. int failed;
  67. struct smart_values data;
  68. } *smart_devs = NULL;
  69. static int smart_size = 0;
  70. void
  71. init_smart(struct stream *st)
  72. {
  73. int i;
  74. struct smart_device *sd;
  75. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  76. fatal("smart: internal error: smart values structure is broken");
  77. }
  78. if (st->arg == NULL) {
  79. fatal("smart: need a <device> argument");
  80. }
  81. /* look for drive in our global table */
  82. for (i = 0; i < smart_size; i++) {
  83. if (strncmp(smart_devs[i].name, st->arg, MAX_PATH_LEN) == 0) {
  84. st->parg.smart = i;
  85. return;
  86. }
  87. }
  88. /* this is a new drive; allocate the command and data block */
  89. smart_size++;
  90. smart_devs = xrealloc(smart_devs, smart_size * sizeof(struct smart_device));
  91. sd = &smart_devs[smart_size - 1];
  92. bzero(sd, sizeof(struct smart_device));
  93. /* store drivename in new block */
  94. snprintf(sd->name, MAX_PATH_LEN, "%s", st->arg);
  95. /* store filedescriptor to device */
  96. if ((sd->fd = opendisk(st->arg, O_RDONLY | O_NONBLOCK, sd->name, sizeof(sd->name), 0)) == -1) {
  97. if (errno == ENOENT) {
  98. /* Device does not exist, retry using cooked name semantics */
  99. if ((sd->fd = opendisk(st->arg, O_RDONLY | O_NONBLOCK, sd->name, sizeof(sd->name), 1)) == -1) {
  100. fatal("smart: could not open '%s' for read", st->arg);
  101. }
  102. } else {
  103. fatal("smart: could not open '%s' for read", st->arg);
  104. }
  105. }
  106. /* store smart dev entry in stream to facilitate quick get */
  107. st->parg.smart = smart_size - 1;
  108. info("started module smart(%.200s)", st->arg);
  109. }
  110. void
  111. gets_smart()
  112. {
  113. int i;
  114. struct atareq cmd;
  115. for (i = 0; i < smart_size; i++) {
  116. /* populate ata command header */
  117. memcpy(&cmd, (void *) &smart_cmd, sizeof(struct atareq));
  118. cmd.databuf = (caddr_t)&smart_devs[i].data;
  119. if (ioctl(smart_devs[i].fd, ATAIOCCOMMAND, &cmd)) {
  120. warning("smart: ioctl for drive '%s' failed: %d",
  121. &smart_devs[i].name, errno);
  122. smart_devs[i].failed = 1;
  123. }
  124. /* check ATA command completion status */
  125. switch (cmd.retsts) {
  126. case ATACMD_OK:
  127. break;
  128. case ATACMD_TIMEOUT:
  129. warning("smart: ATA command timed out for drive '%s'", &smart_devs[i].name);
  130. smart_devs[i].failed = 1;
  131. break;
  132. case ATACMD_DF:
  133. warning("smart: ATA device '%s' returned a Device Fault", &smart_devs[i].name);
  134. smart_devs[i].failed = 1;
  135. break;
  136. case ATACMD_ERROR:
  137. if (cmd.error & WDCE_ABRT)
  138. warning("smart: ATA device '%s' returned Aborted Command", &smart_devs[i].name);
  139. else
  140. warning("smart: ATA device '%s' returned error register %0x", &smart_devs[i].name, cmd.error);
  141. smart_devs[i].failed = 1;
  142. break;
  143. default:
  144. warning("smart: ATAIOCCOMMAND returned unknown result code %d for drive '%s'", cmd.retsts, &smart_devs[i].name);
  145. smart_devs[i].failed = 1;
  146. break;
  147. }
  148. /* Some drives do not calculate the smart checksum correctly;
  149. * additional code that identifies these drives would increase our
  150. * footprint and the amount of datajuggling we need to do; we would
  151. * rather ignore the checksums.
  152. */
  153. }
  154. return;
  155. }
  156. int
  157. get_smart(char *symon_buf, int maxlen, struct stream *st)
  158. {
  159. struct smart_report sr;
  160. if ((st->parg.smart < smart_size) &&
  161. (!smart_devs[st->parg.smart].failed))
  162. {
  163. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  164. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  165. sr.read_error_rate,
  166. sr.reallocated_sectors,
  167. sr.spin_retries,
  168. sr.air_flow_temp,
  169. sr.temperature,
  170. sr.reallocations,
  171. sr.current_pending,
  172. sr.uncorrectables,
  173. sr.soft_read_error_rate,
  174. sr.g_sense_error_rate,
  175. sr.temperature2,
  176. sr.free_fall_protection);
  177. }
  178. return 0;
  179. }