2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
22 * Higher Level Device routines.
23 * Knows about Bacula tape labels and such
25 * NOTE! In general, subroutines that have the word
26 * "device" in the name do locking. Subroutines
27 * that have the word "dev" in the name do not
28 * do locking. Thus if xxx_device() calls
29 * yyy_dev(), all is OK, but if xxx_device()
30 * calls yyy_device(), everything will hang.
31 * Obviously, no zzz_dev() is allowed to call
32 * a www_device() or everything falls apart.
34 * Concerning the routines dev->rLock()() and block_device()
35 * see the end of this module for details. In general,
36 * blocking a device leaves it in a state where all threads
37 * other than the current thread block when they attempt to
38 * lock the device. They remain suspended (blocked) until the device
39 * is unblocked. So, a device is blocked during an operation
40 * that takes a long time (initialization, mounting a new
41 * volume, ...) locking a device is done for an operation
42 * that takes a short time such as writing data to the
46 * Kern Sibbald, MM, MMI
50 #include "bacula.h" /* pull in global headers */
51 #include "stored.h" /* pull in Storage Deamon headers */
53 /* Forward referenced functions */
56 * This is the dreaded moment. We either have an end of
57 * medium condition or worse, and error condition.
58 * Attempt to "recover" by obtaining a new Volume.
60 * Here are a few things to know:
61 * dcr->VolCatInfo contains the info on the "current" tape for this job.
62 * dev->VolCatInfo contains the info on the tape in the drive.
63 * The tape in the drive could have changed several times since
64 * the last time the job used it (jcr->VolCatInfo).
65 * dcr->VolumeName is the name of the current/desired tape in the drive.
67 * We enter with device locked, and
68 * exit with device locked.
70 * Note, we are called only from one place in block.c for the daemons.
71 * The btape utility calls it from btape.c.
73 * Returns: true on success
76 bool fixup_device_block_write_error(DCR *dcr, int retries)
78 char PrevVolName[MAX_NAME_LENGTH];
81 DEV_BLOCK *ameta_block = dcr->ameta_block;
84 char dt[MAX_TIME_LENGTH];
87 int blocked; /* save any previous blocked status */
91 blocked = dev->blocked();
94 wait_time = time(NULL);
96 Dmsg0(100, "=== Enter fixup_device_block_write_error\n");
99 * If we are blocked at entry, unblock it, and set our own block status
101 if (blocked != BST_NOT_BLOCKED) {
104 block_device(dev, BST_DOING_ACQUIRE);
106 /* Continue unlocked, but leave BLOCKED */
109 bstrncpy(PrevVolName, dev->getVolCatName(), sizeof(PrevVolName));
110 bstrncpy(dev->VolHdr.PrevVolumeName, PrevVolName, sizeof(dev->VolHdr.PrevVolumeName));
112 label_blk = new_block(dev);
113 dcr->ameta_block = dcr->block = label_blk;
115 /* Inform User about end of medium */
116 Jmsg(jcr, M_INFO, 0, _("End of medium on Volume \"%s\" Bytes=%s Blocks=%s at %s.\n"),
117 PrevVolName, edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
118 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
119 bstrftime(dt, sizeof(dt), time(NULL)));
121 Dmsg1(150, "set_unload dev=%s\n", dev->print_name());
124 /* Clear DCR Start/End Block/File positions */
125 dcr->StartBlock = dcr->EndBlock = 0;
126 dcr->StartFile = dcr->EndFile = 0;
128 if (!dcr->mount_next_write_volume()) {
129 free_block(label_blk);
131 dcr->ameta_block = ameta_block;
135 Dmsg2(150, "must_unload=%d dev=%s\n", dev->must_unload(), dev->print_name());
137 dev->notify_newvol_in_attached_dcrs(dcr->VolumeName);
138 dev->Lock(); /* lock again */
140 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs on vol */
141 if (!dir_update_volume_info(dcr, false, false)) { /* send Volume info to Director */
145 Jmsg(jcr, M_INFO, 0, _("New volume \"%s\" mounted on device %s at %s.\n"),
146 dcr->VolumeName, dev->print_name(), bstrftime(dt, sizeof(dt), time(NULL)));
149 * If this is a new tape, the label_blk will contain the
150 * label, so write it now. If this is a previously
151 * used tape, mount_next_write_volume() will return an
152 * empty label_blk, and nothing will be written.
154 Dmsg0(190, "write label block to dev\n");
155 if (!dcr->write_block_to_dev()) {
157 Pmsg1(0, _("write_block_to_device Volume label failed. ERR=%s"),
158 be.bstrerror(dev->dev_errno));
159 free_block(label_blk);
161 dcr->ameta_block = ameta_block;
164 free_block(label_blk);
166 dcr->ameta_block = ameta_block;
168 /* Clear NewVol now because dir_get_volume_info() already done */
169 jcr->dcr->NewVol = false;
170 set_new_volume_parameters(dcr);
172 jcr->run_time += time(NULL) - wait_time; /* correct run time for mount wait */
174 /* Write overflow block to device */
175 Dmsg0(190, "Write overflow block to dev\n");
176 if (!dcr->write_block_to_dev()) {
178 Dmsg1(0, _("write_block_to_device overflow block failed. ERR=%s"),
179 be.bstrerror(dev->dev_errno));
180 /* Note: recursive call */
181 if (retries-- <= 0 || !fixup_device_block_write_error(dcr, retries)) {
182 Jmsg2(jcr, M_FATAL, 0,
183 _("Catastrophic error. Cannot write overflow block to device %s. ERR=%s"),
184 dev->print_name(), be.bstrerror(dev->dev_errno));
192 * At this point, the device is locked and blocked.
193 * Unblock the device, restore any entry blocked condition, then
194 * return leaving the device locked (as it was on entry).
197 if (blocked != BST_NOT_BLOCKED) {
198 block_device(dev, blocked);
200 return ok; /* device locked */
203 void set_start_vol_position(DCR *dcr)
205 DEVICE *dev = dcr->dev;
206 /* Set new start position */
207 if (dev->is_tape()) {
208 dcr->StartBlock = dcr->EndBlock = dev->block_num;
209 dcr->StartFile = dcr->EndFile = dev->file;
211 dcr->StartBlock = dcr->EndBlock = (uint32_t)dev->file_addr;
212 dcr->StartFile = dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
217 * We have a new Volume mounted, so reset the Volume parameters
218 * concerning this job. The global changes were made earlier
219 * in the dev structure.
221 void set_new_volume_parameters(DCR *dcr)
224 Dmsg1(40, "set_new_volume_parameters dev=%s\n", dcr->dev->print_name());
226 while (dcr->VolumeName[0] == 0) {
228 wait_for_device(dcr, retries);
230 if (dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
231 dcr->dev->clear_wait();
233 Dmsg1(40, "getvolinfo failed. No new Vol: %s", jcr->errmsg);
236 set_new_file_parameters(dcr);
237 jcr->NumWriteVolumes++;
242 * We are now in a new Volume file, so reset the Volume parameters
243 * concerning this job. The global changes were made earlier
244 * in the dev structure.
246 void set_new_file_parameters(DCR *dcr)
248 set_start_vol_position(dcr);
251 Dmsg3(1000, "Reset indices Vol=%s were: FI=%d LI=%d\n", dcr->VolumeName,
252 dcr->VolFirstIndex, dcr->VolLastIndex);
253 dcr->VolFirstIndex = 0;
254 dcr->VolLastIndex = 0;
255 dcr->NewFile = false;
256 dcr->WroteVol = false;
262 * First Open of the device. Expect dev to already be initialized.
264 * This routine is used only when the Storage daemon starts
265 * and always_open is set, and in the stand-alone utility
266 * routines such as bextract.
268 * Note, opening of a normal file is deferred to later so
269 * that we can get the filename; the device_name for
270 * a file is the directory only.
272 * Returns: false on failure
275 bool first_open_device(DCR *dcr)
277 DEVICE *dev = dcr->dev;
280 Dmsg0(120, "start open_output_device()\n");
287 /* Defer opening files */
288 if (!dev->is_tape()) {
289 Dmsg0(129, "Device is file, deferring open.\n");
294 if (dev->has_cap(CAP_STREAM)) {
295 mode = OPEN_WRITE_ONLY;
297 mode = OPEN_READ_ONLY;
299 Dmsg0(129, "Opening device.\n");
300 if (!dev->open(dcr, mode)) {
301 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
305 Dmsg1(129, "open dev %s OK\n", dev->print_name());
313 * Make sure device is open, if not do so
315 bool open_device(DCR *dcr)
317 DEVICE *dev = dcr->dev;
320 if (dev->has_cap(CAP_STREAM)) {
321 mode = OPEN_WRITE_ONLY;
323 mode = OPEN_READ_WRITE;
325 if (!dev->open(dcr, mode)) {
326 /* If polling, ignore the error */
327 /* If DVD, also ignore the error, very often you cannot open the device
328 * (when there is no DVD, or when the one inserted is a wrong one) */
329 if (!dev->poll && !dev->is_dvd() && !dev->is_removable()) {
330 Jmsg2(dcr->jcr, M_FATAL, 0, _("Unable to open device %s: ERR=%s\n"),
331 dev->print_name(), dev->bstrerror());
332 Pmsg2(000, _("Unable to open archive %s: ERR=%s\n"),
333 dev->print_name(), dev->bstrerror());