📄 os_freebsd.cpp
字号:
/* * os_freebsd.c * * Home page of code is: http://smartmontools.sourceforge.net * * Copyright (C) 2003-8 Eduard Martinescu <smartmontools-support@lists.sourceforge.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * You should have received a copy of the GNU General Public License * (for example COPYING); if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <stdio.h>#include <sys/types.h>#include <dirent.h>#include <fcntl.h>#include <err.h>#include <camlib.h>#include <cam/scsi/scsi_message.h>#if defined(__DragonFly__)#include <sys/nata.h>#else#include <sys/ata.h>#endif#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <glob.h>#include <fcntl.h>#include <stddef.h>#include "config.h"#include "int64.h"#include "atacmds.h"#include "scsicmds.h"#include "cciss.h"#include "utility.h"#include "extern.h"#include "os_freebsd.h"static const char *filenameandversion="$Id: os_freebsd.cpp,v 1.58 2008/03/04 22:09:47 ballen4705 Exp $";const char *os_XXXX_c_cvsid="$Id: os_freebsd.cpp,v 1.58 2008/03/04 22:09:47 ballen4705 Exp $" \ATACMDS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_FREEBSD_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID;// to hold onto exit code for atexit routineextern int exitstatus;extern smartmonctrl * con;// Private table of open devices: guaranteed zero on startup since// part of static data.struct freebsd_dev_channel *devicetable[FREEBSD_MAXDEV];// forward declarationstatic int parse_ata_chan_dev(const char * dev_name, struct freebsd_dev_channel *ch);// print examples for smartctlvoid print_smartctl_examples(){ printf("=================================================== SMARTCTL EXAMPLES =====\n\n");#ifdef HAVE_GETOPT_LONG printf( " smartctl -a /dev/ad0 (Prints all SMART information)\n\n" " smartctl --smart=on --offlineauto=on --saveauto=on /dev/ad0\n" " (Enables SMART on first disk)\n\n" " smartctl -t long /dev/ad0 (Executes extended disk self-test)\n\n" " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/ad0\n" " (Prints Self-Test & Attribute errors)\n" " (Prints Self-Test & Attribute errors)\n\n" " smartctl -a --device=3ware,2 /dev/twa0\n" " smartctl -a --device=3ware,2 /dev/twe0\n" " (Prints all SMART information for ATA disk on\n" " third port of first 3ware RAID controller)\n" );#else printf( " smartctl -a /dev/ad0 (Prints all SMART information)\n" " smartctl -s on -o on -S on /dev/ad0 (Enables SMART on first disk)\n" " smartctl -t long /dev/ad0 (Executes extended disk self-test)\n" " smartctl -A -l selftest -q errorsonly /dev/ad0\n" " (Prints Self-Test & Attribute errors)\n" " smartctl -a -d 3ware,2 /dev/twa0\n" " smartctl -a -d 3ware,2 /dev/twe0\n" );#endif return;}// Like open(). Return positive integer handle, used by functions below only. mode=="ATA" or "SCSI".int deviceopen (const char* dev, __unused char* mode) { struct freebsd_dev_channel *fdchan; int parse_ok, i; // Search table for a free entry for (i=0; i<FREEBSD_MAXDEV; i++) if (!devicetable[i]) break; // If no free entry found, return error. We have max allowed number // of "file descriptors" already allocated. if (i==FREEBSD_MAXDEV) { errno=EMFILE; return -1; } fdchan = (struct freebsd_dev_channel *)calloc(1,sizeof(struct freebsd_dev_channel)); if (fdchan == NULL) { // errno already set by call to malloc() return -1; } parse_ok = parse_ata_chan_dev (dev,fdchan); if (parse_ok == CONTROLLER_UNKNOWN) { free(fdchan); errno = ENOTTY; return -1; // can't handle what we don't know } if (parse_ok == CONTROLLER_ATA) {#ifdef IOCATAREQUEST if ((fdchan->device = open(dev,O_RDONLY))<0) {#else if ((fdchan->atacommand = open("/dev/ata",O_RDWR))<0) {#endif int myerror = errno; //preserve across free call free (fdchan); errno = myerror; return -1; } } if (parse_ok == CONTROLLER_3WARE_678K_CHAR) { char buf[512]; sprintf(buf,"/dev/twe%d",fdchan->device);#ifdef IOCATAREQUEST if ((fdchan->device = open(buf,O_RDWR))<0) {#else if ((fdchan->atacommand = open(buf,O_RDWR))<0) {#endif int myerror = errno; // preserver across free call free(fdchan); errno=myerror; return -1; } } if (parse_ok == CONTROLLER_3WARE_9000_CHAR) { char buf[512]; sprintf(buf,"/dev/twa%d",fdchan->device);#ifdef IOCATAREQUEST if ((fdchan->device = open(buf,O_RDWR))<0) {#else if ((fdchan->atacommand = open(buf,O_RDWR))<0) {#endif int myerror = errno; // preserver across free call free(fdchan); errno=myerror; return -1; } } if (parse_ok == CONTROLLER_CCISS) { if ((fdchan->device = open(dev,O_RDWR))<0) { int myerror = errno; // preserver across free call free(fdchan); errno=myerror; return -1; } } if (parse_ok == CONTROLLER_SCSI) { // this is really a NO-OP, as the parse takes care // of filling in correct details } // return pointer to "file descriptor" table entry, properly offset. devicetable[i]=fdchan; return i+FREEBSD_FDOFFSET;}// Returns 1 if device not available/open/found else 0. Also shifts fd into valid range.static int isnotopen(int *fd, struct freebsd_dev_channel** fdchan) { // put valid "file descriptor" into range 0...FREEBSD_MAXDEV-1 *fd -= FREEBSD_FDOFFSET; // check for validity of "file descriptor". if (*fd<0 || *fd>=FREEBSD_MAXDEV || !((*fdchan)=devicetable[*fd])) { errno = ENODEV; return 1; } return 0;}// Like close(). Acts on handles returned by above function.int deviceclose (int fd) { struct freebsd_dev_channel *fdchan; int failed = 0; // check for valid file descriptor if (isnotopen(&fd, &fdchan)) return -1; // did we allocate a SCSI device name? if (fdchan->devname) free(fdchan->devname); // close device, if open#ifdef IOCATAREQUEST if (fdchan->device) failed=close(fdchan->device);#else if (fdchan->atacommand) failed=close(fdchan->atacommand);#endif if (fdchan->scsicontrol) failed=close(fdchan->scsicontrol); // if close succeeded, then remove from device list // Eduard, should we also remove it from list if close() fails? I'm // not sure. Here I only remove it from list if close() worked. if (!failed) { free(fdchan); devicetable[fd]=NULL; } return failed;}#define NO_RETURN 0#define BAD_SMART 1#define NO_DISK_3WARE 2#define BAD_KERNEL 3#define MAX_MSG 3// Utility function for printing warningsvoid printwarning(int msgNo, const char* extra) { static int printed[] = {0,0,0,0}; static const char* message[]={ "The SMART RETURN STATUS return value (smartmontools -H option/Directive)\n can not be retrieved with this version of ATAng, please do not rely on this value\nYou should update to at least 5.2\n", "Error SMART Status command failed\nPlease get assistance from \n" PACKAGE_HOMEPAGE "\nRegister values returned from SMART Status command are:\n", "You must specify a DISK # for 3ware drives with -d 3ware,<n> where <n> begins with 1 for first disk drive\n", "ATA support is not provided for this kernel version. Please ugrade to a recent 5-CURRENT kernel (post 09/01/2003 or so)\n" }; if (msgNo >= 0 && msgNo <= MAX_MSG) { if (!printed[msgNo]) { printed[msgNo] = 1; pout("%s", message[msgNo]); if (extra) pout("%s",extra); } } return;}// Interface to ATA devices. See os_linux.cint marvell_command_interface(__unused int fd, __unused smart_command_set command, __unused int select, __unused char *data) { return -1;}int highpoint_command_interface(__unused int fd, __unused smart_command_set command, __unused int select, __unused char *data) {{ return -1;}int ata_command_interface(int fd, smart_command_set command, int select, char *data) {#if !defined(ATAREQUEST) && !defined(IOCATAREQUEST) // sorry, but without ATAng, we can't do anything here printwarning(BAD_KERNEL,NULL); errno = ENOSYS; return -1;#else struct freebsd_dev_channel* con; int retval, copydata=0;#ifdef IOCATAREQUEST struct ata_ioc_request request;#else struct ata_cmd iocmd;#endif unsigned char buff[512]; // check that "file descriptor" is valid if (isnotopen(&fd,&con)) return -1; bzero(buff,512);#ifdef IOCATAREQUEST bzero(&request,sizeof(struct ata_ioc_request));#else bzero(&iocmd,sizeof(struct ata_cmd));#endif bzero(buff,512);#ifndef IOCATAREQUEST iocmd.cmd=ATAREQUEST; iocmd.channel=con->channel; iocmd.device=con->device;#define request iocmd.u.request#endif request.u.ata.command=ATA_SMART_CMD; request.timeout=600; switch (command){ case READ_VALUES: request.u.ata.feature=ATA_SMART_READ_VALUES; request.u.ata.lba=0xc24f<<8; request.flags=ATA_CMD_READ; request.data=(char *)buff; request.count=512; copydata=1; break; case READ_THRESHOLDS: request.u.ata.feature=ATA_SMART_READ_THRESHOLDS; request.u.ata.count=1; request.u.ata.lba=1|(0xc24f<<8); request.flags=ATA_CMD_READ; request.data=(char *)buff; request.count=512; copydata=1; break; case READ_LOG: request.u.ata.feature=ATA_SMART_READ_LOG_SECTOR; request.u.ata.lba=select|(0xc24f<<8); request.u.ata.count=1; request.flags=ATA_CMD_READ; request.data=(char *)buff; request.count=512; copydata=1; break; case IDENTIFY: request.u.ata.command=ATA_IDENTIFY_DEVICE; request.flags=ATA_CMD_READ; request.data=(char *)buff; request.count=512; copydata=1; break; case PIDENTIFY: request.u.ata.command=ATA_IDENTIFY_PACKET_DEVICE; request.flags=ATA_CMD_READ; request.data=(char *)buff; request.count=512; copydata=1; break; case ENABLE: request.u.ata.feature=ATA_SMART_ENABLE; request.u.ata.lba=0xc24f<<8; request.flags=ATA_CMD_CONTROL; break; case DISABLE: request.u.ata.feature=ATA_SMART_DISABLE; request.u.ata.lba=0xc24f<<8; request.flags=ATA_CMD_CONTROL; break; case AUTO_OFFLINE: // NOTE: According to ATAPI 4 and UP, this command is obsolete request.u.ata.feature=ATA_SMART_AUTO_OFFLINE; request.u.ata.lba=0xc24f<<8; request.u.ata.count=select; request.flags=ATA_CMD_CONTROL; break; case AUTOSAVE: request.u.ata.feature=ATA_SMART_AUTOSAVE; request.u.ata.lba=0xc24f<<8;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -