CleanerSession.cpp 12.9 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
24
25
26
27
28
29
/******************************************************************************
 *
 * 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
 *****************************************************************************/

#include "castor/tape/tapeserver/daemon/CleanerSession.hpp"

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::CleanerSession::CleanerSession(
30
  server::ProcessCap &capUtils,
31
  mediachanger::MediaChangerFacade &mc,
32
  castor::log::Logger &log,
33
  const DriveConfig &driveConfig,
34
  System::virtualWrapper &sysWrapper,
35
  const std::string &vid,
36
37
  const bool waitMediaInDrive,
  const uint32_t waitMediaInDriveTimeout):
38
  m_capUtils(capUtils),
39
  m_mc(mc),
40
41
  m_log(log),
  m_driveConfig(driveConfig),
42
  m_sysWrapper(sysWrapper),
43
  m_vid(vid),
44
45
  m_waitMediaInDrive(waitMediaInDrive),
  m_waitMediaInDriveTimeout(waitMediaInDriveTimeout) {
46
47
48
}

//------------------------------------------------------------------------------
49
// execute
50
//------------------------------------------------------------------------------
51
castor::tape::tapeserver::daemon::Session::EndOfSessionAction
52
  castor::tape::tapeserver::daemon::CleanerSession::execute() throw() {
53
54
55
56
57
58
59
60
61
62
63
64
65
  std::string errorMessage;

  try {
    return exceptionThrowingExecute();
  } catch(castor::exception::Exception &ex) {
    errorMessage = ex.getMessage().str();
  } catch(std::exception &se) {
    errorMessage = se.what();
  } catch(...) {
    errorMessage = "Caught an unknown exception";
  }

  // Reaching this point means the cleaner failed and an exception was thrown
66
  std::list<log::Param> params = {
67
    log::Param("TPVID", m_vid),
68
    log::Param("unitName", m_driveConfig.getUnitName()),
69
70
71
72
73
74
75
76
77
78
79
80
81
    log::Param("message", errorMessage)};
  m_log(LOG_ERR, "Cleaner failed", params);
  return MARK_DRIVE_AS_DOWN;
}

//------------------------------------------------------------------------------
// exceptionThrowingExecute
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::Session::EndOfSessionAction
  castor::tape::tapeserver::daemon::CleanerSession::exceptionThrowingExecute() {

  setProcessCapabilities("cap_sys_rawio+ep");

82
  std::unique_ptr<drive::DriveInterface> drivePtr = createDrive();
83
  drive::DriveInterface &drive = *drivePtr.get();
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  try {
    cleanDrive(drive);
  } catch(...) {
    logAndClearTapeAlerts(drive);
    throw;
  }
  
  logAndClearTapeAlerts(drive);
  return MARK_DRIVE_AS_UP;
}

//------------------------------------------------------------------------------
// cleanDrive
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::cleanDrive(drive::DriveInterface &drive) {
100
101
102
  if(m_waitMediaInDrive) {
    waitUntilMediaIsReady(drive);
  }
103

104
  if(!drive.hasTapeInPlace()) {
105
106
107
    std::list<log::Param> params;
    params.push_back(log::Param("TPVID", m_vid));
    params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
108
    m_log(LOG_INFO, "Cleaner found tape drive empty", params);
109
    return; //return immediately if there is no tape
110
111
  }

112
  rewindDrive(drive);
113

114
  checkTapeContainsData(drive);
115

116
  const std::string volumeLabelVSN = checkVolumeLabel(drive);
117

118
  unloadTape(volumeLabelVSN, drive);
119
120

  dismountTape(volumeLabelVSN);
121
  drive.disableLogicalBlockProtection();
122
}
123

