3 * Routines for handling mounting tapes for reading and for
6 * Kern Sibbald, August MMII
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 #include "bacula.h" /* pull in global headers */
31 #include "stored.h" /* pull in Storage Deamon headers */
33 /* Forward referenced functions */
34 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
38 * If release is set, we rewind the current volume,
39 * which we no longer want, and ask the user (console)
40 * to mount the next volume.
42 * Continue trying until we get it, and then ensure
43 * that we can write on it.
45 * This routine returns a 0 only if it is REALLY
46 * impossible to get the requested Volume.
48 int mount_next_write_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, int release)
50 int recycle, ask, retry = 0, autochanger;
52 Dmsg0(190, "Enter mount_next_volume()\n");
56 Jmsg(jcr, M_FATAL, 0, _("Too many errors on device %s.\n"), dev_name(dev));
59 if (job_cancelled(jcr)) {
60 Jmsg(jcr, M_FATAL, 0, _("Job cancelled.\n"));
63 recycle = ask = autochanger = 0;
65 Dmsg0(500, "mount_next_volume release=1\n");
67 * First erase all memory of the current volume
71 dev->LastBlockNumWritten = 0;
72 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
73 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
74 dev->state &= ~ST_LABEL; /* label not yet read */
76 if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
77 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
83 /* If we have not closed the device, then at least rewind the tape */
84 if (dev->state & ST_OPENED) {
85 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
88 if (!rewind_dev(dev)) {
89 Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"),
90 dev_name(dev), strerror_dev(dev));
93 ask = 1; /* ask operator to mount tape */
96 * Get Director's idea of what tape we should have mounted.
98 if (!dir_find_next_appendable_volume(jcr)) {
99 ask = 1; /* we must ask */
102 release = 1; /* release if we "recurse" */
105 * Get next volume and ready it for append
106 * This code ensures that the device is ready for
107 * writing. We start from the assumption that there
108 * may not be a tape mounted.
110 * If the device is a file, we create the output
111 * file. If it is a tape, we check the volume name
112 * and move the tape to the end of data.
114 * It assumes that the device is not already in use!
118 Dmsg0(100, "Enter ready_dev_for_append\n");
120 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
122 jcr->VolFirstFile = jcr->JobFiles; /* first update of Vol FileIndex */
124 int slot = jcr->VolCatInfo.Slot;
127 * Handle autoloaders here. If we cannot autoload it, we
128 * will fall through to ask the sysop.
130 if (dev->capabilities && CAP_AUTOCHANGER && slot <= 0) {
131 if (dir_find_next_appendable_volume(jcr)) {
132 slot = jcr->VolCatInfo.Slot;
135 Dmsg1(100, "Want changer slot=%d\n", slot);
137 if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
138 uint32_t timeout = jcr->device->max_changer_wait;
139 POOLMEM *changer, *results;
142 results = get_pool_memory(PM_MESSAGE);
143 changer = get_pool_memory(PM_FNAME);
144 /* Find out what is loaded */
145 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
147 status = run_program(changer, timeout, results);
149 loaded = atoi(results);
151 loaded = -1; /* force unload */
153 Dmsg1(100, "loaded=%s\n", results);
155 /* If bad status or tape we want is not loaded, load it. */
156 if (status != 0 || loaded != slot) {
157 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
160 /* We are going to load a new tape, so close the device */
161 force_close_dev(dev);
162 if (loaded != 0) { /* must unload drive */
163 Dmsg0(100, "Doing changer unload.\n");
164 changer = edit_device_codes(jcr, changer,
165 jcr->device->changer_command, "unload");
166 status = run_program(changer, timeout, NULL);
167 Dmsg1(100, "unload status=%d\n", status);
170 * Load the desired cassette
172 Dmsg1(100, "Doing changer load slot %d\n", slot);
173 changer = edit_device_codes(jcr, changer,
174 jcr->device->changer_command, "load");
175 status = run_program(changer, timeout, NULL);
176 Dmsg2(100, "load slot %d status=%d\n", slot, status);
178 free_pool_memory(changer);
179 free_pool_memory(results);
180 Dmsg1(100, "After changer, status=%d\n", status);
181 if (status == 0) { /* did we succeed? */
182 ask = 0; /* yes, got vol, no need to ask sysop */
183 autochanger = 1; /* tape loaded by changer */
188 if (ask && !dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
189 return 0; /* error return */
191 Dmsg1(200, "want vol=%s\n", jcr->VolumeName);
194 for ( ; !(dev->state & ST_OPENED); ) {
195 if (open_dev(dev, jcr->VolCatInfo.VolCatName, READ_WRITE) < 0) {
196 if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
199 Jmsg2(jcr, M_FATAL, 0, _("Unable to open device %s. ERR=%s\n"),
200 dev_name(dev), strerror_dev(dev));
206 * Now make sure we have the right tape mounted
209 switch (read_dev_volume_label(jcr, dev, block)) {
211 Dmsg1(500, "Vol OK name=%s\n", jcr->VolumeName);
212 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
213 if (strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0) {
218 Dmsg1(500, "Vol NAME Error Name=%s\n", jcr->VolumeName);
219 /* Check if we can accept this as an anonymous volume */
220 strcpy(jcr->VolumeName, dev->VolHdr.VolName);
221 if (!dev->capabilities & CAP_ANONVOLS || !dir_get_volume_info(jcr)) {
224 Dmsg1(200, "want new name=%s\n", jcr->VolumeName);
225 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
230 Dmsg1(500, "Vol NO_LABEL or IO_ERROR name=%s\n", jcr->VolumeName);
231 /* If permitted, create a label */
232 if (dev->capabilities & CAP_LABEL) {
233 Dmsg0(190, "Create volume label\n");
234 if (!write_volume_label_to_dev(jcr, (DEVRES *)dev->device, jcr->VolumeName,
238 Jmsg(jcr, M_INFO, 0, _("Created Volume label %s on device %s.\n"),
239 jcr->VolumeName, dev_name(dev));
240 goto read_volume; /* read label we just wrote */
242 /* NOTE! Fall-through wanted. */
244 /* Send error message */
245 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
247 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume %s not found in slot %d.\n\
248 Setting slot to zero in catalog.\n"),
249 jcr->VolumeName, jcr->VolCatInfo.Slot);
250 jcr->VolCatInfo.Slot = 0; /* invalidate slot */
251 dir_update_volume_info(jcr, &jcr->VolCatInfo, 1); /* set slot */
259 * See if we have a fresh tape or tape with data.
261 * Note, if the LabelType is PRE_LABEL, it was labeled
262 * but never written. If so, rewrite the label but set as
263 * VOL_LABEL. We rewind and return the label (reconstructed)
264 * in the block so that in the case of a new tape, data can
265 * be appended just after the block label. If we are writing
266 * an second volume, the calling routine will write the label
267 * before writing the overflow block.
269 * If the tape is marked as Recycle, we rewrite the label.
271 if (dev->VolHdr.LabelType == PRE_LABEL || recycle) {
272 Dmsg1(190, "ready_for_append found freshly labeled volume. dev=%x\n", dev);
273 dev->VolHdr.LabelType = VOL_LABEL; /* set Volume label */
274 write_volume_label_to_block(jcr, dev, block);
276 * Write the block now to ensure we have write permission.
277 * It is better to find out now rather than later.
279 dev->VolCatInfo.VolCatBytes = 0;
280 if (!rewind_dev(dev)) {
281 Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"),
282 dev_name(dev), strerror_dev(dev));
285 if (!truncate_dev(dev)) {
286 Jmsg2(jcr, M_WARNING, 0, _("Truncate error on device %s. ERR=%s\n"),
287 dev_name(dev), strerror_dev(dev));
290 if (!write_block_to_dev(dev, block)) {
291 Jmsg2(jcr, M_ERROR, 0, _("Unable to write device %s. ERR=%s\n"),
292 dev_name(dev), strerror_dev(dev));
295 if (!rewind_dev(dev)) {
296 Jmsg2(jcr, M_ERROR, 0, _("Unable to rewind device %s. ERR=%s\n"),
297 dev_name(dev), strerror_dev(dev));
300 /* Recreate a correct volume label and return it in the block */
301 write_volume_label_to_block(jcr, dev, block);
302 dev->VolCatInfo.VolCatJobs = 1;
303 dev->VolCatInfo.VolCatFiles = 1;
304 dev->VolCatInfo.VolCatErrors = 0;
305 dev->VolCatInfo.VolCatBlocks = 1;
307 dev->VolCatInfo.VolCatMounts++;
308 dev->VolCatInfo.VolCatRecycles++;
310 dev->VolCatInfo.VolCatMounts = 1;
311 dev->VolCatInfo.VolCatRecycles = 0;
312 dev->VolCatInfo.VolCatWrites = 1;
313 dev->VolCatInfo.VolCatReads = 1;
315 strcpy(dev->VolCatInfo.VolCatStatus, "Append");
316 Dmsg0(100, "dir_update_vol_info. Set Append\n");
317 dir_update_volume_info(jcr, &dev->VolCatInfo, 1); /* indicate doing relabel */
319 Jmsg(jcr, M_INFO, 0, _("Recycled volume %s on device %s, all previous data lost.\n"),
320 jcr->VolumeName, dev_name(dev));
322 Jmsg(jcr, M_INFO, 0, _("Wrote label to prelabeled Volume %s on device %s\n"),
323 jcr->VolumeName, dev_name(dev));
328 * OK, at this point, we have a valid Bacula label, but
329 * we need to position to the end of the volume, since we are
330 * just now putting it into append mode.
332 Dmsg0(200, "Device previously written, moving to end of data\n");
333 Jmsg(jcr, M_INFO, 0, _("Volume %s previously written, moving to end of data.\n"),
336 Jmsg(jcr, M_ERROR, 0, _("Unable to position to end of data %s. ERR=%s\n"),
337 dev_name(dev), strerror_dev(dev));
338 Jmsg(jcr, M_INFO, 0, _("Marking Volume %s in Error in Catalog.\n"),
340 strcpy(dev->VolCatInfo.VolCatStatus, "Error");
341 Dmsg0(100, "dir_update_vol_info. Set Error.\n");
342 dir_update_volume_info(jcr, &dev->VolCatInfo, 0);
345 /* *****FIXME**** we should do some checking for files too */
346 if (dev_is_tape(dev)) {
347 Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume at file=%d.\n"), dev_file(dev));
349 * Check if we are positioned on the tape at the same place
350 * that the database says we should be.
352 if (dev->VolCatInfo.VolCatFiles != dev_file(dev) + 1) {
353 /* ****FIXME**** this should refuse to write on tape */
354 Jmsg(jcr, M_ERROR, 0, _("Hey! Num files mismatch! Volume=%d Catalog=%d\n"),
355 dev_file(dev)+1, dev->VolCatInfo.VolCatFiles);
358 /* Update Volume Info -- will be written at end of Job */
359 dev->VolCatInfo.VolCatMounts++; /* Update mounts */
360 dev->VolCatInfo.VolCatJobs++;
361 /* Return an empty block */
362 empty_block(block); /* we used it for reading so set for write */
364 dev->state |= ST_APPEND;
365 Dmsg0(100, "Normal return from read_dev_for_append\n");
369 int mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
371 Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
373 * End Of Tape -- mount next Volume (if another specified)
375 if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
376 VOL_LIST *vol = jcr->VolList;
377 /* Find next Volume */
379 for (int i=1; i<jcr->CurVolume; i++) {
382 strcpy(jcr->VolumeName, vol->VolumeName);
383 Dmsg1(400, "There is another volume %s.\n", jcr->VolumeName);
386 dev->state &= ~ST_READ;
387 if (!acquire_device_for_read(jcr, dev, block)) {
388 Emsg2(M_FATAL, 0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev),
392 return 1; /* next volume mounted */
394 Dmsg0(90, "End of Device reached.\n");
400 * Edit codes into ChangerCommand
402 * %a = archive device name
403 * %c = changer device name
412 * omsg = edited output message
413 * imsg = input string containing edit codes (%x)
414 * cmd = command string (load, unload, ...)
417 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
424 Dmsg1(200, "edit_device_codes: %s\n", imsg);
425 for (p=imsg; *p; p++) {
432 str = jcr->device->dev->dev_name;
435 str = NPRT(jcr->device->changer_name);
441 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
445 sprintf(add, "%d", jcr->VolCatInfo.Slot);
448 case 'j': /* Job name */
452 str = NPRT(jcr->VolumeName);
455 str = NPRT(jcr->client_name);
470 Dmsg1(200, "add_str %s\n", str);
471 pm_strcat(&omsg, (char *)str);
472 Dmsg1(200, "omsg=%s\n", omsg);