3 * Higher Level Device routines.
4 * Knows about Bacula tape labels and such
6 * NOTE! In general, subroutines that have the word
7 * "device" in the name do locking. Subroutines
8 * that have the word "dev" in the name do not
9 * do locking. Thus if xxx_device() calls
10 * yyy_dev(), all is OK, but if xxx_device()
11 * calls yyy_device(), everything will hang.
12 * Obviously, no zzz_dev() is allowed to call
13 * a www_device() or everything falls apart.
15 * Concerning the routines lock_device() and block_device()
16 * see the end of this module for details. In general,
17 * blocking a device leaves it in a state where all threads
18 * other than the current thread block when they attempt to
19 * lock the device. They remain suspended (blocked) until the device
20 * is unblocked. So, a device is blocked during an operation
21 * that takes a long time (initialization, mounting a new
22 * volume, ...) locking a device is done for an operation
23 * that takes a short time such as writing data to the
27 * Kern Sibbald, MM, MMI
32 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
34 This program is free software; you can redistribute it and/or
35 modify it under the terms of the GNU General Public License as
36 published by the Free Software Foundation; either version 2 of
37 the License, or (at your option) any later version.
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 General Public License for more details.
44 You should have received a copy of the GNU General Public
45 License along with this program; if not, write to the Free
46 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
51 #include "bacula.h" /* pull in global headers */
52 #include "stored.h" /* pull in Storage Deamon headers */
54 /* Forward referenced functions */
55 static int mount_next_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *label_blk, int release);
56 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
58 extern char my_name[];
59 extern int debug_level;
62 /*********************************************************************
63 * Acquire device for reading. We permit (for the moment)
64 * only one reader. We read the Volume label from the block and
65 * leave the block pointers just after the label.
67 * Returns: 0 if failed for any reason
70 int acquire_device_for_read(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
75 if (dev->state & ST_READ || dev->num_writers > 0) {
76 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy.\n"), dev_name(dev));
80 dev->state &= ~ST_LABEL; /* force reread of label */
81 block_device(dev, BST_DOING_ACQUIRE);
83 stat = ready_dev_for_read(jcr, dev, block);
91 * Acquire device for writing. We permit multiple writers.
92 * If this is the first one, we read the label.
94 * Returns: 0 if failed for any reason
97 int acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
103 Dmsg1(90, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
106 if (dev->state & ST_APPEND) {
108 * Device already in append mode
110 * Check if we have the right Volume mounted
111 * OK if AnonVols and volume info OK
112 * OK if next volume matches current volume
113 * otherwise mount desired volume obtained from
114 * dir_find_next_appendable_volume
116 strcpy(jcr->VolumeName, dev->VolHdr.VolName);
117 if (((dev->capabilities & CAP_ANONVOLS) &&
118 !dir_get_volume_info(jcr)) ||
119 (!dir_find_next_appendable_volume(jcr) ||
120 strcmp(dev->VolHdr.VolName, jcr->VolumeName) != 0)) { /* wrong tape mounted */
121 if (dev->num_writers != 0) {
122 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing with another Volume.\n"), dev_name(dev));
126 /* Wrong tape mounted, release it, then fall through to get correct one */
131 /* Not already in append mode, so mount the device */
132 if (dev->state & ST_READ) {
133 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
137 ASSERT(dev->num_writers == 0);
142 block_device(dev, BST_DOING_ACQUIRE);
144 if (!mount_next_volume(jcr, dev, block, release)) {
145 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
157 if (dev->num_writers > 1) {
158 Dmsg2(0, "Hey!!!! There are %d writers on device %s\n", dev->num_writers,
161 if (jcr->NumVolumes == 0) {
165 return 1; /* got it */
169 * This job is done, so release the device. From a Unix standpoint,
170 * the device remains open.
173 int release_device(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
176 Dmsg1(90, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
177 if (dev->state & ST_READ) {
178 dev->state &= ~ST_READ; /* clear read bit */
179 if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
180 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
185 /******FIXME**** send read volume usage statistics to director */
187 } else if (dev->num_writers > 0) {
189 Dmsg1(90, "There are %d writers in release_device\n", dev->num_writers);
190 if (dev->num_writers == 0) {
192 dir_create_job_media_record(jcr);
193 dev->VolCatInfo.VolCatFiles++; /* increment number of files */
194 /* Note! do volume update before close, which zaps VolCatInfo */
195 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
197 if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
198 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
204 dir_create_job_media_record(jcr);
205 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
208 Jmsg1(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s not in use.\n"), dev_name(dev));
217 * If release is set, we rewind the current volume,
218 * which we no longer want, and ask the user (console)
219 * to mount the next volume.
221 * Continue trying until we get it, and then ensure
222 * that we can write on it.
224 * This routine returns a 0 only if it is REALLY
225 * impossible to get the requested Volume.
227 static int mount_next_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, int release)
229 int recycle, ask, retry = 0, autochanger;
231 Dmsg0(90, "Enter mount_next_volume()\n");
235 Jmsg(jcr, M_FATAL, 0, _("Too many errors on device %s.\n"), dev_name(dev));
238 if (job_cancelled(jcr)) {
239 Jmsg(jcr, M_FATAL, 0, _("Job cancelled.\n"));
242 recycle = ask = autochanger = 0;
244 Dmsg0(500, "mount_next_volume release=1\n");
246 * First erase all memory of the current volume
250 dev->LastBlockNumWritten = 0;
251 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
252 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
253 dev->state &= ~ST_LABEL; /* label not yet read */
255 if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
256 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
262 /* If we have not closed the device, then at least rewind the tape */
263 if (dev->state & ST_OPENED) {
264 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
267 if (!rewind_dev(dev)) {
268 Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"),
269 dev_name(dev), strerror_dev(dev));
272 ask = 1; /* ask operator to mount tape */
275 * Get Director's idea of what tape we should have mounted.
277 if (!dir_find_next_appendable_volume(jcr)) {
278 ask = 1; /* we must ask */
281 release = 1; /* release if we "recurse" */
284 * Get next volume and ready it for append
285 * This code ensures that the device is ready for
286 * writing. We start from the assumption that there
287 * may not be a tape mounted.
289 * If the device is a file, we create the output
290 * file. If it is a tape, we check the volume name
291 * and move the tape to the end of data.
293 * It assumes that the device is not already in use!
297 Dmsg0(100, "Enter ready_dev_for_append\n");
299 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
301 jcr->VolFirstFile = 0; /* first update of Vol FileIndex */
303 int slot = jcr->VolCatInfo.Slot;
306 * Handle autoloaders here. If we cannot autoload it, we
307 * will fall through to ask the sysop.
309 if (dev->capabilities && CAP_AUTOCHANGER && slot <= 0) {
310 if (dir_find_next_appendable_volume(jcr)) {
311 slot = jcr->VolCatInfo.Slot;
314 Dmsg1(100, "Want changer slot=%d\n", slot);
316 if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
317 uint32_t timeout = jcr->device->changer_timeout;
318 POOLMEM *changer, *results;
321 results = get_pool_memory(PM_MESSAGE);
322 changer = get_pool_memory(PM_FNAME);
323 /* Find out what is loaded */
324 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
326 status = run_program(changer, timeout, results);
328 loaded = atoi(results);
330 loaded = -1; /* force unload */
332 Dmsg1(100, "loaded=%s\n", results);
334 /* If bad status or tape we want is not loaded, load it. */
335 if (status != 0 || loaded != slot) {
336 if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
339 /* We are going to load a new tape, so close the device */
340 force_close_dev(dev);
341 if (loaded != 0) { /* must unload drive */
342 Dmsg0(100, "Doing changer unload.\n");
343 changer = edit_device_codes(jcr, changer,
344 jcr->device->changer_command, "unload");
345 status = run_program(changer, timeout, NULL);
346 Dmsg1(100, "unload status=%d\n", status);
349 * Load the desired cassette
351 Dmsg1(100, "Doing changer load slot %d\n", slot);
352 changer = edit_device_codes(jcr, changer,
353 jcr->device->changer_command, "load");
354 status = run_program(changer, timeout, NULL);
355 Dmsg2(100, "load slot %d status=%d\n", slot, status);
357 free_pool_memory(changer);
358 free_pool_memory(results);
359 Dmsg1(100, "After changer, status=%d\n", status);
360 if (status == 0) { /* did we succeed? */
361 ask = 0; /* yes, got vol, no need to ask sysop */
362 autochanger = 1; /* tape loaded by changer */
367 if (ask && !dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
368 return 0; /* error return */
370 Dmsg1(200, "want vol=%s\n", jcr->VolumeName);
373 for ( ; !(dev->state & ST_OPENED); ) {
374 if (open_dev(dev, jcr->VolCatInfo.VolCatName, READ_WRITE) < 0) {
375 if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
378 Jmsg2(jcr, M_FATAL, 0, _("Unable to open device %s. ERR=%s\n"),
379 dev_name(dev), strerror_dev(dev));
385 * Now make sure we have the right tape mounted
388 switch (read_dev_volume_label(jcr, dev, block)) {
390 Dmsg1(500, "Vol OK name=%s\n", jcr->VolumeName);
391 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
392 if (strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0) {
397 Dmsg1(500, "Vol NAME Error Name=%s\n", jcr->VolumeName);
398 /* Check if we can accept this as an anonymous volume */
399 strcpy(jcr->VolumeName, dev->VolHdr.VolName);
400 if (!dev->capabilities & CAP_ANONVOLS || !dir_get_volume_info(jcr)) {
403 Dmsg1(200, "want new name=%s\n", jcr->VolumeName);
404 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
409 Dmsg1(500, "Vol NO_LABEL or IO_ERROR name=%s\n", jcr->VolumeName);
410 /* If permitted, create a label */
411 if (dev->capabilities & CAP_LABEL) {
412 Dmsg0(90, "Create volume label\n");
413 if (!write_volume_label_to_dev(jcr, (DEVRES *)dev->device, jcr->VolumeName,
417 Jmsg(jcr, M_INFO, 0, _("Created Volume label %s on device %s.\n"),
418 jcr->VolumeName, dev_name(dev));
419 goto read_volume; /* read label we just wrote */
421 /* NOTE! Fall-through wanted. */
423 /* Send error message */
424 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
426 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume %s not found in slot %d.\n\
427 Setting slot to zero in catalog.\n"),
428 jcr->VolumeName, jcr->VolCatInfo.Slot);
429 jcr->VolCatInfo.Slot = 0; /* invalidate slot */
430 dir_update_volume_info(jcr, &jcr->VolCatInfo, 1); /* set slot */
438 * See if we have a fresh tape or tape with data.
440 * Note, if the LabelType is PRE_LABEL, it was labeled
441 * but never written. If so, rewrite the label but set as
442 * VOL_LABEL. We rewind and return the label (reconstructed)
443 * in the block so that in the case of a new tape, data can
444 * be appended just after the block label. If we are writing
445 * an second volume, the calling routine will write the label
446 * before writing the overflow block.
448 * If the tape is marked as Recycle, we rewrite the label.
450 if (dev->VolHdr.LabelType == PRE_LABEL || recycle) {
451 Dmsg1(90, "ready_for_append found freshly labeled volume. dev=%x\n", dev);
452 dev->VolHdr.LabelType = VOL_LABEL; /* set Volume label */
453 write_volume_label_to_block(jcr, dev, block);
455 * Write the block now to ensure we have write permission.
456 * It is better to find out now rather than later.
458 dev->VolCatInfo.VolCatBytes = 0;
459 if (!rewind_dev(dev)) {
460 Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"),
461 dev_name(dev), strerror_dev(dev));
464 if (!truncate_dev(dev)) {
465 Jmsg2(jcr, M_WARNING, 0, _("Truncate error on device %s. ERR=%s\n"),
466 dev_name(dev), strerror_dev(dev));
469 if (!write_block_to_dev(dev, block)) {
470 Jmsg2(jcr, M_ERROR, 0, _("Unable to write device %s. ERR=%s\n"),
471 dev_name(dev), strerror_dev(dev));
474 if (!rewind_dev(dev)) {
475 Jmsg2(jcr, M_ERROR, 0, _("Unable to rewind device %s. ERR=%s\n"),
476 dev_name(dev), strerror_dev(dev));
479 /* Recreate a correct volume label and return it in the block */
480 write_volume_label_to_block(jcr, dev, block);
481 dev->VolCatInfo.VolCatJobs = 1;
482 dev->VolCatInfo.VolCatFiles = 1;
483 dev->VolCatInfo.VolCatErrors = 0;
484 dev->VolCatInfo.VolCatBlocks = 1;
486 dev->VolCatInfo.VolCatMounts++;
487 dev->VolCatInfo.VolCatRecycles++;
489 dev->VolCatInfo.VolCatMounts = 1;
490 dev->VolCatInfo.VolCatRecycles = 0;
491 dev->VolCatInfo.VolCatWrites = 1;
492 dev->VolCatInfo.VolCatReads = 1;
494 strcpy(dev->VolCatInfo.VolCatStatus, "Append");
495 dir_update_volume_info(jcr, &dev->VolCatInfo, 1); /* indicate doing relabel */
497 Jmsg(jcr, M_INFO, 0, _("Recycled volume %s on device %s, all previous data lost.\n"),
498 jcr->VolumeName, dev_name(dev));
500 Jmsg(jcr, M_INFO, 0, _("Wrote label to prelabeled Volume %s on device %s\n"),
501 jcr->VolumeName, dev_name(dev));
506 * OK, at this point, we have a valid Bacula label, but
507 * we need to position to the end of the volume, since we are
508 * just now putting it into append mode.
510 Dmsg0(20, "Device previously written, moving to end of data\n");
511 Jmsg(jcr, M_INFO, 0, _("Volume %s previously written, moving to end of data.\n"),
514 Jmsg(jcr, M_ERROR, 0, _("Unable to position to end of data %s. ERR=%s\n"),
515 dev_name(dev), strerror_dev(dev));
516 Jmsg(jcr, M_INFO, 0, _("Marking Volume %s in Error in Catalog.\n"),
518 strcpy(dev->VolCatInfo.VolCatStatus, "Error");
519 dir_update_volume_info(jcr, &dev->VolCatInfo, 0);
522 /* *****FIXME**** we should do some checking for files too */
523 if (dev_is_tape(dev)) {
524 Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume at file=%d.\n"), dev_file(dev));
526 * Check if we are positioned on the tape at the same place
527 * that the database says we should be.
529 if (dev->VolCatInfo.VolCatFiles != dev_file(dev) + 1) {
530 /* ****FIXME**** this should refuse to write on tape */
531 Jmsg(jcr, M_ERROR, 0, _("Hey! Num files mismatch! Volume=%d Catalog=%d\n"),
532 dev_file(dev)+1, dev->VolCatInfo.VolCatFiles);
535 /* Update Volume Info -- will be written at end of Job */
536 dev->VolCatInfo.VolCatMounts++; /* Update mounts */
537 dev->VolCatInfo.VolCatJobs++;
538 /* Return an empty block */
539 empty_block(block); /* we used it for reading so set for write */
541 dev->state |= ST_APPEND;
542 Dmsg0(100, "Normal return from read_dev_for_append\n");
547 * This routine ensures that the device is ready for
548 * reading. If it is a file, it opens it.
549 * If it is a tape, it checks the volume name
551 * Returns 0 on failure
552 * Returns 1 on success
554 int ready_dev_for_read(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
556 if (!(dev->state & ST_OPENED)) {
557 Dmsg1(20, "bstored: open vol=%s\n", jcr->VolumeName);
558 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
559 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
560 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
563 Dmsg1(29, "open_dev %s OK\n", dev_name(dev));
567 if (job_cancelled(jcr)) {
568 Mmsg0(&dev->errmsg, _("Job cancelled.\n"));
571 if (!rewind_dev(dev)) {
572 Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"),
573 dev_name(dev), strerror_dev(dev));
575 switch (read_dev_volume_label(jcr, dev, block)) {
579 /* Send error message generated by read_dev_volume_label() */
580 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
581 if (!rewind_dev(dev)) {
582 Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"),
583 dev_name(dev), strerror_dev(dev));
585 /* Mount a specific volume and no other */
586 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
587 return 0; /* error return */
589 continue; /* try reading again */
594 dev->state |= ST_READ;
599 * This is the dreaded moment. We either have an end of
600 * medium condition or worse, and error condition.
601 * Attempt to "recover" by obtaining a new Volume.
603 * We enter with device locked, and
604 * exit with device locked.
606 * Note, we are called only from one place in block.c
608 * Returns: 1 on success
611 int fixup_device_block_write_error(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
614 char PrevVolName[MAX_NAME_LENGTH];
615 DEV_BLOCK *label_blk;
619 wait_time = time(NULL);
620 status_dev(dev, &stat);
622 Dmsg0(90, "======= Got EOD ========\n");
624 block_device(dev, BST_DOING_ACQUIRE);
626 strcpy(dev->VolCatInfo.VolCatStatus, "Full");
627 Dmsg0(90, "Call update_vol_info\n");
628 /* Update position counters */
629 jcr->end_block = dev->block_num;
630 jcr->end_file = dev->file;
632 * ****FIXME**** update JobMedia record of every job using
635 if (!dir_create_job_media_record(jcr) ||
636 !dir_update_volume_info(jcr, &dev->VolCatInfo, 0)) { /* send Volume info to Director */
637 Jmsg(jcr, M_ERROR, 0, _("Could not update Volume info Volume=%s Job=%s\n"),
638 dev->VolCatInfo.VolCatName, jcr->Job);
639 return 0; /* device locked */
641 Dmsg0(90, "Back from update_vol_info\n");
643 strcpy(PrevVolName, dev->VolCatInfo.VolCatName);
644 strcpy(dev->VolHdr.PrevVolName, PrevVolName);
646 label_blk = new_block(dev);
648 /* Inform User about end of media */
649 Jmsg(jcr, M_INFO, 0, _("End of media on Volume %s Bytes=%s Blocks=%s.\n"),
650 PrevVolName, edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
651 edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2));
653 /* Unlock, but leave BLOCKED */
655 if (!mount_next_volume(jcr, dev, label_blk, 1)) {
658 return 0; /* device locked */
661 P(dev->mutex); /* lock again */
663 Jmsg(jcr, M_INFO, 0, _("New volume %s mounted on device %s\n"),
664 jcr->VolumeName, dev_name(dev));
667 * If this is a new tape, the label_blk will contain the
668 * label, so write it now. If this is a previously
669 * used tape, mount_next_volume() will return an
670 * empty label_blk, and nothing will be written.
672 Dmsg0(90, "write label block to dev\n");
673 if (!write_block_to_dev(dev, label_blk)) {
674 Dmsg1(0, "write_block_to_device Volume label failed. ERR=%s",
676 free_block(label_blk);
678 return 0; /* device locked */
681 /* Write overflow block to tape */
682 Dmsg0(90, "Write overflow block to dev\n");
683 if (!write_block_to_dev(dev, block)) {
684 Dmsg1(0, "write_block_to_device overflow block failed. ERR=%s",
686 free_block(label_blk);
688 return 0; /* device locked */
692 Dmsg0(90, "Wake up any waiting threads.\n");
693 free_block(label_blk);
694 /* Set new start/end positions */
695 jcr->start_block = dev->block_num;
696 jcr->start_file = dev->file;
698 jcr->run_time += time(NULL) - wait_time; /* correct run time */
699 return 1; /* device locked */
701 free_block(label_blk);
702 return 0; /* device locked */
707 * Open the device. Expect dev to already be initialized.
709 * This routine is used only when the Storage daemon starts
710 * and always_open is set, and in the stand-alone utility
711 * routines such as bextract.
713 * Note, opening of a normal file is deferred to later so
714 * that we can get the filename; the device_name for
715 * a file is the directory only.
717 * Retuns: 0 on failure
720 int open_device(DEVICE *dev)
722 Dmsg0(20, "start open_output_device()\n");
729 /* Defer opening files */
730 if (!dev_is_tape(dev)) {
731 Dmsg0(29, "Device is file, deferring open.\n");
736 if (!(dev->state & ST_OPENED)) {
737 Dmsg0(29, "Opening device.\n");
738 if (open_dev(dev, NULL, READ_WRITE) < 0) {
739 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
744 Dmsg1(29, "open_dev %s OK\n", dev_name(dev));
752 * When dev_blocked is set, all threads EXCEPT thread with id no_wait_id
753 * must wait. The no_wait_id thread is out obtaining a new volume
754 * and preparing the label.
756 void lock_device(DEVICE *dev)
760 Dmsg1(90, "lock %d\n", dev->dev_blocked);
762 if (dev->dev_blocked && !pthread_equal(dev->no_wait_id, pthread_self())) {
763 dev->num_waiting++; /* indicate that I am waiting */
764 while (dev->dev_blocked) {
765 if ((stat = pthread_cond_wait(&dev->wait, &dev->mutex)) != 0) {
767 Emsg1(M_ABORT, 0, _("pthread_cond_wait failure. ERR=%s\n"),
771 dev->num_waiting--; /* no longer waiting */
775 void unlock_device(DEVICE *dev)
777 Dmsg0(90, "unlock\n");
782 * Block all other threads from using the device
783 * Device must already be locked. After this call,
784 * the device is blocked to any thread calling lock_device(),
785 * but the device is not locked (i.e. no P on device). Also,
786 * the current thread can do slip through the lock_device()
787 * calls without blocking.
789 void block_device(DEVICE *dev, int state)
791 Dmsg1(90, "block set %d\n", state);
792 ASSERT(dev->dev_blocked == BST_NOT_BLOCKED);
793 dev->dev_blocked = state; /* make other threads wait */
794 dev->no_wait_id = pthread_self(); /* allow us to continue */
798 * Unblock the device, and wake up anyone who went to sleep.
800 void unblock_device(DEVICE *dev)
802 Dmsg1(90, "unblock %d\n", dev->dev_blocked);
803 ASSERT(dev->dev_blocked);
804 dev->dev_blocked = BST_NOT_BLOCKED;
805 if (dev->num_waiting > 0) {
806 pthread_cond_broadcast(&dev->wait); /* wake them up */
813 * Edit codes into ChangerCommand
815 * %a = archive device name
816 * %c = changer device name
825 * omsg = edited output message
826 * imsg = input string containing edit codes (%x)
827 * cmd = command string (load, unload, ...)
830 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
836 Dmsg1(200, "edit_job_codes: %s\n", imsg);
839 for (p=imsg; *p; p++) {
848 str = jcr->device->dev->dev_name;
851 str = NPRT(jcr->device->changer_name);
857 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
861 sprintf(add, "%d", jcr->VolCatInfo.Slot);
864 case 'j': /* Job name */
868 str = jcr->VolumeName;
874 str = jcr->client_name;
891 Dmsg1(200, "add_str %s\n", str);
892 add_str_to_pool_mem(&omsg, &o, (char *)str);
894 Dmsg1(200, "omsg=%s\n", omsg);