Device.cpp 9.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/******************************************************************************
 *                      SCSI/Device.cpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * 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
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/
24
25
26
27

#include <cstdlib>
#include <scsi/sg.h>

28
#include "Device.hpp"
29

30
31
using namespace castor::tape;

32
33
34
35
36
37
/**
 * Fill up the array that the device list is with all the system's
 * SCSI devices information.
 * 
 * (all code using templates must be in the header file)
 */
38
SCSI::DeviceVector::DeviceVector(System::virtualWrapper& sysWrapper) : m_sysWrapper(sysWrapper) {
39
  std::string sysDevsPath = "/sys/bus/scsi/devices";
40
  Utils::regex ifFirstCharIsDigit("^[[:digit:]]");
41
  std::vector<std::string> checkResult;
42
  DIR* dirp = m_sysWrapper.opendir(sysDevsPath.c_str());
43
  if (!dirp) throw Exceptions::Errnum("Error opening sysfs scsi devs");
44
45
46
  while (struct dirent * dent = m_sysWrapper.readdir(dirp)) {
    std::string dn(dent->d_name);
    if ("." == dn || ".." == dn) continue;
47
48
    checkResult = ifFirstCharIsDigit.exec(dn);
    if (0 == checkResult.size()) continue; /* we only use digital names like 6:0:3:0 */
49
50
51
52
    std::string fullpath = sysDevsPath + "/" + std::string(dent->d_name);
    /* We expect only symbolic links in this directory, */
    char rp[PATH_MAX];
    if (NULL == m_sysWrapper.realpath(fullpath.c_str(), rp))
53
      throw Exceptions::Errnum("Could not find realpath for " + fullpath);
54
55
56
57
58
59
60
61
    this->push_back(getDeviceInfo(rp));
  }
  sysWrapper.closedir(dirp);
}

std::string SCSI::DeviceVector::readfile(std::string path) {
  int fd = m_sysWrapper.open(path.c_str(), 0);
  if (-1 == fd) {
62
    throw Exceptions::Errnum("Could not open file " + path);
63
64
65
66
67
  }
  char buf[readfileBlockSize];
  std::string ret;
  while (ssize_t sread = m_sysWrapper.read(fd, buf, readfileBlockSize)) {
    if (-1 == sread)
68
      throw Exceptions::Errnum("Could not read from open file " + path);
69
70
71
    ret.append(buf, sread);
  }
  if (m_sysWrapper.close(fd))
72
    throw Exceptions::Errnum("Error closing file " + path);
73
74
75
76
77
78
79
  return ret;
}

SCSI::DeviceInfo::DeviceFile SCSI::DeviceVector::readDeviceFile(std::string path) {
  DeviceInfo::DeviceFile ret;
  std::string file = readfile(path);
  if (!::sscanf(file.c_str(), "%d:%d\n", &ret.major, &ret.minor))
80
    throw Exception(std::string("Could not parse file: ") + path);
81
82
83
84
85
86
  return ret;
}

SCSI::DeviceInfo::DeviceFile SCSI::DeviceVector::statDeviceFile(std::string path) {
  struct stat sbuf;
  if (m_sysWrapper.stat(path.c_str(), &sbuf))
87
    throw Exceptions::Errnum("Could not stat file " + path);
88
  if (!S_ISCHR(sbuf.st_mode))
89
    throw Exception("Device file " + path + " is not a character device");
90
91
92
93
94
95
96
97
98
99
100
101
102
  DeviceInfo::DeviceFile ret;
  ret.major = major(sbuf.st_rdev);
  ret.minor = minor(sbuf.st_rdev);
  return ret;
}

/**
 * Part factored out of getDeviceInfo: get the tape specifics
 * from sysfs.
 * @param devinfo
 */
