1 /*- 2 * Copyright (c) 2005-2006 The FreeBSD Project 3 * All rights reserved. 4 * 5 * Author: Victor Cruceru <soc-victor@freebsd.org> 6 * 7 * Redistribution of this software and documentation and use in source and 8 * binary forms, with or without modification, are permitted provided that 9 * the following conditions are met: 10 * 11 * 1. Redistributions of source code or documentation must retain the above 12 * copyright notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * Host Resources MIB for SNMPd. Implementation for the hrDiskStorageTable 34 */ 35 36 #include <sys/types.h> 37 #include <sys/param.h> 38 #include <sys/ata.h> 39 #include <sys/disk.h> 40 #include <sys/linker.h> 41 #include <sys/mdioctl.h> 42 #include <sys/module.h> 43 #include <sys/sysctl.h> 44 45 #include <assert.h> 46 #include <ctype.h> 47 #include <err.h> 48 #include <errno.h> 49 #include <paths.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <syslog.h> 53 #include <unistd.h> 54 55 #include "hostres_snmp.h" 56 #include "hostres_oid.h" 57 #include "hostres_tree.h" 58 59 enum hrDiskStrorageAccess { 60 DS_READ_WRITE = 1, 61 DS_READ_ONLY = 2 62 }; 63 64 enum hrDiskStrorageMedia { 65 DSM_OTHER = 1, 66 DSM_UNKNOWN = 2, 67 DSM_HARDDISK = 3, 68 DSM_FLOPPYDISK = 4, 69 DSM_OPTICALDISKROM= 5, 70 DSM_OPTICALDISKWORM= 6, 71 DSM_OPTICALDISKRW= 7, 72 DSM_RAMDISK = 8 73 }; 74 75 /* 76 * This structure is used to hold a SNMP table entry for HOST-RESOURCES-MIB's 77 * hrDiskStorageTable. Note that index is external being allocated and 78 * maintained by the hrDeviceTable code. 79 * 80 * NOTE: according to MIB removable means removable media, not the 81 * device itself (like a USB card reader) 82 */ 83 struct disk_entry { 84 int32_t index; 85 int32_t access; /* enum hrDiskStrorageAccess */ 86 int32_t media; /* enum hrDiskStrorageMedia*/ 87 int32_t removable; /* enum snmpTCTruthValue*/ 88 int32_t capacity; 89 TAILQ_ENTRY(disk_entry) link; 90 /* 91 * next items are not from the SNMP mib table, only to be used 92 * internally 93 */ 94 #define HR_DISKSTORAGE_FOUND 0x001 95 #define HR_DISKSTORAGE_ATA 0x002 /* belongs to the ATA subsystem */ 96 #define HR_DISKSTORAGE_MD 0x004 /* it is a MD (memory disk) */ 97 uint32_t flags; 98 uint64_t r_tick; 99 u_char dev_name[32]; /* device name, i.e. "ad4" or "acd0" */ 100 }; 101 TAILQ_HEAD(disk_tbl, disk_entry); 102 103 /* the head of the list with hrDiskStorageTable's entries */ 104 static struct disk_tbl disk_tbl = 105 TAILQ_HEAD_INITIALIZER(disk_tbl); 106 107 /* last tick when hrFSTable was updated */ 108 static uint64_t disk_storage_tick; 109 110 /* minimum number of ticks between refreshs */ 111 uint32_t disk_storage_tbl_refresh = HR_DISK_TBL_REFRESH * 100; 112 113 /* fd for "/dev/mdctl"*/ 114 static int md_fd = -1; 115 116 /* buffer for sysctl("kern.disks") */ 117 static char *disk_list; 118 static size_t disk_list_len; 119 120 /* some constants */ 121 static const struct asn_oid OIDX_hrDeviceDiskStorage_c = 122 OIDX_hrDeviceDiskStorage; 123 124 /** 125 * Load the MD driver if it isn't loaded already. 126 */ 127 static void 128 mdmaybeload(void) 129 { 130 char name1[64], name2[64]; 131 132 snprintf(name1, sizeof(name1), "g_%s", MD_NAME); 133 snprintf(name2, sizeof(name2), "geom_%s", MD_NAME); 134 if (modfind(name1) == -1) { 135 /* Not present in kernel, try loading it. */ 136 if (kldload(name2) == -1 || modfind(name1) == -1) { 137 if (errno != EEXIST) { 138 errx(EXIT_FAILURE, 139 "%s module not available!", name2); 140 } 141 } 142 } 143 } 144 145 /** 146 * Create a new entry into the DiskStorageTable. 147 */ 148 static struct disk_entry * 149 disk_entry_create(const struct device_entry *devEntry) 150 { 151 struct disk_entry *entry; 152 153 assert(devEntry != NULL); 154 if (devEntry == NULL) 155 return NULL; 156 157 if ((entry = malloc(sizeof(*entry))) == NULL) { 158 syslog(LOG_WARNING, "hrDiskStorageTable: %s: %m", __func__); 159 return (NULL); 160 } 161 162 memset(entry, 0, sizeof(*entry)); 163 entry->index = devEntry->index; 164 INSERT_OBJECT_INT(entry, &disk_tbl); 165 166 return (entry); 167 } 168 169 /** 170 * Delete a disk table entry. 171 */ 172 static void 173 disk_entry_delete(struct disk_entry *entry) 174 { 175 176 assert(entry != NULL); 177 178 TAILQ_REMOVE(&disk_tbl, entry, link); 179 free(entry); 180 } 181 182 /** 183 * Find a disk storage entry given its index. 184 */ 185 static struct disk_entry * 186 disk_find_by_index(int32_t idx) 187 { 188 struct disk_entry *entry; 189 190 TAILQ_FOREACH(entry, &disk_tbl, link) 191 if (entry->index == idx) 192 return (entry); 193 194 return (NULL); 195 } 196 197 /** 198 * Get the disk parameters 199 */ 200 static void 201 disk_query_disk(struct disk_entry *entry) 202 { 203 char dev_path[128]; 204 int fd; 205 off_t mediasize; 206 207 if (entry == NULL || entry->dev_name[0] == '\0') 208 return; 209 210 snprintf(dev_path, sizeof(dev_path), 211 "%s%s", _PATH_DEV, entry->dev_name); 212 entry->capacity = 0; 213 214 HRDBG("OPENING device %s", dev_path); 215 if ((fd = open(dev_path, O_RDONLY|O_NONBLOCK)) == -1) { 216 HRDBG("OPEN device %s failed: %s", dev_path, strerror(errno)); 217 return; 218 } 219 220 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) { 221 HRDBG("DIOCGMEDIASIZE for device %s failed: %s", 222 dev_path, strerror(errno)); 223 (void)close(fd); 224 return; 225 } 226 227 mediasize = mediasize / 1024; 228 entry->capacity = (mediasize > INT_MAX ? INT_MAX : mediasize); 229 partition_tbl_handle_disk(entry->index, entry->dev_name); 230 231 (void)close(fd); 232 } 233 234 /** 235 * Find all ATA disks in the device table. 236 */ 237 static void 238 disk_OS_get_ATA_disks(void) 239 { 240 struct device_map_entry *map; 241 struct device_entry *entry; 242 struct disk_entry *disk_entry; 243 const struct disk_entry *found; 244 245 /* Things we know are ata disks */ 246 static const struct disk_entry lookup[] = { 247 { 248 .dev_name = "ad", 249 .media = DSM_HARDDISK, 250 .removable = SNMP_FALSE 251 }, 252 { 253 .dev_name = "ar", 254 .media = DSM_OTHER, 255 .removable = SNMP_FALSE 256 }, 257 { 258 .dev_name = "acd", 259 .media = DSM_OPTICALDISKROM, 260 .removable = SNMP_TRUE 261 }, 262 { 263 .dev_name = "afd", 264 .media = DSM_FLOPPYDISK, 265 .removable = SNMP_TRUE 266 }, 267 { 268 .dev_name = "ast", 269 .media = DSM_OTHER, 270 .removable = SNMP_TRUE 271 }, 272 273 { .media = DSM_UNKNOWN } 274 }; 275 276 /* Walk over the device table looking for ata disks */ 277 STAILQ_FOREACH(map, &device_map, link) { 278 for (found = lookup; found->media != DSM_UNKNOWN; found++) { 279 if (strncmp(map->name_key, found->dev_name, 280 strlen(found->dev_name)) != 0) 281 continue; 282 283 /* 284 * Avoid false disk devices. For example adw(4) and 285 * adv(4) - they are not disks! 286 */ 287 if (strlen(map->name_key) > strlen(found->dev_name) && 288 !isdigit(map->name_key[strlen(found->dev_name)])) 289 continue; 290 291 /* First get the entry from the hrDeviceTbl */ 292 entry = map->entry_p; 293 entry->type = OIDX_hrDeviceDiskStorage_c; 294 295 /* Then check hrDiskStorage table for this device */ 296 disk_entry = disk_find_by_index(entry->index); 297 if (disk_entry == NULL) { 298 disk_entry = disk_entry_create(entry); 299 if (disk_entry == NULL) 300 continue; 301 302 disk_entry->access = DS_READ_WRITE; 303 strlcpy(disk_entry->dev_name, entry->name, 304 sizeof(disk_entry->dev_name)); 305 306 disk_entry->media = found->media; 307 disk_entry->removable = found->removable; 308 } 309 310 disk_entry->flags |= HR_DISKSTORAGE_FOUND; 311 disk_entry->flags |= HR_DISKSTORAGE_ATA; 312 313 disk_query_disk(disk_entry); 314 disk_entry->r_tick = this_tick; 315 } 316 } 317 } 318 319 /** 320 * Find MD disks in the device table. 321 */ 322 static void 323 disk_OS_get_MD_disks(void) 324 { 325 struct device_map_entry *map; 326 struct device_entry *entry; 327 struct disk_entry *disk_entry; 328 struct md_ioctl mdio; 329 int unit; 330 331 /* Look for md devices */ 332 STAILQ_FOREACH(map, &device_map, link) { 333 if (sscanf(map->name_key, "md%d", &unit) != 1) 334 continue; 335 336 /* First get the entry from the hrDeviceTbl */ 337 entry = device_find_by_index(map->hrIndex); 338 entry->type = OIDX_hrDeviceDiskStorage_c; 339 340 /* Then check hrDiskStorage table for this device */ 341 disk_entry = disk_find_by_index(entry->index); 342 if (disk_entry == NULL) { 343 disk_entry = disk_entry_create(entry); 344 if (disk_entry == NULL) 345 continue; 346 347 memset(&mdio, 0, sizeof(mdio)); 348 mdio.md_version = MDIOVERSION; 349 mdio.md_unit = unit; 350 351 if (ioctl(md_fd, MDIOCQUERY, &mdio) < 0) { 352 syslog(LOG_ERR, 353 "hrDiskStorageTable: Couldnt ioctl"); 354 continue; 355 } 356 357 if ((mdio.md_options & MD_READONLY) == MD_READONLY) 358 disk_entry->access = DS_READ_ONLY; 359 else 360 disk_entry->access = DS_READ_WRITE; 361 362 strlcpy(disk_entry->dev_name, entry->name, 363 sizeof(disk_entry->dev_name)); 364 365 disk_entry->media = DSM_RAMDISK; 366 disk_entry->removable = SNMP_FALSE; 367 } 368 369 disk_entry->flags |= HR_DISKSTORAGE_FOUND; 370 disk_entry->flags |= HR_DISKSTORAGE_MD; 371 disk_entry->r_tick = this_tick; 372 } 373 } 374 375 /** 376 * Find rest of disks 377 */ 378 static void 379 disk_OS_get_disks(void) 380 { 381 size_t disk_cnt = 0; 382 struct device_entry *entry; 383 struct disk_entry *disk_entry; 384 385 size_t need = 0; 386 387 if (sysctlbyname("kern.disks", NULL, &need, NULL, 0) == -1) { 388 syslog(LOG_ERR, "%s: sysctl_1 kern.disks failed: %m", __func__); 389 return; 390 } 391 392 if (need == 0) 393 return; 394 395 if (disk_list_len != need + 1 || disk_list == NULL) { 396 disk_list_len = need + 1; 397 disk_list = reallocf(disk_list, disk_list_len); 398 } 399 400 if (disk_list == NULL) { 401 syslog(LOG_ERR, "%s: reallocf failed", __func__); 402 disk_list_len = 0; 403 return; 404 } 405 406 memset(disk_list, 0, disk_list_len); 407 408 if (sysctlbyname("kern.disks", disk_list, &need, NULL, 0) == -1 || 409 disk_list[0] == 0) { 410 syslog(LOG_ERR, "%s: sysctl_2 kern.disks failed: %m", __func__); 411 return; 412 } 413 414 for (disk_cnt = 0; disk_cnt < need; disk_cnt++) { 415 char *disk = NULL; 416 char disk_device[128] = ""; 417 418 disk = strsep(&disk_list, " "); 419 if (disk == NULL) 420 break; 421 422 snprintf(disk_device, sizeof(disk_device), 423 "%s%s", _PATH_DEV, disk); 424 425 /* First check if the disk is in the hrDeviceTable. */ 426 if ((entry = device_find_by_name(disk)) == NULL) { 427 /* 428 * not found there - insert it as immutable 429 * XXX somehow we should remove it if it disappears 430 */ 431 syslog(LOG_WARNING, "%s: device '%s' not in " 432 "device list", __func__, disk); 433 434 if ((entry = device_entry_create(disk, "", "")) == NULL) 435 continue; 436 437 entry->flags |= HR_DEVICE_IMMUTABLE; 438 } 439 440 entry->type = OIDX_hrDeviceDiskStorage_c; 441 442 /* Then check hrDiskStorage table for this device */ 443 disk_entry = disk_find_by_index(entry->index); 444 if (disk_entry == NULL) { 445 disk_entry = disk_entry_create(entry); 446 if (disk_entry == NULL) 447 continue; 448 } 449 450 disk_entry->flags |= HR_DISKSTORAGE_FOUND; 451 452 if ((disk_entry->flags & HR_DISKSTORAGE_ATA) || 453 (disk_entry->flags & HR_DISKSTORAGE_MD)) { 454 /* 455 * ATA/MD detection is running before this one, 456 * so don't waste the time here 457 */ 458 continue; 459 } 460 461 disk_entry->access = DS_READ_WRITE; 462 disk_entry->media = DSM_UNKNOWN; 463 disk_entry->removable = SNMP_FALSE; 464 465 if (strncmp(disk_entry->dev_name, "da", 2) == 0) { 466 disk_entry->media = DSM_HARDDISK; 467 disk_entry->removable = SNMP_FALSE; 468 } else if (strncmp(disk_entry->dev_name, "cd", 2) == 0) { 469 disk_entry->media = DSM_OPTICALDISKROM; 470 disk_entry->removable = SNMP_TRUE; 471 } else { 472 disk_entry->media = DSM_UNKNOWN; 473 disk_entry->removable = SNMP_FALSE; 474 } 475 476 strlcpy((char *)disk_entry->dev_name, disk, 477 sizeof(disk_entry->dev_name)); 478 479 disk_query_disk(disk_entry); 480 disk_entry->r_tick = this_tick; 481 } 482 } 483 484 /** 485 * Refresh routine for hrDiskStorageTable 486 * Usable for polling the system for any changes. 487 */ 488 void 489 refresh_disk_storage_tbl(int force) 490 { 491 struct disk_entry *entry, *entry_tmp; 492 493 if (disk_storage_tick != 0 && !force && 494 this_tick - disk_storage_tick < disk_storage_tbl_refresh) { 495 HRDBG("no refresh needed"); 496 return; 497 } 498 499 partition_tbl_pre_refresh(); 500 501 /* mark each entry as missing */ 502 TAILQ_FOREACH(entry, &disk_tbl, link) 503 entry->flags &= ~HR_DISKSTORAGE_FOUND; 504 505 disk_OS_get_ATA_disks(); /* this must be called first ! */ 506 disk_OS_get_MD_disks(); 507 disk_OS_get_disks(); 508 509 /* 510 * Purge items that disappeared 511 */ 512 TAILQ_FOREACH_SAFE(entry, &disk_tbl, link, entry_tmp) 513 if (!(entry->flags & HR_DISKSTORAGE_FOUND)) 514 /* XXX remove IMMUTABLE entries that have disappeared */ 515 disk_entry_delete(entry); 516 517 disk_storage_tick = this_tick; 518 519 partition_tbl_post_refresh(); 520 521 HRDBG("refresh DONE"); 522 } 523 524 /* 525 * Init the things for both of hrDiskStorageTable 526 */ 527 int 528 init_disk_storage_tbl(void) 529 { 530 char mddev[32] = ""; 531 532 /* Try to load md.ko if not loaded already */ 533 mdmaybeload(); 534 535 md_fd = -1; 536 snprintf(mddev, sizeof(mddev) - 1, "%s%s", _PATH_DEV, MDCTL_NAME); 537 if ((md_fd = open(mddev, O_RDWR)) == -1) { 538 syslog(LOG_ERR, "open %s failed: %m", mddev); 539 return (-1); 540 } 541 542 refresh_disk_storage_tbl(1); 543 544 return (0); 545 } 546 547 /* 548 * Finalization routine for hrDiskStorageTable 549 * It destroys the lists and frees any allocated heap memory 550 */ 551 void 552 fini_disk_storage_tbl(void) 553 { 554 struct disk_entry *n1; 555 556 while ((n1 = TAILQ_FIRST(&disk_tbl)) != NULL) { 557 TAILQ_REMOVE(&disk_tbl, n1, link); 558 free(n1); 559 } 560 561 free(disk_list); 562 563 if (md_fd > 0) { 564 if (close(md_fd) == -1) 565 syslog(LOG_ERR,"close (/dev/mdctl) failed: %m"); 566 md_fd = -1; 567 } 568 } 569 570 /* 571 * This is the implementation for a generated (by our SNMP "compiler" tool) 572 * function prototype, see hostres_tree.h 573 * It handles the SNMP operations for hrDiskStorageTable 574 */ 575 int 576 op_hrDiskStorageTable(struct snmp_context *ctx __unused, 577 struct snmp_value *value, u_int sub, u_int iidx __unused, 578 enum snmp_op curr_op) 579 { 580 struct disk_entry *entry; 581 582 refresh_disk_storage_tbl(0); 583 584 switch (curr_op) { 585 586 case SNMP_OP_GETNEXT: 587 if ((entry = NEXT_OBJECT_INT(&disk_tbl, 588 &value->var, sub)) == NULL) 589 return (SNMP_ERR_NOSUCHNAME); 590 value->var.len = sub + 1; 591 value->var.subs[sub] = entry->index; 592 goto get; 593 594 case SNMP_OP_GET: 595 if ((entry = FIND_OBJECT_INT(&disk_tbl, 596 &value->var, sub)) == NULL) 597 return (SNMP_ERR_NOSUCHNAME); 598 goto get; 599 600 case SNMP_OP_SET: 601 if ((entry = FIND_OBJECT_INT(&disk_tbl, 602 &value->var, sub)) == NULL) 603 return (SNMP_ERR_NO_CREATION); 604 return (SNMP_ERR_NOT_WRITEABLE); 605 606 case SNMP_OP_ROLLBACK: 607 case SNMP_OP_COMMIT: 608 abort(); 609 } 610 abort(); 611 612 get: 613 switch (value->var.subs[sub - 1]) { 614 615 case LEAF_hrDiskStorageAccess: 616 value->v.integer = entry->access; 617 return (SNMP_ERR_NOERROR); 618 619 case LEAF_hrDiskStorageMedia: 620 value->v.integer = entry->media; 621 return (SNMP_ERR_NOERROR); 622 623 case LEAF_hrDiskStorageRemoveble: 624 value->v.integer = entry->removable; 625 return (SNMP_ERR_NOERROR); 626 627 case LEAF_hrDiskStorageCapacity: 628 value->v.integer = entry->capacity; 629 return (SNMP_ERR_NOERROR); 630 } 631 abort(); 632 } 633