CleanerSession.cpp 13.3 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
  cta::server::ProcessCap &capUtils,
31
  cta::mediachanger::MediaChangerFacade &mc,
Victor Kotlyar's avatar
Victor Kotlyar committed
32
  cta::log::Logger &log,
33
  const cta::tape::daemon::TpconfigLine &driveConfig,
34
  System::virtualWrapper &sysWrapper,
35
  const std::string &vid,
36
  const bool waitMediaInDrive,
37
38
  const uint32_t waitMediaInDriveTimeout,
  const std::string & externalEncryptionKeyScript):
39
  m_capUtils(capUtils),
40
  m_mc(mc),
41
42
  m_log(log),
  m_driveConfig(driveConfig),
43
  m_sysWrapper(sysWrapper),
44
  m_vid(vid),
45
  m_waitMediaInDrive(waitMediaInDrive),
46
47
  m_waitMediaInDriveTimeout(waitMediaInDriveTimeout),
  m_encryptionControl(externalEncryptionKeyScript) {}
48
49

//------------------------------------------------------------------------------
50
// execute
51
//------------------------------------------------------------------------------
52
castor::tape::tapeserver::daemon::Session::EndOfSessionAction
53
  castor::tape::tapeserver::daemon::CleanerSession::execute() throw() {
54
55
56
57
  std::string errorMessage;

  try {
    return exceptionThrowingExecute();
58
  } catch(cta::exception::Exception &ex) {
59
60
61
62
63
64
65
66
    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
Victor Kotlyar's avatar
Victor Kotlyar committed
67
  std::list<cta::log::Param> params = {
68
69
    cta::log::Param("tapeVid", m_vid),
    cta::log::Param("tapeDrive", m_driveConfig.unitName),
Victor Kotlyar's avatar
Victor Kotlyar committed
70
71
    cta::log::Param("message", errorMessage)};
  m_log(cta::log::ERR, "Cleaner failed", params);
72
73
74
75
76
77
78
79
80
81
82
  return MARK_DRIVE_AS_DOWN;
}

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

  setProcessCapabilities("cap_sys_rawio+ep");

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

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  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) {
101
102
103
  if(m_waitMediaInDrive) {
    waitUntilMediaIsReady(drive);
  }
104

105
  if(!drive.hasTapeInPlace()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
106
    std::list<cta::log::Param> params;
107
108
    params.push_back(cta::log::Param("tapeVid", m_vid));
    params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
Victor Kotlyar's avatar
Victor Kotlyar committed
109
    m_log(cta::log::INFO, "Cleaner found tape drive empty", params);
110
    return; //return immediately if there is no tape
111
112
  }

113
114
  m_encryptionControl.disable(drive);

115
  rewindDrive(drive);
116
  drive.disableLogicalBlockProtection();
117

118
  checkTapeContainsData(drive);
119

120
  const std::string volumeLabelVSN = checkVolumeLabel(drive);
121

122
  unloadTape(volumeLabelVSN, drive);
123
124

  dismountTape(volumeLabelVSN);
125
}
126

127
128
129
130
131
132
133
134
135
136
137
138
139
//------------------------------------------------------------------------------
// 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++)
      {
Victor Kotlyar's avatar
Victor Kotlyar committed
140
141
142
143
144
        std::list<cta::log::Param> params = {
          cta::log::Param("tapeAlert",*ta),
          cta::log::Param("tapeAlertNumber", alertNumber++),
          cta::log::Param("tapeAlertCount", tapeAlerts.size())};
        m_log(cta::log::WARNING, "Tape alert detected",params);
145
146
147
      }
    }
    return;
148
  } catch(cta::exception::Exception &ex) {
149
150
151
152
153
154
155
156
    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)
Victor Kotlyar's avatar
Victor Kotlyar committed
157
  std::list<cta::log::Param> params = {
158
159
    cta::log::Param("tapeVid", m_vid),
    cta::log::Param("tapeDrive", m_driveConfig.unitName),
Victor Kotlyar's avatar
Victor Kotlyar committed
160
161
    cta::log::Param("message", errorMessage)};
  m_log(cta::log::ERR, "Cleaner failed getting tape alerts from the drive", params);  
162
163
164
165
166
167
168
169
}

//------------------------------------------------------------------------------
// setProcessCapabilities
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::setProcessCapabilities(
  const std::string &capabilities) {
  m_capUtils.setProcText(capabilities);
170
  {
Victor Kotlyar's avatar
Victor Kotlyar committed
171
172
173
    std::list<cta::log::Param> params = {
      cta::log::Param("capabilities", m_capUtils.getProcText())};
    m_log(cta::log::INFO, "Cleaner set process capabilities for using tape",
174
175
      params);
  }
176
}
177

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

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

//------------------------------------------------------------------------------
// waitUntilDriveIsReady
//------------------------------------------------------------------------------
202
203
void castor::tape::tapeserver::daemon::CleanerSession::waitUntilMediaIsReady(
  drive::DriveInterface &drive) {  
Victor Kotlyar's avatar
Victor Kotlyar committed
204
  std::list<cta::log::Param> params;
205
206
  params.push_back(cta::log::Param("tapeVid", m_vid));
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
Victor Kotlyar's avatar
Victor Kotlyar committed
207
  params.push_back(cta::log::Param("waitMediaInDriveTimeout",
208
209
210
    m_waitMediaInDriveTimeout));

  try {
Victor Kotlyar's avatar
Victor Kotlyar committed
211
    m_log(cta::log::INFO, "Cleaner waiting for drive to be ready", params);
212
    drive.waitUntilReady(m_waitMediaInDriveTimeout);
Victor Kotlyar's avatar
Victor Kotlyar committed
213
    m_log(cta::log::INFO, "Cleaner detected drive is ready", params);
214
  } catch (cta::exception::Exception &ex) {
Victor Kotlyar's avatar
Victor Kotlyar committed
215
216
    params.push_back(cta::log::Param("message", ex.getMessage().str()));
    m_log(cta::log::INFO, "Cleaner caught non-fatal exception whilst waiting for"
217
      " drive to become ready", params);
218
  }
219
220
221
222
223
224
}

//------------------------------------------------------------------------------
// rewindDrive
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::rewindDrive(
225
  drive::DriveInterface &drive) {
Victor Kotlyar's avatar
Victor Kotlyar committed
226
  std::list<cta::log::Param> params;
227
228
  params.push_back(cta::log::Param("tapeVid", m_vid));
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
229

Victor Kotlyar's avatar
Victor Kotlyar committed
230
  m_log(cta::log::INFO, "Cleaner rewinding tape", params);
231
  drive.rewind();
Victor Kotlyar's avatar
Victor Kotlyar committed
232
  m_log(cta::log::INFO, "Cleaner successfully rewound tape", params);
233
234
235
236
237
238
}

//------------------------------------------------------------------------------
// checkTapeContainsData
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::checkTapeContainsData(
239
  drive::DriveInterface &drive) {
Victor Kotlyar's avatar
Victor Kotlyar committed
240
  std::list<cta::log::Param> params;
241
242
  params.push_back(cta::log::Param("tapeVid", m_vid));
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
243

Victor Kotlyar's avatar
Victor Kotlyar committed
244
  m_log(cta::log::INFO, "Cleaner checking tape contains data", params);
245
  if(drive.isTapeBlank()) {
246
    cta::exception::Exception ex;
247
248
249
    ex.getMessage() << "Tape is completely blank when it should be labeled";
    throw ex;
  }
Victor Kotlyar's avatar
Victor Kotlyar committed
250
  m_log(cta::log::INFO, "Cleaner successfully detected tape contains data", params);
251
252
253
254
255
256
}