void SCSI::DeviceVector::getTapeInfo(DeviceInfo & devinfo) {
  /* Find the st and nst devices for this SCSI device */
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  DIR * dirp; 
  /* SLC6 default tapeDir and scsiPrefix */
  std::string tapeDir="/scsi_tape"; /* The name of the tape dir inside of
                                     * devinfo.sysfs_entry. /scsi_tape/ on SLC6.
                                     */
  std::string scsiPrefix="^";        /* The prefix for the name for the tape 
                                      * device name for the regexp.
                                      * scsi_tape: on SLC5.
                                      */ 
  /* we try to open /scsi_tape first for SLC6 */
  dirp = m_sysWrapper.opendir((devinfo.sysfs_entry+tapeDir).c_str());
  
  if (!dirp) {
    /* here we open sysfs_entry for SLC5 */
    dirp = m_sysWrapper.opendir(devinfo.sysfs_entry.c_str());
118
    if (!dirp) throw Exceptions::Errnum(
119
120
121
122
123
124
125
126
127
      std::string("Error opening tape device directory ") +
      devinfo.sysfs_entry + tapeDir+" or "+devinfo.sysfs_entry);
    else {
      /* we are not on SLC6 */
      scsiPrefix="^scsi_tape:"; 
      tapeDir ="";
    }
  } 
  
128
129
  Utils::regex st_re((scsiPrefix+"(st[[:digit:]]+)$").c_str());
  Utils::regex nst_re((scsiPrefix+"(nst[[:digit:]]+)$").c_str());
130
  
131
132
133
  while (struct dirent * dent = m_sysWrapper.readdir(dirp)) {
    std::vector<std::string> res;
    /* Check if it's the st information */
134
    res = st_re.exec(dent->d_name);
135
136
    if (res.size()) {
      if (!devinfo.st_dev.size()) {
137
        devinfo.st_dev = std::string("/dev/") + res[1];     
138
      } else
139
        throw Exception("Matched st device several times!");
140
      /* Read the major and major number */
141
      devinfo.st = readDeviceFile(devinfo.sysfs_entry + tapeDir+ "/"
142
143
144
145
146
147
148
149
150
151
          + std::string(dent->d_name) + "/dev");
      /* Check the actual device file */
      DeviceInfo::DeviceFile realFile = statDeviceFile(devinfo.st_dev);
      if (devinfo.st != realFile) {
        std::stringstream err;
        err << "Mismatch between sysfs info and actual device file: "
            << devinfo.sysfs_entry + "/" + dent->d_name << " indicates "
            << devinfo.st.major << ":" << devinfo.st.minor
            << " while " << devinfo.st_dev << " is: "
            << realFile.major << ":" << realFile.minor;
152
        throw Exception(err.str());
153
154
155
      }
    }
    /* Check if it's the nst information */
156
    res = nst_re.exec(dent->d_name);
157
158
159
160
    if (res.size()) {
      if (!devinfo.nst_dev.size()) {
        devinfo.nst_dev = std::string("/dev/") + res[1];
      } else
161
        throw Exception("Matched nst device several times!");
162
      /* Read the major and major number */
163
      devinfo.nst = readDeviceFile(devinfo.sysfs_entry + tapeDir + "/"
164
165
166
167
168
169
170
171
172
173
          + std::string(dent->d_name) + "/dev");
      /* Check the actual device file */
      DeviceInfo::DeviceFile realFile = statDeviceFile(devinfo.nst_dev);
      if (devinfo.nst != realFile) {
        std::stringstream err;
        err << "Mismatch between sysfs info and actual device file: "
            << devinfo.sysfs_entry + "/" + dent->d_name << " indicates "
            << devinfo.nst.major << ":" << devinfo.nst.minor
            << " while " << devinfo.st_dev << " is: "
            << realFile.major << ":" << realFile.minor;
174
        throw Exception(err.str());
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
      }
    }
  }
  m_sysWrapper.closedir(dirp);
}

/**
 * Extract information from sysfs about a SCSI device.
 * @param path Path to the directory with information about 
 * @return 
 */
SCSI::DeviceInfo SCSI::DeviceVector::getDeviceInfo(const char * path) {
  DeviceInfo ret;
  ret.sysfs_entry = path;
  std::string buf;
  /* Get device type */
  {
    buf = readfile(ret.sysfs_entry + "/type");
    if (!sscanf(buf.c_str(), "%d", &ret.type))
194
      throw Exception(std::string("Could not parse file: ") + ret.sysfs_entry + "/type");
195
  }
Eric Cano's avatar
Eric Cano committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  /* Get vendor (trimmed of trailing newline, not of spaces) */
  {
    buf = readfile(ret.sysfs_entry + "/vendor");
    size_t endpos  = buf.find_first_not_of("\n");
    if (std::string::npos != endpos) endpos++;
    ret.vendor = buf.substr(0, endpos);
  }
  /* Get model (trimmed of trailing newline, not of spaces) */
  {
    buf = readfile(ret.sysfs_entry + "/model");
    size_t endpos  = buf.find_first_not_of("\n");
    if (std::string::npos != endpos) endpos++;
    ret.product = buf.substr(0, endpos);
  }
  /* Get revision (trimmed of trailing newline, not of spaces) */
  {
    buf = readfile(ret.sysfs_entry + "/rev");
    size_t endpos  = buf.find_first_not_of("\n");
    if (std::string::npos != endpos) endpos++;
    ret.productRevisionLevel = buf.substr(0, endpos);
  }
217
218
219
220
221
  /* Get name of sg device */
  {
    char rl[PATH_MAX];
    std::string lp = ret.sysfs_entry + "/generic";
    if (-1 == m_sysWrapper.readlink(lp.c_str(), rl, sizeof (rl)))
222
      throw Exceptions::Errnum("Could not read link " + lp);
223
224
225
    std::string gl(rl);
    size_t pos = gl.find_last_of("/");
    if (pos == std::string::npos)
226
      throw Exception(std::string("Could not find last / in link: ") + gl +
227
228
229
230
231
232
233
234
235
236
237
238
239
240
        " read from " + ret.sysfs_entry + "/generic");
    ret.sg_dev = std::string("/dev/") + gl.substr(pos + 1);
  }
  /* Get the major and minor number of the device file */
  ret.sg = readDeviceFile(ret.sysfs_entry + "/generic/dev");
  /* Check that we have an agreement with the actual device file */
  DeviceInfo::DeviceFile realFile = statDeviceFile(ret.sg_dev);
  if (ret.sg != realFile) {
    std::stringstream err;
    err << "Mismatch between sysfs info and actual device file: "
        << ret.sysfs_entry + "/generic/dev" << " indicates "
        << ret.sg.major << ":" << ret.sg.minor
        << " while " << ret.sg_dev << " is: "
        << realFile.major << ":" << realFile.minor;
241
    throw Exception(err.str());
242
243
244
245
246
247
  }
  /* Handle more if we have a tape device */
  if (Types::tape == ret.type)
    getTapeInfo(ret);
  return ret;
}