PERFORCE change 81259 for review
Victor Cruceru
soc-victor at FreeBSD.org
Sun Jul 31 22:54:32 GMT 2005
http://perforce.freebsd.org/chv.cgi?CH=81259
Change 81259 by soc-victor at soc-victor_82.76.158.176 on 2005/07/31 22:53:39
hrDiskStorageTable step #3: added the md(4) support.
The issue with removable media (CD, DVD, CF) not solved yet. Blocking is bad ....
Affected files ...
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/Makefile#15 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_diskstorage_tbl.c#3 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#14 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#18 edit
Differences ...
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/Makefile#15 (text+ko) ====
@@ -38,7 +38,7 @@
WARNS?= 6
#Not having NDEBUG defined will enable assertions and a lot of output on stderr
-#CFLAGS+= -DNDEBUG
+CFLAGS+= -DNDEBUG
XSYM= host hrStorageOther hrStorageRam hrStorageVirtualMemory \
hrStorageFixedDisk hrStorageRemovableDisk hrStorageFloppyDisk \
hrStorageCompactDisc hrStorageRamDisk hrStorageFlashMemory \
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_diskstorage_tbl.c#3 (text+ko) ====
@@ -46,6 +46,7 @@
#include <sys/ata.h>
#include <errno.h>
#include <sys/disk.h>
+#include <sys/mdioctl.h>
extern
struct hrDeviceTblEntry *
@@ -246,11 +247,9 @@
struct ata_cmd iocmd;
int maxunit, unit, i;
if ( hrState_g.ata_fd < 0 ){
- if ((hrState_g.ata_fd = open("/dev/ata", O_RDWR)) < 0) {
- syslog(LOG_ERR, "open /dev/ata failed:%m");
- return;
- }
+ return;
}
+
iocmd.cmd = ATAGMAXCHANNEL;
if (ioctl(hrState_g.ata_fd, IOCATA, &iocmd) < 0) {
syslog(LOG_ERR, "ioctl(ATAGMAXCHANNEL) failed: %m");
@@ -277,7 +276,7 @@
entry = hrDeviceTblEntry_find_by_name(u_iocmd.u.param.name[i]);
if (entry == NULL) {
HR_DPRINTF((stderr,
- "hrDiskStorageTable; disk %s not in hrDeviceTable\n",
+ "hrDiskStorageTable: ATA disk %s not in hrDeviceTable\n",
u_iocmd.u.param.name[i] ));
entry = hrDeviceTblEntry_create(u_iocmd.u.param.name[i]);
assert(entry != NULL);
@@ -297,7 +296,7 @@
}else{
HR_DPRINTF((stderr,
- "hrDiskStorageTable; disk %s already in hrDeviceTable\n",
+ "hrDiskStorageTable; ATA disk %s already in hrDeviceTable\n",
u_iocmd.u.param.name[i] ));
}
entry->flags |= HR_DEVICE_IMMUTABLE;
@@ -346,7 +345,102 @@
ATA_DONE:
return;
}
+
+static
+void
+hrDiskStorage_OS_get_MD_disks_v(void) {
+ struct hrDeviceTblEntry *entry = NULL;
+ struct hrDiskStorageTblEntry *disk_entry = NULL;
+
+ struct md_ioctl mdio;
+ int unit = 0;
+
+
+ if ( hrState_g.md_fd < 0 ){
+ return;
+ }
+ memset(&mdio, 0, sizeof(mdio));
+ mdio.md_version = MDIOVERSION;
+ mdio.md_options = MD_AUTOUNIT;
+ if (ioctl( hrState_g.md_fd, MDIOCLIST, &mdio) < 0) {
+ syslog(LOG_ERR, "ioctl(/dev/%s) failed: %m", MDCTL_NAME);
+ return;
+ }
+ for (unit = 0; unit < mdio.md_pad[0] && unit < MDNPAD - 1; unit++) {
+ char mddev[64] = "";
+ snprintf(mddev, sizeof(mddev) - 1, "%s%d", MD_NAME, mdio.md_pad[unit + 1]);
+ struct md_ioctl u_mdio;
+
+ memset(&u_mdio, 0, sizeof(u_mdio));
+ u_mdio.md_version = MDIOVERSION;
+ u_mdio.md_unit = mdio.md_pad[unit + 1];
+
+ if (ioctl( hrState_g.md_fd, MDIOCQUERY, &u_mdio) < 0) {
+ syslog(LOG_ERR, "ioctl(/dev/%s, MDIOCQUERY) [%s] failed: %m", MDCTL_NAME, mddev);
+ continue;
+ }
+
+
+ /*first check if this disk is in hrDeiveTable*/
+ entry = hrDeviceTblEntry_find_by_name(mddev);
+ if (entry == NULL) {
+ HR_DPRINTF((stderr,
+ "hrDiskStorageTable; MD %s not in hrDeviceTable\n",
+ mddev ));
+ entry = hrDeviceTblEntry_create(mddev);
+ assert(entry != NULL);
+ if (entry == NULL) {
+ continue;
+ }
+
+ entry->type = (struct asn_oid)OIDX_hrDeviceDiskStorage;
+ entry->id = oid_zeroDotZero; /*unknown id - FIX ME*/
+ entry->status = DR_RUNNING;
+ entry->errors = 0; /*FIX ME*/
+ snprintf(entry->descr, sizeof(entry->descr) - 1,
+ "%s: Memory Disk ",
+ mddev);
+
+ }else{
+ HR_DPRINTF((stderr,
+ "hrDiskStorageTable; MD %s already in hrDeviceTable\n",
+ mddev ));
+ }
+ entry->flags |= HR_DEVICE_IMMUTABLE;
+ entry->flags |= HR_DEVICE_FOUND;
+
+ /*Then check hrDiskStorage table for this device*/
+ disk_entry = hrDiskStorageTblEntry_find_by_index(entry->index);
+ if (disk_entry == NULL) {
+ disk_entry = hrDiskStorageEntry_create(entry);
+ assert(disk_entry != NULL);
+ if (disk_entry == NULL) {
+ continue;
+ }
+ if ((u_mdio.md_options & MD_READONLY) == MD_READONLY) {
+ disk_entry->access = DS_READ_ONLY;
+ } else {
+ disk_entry->access = DS_READ_WRITE;
+ }
+ memset(&disk_entry->dev_name[0], 0, sizeof(disk_entry->dev_name));
+ strncpy(disk_entry->dev_name, mddev, sizeof(disk_entry->dev_name)-1);
+
+ disk_entry->media = DSM_RAMDISK;
+ disk_entry->removable = SNMP_FALSE;
+
+
+ }
+ disk_entry->flags |= HR_DISKSTORAGE_FOUND;
+ disk_entry->flags |= HR_DISKSTORAGE_MD;
+ disk_entry->capacity = u_mdio.md_size / 2;
+
+ disk_entry->r_tick = this_tick;
+
+ }
+}
+
+
static
void
hrDiskStorage_OS_get_disks_v(void) {
@@ -424,7 +518,8 @@
assert(disk_entry != NULL);
disk_entry->flags |= HR_DISKSTORAGE_FOUND;
- if ( (disk_entry->flags & HR_DISKSTORAGE_ATA) == HR_DISKSTORAGE_ATA ) {
+ if ((disk_entry->flags & HR_DISKSTORAGE_ATA) == HR_DISKSTORAGE_ATA ||
+ (disk_entry->flags & HR_DISKSTORAGE_MD) == HR_DISKSTORAGE_MD) {
/*ATA detection is running before this one, so don't waste the time here */
continue;
}
@@ -500,7 +595,7 @@
}
hrDiskStorage_OS_get_ATA_disks_v(); /*this must be called first !*/
-
+ hrDiskStorage_OS_get_MD_disks_v();
hrDiskStorage_OS_get_disks_v();
/*
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#14 (text+ko) ====
@@ -28,6 +28,22 @@
*
* Host Resources MIB implementation for SNMPd.
*/
+
+/* This C file contains code developed by Poul-Henning Kamp
+ * under the following license:
+ */
+ /*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <phk at FreeBSD.ORG> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
+ * ----------------------------------------------------------------------------
+ *
+ * $FreeBSD: src/sbin/mdconfig/mdconfig.c,v 1.33.2.1 2004/09/14 03:32:21 jmg Exp $
+ *
+ */
+
#include "hostres_snmp.h"
#include "hostres_oid.h"
#include "hostres_tree.h"
@@ -38,6 +54,10 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
+#include <sys/mdioctl.h>
+#include <sys/param.h>
+#include <sys/linker.h>
+#include <sys/module.h>
/*internal id got after we'll register this module with the agent */
@@ -54,13 +74,49 @@
struct hostres_state hrState_g;
+static
+void
+mdmaybeload(void)
+{
+ struct module_stat mstat;
+ int fileid, modid;
+ const char *name;
+ char *cp;
+
+ name = MD_MODNAME;
+ /* scan files in kernel */
+ mstat.version = sizeof(struct module_stat);
+ for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
+ /* scan modules in file */
+ for (modid = kldfirstmod(fileid); modid > 0;
+ modid = modfnext(modid)) {
+ if (modstat(modid, &mstat) < 0)
+ continue;
+ /* strip bus name if present */
+ if ((cp = strchr(mstat.name, '/')) != NULL) {
+ cp++;
+ } else {
+ cp = mstat.name;
+ }
+ /* already loaded? */
+ if (!strcmp(name, cp))
+ return;
+ }
+ }
+ /* not present, we should try to load it */
+ if (kldload(name) < 0) {
+ syslog(LOG_ERR,"kldload(%s) failed: %m\n", name);
+ }
+}
+
+
/*
* HOST RESOURCES mib module initialization hook.
* Returns 0 on success, < 0 on error
*/
static
int hostres_init(struct lmodule * mod, int argc __unused, char *argv[] __unused) {
-
+ char mddev[32] = "";
hostres_module = mod;
@@ -91,7 +147,18 @@
return (-1); /*error*/
}
hrState_g.ata_fd = -1;
-
+ if ((hrState_g.ata_fd = open("/dev/ata", O_RDWR)) < 0) {
+ syslog(LOG_ERR, "open /dev/ata failed:%m");
+ }
+
+ mdmaybeload();
+
+ hrState_g.md_fd = -1;
+ snprintf(mddev, sizeof(mddev) - 1, "/dev/%s", MDCTL_NAME);
+ if ((hrState_g.md_fd = open(mddev, O_RDWR)) < 0) {
+ syslog(LOG_ERR, "open %s failed: %m", mddev);
+ }
+
hrState_g.dev_root = NULL;
hrState_g.hrSWOSIndex = 0;
@@ -242,6 +309,13 @@
}
hrState_g.ata_fd = -1;
}
+ if (hrState_g.md_fd > 0) {
+ if (close(hrState_g.md_fd) < 0) {
+ syslog(LOG_ERR,"close (/dev/mdctl) failed: %m");
+ }
+ hrState_g.md_fd = -1;
+ }
+
if( host_registration_id > 0){
or_unregister(host_registration_id);
}
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#18 (text+ko) ====
@@ -257,6 +257,7 @@
/*next items are not from the SNMP mib table, only to be used internally*/
#define HR_DISKSTORAGE_FOUND 0x001
#define HR_DISKSTORAGE_ATA 0x002 /*belongs to the ATA subsystem*/
+#define HR_DISKSTORAGE_MD 0x004 /*it is a MD (memory disk) */
uint32_t flags;
uint64_t r_tick;
u_char dev_name[32]; /*device name, i.e. "ad4" or "acd0"*/
@@ -413,6 +414,7 @@
char* disk_list; /*returned by sysctl("kern.disks")*/
size_t disk_list_len;
int ata_fd; /* fd for "/dev/ata"*/
+ int md_fd; /* fd for "/dev/mdctl"*/
};
More information about the p4-projects
mailing list