124
125
126
127
128
129
130
131
132
133
134
135
136
//------------------------------------------------------------------------------
// logAndClearTapeAlerts
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::logAndClearTapeAlerts(drive::DriveInterface &drive) throw() {
  std::string errorMessage;
  try{
    std::vector<uint16_t> tapeAlertCodes = drive.getTapeAlertCodes();
    if (!tapeAlertCodes.empty()) {
      size_t alertNumber = 0;
      // Log tape alerts in the logs.
      std::vector<std::string> tapeAlerts = drive.getTapeAlerts(tapeAlertCodes);
      for (std::vector<std::string>::iterator ta=tapeAlerts.begin(); ta!=tapeAlerts.end();ta++)
      {
137
        std::list<log::Param> params = {
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
          log::Param("tapeAlert",*ta),
          log::Param("tapeAlertNumber", alertNumber++),
          log::Param("tapeAlertCount", tapeAlerts.size())};
        m_log(LOG_WARNING, "Tape alert detected",params);
      }
    }
    return;
  } catch(castor::exception::Exception &ex) {
    errorMessage = ex.getMessage().str();
  } catch(std::exception &se) {
    errorMessage = se.what();
  } catch(...) {
    errorMessage = "Caught an unknown exception";
  }

  // Reaching this point means it failed and an exception was thrown (because of the "return" above)
154
  std::list<log::Param> params = {
155
156
157
158
    log::Param("TPVID", m_vid),
    log::Param("unitName", m_driveConfig.getUnitName()),
    log::Param("message", errorMessage)};
  m_log(LOG_ERR, "Cleaner failed getting tape alerts from the drive", params);  
159
160
161
162
163
164
165
166
}

//------------------------------------------------------------------------------
// setProcessCapabilities
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::setProcessCapabilities(
  const std::string &capabilities) {
  m_capUtils.setProcText(capabilities);
167
  {
168
    std::list<log::Param> params = {
169
      log::Param("capabilities", m_capUtils.getProcText())};
170
    m_log(LOG_INFO, "Cleaner set process capabilities for using tape",
171
172
      params);
  }
173
}
174

175
176
177
//------------------------------------------------------------------------------
// createDrive
//------------------------------------------------------------------------------
178
std::unique_ptr<castor::tape::tapeserver::drive::DriveInterface>
179
  castor::tape::tapeserver::daemon::CleanerSession::createDrive() {
180
  SCSI::DeviceVector dv(m_sysWrapper);
181
  SCSI::DeviceInfo driveInfo = dv.findBySymlink(m_driveConfig.getDevFilename());
182
183
  
  // Instantiate the drive object
184
  std::unique_ptr<castor::tape::tapeserver::drive::DriveInterface>
185
    drive(drive::createDrive(driveInfo, m_sysWrapper));
186

187
  if(NULL == drive.get()) {
188
    castor::exception::Exception ex;
189
    ex.getMessage() << "Failed to instantiate drive object";
190
    throw ex;
191
192
  } 
    
193
194
195
196
197
198
  return drive;
}

//------------------------------------------------------------------------------
// waitUntilDriveIsReady
//------------------------------------------------------------------------------
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
void castor::tape::tapeserver::daemon::CleanerSession::waitUntilMediaIsReady(
  drive::DriveInterface &drive) {  
  std::list<log::Param> params;
  params.push_back(log::Param("TPVID", m_vid));
  params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
  params.push_back(log::Param("waitMediaInDriveTimeout",
    m_waitMediaInDriveTimeout));

  try {
    m_log(LOG_INFO, "Cleaner waiting for drive to be ready", params);
    drive.waitUntilReady(m_waitMediaInDriveTimeout);
    m_log(LOG_INFO, "Cleaner detected drive is ready", params);
  } catch (castor::exception::Exception &ex) {
    params.push_back(log::Param("message", ex.getMessage().str()));
    m_log(LOG_INFO, "Cleaner caught non-fatal exception whilst waiting for"
      " drive to become ready", params);
215
  }
216
217
218
219
220
221
}

//------------------------------------------------------------------------------
// rewindDrive
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::rewindDrive(
222
  drive::DriveInterface &drive) {
223
224
  std::list<log::Param> params;
  params.push_back(log::Param("TPVID", m_vid));
225
  params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
226

Steven Murray's avatar
Steven Murray committed
227
  m_log(LOG_INFO, "Cleaner rewinding tape", params);
228
  drive.rewind();
Steven Murray's avatar
Steven Murray committed
229
  m_log(LOG_INFO, "Cleaner successfully rewound tape", params);
230
231
232
233
234
235
}

