sm_smart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. };
  69. static struct smart_device *smart_devs = NULL;
  70. static int smart_size = 0;
  71. void
  72. init_smart(struct stream *st)
  73. {
  74. int i;
  75. char drivename[MAX_PATH_LEN];
  76. struct smart_device *sd;
  77. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  78. fatal("smart: internal error: smart values structure is broken");
  79. }
  80. if (st->arg == NULL) {
  81. fatal("smart: need a <device> argument");
  82. }
  83. bzero(drivename, MAX_PATH_LEN);
  84. snprintf(drivename, MAX_PATH_LEN, "/dev/%s", st->arg);
  85. /* look for drive in our global table */
  86. for (i = 0; i < smart_size; i++) {
  87. if (strncmp(smart_devs[i].name, drivename, MAX_PATH_LEN) == 0) {
  88. st->parg.smart = i;
  89. return;
  90. }
  91. }
  92. /* this is a new drive; allocate the command and data block */
  93. smart_size++;
  94. smart_devs = xrealloc(smart_devs, smart_size * sizeof(struct smart_device));
  95. sd = &smart_devs[smart_size - 1];
  96. bzero(sd, sizeof(struct smart_device));
  97. /* rewire all bufferlocations, as our addresses may have changed */
  98. for (i = 0; i < smart_size; i++)
  99. smart_devs[i].cmd.databuf = (caddr_t)&smart_devs[i].data;
  100. /* store drivename in new block */
  101. snprintf(sd->name, MAX_PATH_LEN, "%s", drivename);
  102. /* store filedescriptor to device */
  103. if ((sd->fd = opendisk(drivename, O_RDONLY | O_NONBLOCK, sd->name, sizeof(sd->name), 0)) == -1) {
  104. if (errno == ENOENT) {
  105. /* Device does not exist, retry using cooked name semantics */
  106. if ((sd->fd = opendisk(drivename, O_RDONLY | O_NONBLOCK, sd->name, sizeof(sd->name), 1)) == -1) {
  107. fatal("smart: could not open '%s' for read", drivename);
  108. }
  109. } else {
  110. fatal("smart: could not open '%s' for read", drivename);
  111. }
  112. }
  113. /* store smart dev entry in stream to facilitate quick get */
  114. st->parg.smart = smart_size - 1;
  115. info("started module smart(%.200s)", st->arg);
  116. }
  117. void
  118. gets_smart()
  119. {
  120. int i;
  121. struct atareq cmd;
  122. for (i = 0; i < smart_size; i++) {
  123. /* populate ata command header */
  124. memcpy(&cmd, (void *) &smart_cmd, sizeof(struct atareq));
  125. cmd.databuf = (caddr_t)&smart_devs[i].data;
  126. if (ioctl(smart_devs[i].fd, ATAIOCCOMMAND, &smart_devs[i].cmd)) {
  127. warning("smart: ioctl for drive '%s' failed: %d",
  128. &smart_devs[i].name, errno);
  129. smart_devs[i].failed = 1;
  130. }
  131. /* check ATA command completion status */
  132. switch (smart_devs[i].cmd.retsts) {
  133. case ATACMD_OK:
  134. break;
  135. case ATACMD_TIMEOUT:
  136. warning("smart: ATA command timed out for drive '%s'", &smart_devs[i].name);
  137. smart_devs[i].failed = 1;
  138. break;
  139. case ATACMD_DF:
  140. warning("smart: ATA device '%s' returned a Device Fault", &smart_devs[i].name);
  141. smart_devs[i].failed = 1;
  142. break;
  143. case ATACMD_ERROR:
  144. if (smart_devs[i].cmd.error & WDCE_ABRT)
  145. warning("smart: ATA device '%s' returned Aborted Command", &smart_devs[i].name);
  146. else
  147. warning("smart: ATA device '%s' returned error register %0x", &smart_devs[i].name, smart_devs[i].cmd.error);
  148. smart_devs[i].failed = 1;
  149. break;
  150. default:
  151. warning("smart: ATAIOCCOMMAND returned unknown result code %d for drive '%s'", smart_devs[i].cmd.retsts, &smart_devs[i].name);
  152. smart_devs[i].failed = 1;
  153. break;
  154. }
  155. /* Some drives do not calculate the smart checksum correctly;
  156. * additional code that identifies these drives would increase our
  157. * footprint and the amount of datajuggling we need to do; we would
  158. * rather ignore the checksums.
  159. */
  160. smart_devs[i].failed = 0;
  161. }
  162. return;
  163. }
  164. int
  165. get_smart(char *symon_buf, int maxlen, struct stream *st)
  166. {
  167. struct smart_report sr;
  168. if ((st->parg.smart < smart_size) &&
  169. (!smart_devs[st->parg.smart].failed))
  170. {
  171. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  172. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  173. sr.read_error_rate,
  174. sr.reallocated_sectors,
  175. sr.spin_retries,
  176. sr.air_flow_temp,
  177. sr.temperature,
  178. sr.reallocations,
  179. sr.current_pending,
  180. sr.uncorrectables,
  181. sr.soft_read_error_rate,
  182. sr.g_sense_error_rate,
  183. sr.temperature2,
  184. sr.free_fall_protection);
  185. }
  186. return 0;
  187. }