]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/device.c
Tweak dir_update_volume_info
[bacula/bacula] / bacula / src / stored / device.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6
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.
9
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.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *
22  *  Higher Level Device routines.
23  *  Knows about Bacula tape labels and such
24  *
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.
33  *
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
43  *  device.
44  *
45  *
46  *   Kern Sibbald, MM, MMI
47  *
48  */
49
50 #include "bacula.h"                   /* pull in global headers */
51 #include "stored.h"                   /* pull in Storage Deamon headers */
52
53 /* Forward referenced functions */
54
55 /*
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.
59  *
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.
66  *
67  * We enter with device locked, and
68  *     exit with device locked.
69  *
70  * Note, we are called only from one place in block.c for the daemons.
71  *     The btape utility calls it from btape.c.
72  *
73  *  Returns: true  on success
74  *           false on failure
75  */
76 bool fixup_device_block_write_error(DCR *dcr, int retries)
77 {
78    char PrevVolName[MAX_NAME_LENGTH];
79    DEV_BLOCK *label_blk;
80    DEV_BLOCK *block;
81    DEV_BLOCK *ameta_block = dcr->ameta_block;
82    char b1[30], b2[30];
83    time_t wait_time;
84    char dt[MAX_TIME_LENGTH];
85    JCR *jcr = dcr->jcr;
86    DEVICE *dev;
87    int blocked;              /* save any previous blocked status */
88    bool ok = false;
89
90    dev = dcr->dev;
91    blocked = dev->blocked();
92    block = dcr->block;
93
94    wait_time = time(NULL);
95
96    Dmsg0(100, "=== Enter fixup_device_block_write_error\n");
97
98    /*
99     * If we are blocked at entry, unblock it, and set our own block status
100     */
101    if (blocked != BST_NOT_BLOCKED) {
102       unblock_device(dev);
103    }
104    block_device(dev, BST_DOING_ACQUIRE);
105
106    /* Continue unlocked, but leave BLOCKED */
107    dev->Unlock();
108
109    bstrncpy(PrevVolName, dev->getVolCatName(), sizeof(PrevVolName));
110    bstrncpy(dev->VolHdr.PrevVolumeName, PrevVolName, sizeof(dev->VolHdr.PrevVolumeName));
111
112    label_blk = new_block(dev);
113    dcr->ameta_block = dcr->block = label_blk;
114
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)));
120
121    Dmsg1(150, "set_unload dev=%s\n", dev->print_name());
122    dev->set_unload();
123
124    /* Clear DCR Start/End Block/File positions */
125    dcr->StartBlock = dcr->EndBlock = 0;
126    dcr->StartFile  = dcr->EndFile = 0;
127
128    if (!dcr->mount_next_write_volume()) {
129       free_block(label_blk);
130       dcr->block = block;
131       dcr->ameta_block = ameta_block;
132       dev->Lock();
133       goto bail_out;
134    }
135    Dmsg2(150, "must_unload=%d dev=%s\n", dev->must_unload(), dev->print_name());
136
137    dev->notify_newvol_in_attached_dcrs(dcr->VolumeName);
138    dev->Lock();                    /* lock again */
139
140    dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs on vol */
141    if (!dir_update_volume_info(dcr, false, false)) { /* send Volume info to Director */
142       goto bail_out;
143    }
144
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)));
147
148    /*
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.
153     */
154    Dmsg0(190, "write label block to dev\n");
155    if (!dcr->write_block_to_dev()) {
156       berrno be;
157       Pmsg1(0, _("write_block_to_device Volume label failed. ERR=%s"),
158         be.bstrerror(dev->dev_errno));
159       free_block(label_blk);
160       dcr->block = block;
161       dcr->ameta_block = ameta_block;
162       goto bail_out;
163    }
164    free_block(label_blk);
165    dcr->block = block;
166    dcr->ameta_block = ameta_block;
167
168    /* Clear NewVol now because dir_get_volume_info() already done */
169    jcr->dcr->NewVol = false;
170    set_new_volume_parameters(dcr);
171
172    jcr->run_time += time(NULL) - wait_time; /* correct run time for mount wait */
173
174    /* Write overflow block to device */
175    Dmsg0(190, "Write overflow block to dev\n");
176    if (!dcr->write_block_to_dev()) {
177       berrno be;
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));
185          goto bail_out;
186       }
187    }
188    ok = true;
189
190 bail_out:
191    /*
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).
195     */
196    unblock_device(dev);
197    if (blocked != BST_NOT_BLOCKED) {
198       block_device(dev, blocked);
199    }
200    return ok;                               /* device locked */
201 }
202
203 void set_start_vol_position(DCR *dcr)
204 {
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;
210    } else {
211       dcr->StartBlock = dcr->EndBlock = (uint32_t)dev->file_addr;
212       dcr->StartFile  = dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
213    }
214 }
215
216 /*
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.
220  */
221 void set_new_volume_parameters(DCR *dcr)
222 {
223    JCR *jcr = dcr->jcr;
224    Dmsg1(40, "set_new_volume_parameters dev=%s\n", dcr->dev->print_name());
225    if (dcr->NewVol) {
226       while (dcr->VolumeName[0] == 0) {
227          int retries = 5;
228          wait_for_device(dcr, retries);
229       }
230       if (dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
231          dcr->dev->clear_wait();
232       } else {
233          Dmsg1(40, "getvolinfo failed. No new Vol: %s", jcr->errmsg);
234       }
235    }
236    set_new_file_parameters(dcr);
237    jcr->NumWriteVolumes++;
238    dcr->NewVol = false;
239 }
240
241 /*
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.
245  */
246 void set_new_file_parameters(DCR *dcr)
247 {
248    set_start_vol_position(dcr);
249
250    /* Reset indicies */
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;
257 }
258
259
260
261 /*
262  *   First Open of the device. Expect dev to already be initialized.
263  *
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.
267  *
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.
271  *
272  *   Returns: false on failure
273  *            true  on success
274  */
275 bool first_open_device(DCR *dcr)
276 {
277    DEVICE *dev = dcr->dev;
278    bool ok = true;
279
280    Dmsg0(120, "start open_output_device()\n");
281    if (!dev) {
282       return false;
283    }
284
285    dev->rLock(false);
286
287    /* Defer opening files */
288    if (!dev->is_tape()) {
289       Dmsg0(129, "Device is file, deferring open.\n");
290       goto bail_out;
291    }
292
293     int mode;
294     if (dev->has_cap(CAP_STREAM)) {
295        mode = OPEN_WRITE_ONLY;
296     } else {
297        mode = OPEN_READ_ONLY;
298     }
299    Dmsg0(129, "Opening device.\n");
300    if (!dev->open(dcr, mode)) {
301       Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
302       ok = false;
303       goto bail_out;
304    }
305    Dmsg1(129, "open dev %s OK\n", dev->print_name());
306
307 bail_out:
308    dev->rUnlock();
309    return ok;
310 }
311
312 /*
313  * Make sure device is open, if not do so
314  */
315 bool open_device(DCR *dcr)
316 {
317    DEVICE *dev = dcr->dev;
318    /* Open device */
319    int mode;
320    if (dev->has_cap(CAP_STREAM)) {
321       mode = OPEN_WRITE_ONLY;
322    } else {
323       mode = OPEN_READ_WRITE;
324    }
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());
334       }
335       return false;
336    }
337    return true;
338 }