2 * Copyright (c) 2005-2006 The FreeBSD Project
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
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:
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.
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
33 * Host Resources MIB for SNMPd. Implementation for the hrDiskStorageTable
36 #include <sys/types.h>
37 #include <sys/param.h>
40 #include <sys/linker.h>
41 #include <sys/mdioctl.h>
42 #include <sys/module.h>
43 #include <sys/sysctl.h>
55 #include "hostres_snmp.h"
56 #include "hostres_oid.h"
57 #include "hostres_tree.h"
59 enum hrDiskStrorageAccess {
64 enum hrDiskStrorageMedia {
69 DSM_OPTICALDISKROM= 5,
70 DSM_OPTICALDISKWORM= 6,
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.
80 * NOTE: according to MIB removable means removable media, not the
81 * device itself (like a USB card reader)
85 int32_t access; /* enum hrDiskStrorageAccess */
86 int32_t media; /* enum hrDiskStrorageMedia*/
87 int32_t removable; /* enum snmpTCTruthValue*/
89 TAILQ_ENTRY(disk_entry) link;
91 * next items are not from the SNMP mib table, only to be used
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) */
99 u_char dev_name[32]; /* device name, i.e. "ad4" or "acd0" */
101 TAILQ_HEAD(disk_tbl, disk_entry);
103 /* the head of the list with hrDiskStorageTable's entries */
104 static struct disk_tbl disk_tbl =
105 TAILQ_HEAD_INITIALIZER(disk_tbl);
107 /* last tick when hrFSTable was updated */
108 static uint64_t disk_storage_tick;
110 /* minimum number of ticks between refreshs */
111 uint32_t disk_storage_tbl_refresh = HR_DISK_TBL_REFRESH * 100;
113 /* fd for "/dev/mdctl"*/
114 static int md_fd = -1;
116 /* buffer for sysctl("kern.disks") */
117 static char *disk_list;
118 static size_t disk_list_len;
121 static const struct asn_oid OIDX_hrDeviceDiskStorage_c =
122 OIDX_hrDeviceDiskStorage;
125 * Load the MD driver if it isn't loaded already.
130 char name1[64], name2[64];
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) {
139 "%s module not available!", name2);
146 * Create a new entry into the DiskStorageTable.
148 static struct disk_entry *
149 disk_entry_create(const struct device_entry *devEntry)
151 struct disk_entry *entry;
153 assert(devEntry != NULL);
154 if (devEntry == NULL)
157 if ((entry = malloc(sizeof(*entry))) == NULL) {
158 syslog(LOG_WARNING, "hrDiskStorageTable: %s: %m", __func__);
162 memset(entry, 0, sizeof(*entry));
163 entry->index = devEntry->index;
164 INSERT_OBJECT_INT(entry, &disk_tbl);
170 * Delete a disk table entry.
173 disk_entry_delete(struct disk_entry *entry)
175 struct device_entry *devEntry;
177 assert(entry != NULL);
178 TAILQ_REMOVE(&disk_tbl, entry, link);
180 devEntry = device_find_by_index(entry->index);
185 * Also delete the respective device entry -
186 * this is needed for disk devices that are not
187 * detected by libdevinfo
189 if (devEntry != NULL &&
190 (devEntry->flags & HR_DEVICE_IMMUTABLE) == HR_DEVICE_IMMUTABLE)
191 device_entry_delete(devEntry);
195 * Find a disk storage entry given its index.
197 static struct disk_entry *
198 disk_find_by_index(int32_t idx)
200 struct disk_entry *entry;
202 TAILQ_FOREACH(entry, &disk_tbl, link)
203 if (entry->index == idx)
210 * Get the disk parameters
213 disk_query_disk(struct disk_entry *entry)
219 if (entry == NULL || entry->dev_name[0] == '\0')
222 snprintf(dev_path, sizeof(dev_path),
223 "%s%s", _PATH_DEV, entry->dev_name);
226 HRDBG("OPENING device %s", dev_path);
227 if ((fd = open(dev_path, O_RDONLY|O_NONBLOCK)) == -1) {
228 HRDBG("OPEN device %s failed: %s", dev_path, strerror(errno));
232 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
233 HRDBG("DIOCGMEDIASIZE for device %s failed: %s",
234 dev_path, strerror(errno));
239 mediasize = mediasize / 1024;
240 entry->capacity = (mediasize > INT_MAX ? INT_MAX : mediasize);
241 partition_tbl_handle_disk(entry->index, entry->dev_name);
247 * Find all ATA disks in the device table.
250 disk_OS_get_ATA_disks(void)
252 struct device_map_entry *map;
253 struct device_entry *entry;
254 struct disk_entry *disk_entry;
255 const struct disk_entry *found;
257 /* Things we know are ata disks */
258 static const struct disk_entry lookup[] = {
261 .media = DSM_HARDDISK,
262 .removable = SNMP_FALSE
267 .removable = SNMP_FALSE
271 .media = DSM_OPTICALDISKROM,
272 .removable = SNMP_TRUE
276 .media = DSM_FLOPPYDISK,
277 .removable = SNMP_TRUE
282 .removable = SNMP_TRUE
285 { .media = DSM_UNKNOWN }
288 /* Walk over the device table looking for ata disks */
289 STAILQ_FOREACH(map, &device_map, link) {
290 for (found = lookup; found->media != DSM_UNKNOWN; found++) {
291 if (strncmp(map->name_key, found->dev_name,
292 strlen(found->dev_name)) != 0)
296 * Avoid false disk devices. For example adw(4) and
297 * adv(4) - they are not disks!
299 if (strlen(map->name_key) > strlen(found->dev_name) &&
300 !isdigit(map->name_key[strlen(found->dev_name)]))
303 /* First get the entry from the hrDeviceTbl */
304 entry = map->entry_p;
305 entry->type = &OIDX_hrDeviceDiskStorage_c;
307 /* Then check hrDiskStorage table for this device */
308 disk_entry = disk_find_by_index(entry->index);
309 if (disk_entry == NULL) {
310 disk_entry = disk_entry_create(entry);
311 if (disk_entry == NULL)
314 disk_entry->access = DS_READ_WRITE;
315 strlcpy(disk_entry->dev_name, entry->name,
316 sizeof(disk_entry->dev_name));
318 disk_entry->media = found->media;
319 disk_entry->removable = found->removable;
322 disk_entry->flags |= HR_DISKSTORAGE_FOUND;
323 disk_entry->flags |= HR_DISKSTORAGE_ATA;
325 disk_query_disk(disk_entry);
326 disk_entry->r_tick = this_tick;
332 * Find MD disks in the device table.
335 disk_OS_get_MD_disks(void)
337 struct device_map_entry *map;
338 struct device_entry *entry;
339 struct disk_entry *disk_entry;
340 struct md_ioctl mdio;
343 /* Look for md devices */
344 STAILQ_FOREACH(map, &device_map, link) {
345 if (sscanf(map->name_key, "md%d", &unit) != 1)
348 /* First get the entry from the hrDeviceTbl */
349 entry = device_find_by_index(map->hrIndex);
350 entry->type = &OIDX_hrDeviceDiskStorage_c;
352 /* Then check hrDiskStorage table for this device */
353 disk_entry = disk_find_by_index(entry->index);
354 if (disk_entry == NULL) {
355 disk_entry = disk_entry_create(entry);
356 if (disk_entry == NULL)
359 memset(&mdio, 0, sizeof(mdio));
360 mdio.md_version = MDIOVERSION;
363 if (ioctl(md_fd, MDIOCQUERY, &mdio) < 0) {
365 "hrDiskStorageTable: Couldnt ioctl");
369 if ((mdio.md_options & MD_READONLY) == MD_READONLY)
370 disk_entry->access = DS_READ_ONLY;
372 disk_entry->access = DS_READ_WRITE;
374 strlcpy(disk_entry->dev_name, entry->name,
375 sizeof(disk_entry->dev_name));
377 disk_entry->media = DSM_RAMDISK;
378 disk_entry->removable = SNMP_FALSE;
381 disk_entry->flags |= HR_DISKSTORAGE_FOUND;
382 disk_entry->flags |= HR_DISKSTORAGE_MD;
383 disk_entry->r_tick = this_tick;
391 disk_OS_get_disks(void)
394 struct device_entry *entry;
395 struct disk_entry *disk_entry;
399 if (sysctlbyname("kern.disks", NULL, &need, NULL, 0) == -1) {
400 syslog(LOG_ERR, "%s: sysctl_1 kern.disks failed: %m", __func__);
407 if (disk_list_len != need + 1 || disk_list == NULL) {
408 disk_list_len = need + 1;
409 disk_list = reallocf(disk_list, disk_list_len);
412 if (disk_list == NULL) {
413 syslog(LOG_ERR, "%s: reallocf failed", __func__);
418 memset(disk_list, 0, disk_list_len);
420 if (sysctlbyname("kern.disks", disk_list, &need, NULL, 0) == -1 ||
422 syslog(LOG_ERR, "%s: sysctl_2 kern.disks failed: %m", __func__);
426 for (disk_cnt = 0; disk_cnt < need; disk_cnt++) {
428 char disk_device[128] = "";
430 disk = strsep(&disk_list, " ");
434 snprintf(disk_device, sizeof(disk_device),
435 "%s%s", _PATH_DEV, disk);
437 /* First check if the disk is in the hrDeviceTable. */
438 if ((entry = device_find_by_name(disk)) == NULL) {
440 * not found there - insert it as immutable
442 syslog(LOG_WARNING, "%s: device '%s' not in "
443 "device list", __func__, disk);
445 if ((entry = device_entry_create(disk, "", "")) == NULL)
448 entry->flags |= HR_DEVICE_IMMUTABLE;
451 entry->type = &OIDX_hrDeviceDiskStorage_c;
453 /* Then check hrDiskStorage table for this device */
454 disk_entry = disk_find_by_index(entry->index);
455 if (disk_entry == NULL) {
456 disk_entry = disk_entry_create(entry);
457 if (disk_entry == NULL)
461 disk_entry->flags |= HR_DISKSTORAGE_FOUND;
463 if ((disk_entry->flags & HR_DISKSTORAGE_ATA) ||
464 (disk_entry->flags & HR_DISKSTORAGE_MD)) {
466 * ATA/MD detection is running before this one,
467 * so don't waste the time here
472 disk_entry->access = DS_READ_WRITE;
473 disk_entry->media = DSM_UNKNOWN;
474 disk_entry->removable = SNMP_FALSE;
476 if (strncmp(disk_entry->dev_name, "da", 2) == 0) {
477 disk_entry->media = DSM_HARDDISK;
478 disk_entry->removable = SNMP_FALSE;
479 } else if (strncmp(disk_entry->dev_name, "cd", 2) == 0) {
480 disk_entry->media = DSM_OPTICALDISKROM;
481 disk_entry->removable = SNMP_TRUE;
483 disk_entry->media = DSM_UNKNOWN;
484 disk_entry->removable = SNMP_FALSE;
487 strlcpy((char *)disk_entry->dev_name, disk,
488 sizeof(disk_entry->dev_name));
490 disk_query_disk(disk_entry);
491 disk_entry->r_tick = this_tick;
496 * Refresh routine for hrDiskStorageTable
497 * Usable for polling the system for any changes.
500 refresh_disk_storage_tbl(int force)
502 struct disk_entry *entry, *entry_tmp;
504 if (disk_storage_tick != 0 && !force &&
505 this_tick - disk_storage_tick < disk_storage_tbl_refresh) {
506 HRDBG("no refresh needed");
510 partition_tbl_pre_refresh();
512 /* mark each entry as missing */
513 TAILQ_FOREACH(entry, &disk_tbl, link)
514 entry->flags &= ~HR_DISKSTORAGE_FOUND;
516 disk_OS_get_ATA_disks(); /* this must be called first ! */
517 disk_OS_get_MD_disks();
521 * Purge items that disappeared
523 TAILQ_FOREACH_SAFE(entry, &disk_tbl, link, entry_tmp)
524 if (!(entry->flags & HR_DISKSTORAGE_FOUND))
525 /* XXX remove IMMUTABLE entries that have disappeared */
526 disk_entry_delete(entry);
528 disk_storage_tick = this_tick;
530 partition_tbl_post_refresh();
532 HRDBG("refresh DONE");
536 * Init the things for both of hrDiskStorageTable
539 init_disk_storage_tbl(void)
543 /* Try to load md.ko if not loaded already */
547 snprintf(mddev, sizeof(mddev) - 1, "%s%s", _PATH_DEV, MDCTL_NAME);
548 if ((md_fd = open(mddev, O_RDWR)) == -1) {
549 syslog(LOG_ERR, "open %s failed: %m", mddev);
553 refresh_disk_storage_tbl(1);
559 * Finalization routine for hrDiskStorageTable
560 * It destroys the lists and frees any allocated heap memory
563 fini_disk_storage_tbl(void)
565 struct disk_entry *n1;
567 while ((n1 = TAILQ_FIRST(&disk_tbl)) != NULL) {
568 TAILQ_REMOVE(&disk_tbl, n1, link);
575 if (close(md_fd) == -1)
576 syslog(LOG_ERR,"close (/dev/mdctl) failed: %m");
582 * This is the implementation for a generated (by our SNMP "compiler" tool)
583 * function prototype, see hostres_tree.h
584 * It handles the SNMP operations for hrDiskStorageTable
587 op_hrDiskStorageTable(struct snmp_context *ctx __unused,
588 struct snmp_value *value, u_int sub, u_int iidx __unused,
589 enum snmp_op curr_op)
591 struct disk_entry *entry;
593 refresh_disk_storage_tbl(0);
597 case SNMP_OP_GETNEXT:
598 if ((entry = NEXT_OBJECT_INT(&disk_tbl,
599 &value->var, sub)) == NULL)
600 return (SNMP_ERR_NOSUCHNAME);
601 value->var.len = sub + 1;
602 value->var.subs[sub] = entry->index;
606 if ((entry = FIND_OBJECT_INT(&disk_tbl,
607 &value->var, sub)) == NULL)
608 return (SNMP_ERR_NOSUCHNAME);
612 if ((entry = FIND_OBJECT_INT(&disk_tbl,
613 &value->var, sub)) == NULL)
614 return (SNMP_ERR_NO_CREATION);
615 return (SNMP_ERR_NOT_WRITEABLE);
617 case SNMP_OP_ROLLBACK:
624 switch (value->var.subs[sub - 1]) {
626 case LEAF_hrDiskStorageAccess:
627 value->v.integer = entry->access;
628 return (SNMP_ERR_NOERROR);
630 case LEAF_hrDiskStorageMedia:
631 value->v.integer = entry->media;
632 return (SNMP_ERR_NOERROR);
634 case LEAF_hrDiskStorageRemoveble:
635 value->v.integer = entry->removable;
636 return (SNMP_ERR_NOERROR);
638 case LEAF_hrDiskStorageCapacity:
639 value->v.integer = entry->capacity;
640 return (SNMP_ERR_NOERROR);