//------------------------------------------------------------------------------
// checkTapeContainsData
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::checkTapeContainsData(
236
  drive::DriveInterface &drive) {
237
238
  std::list<log::Param> params;
  params.push_back(log::Param("TPVID", m_vid));
239
  params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
240
241

  m_log(LOG_INFO, "Cleaner checking tape contains data", params);
242
  if(drive.isTapeBlank()) {
243
244
245
246
247
248
249
250
251
252
253
    castor::exception::Exception ex;
    ex.getMessage() << "Tape is completely blank when it should be labeled";
    throw ex;
  }
  m_log(LOG_INFO, "Cleaner successfully detected tape contains data", params);
}

//------------------------------------------------------------------------------
// checkVolumeLabel
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::CleanerSession::checkVolumeLabel(
254
  drive::DriveInterface &drive) {
255
256
257
  tapeFile::VOL1 vol1;
  std::list<log::Param> params;
  params.push_back(log::Param("TPVID", m_vid));
258
  params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
259
  
260
  try {
261
    drive.readExactBlock((void * )&vol1, sizeof(vol1),
262
263
264
265
266
267
268
269
270
271
272
273
274
      "[CleanerSession::clean()] - Reading header VOL1");
    vol1.verify();

    const std::string &volumeLabelVSN = vol1.getVSN();
    params.push_back(log::Param("volumeLabelVSN", volumeLabelVSN));

    m_log(LOG_INFO, "Cleaner read VSN from volume label", params);

    // If the cleaner was given a VID
    if(!m_vid.empty()) {
      if(m_vid == volumeLabelVSN) {
        m_log(LOG_INFO, "Cleaner detected volume label contains expected VSN",
          params);
275
      } else {
276
277
        m_log(LOG_WARNING,
          "Cleaner detected volume label does not contain expected VSN", params);
278
      }
279
    }
280
281
282
283
284
285
286
287
288
289
290
291
292

    return volumeLabelVSN;
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to check volume label: " << ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// unloadTape
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::unloadTape(
293
  const std::string &vid, drive::DriveInterface &drive) {
294
  const mediachanger::LibrarySlot &librarySlot = m_driveConfig.getLibrarySlot();
295
296
  std::list<log::Param> params;
  params.push_back(log::Param("TPVID", vid));
297
298
  params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
  params.push_back(log::Param("librarySlot", librarySlot.str()));
299
300
301

  // We implement the same policy as with the tape sessions: 
  // if the librarySlot parameter is "manual", do nothing.
302
  if(mediachanger::TAPE_LIBRARY_TYPE_MANUAL == librarySlot.getLibraryType()) {
303
304
305
306
307
308
309
    m_log(LOG_INFO, "Cleaner not unloading tape because media changer is"
      " manual", params);
    return;
  }

  try {
    m_log(LOG_INFO, "Cleaner unloading tape", params);
310
    drive.unloadTape();
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    m_log(LOG_INFO, "Cleaner unloaded tape", params);
  } catch (castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Cleaner failed to unload tape: " <<
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// dismountTape
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::dismountTape(
  const std::string &vid) {
325
  const mediachanger::LibrarySlot &librarySlot = m_driveConfig.getLibrarySlot();
326
327
  std::list<log::Param> params;
  params.push_back(log::Param("TPVID", vid));
328
329
  params.push_back(log::Param("unitName", m_driveConfig.getUnitName()));
  params.push_back(log::Param("librarySlot", librarySlot.str()));
330
331

  try {
332
    m_mc.dismountTape(vid, librarySlot);
333
    const bool dismountWasManual = mediachanger::TAPE_LIBRARY_TYPE_MANUAL ==
334
      librarySlot.getLibraryType();
335
336
337
338
339
    if(dismountWasManual) {
      m_log(LOG_INFO, "Cleaner did not dismount tape because media changer is"
        " manual", params);
    } else {
      m_log(LOG_INFO, "Cleaner dismounted tape", params);
340
    }
341
342
343
344
345
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Cleaner failed to dismount tape: " <<
      ne.getMessage().str();
    throw ex;
346
347
  }
}