]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/device.c
be01f9a409dd1c8a23fccd41df1ea565c773c60d
[bacula/bacula] / bacula / src / stored / device.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  *  Higher Level Device routines.
22  *  Knows about Bacula tape labels and such
23  *
24  *  NOTE! In general, subroutines that have the word
25  *        "device" in the name do locking.  Subroutines
26  *        that have the word "dev" in the name do not
27  *        do locking.  Thus if xxx_device() calls
28  *        yyy_dev(), all is OK, but if xxx_device()
29  *        calls yyy_device(), everything will hang.
30  *        Obviously, no zzz_dev() is allowed to call
31  *        a www_device() or everything falls apart.
32  *
33  * Concerning the routines dev->rLock()() and block_device()
34  *  see the end of this module for details.  In general,
35  *  blocking a device leaves it in a state where all threads
36  *  other than the current thread block when they attempt to
37  *  lock the device. They remain suspended (blocked) until the device
38  *  is unblocked. So, a device is blocked during an operation
39  *  that takes a long time (initialization, mounting a new
40  *  volume, ...) locking a device is done for an operation
41  *  that takes a short time such as writing data to the
42  *  device.
43  *
44  *
45  *   Kern Sibbald, MM, MMI
46  *
47  */
48
49 #include "bacula.h"                   /* pull in global headers */
50 #include "stored.h"                   /* pull in Storage Deamon headers */
51
52 /* Forward referenced functions */
53
54 /*
55  * This is the dreaded moment. We either have an end of
56  * medium condition or worse, and error condition.
57  * Attempt to "recover" by obtaining a new Volume.
58  *
59  * Here are a few things to know:
60  *  dcr->VolCatInfo contains the info on the "current" tape for this job.
61  *  dev->VolCatInfo contains the info on the tape in the drive.
62  *    The tape in the drive could have changed several times since
63  *    the last time the job used it (jcr->VolCatInfo).
64  *  dcr->VolumeName is the name of the current/desired tape in the drive.
65  *
66  * We enter with device locked, and
67  *     exit with device locked.
68  *
69  * Note, we are called only from one place in block.c for the daemons.
70  *     The btape utility calls it from btape.c.
71  *
72  *  Returns: true  on success
73  *           false on failure
74  */
75 bool fixup_device_block_write_error(DCR *dcr, int retries)
76 {
77    char PrevVolName[MAX_NAME_LENGTH];
78    DEV_BLOCK *label_blk;
79    DEV_BLOCK *block;
80    DEV_BLOCK *ameta_block = dcr->ameta_block;
81    char b1[30], b2[30];
82    time_t wait_time;
83    char dt[MAX_TIME_LENGTH];
84    JCR *jcr = dcr->jcr;
85    DEVICE *dev;
86    int blocked;              /* save any previous blocked status */
87    bool ok = false;
88
89    dev = dcr->dev;
90    blocked = dev->blocked();
91    block = dcr->block;
92
93    wait_time = time(NULL);
94
95    Dmsg0(100, "=== Enter fixup_device_block_write_error\n");
96
97    /*
98     * If we are blocked at entry, unblock it, and set our own block status
99     */
100    if (blocked != BST_NOT_BLOCKED) {
101       unblock_device(dev);
102    }
103    block_device(dev, BST_DOING_ACQUIRE);
104
105    /* Continue unlocked, but leave BLOCKED */
106    dev->Unlock();
107
108    bstrncpy(PrevVolName, dev->getVolCatName(), sizeof(PrevVolName));
109    bstrncpy(dev->VolHdr.PrevVolumeName, PrevVolName, sizeof(dev->VolHdr.PrevVolumeName));
110
111    label_blk = new_block(dev);
112    dcr->ameta_block = dcr->block = label_blk;
113
114    /* Inform User about end of medium */
115    Jmsg(jcr, M_INFO, 0, _("End of medium on Volume \"%s\" Bytes=%s Blocks=%s at %s.\n"),
116         PrevVolName, edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
117         edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2),
118         bstrftime(dt, sizeof(dt), time(NULL)));
119
120    Dmsg1(150, "set_unload dev=%s\n", dev->print_name());
121    dev->set_unload();
122
123    /* Clear DCR Start/End Block/File positions */
124    dcr->StartBlock = dcr->EndBlock = 0;
125    dcr->StartFile  = dcr->EndFile = 0;
126
127    if (!dcr->mount_next_write_volume()) {
128       free_block(label_blk);
129       dcr->block = block;
130       dcr->ameta_block = ameta_block;
131       dev->Lock();
132       goto bail_out;
133    }
134    Dmsg2(150, "must_unload=%d dev=%s\n", dev->must_unload(), dev->print_name());
135
136    dev->notify_newvol_in_attached_dcrs(dcr->VolumeName);
137    dev->Lock();                    /* lock again */
138
139    dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs on vol */
140    if (!dir_update_volume_info(dcr, false, false)) { /* send Volume info to Director */
141       goto bail_out;
142    }
143
144    Jmsg(jcr, M_INFO, 0, _("New volume \"%s\" mounted on device %s at %s.\n"),
145       dcr->VolumeName, dev->print_name(), bstrftime(dt, sizeof(dt), time(NULL)));
146
147    /*
148     * If this is a new tape, the label_blk will contain the
149     *  label, so write it now. If this is a previously
150     *  used tape, mount_next_write_volume() will return an
151     *  empty label_blk, and nothing will be written.
152     */
153    Dmsg0(190, "write label block to dev\n");
154    if (!dcr->write_block_to_dev()) {
155       berrno be;
156       Pmsg1(0, _("write_block_to_device Volume label failed. ERR=%s"),
157         be.bstrerror(dev->dev_errno));
158       free_block(label_blk);
159       dcr->block = block;
160       dcr->ameta_block = ameta_block;
161       goto bail_out;
162    }
163    free_block(label_blk);
164    dcr->block = block;
165    dcr->ameta_block = ameta_block;
166
167    /* Clear NewVol now because dir_get_volume_info() already done */
168    jcr->dcr->NewVol = false;
169    set_new_volume_parameters(dcr);
170
171    jcr->run_time += time(NULL) - wait_time; /* correct run time for mount wait */
172
173    /* Write overflow block to device */
174    Dmsg0(190, "Write overflow block to dev\n");
175    if (!dcr->write_block_to_dev()) {
176       berrno be;
177       Dmsg1(0, _("write_block_to_device overflow block failed. ERR=%s"),
178         be.bstrerror(dev->dev_errno));
179       /* Note: recursive call */
180       if (retries-- <= 0 || !fixup_device_block_write_error(dcr, retries)) {
181          Jmsg2(jcr, M_FATAL, 0,
182               _("Catastrophic error. Cannot write overflow block to device %s. ERR=%s"),
183               dev->print_name(), be.bstrerror(dev->dev_errno));
184          goto bail_out;
185       }
186    }
187    ok = true;
188
189 bail_out:
190    /*
191     * At this point, the device is locked and blocked.
192     * Unblock the device, restore any entry blocked condition, then
193     *   return leaving the device locked (as it was on entry).
194     */
195    unblock_device(dev);
196    if (blocked != BST_NOT_BLOCKED) {
197       block_device(dev, blocked);
198    }
199    return ok;                               /* device locked */
200 }
201
202 void set_start_vol_position(DCR *dcr)
203 {
204    DEVICE *dev = dcr->dev;
205    /* Set new start position */
206    if (dev->is_tape()) {
207       dcr->StartBlock = dcr->EndBlock = dev->block_num;
208       dcr->StartFile = dcr->EndFile = dev->file;
209    } else {
210       dcr->StartBlock = dcr->EndBlock = (uint32_t)dev->file_addr;
211       dcr->StartFile  = dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
212    }
213 }
214
215 /*
216  * We have a new Volume mounted, so reset the Volume parameters
217  *  concerning this job.  The global changes were made earlier
218  *  in the dev structure.
219  */
220 void set_new_volume_parameters(DCR *dcr)
221 {
222    JCR *jcr = dcr->jcr;
223    Dmsg1(40, "set_new_volume_parameters dev=%s\n", dcr->dev->print_name());
224    if (dcr->NewVol) {
225       while (dcr->VolumeName[0] == 0) {
226          int retries = 5;
227          wait_for_device(dcr, retries);
228       }
229       if (dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
230          dcr->dev->clear_wait();
231       } else {
232          Dmsg1(40, "getvolinfo failed. No new Vol: %s", jcr->errmsg);
233       }
234    }
235    set_new_file_parameters(dcr);
236    jcr->NumWriteVolumes++;
237    dcr->NewVol = false;
238 }
239
240 /*
241  * We are now in a new Volume file, so reset the Volume parameters
242  *  concerning this job.  The global changes were made earlier
243  *  in the dev structure.
244  */
245 void set_new_file_parameters(DCR *dcr)
246 {
247    set_start_vol_position(dcr);
248
249    /* Reset indicies */
250    Dmsg3(1000, "Reset indices Vol=%s were: FI=%d LI=%d\n", dcr->VolumeName,
251       dcr->VolFirstIndex, dcr->VolLastIndex);
252    dcr->VolFirstIndex = 0;
253    dcr->VolLastIndex = 0;
254    dcr->NewFile = false;
255    dcr->WroteVol = false;
256 }
257
258
259
260 /*
261  *   First Open of the device. Expect dev to already be initialized.
262  *
263  *   This routine is used only when the Storage daemon starts
264  *   and always_open is set, and in the stand-alone utility
265  *   routines such as bextract.
266  *
267  *   Note, opening of a normal file is deferred to later so
268  *    that we can get the filename; the device_name for
269  *    a file is the directory only.
270  *
271  *   Returns: false on failure
272  *            true  on success
273  */
274 bool first_open_device(DCR *dcr)
275 {
276    DEVICE *dev = dcr->dev;
277    bool ok = true;
278
279    Dmsg0(120, "start open_output_device()\n");
280    if (!dev) {
281       return false;
282    }
283
284    dev->rLock(false);
285
286    /* Defer opening files */
287    if (!dev->is_tape()) {
288       Dmsg0(129, "Device is file, deferring open.\n");
289       goto bail_out;
290    }
291
292     int mode;
293     if (dev->has_cap(CAP_STREAM)) {
294        mode = OPEN_WRITE_ONLY;
295     } else {
296        mode = OPEN_READ_ONLY;
297     }
298    Dmsg0(129, "Opening device.\n");
299    if (!dev->open(dcr, mode)) {
300       Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
301       ok = false;
302       goto bail_out;
303    }
304    Dmsg1(129, "open dev %s OK\n", dev->print_name());
305
306 bail_out:
307    dev->rUnlock();
308    return ok;
309 }
310
311 /*
312  * Make sure device is open, if not do so
313  */
314 bool open_device(DCR *dcr)
315 {
316    DEVICE *dev = dcr->dev;
317    /* Open device */
318    int mode;
319    if (dev->has_cap(CAP_STREAM)) {
320       mode = OPEN_WRITE_ONLY;
321    } else {
322       mode = OPEN_READ_WRITE;
323    }
324    if (!dev->open(dcr, mode)) {
325       /* If polling, ignore the error */
326       /* If DVD, also ignore the error, very often you cannot open the device
327        * (when there is no DVD, or when the one inserted is a wrong one) */
328       if (!dev->poll && !dev->is_dvd() && !dev->is_removable()) {
329          Jmsg2(dcr->jcr, M_FATAL, 0, _("Unable to open device %s: ERR=%s\n"),
330             dev->print_name(), dev->bstrerror());
331          Pmsg2(000, _("Unable to open archive %s: ERR=%s\n"),
332             dev->print_name(), dev->bstrerror());
333       }
334       return false;
335    }
336    return true;
337 }