//------------------------------------------------------------------------------
// checkVolumeLabel
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::CleanerSession::checkVolumeLabel(
257
  drive::DriveInterface &drive) {
258
  tapeFile::VOL1 vol1;
Victor Kotlyar's avatar
Victor Kotlyar committed
259
  std::list<cta::log::Param> params;
260
261
  params.push_back(cta::log::Param("tapeVid", m_vid));
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
262
  
263
  try {
264
    drive.readExactBlock((void * )&vol1, sizeof(vol1),
265
266
267
268
      "[CleanerSession::clean()] - Reading header VOL1");
    vol1.verify();

    const std::string &volumeLabelVSN = vol1.getVSN();
Victor Kotlyar's avatar
Victor Kotlyar committed
269
    params.push_back(cta::log::Param("volumeLabelVSN", volumeLabelVSN));
270

Victor Kotlyar's avatar
Victor Kotlyar committed
271
    m_log(cta::log::INFO, "Cleaner read VSN from volume label", params);
272
273
274
275

    // If the cleaner was given a VID
    if(!m_vid.empty()) {
      if(m_vid == volumeLabelVSN) {
Victor Kotlyar's avatar
Victor Kotlyar committed
276
        m_log(cta::log::INFO, "Cleaner detected volume label contains expected VSN",
277
          params);
278
      } else {
Victor Kotlyar's avatar
Victor Kotlyar committed
279
        m_log(cta::log::WARNING,
280
          "Cleaner detected volume label does not contain expected VSN", params);
281
      }
282
    }
283
284

    return volumeLabelVSN;
285
286
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
287
288
289
290
291
292
293
294
295
    ex.getMessage() << "Failed to check volume label: " << ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// unloadTape
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::unloadTape(
296
  const std::string &vid, drive::DriveInterface &drive) {
297
  const cta::mediachanger::LibrarySlot &librarySlot = m_driveConfig.librarySlot();
Victor Kotlyar's avatar
Victor Kotlyar committed
298
  std::list<cta::log::Param> params;
299
300
  params.push_back(cta::log::Param("tapeVid", vid));
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
Victor Kotlyar's avatar
Victor Kotlyar committed
301
  params.push_back(cta::log::Param("librarySlot", librarySlot.str()));
302
303
304

  // We implement the same policy as with the tape sessions: 
  // if the librarySlot parameter is "manual", do nothing.
305
  if(cta::mediachanger::TAPE_LIBRARY_TYPE_MANUAL == librarySlot.getLibraryType()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
306
    m_log(cta::log::INFO, "Cleaner not unloading tape because media changer is"
307
308
309
310
311
      " manual", params);
    return;
  }

  try {
Victor Kotlyar's avatar
Victor Kotlyar committed
312
    m_log(cta::log::INFO, "Cleaner unloading tape", params);
313
    drive.unloadTape();
Victor Kotlyar's avatar
Victor Kotlyar committed
314
    m_log(cta::log::INFO, "Cleaner unloaded tape", params);
315
316
  } catch (cta::exception::Exception &ne) {
    cta::exception::Exception ex;
317
318
319
320
321
322
323
324
325
326
327
    ex.getMessage() << "Cleaner failed to unload tape: " <<
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// dismountTape
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::CleanerSession::dismountTape(
  const std::string &vid) {
328
  const cta::mediachanger::LibrarySlot &librarySlot = m_driveConfig.librarySlot();
Victor Kotlyar's avatar
Victor Kotlyar committed
329
  std::list<cta::log::Param> params;
330
331
  params.push_back(cta::log::Param("tapeVid", vid));
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
Victor Kotlyar's avatar
Victor Kotlyar committed
332
  params.push_back(cta::log::Param("librarySlot", librarySlot.str()));
333
334

  try {
335
    m_mc.dismountTape(vid, librarySlot);
336
    const bool dismountWasManual = cta::mediachanger::TAPE_LIBRARY_TYPE_MANUAL ==
337
      librarySlot.getLibraryType();
338
    if(dismountWasManual) {
Victor Kotlyar's avatar
Victor Kotlyar committed
339
      m_log(cta::log::INFO, "Cleaner did not dismount tape because media changer is"
340
341
        " manual", params);
    } else {
Victor Kotlyar's avatar
Victor Kotlyar committed
342
      m_log(cta::log::INFO, "Cleaner dismounted tape", params);
343
    }
344
345
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
346
347
348
    ex.getMessage() << "Cleaner failed to dismount tape: " <<
      ne.getMessage().str();
    throw ex;
349
350
  }
}