Date: Sun, 31 Jul 2005 17:03:22 GMT From: Victor Cruceru <soc-victor@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 81251 for review Message-ID: <200507311703.j6VH3MLo087933@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=81251 Change 81251 by soc-victor@soc-victor_82.76.158.176 on 2005/07/31 17:02:38 hrDiskStorageTable step #2: disk capacity support implemented and some other improvements. Need to add support for md(4) disks. Also I need to figure out if it possible to DIOCGMEDIASIZE for CD/DVD drives without blocking the SNMP agent till the unit is ready. Affected files ... .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c#7 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_diskstorage_tbl.c#2 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#13 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#17 edit Differences ... ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c#7 (text+ko) ==== @@ -334,6 +334,11 @@ entry = TAILQ_FIRST(&hrState_g.hr_device_tbl); while (entry != NULL) { entry_tmp = TAILQ_NEXT(entry, link); + /* + * If HR_DEVICE_IMMUTABLE bit is set then this means that + * this entry was not detected by the above hrDevice_OS_get_devices_v() + * call. So we are not deleting it there. + */ if (!(entry->flags & HR_DEVICE_FOUND) && !(entry->flags & HR_DEVICE_IMMUTABLE) ) hrDeviceTblEntry_delete_v(entry); entry = entry_tmp; ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_diskstorage_tbl.c#2 (text+ko) ==== @@ -44,6 +44,8 @@ #include <err.h> #include <sys/sysctl.h> #include <sys/ata.h> +#include <errno.h> +#include <sys/disk.h> extern struct hrDeviceTblEntry * @@ -101,18 +103,6 @@ return (NULL); } -static -void hrDiskStorageTbl_fill_entry(const char* dev_name, struct hrDiskStorageTblEntry* entry) { - assert(dev_name != NULL); - assert(entry != NULL); - if (dev_name == NULL || entry == NULL) - return; - /*FIX ME*/ - entry->access = DS_READ_WRITE; - entry->media = DSM_UNKNOWN; - entry->removable = SNMP_FALSE; - entry->capacity = 0; -} static struct hrDeviceTblEntry* @@ -210,20 +200,59 @@ static +void hrDiskStorage_queryDisk(struct hrDiskStorageTblEntry *entry){ + char dev_path[128] = ""; + int fd = -1; + if (entry == NULL) { + return; + } + if( entry->dev_name[0] == '\0' ) { + return; + } + snprintf(dev_path, sizeof(dev_path) - 1, "/dev/%s", entry->dev_name); + entry->capacity = 0; + HR_DPRINTF((stderr,"OPENING device %s\n",dev_path)); + fd = open(dev_path, O_RDONLY|O_NONBLOCK); + if ( fd < 0 ) { + HR_DPRINTF((stderr,"OPEN device %s failed: %d (%s)\n", + dev_path, + errno, + strerror(errno))); + goto QUERY_DONE; + } else { + off_t mediasize = 0; + if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) { + HR_DPRINTF((stderr,"DIOCGMEDIASIZE for device %s failed: %d (%s)\n", + dev_path, + errno, + strerror(errno))); + goto QUERY_DONE; + } + mediasize = mediasize / 1024; + entry->capacity = (mediasize > (off_t)INT_MAX ? INT_MAX : mediasize); + } +QUERY_DONE: + if (fd > 0) { + close(fd); + } + +} +static void hrDiskStorage_OS_get_ATA_disks_v(void) { struct hrDeviceTblEntry *entry = NULL; struct hrDiskStorageTblEntry *disk_entry = NULL; struct ata_cmd iocmd; - int fd, maxunit, unit, i; - - if ((fd = open("/dev/ata", O_RDWR)) < 0) { - syslog(LOG_ERR, "open /dev/ata failed:%m"); - return; + 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; + } } iocmd.cmd = ATAGMAXCHANNEL; - if (ioctl(fd, IOCATA, &iocmd) < 0) { + if (ioctl(hrState_g.ata_fd, IOCATA, &iocmd) < 0) { syslog(LOG_ERR, "ioctl(ATAGMAXCHANNEL) failed: %m"); goto ATA_DONE; } @@ -235,7 +264,7 @@ u_iocmd.channel = unit; u_iocmd.device = -1; u_iocmd.cmd = ATAGPARM; - if (ioctl(fd, IOCATA, &u_iocmd) < 0) { + if (ioctl(hrState_g.ata_fd, IOCATA, &u_iocmd) < 0) { syslog(LOG_ERR, "ioctl(IOCATA) failed: %m"); continue; } @@ -244,72 +273,78 @@ if (!u_iocmd.u.param.type[i]) { continue; } - + /*first check if this disk is in hrDeiveTable*/ entry = hrDeviceTblEntry_find_by_name(u_iocmd.u.param.name[i]); if (entry == NULL) { HR_DPRINTF((stderr, "hrDiskStorageTable; disk %s not in hrDeviceTable\n", u_iocmd.u.param.name[i] )); entry = hrDeviceTblEntry_create(u_iocmd.u.param.name[i]); + 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: <%s, ser. %s> ", + u_iocmd.u.param.name[i], + u_iocmd.u.param.params[i].model, + u_iocmd.u.param.params[i].serial); + }else{ HR_DPRINTF((stderr, "hrDiskStorageTable; disk %s already in hrDeviceTable\n", u_iocmd.u.param.name[i] )); } - if (entry == NULL) - continue; entry->flags |= HR_DEVICE_IMMUTABLE; - 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: <%s, %s> ", - u_iocmd.u.param.name[i], - u_iocmd.u.param.params[i].model, - u_iocmd.u.param.params[i].serial); - + 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); + assert(disk_entry != NULL); + if (disk_entry == NULL) { + continue; + } + disk_entry->access = DS_READ_WRITE; + memset(&disk_entry->dev_name[0], 0, sizeof(disk_entry->dev_name)); + strncpy(disk_entry->dev_name, u_iocmd.u.param.name[i], sizeof(disk_entry->dev_name)-1); - disk_entry->flags |= HR_DISKSTORAGE_FOUND; - disk_entry->access = DS_READ_WRITE; - - switch (u_iocmd.u.param.params[i].config & ATA_ATAPI_TYPE_MASK) { - case ATA_ATAPI_TYPE_DIRECT: + if (strncmp(disk_entry->dev_name, "ad", strlen("ad")) == 0) { disk_entry->media = DSM_HARDDISK; - break; - case ATA_ATAPI_TYPE_TAPE: + disk_entry->removable = SNMP_FALSE; + } else if (strncmp(disk_entry->dev_name, "ar", strlen("ar")) == 0) { disk_entry->media = DSM_OTHER; - break; - - case ATA_ATAPI_TYPE_CDROM: + disk_entry->removable = SNMP_FALSE; + } else if (strncmp(disk_entry->dev_name, "acd", strlen("acd")) == 0) { disk_entry->media = DSM_OPTICALDISKROM; - disk_entry->access = DS_READ_ONLY; - break; - case ATA_ATAPI_TYPE_OPTICAL: - disk_entry->media = DSM_OPTICALDISKRW; - break; - default: + disk_entry->removable = SNMP_TRUE; + } else if (strncmp(disk_entry->dev_name, "afd", strlen("afd")) == 0) { + disk_entry->media = DSM_FLOPPYDISK; + disk_entry->removable = SNMP_TRUE; + } else if (strncmp(disk_entry->dev_name, "ast", strlen("ast")) == 0) { + disk_entry->media = DSM_OTHER; + disk_entry->removable = SNMP_TRUE; + } else { disk_entry->media = DSM_UNKNOWN; - break; + disk_entry->removable = SNMP_FALSE; + } } - - disk_entry->removable = - (u_iocmd.u.param.params[i].removable_status > 0 ? SNMP_TRUE : SNMP_FALSE ); - - disk_entry->capacity = 0; + disk_entry->flags |= HR_DISKSTORAGE_FOUND; + disk_entry->flags |= HR_DISKSTORAGE_ATA; + + hrDiskStorage_queryDisk(disk_entry); + disk_entry->r_tick = this_tick; } } ATA_DONE: - if (close(fd) < 0) { - syslog(LOG_ERR, "close(/dev/ata) failed: %m"); - } + return; } static @@ -387,8 +422,35 @@ disk_entry = hrDiskStorageEntry_create(entry); } assert(disk_entry != NULL); - hrDiskStorageTbl_fill_entry(disk, disk_entry); disk_entry->flags |= HR_DISKSTORAGE_FOUND; + + if ( (disk_entry->flags & HR_DISKSTORAGE_ATA) == HR_DISKSTORAGE_ATA ) { + /*ATA detection is running before this one, so don't waste the time here */ + continue; + } + + disk_entry->access = DS_READ_WRITE; + disk_entry->media = DSM_UNKNOWN; + disk_entry->removable = SNMP_FALSE; + + + if (strncmp(disk_entry->dev_name, "da", strlen("da")) == 0) { + disk_entry->media = DSM_HARDDISK; + disk_entry->removable = SNMP_FALSE; + } else if (strncmp(disk_entry->dev_name, "cd", strlen("cd")) == 0) { + disk_entry->media = DSM_OPTICALDISKROM; + disk_entry->removable = SNMP_TRUE; + } else { + disk_entry->media = DSM_UNKNOWN; + disk_entry->removable = SNMP_FALSE; + } + + memset(&disk_entry->dev_name[0], 0, sizeof(disk_entry->dev_name)); + strncpy(disk_entry->dev_name, disk, sizeof(disk_entry->dev_name)-1); + + hrDiskStorage_queryDisk(disk_entry); + disk_entry->r_tick = this_tick; + } @@ -437,9 +499,9 @@ entry->flags &= ~HR_DISKSTORAGE_FOUND; } + hrDiskStorage_OS_get_ATA_disks_v(); /*this must be called first !*/ hrDiskStorage_OS_get_disks_v(); - hrDiskStorage_OS_get_ATA_disks_v(); /* * Purge items items that disappeared ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#13 (text+ko) ==== @@ -90,7 +90,8 @@ syslog(LOG_ERR, "kvm_open failed: %m "); return (-1); /*error*/ } - + hrState_g.ata_fd = -1; + hrState_g.dev_root = NULL; hrState_g.hrSWOSIndex = 0; @@ -153,7 +154,7 @@ } hrState_g.devd_fd = NULL; - + HR_DPRINTF((stderr, "[%s] done.\n", __func__)); return (0); } @@ -234,7 +235,13 @@ hrState_g.hw_ncpu = 0; hrState_g.fscale = 0; hrState_g.ccpu = 0; - + + if (hrState_g.ata_fd > 0) { + if (close(hrState_g.ata_fd) < 0) { + syslog(LOG_ERR,"close (/dev/ata) failed: %m"); + } + hrState_g.ata_fd = -1; + } if( host_registration_id > 0){ or_unregister(host_registration_id); } @@ -248,6 +255,7 @@ */ static void hostres_idle_v(void) { +#if 0 if ( (time(NULL) - hrState_g.hrStorage_tbl_age) > HR_STORAGE_TBL_REFRESH ) { HR_DPRINTF((stderr, "%s: hrStorageTable needs refresh\n ", __func__)); refresh_hrStorage_tbl_v(); @@ -261,14 +269,15 @@ HR_DPRINTF((stderr, "%s: hrSWRunTable needs refresh\n ", __func__)); refresh_hrSWRun_tbl_v(); } -/* + if ( (time(NULL) - hrState_g.hrDevice_tbl_age) > HR_DEVICE_TBL_REFRESH ) { HR_DPRINTF((stderr, "%s: hrDeviceTable needs refresh\n ", __func__)); refresh_hrDevice_tbl_v(); } -*/ + HR_DPRINTF((stderr, "[%s] done.\n ", __func__)); +#endif } /* ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#17 (text+ko) ==== @@ -203,7 +203,7 @@ /* * This structure is used to hold a SNMP table entry * for HOST-RESOURCES-MIB's hrProcessorTable - * Note that index is external being allocated & mainatined + * Note that index is external being allocated & maintained * by the hrDeviceTable code. */ @@ -239,6 +239,14 @@ DSM_RAMDISK = 8 }; +/* + * This structure is used to hold a SNMP table entry + * for HOST-RESOURCES-MIB's hrDiskStorageTable + * Note that index is external being allocated & maintained + * by the hrDeviceTable code. + * NOTE: according to MIB removable means removable media, not the + * device itself (like a USB card reader) + */ struct hrDiskStorageTblEntry { int32_t index; int32_t access; /*enum hrDiskStrorageAccess */ @@ -247,9 +255,11 @@ int32_t capacity; TAILQ_ENTRY(hrDiskStorageTblEntry) link; /*next items are not from the SNMP mib table, only to be used internally*/ -#define HR_DISKSTORAGE_FOUND 0x001 +#define HR_DISKSTORAGE_FOUND 0x001 +#define HR_DISKSTORAGE_ATA 0x002 /*belongs to the ATA subsystem*/ uint32_t flags; uint64_t r_tick; + u_char dev_name[32]; /*device name, i.e. "ad4" or "acd0"*/ }; @@ -402,6 +412,7 @@ time_t hrDiskStorage_age; char* disk_list; /*returned by sysctl("kern.disks")*/ size_t disk_list_len; + int ata_fd; /* fd for "/dev/ata"*/ };
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200507311703.j6VH3MLo087933>