]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/acquire.c
51827a3351937c69ee0a414f01541801363d7c68
[bacula/bacula] / bacula / src / stored / acquire.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Routines to acquire and release a device for read/write
30  *
31  *   Kern Sibbald, August MMII
32  *
33  *   Version $Id$
34  */
35
36 #include "bacula.h"                   /* pull in global headers */
37 #include "stored.h"                   /* pull in Storage Deamon headers */
38
39 /* Forward referenced functions */
40 static void attach_dcr_to_dev(DCR *dcr);
41
42
43 /*********************************************************************
44  * Acquire device for reading. 
45  *  The drive should have previously been reserved by calling 
46  *  reserve_device_for_read(). We read the Volume label from the block and
47  *  leave the block pointers just after the label.
48  *
49  *  Returns: NULL if failed for any reason
50  *           dcr  if successful
51  */
52 bool acquire_device_for_read(DCR *dcr)
53 {
54    DEVICE *dev = dcr->dev;
55    JCR *jcr = dcr->jcr;
56    bool ok = false;
57    bool tape_previously_mounted;
58    bool tape_initially_mounted;
59    VOL_LIST *vol;
60    bool try_autochanger = true;
61    int i;
62    int vol_label_status;
63    int retry = 0;
64    
65    Dmsg1(50, "jcr->dcr=%p\n", jcr->dcr);
66    dev->dblock(BST_DOING_ACQUIRE);
67
68    if (dev->num_writers > 0) {
69       Jmsg2(jcr, M_FATAL, 0, _("Acquire read: num_writers=%d not zero. Job %d canceled.\n"), 
70          dev->num_writers, jcr->JobId);
71       goto get_out;
72    }
73
74    /* Find next Volume, if any */
75    vol = jcr->VolList;
76    if (!vol) {
77       char ed1[50];
78       Jmsg(jcr, M_FATAL, 0, _("No volumes specified for reading. Job %s canceled.\n"), 
79          edit_int64(jcr->JobId, ed1));
80       goto get_out;
81    }
82    jcr->CurReadVolume++;
83    for (i=1; i<jcr->CurReadVolume; i++) {
84       vol = vol->next;
85    }
86    if (!vol) {
87       Jmsg(jcr, M_FATAL, 0, _("Logic error: no next volume to read. Numvol=%d Curvol=%d\n"),
88          jcr->NumReadVolumes, jcr->CurReadVolume);
89       goto get_out;                   /* should not happen */   
90    }
91    bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
92    bstrncpy(dcr->media_type, vol->MediaType, sizeof(dcr->media_type));
93    dcr->VolCatInfo.Slot = vol->Slot;
94     
95    /*
96     * If the MediaType requested for this volume is not the
97     *  same as the current drive, we attempt to find the same
98     *  device that was used to write the orginal volume.  If
99     *  found, we switch to using that device.
100     */
101    Dmsg2(50, "MediaType dcr=%s dev=%s\n", dcr->media_type, dev->device->media_type);
102    if (dcr->media_type[0] && strcmp(dcr->media_type, dev->device->media_type) != 0) {
103       RCTX rctx;
104       DIRSTORE *store;
105       int stat;
106
107       Jmsg3(jcr, M_INFO, 0, _("Changing device. Want Media Type=\"%s\" have=\"%s\"\n"
108                               "  device=%s\n"), 
109             dcr->media_type, dev->device->media_type, dev->print_name());
110
111       dev->dunblock(DEV_UNLOCKED);
112       detach_dcr_from_dev(dcr);    /* release old device */
113
114       lock_reservations();
115       memset(&rctx, 0, sizeof(RCTX));
116       rctx.jcr = jcr;
117       jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
118       rctx.any_drive = true;
119       rctx.device_name = vol->device;
120       store = new DIRSTORE;
121       memset(store, 0, sizeof(DIRSTORE));
122       store->name[0] = 0; /* No dir name */
123       bstrncpy(store->media_type, vol->MediaType, sizeof(store->media_type));
124       bstrncpy(store->pool_name, dcr->pool_name, sizeof(store->pool_name));
125       bstrncpy(store->pool_type, dcr->pool_type, sizeof(store->pool_type));
126       store->append = false;
127       rctx.store = store;
128       
129       /*
130        * Note, if search_for_device() succeeds, we get a new dcr,
131        *  which we do not use except for the dev info.
132        */
133       stat = search_res_for_device(rctx);
134       release_reserve_messages(jcr);         /* release queued messages */
135       unlock_reservations();
136       if (stat == 1) {
137          dcr->VolumeName[0] = 0;
138          dev->dblock(BST_DOING_ACQUIRE); 
139          Jmsg(jcr, M_INFO, 0, _("Media Type change.  New device %s chosen.\n"),
140             dev->print_name());
141          bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
142          bstrncpy(dcr->media_type, vol->MediaType, sizeof(dcr->media_type));
143          dcr->VolCatInfo.Slot = vol->Slot;
144          bstrncpy(dcr->pool_name, store->pool_name, sizeof(dcr->pool_name));
145          bstrncpy(dcr->pool_type, store->pool_type, sizeof(dcr->pool_type));
146       } else if (stat == 0) {   /* device busy */
147          Pmsg1(000, "Device %s is busy.\n", vol->device);
148       } else {
149          /* error */
150          Jmsg1(jcr, M_FATAL, 0, _("No suitable device found to read Volume \"%s\"\n"),
151             vol->VolumeName);
152          goto get_out;
153       }
154    }
155
156
157    init_device_wait_timers(dcr);
158
159    tape_previously_mounted = dev->can_read() || dev->can_append() ||
160                              dev->is_labeled();
161    tape_initially_mounted = tape_previously_mounted;
162
163
164    /* Volume info is always needed because of VolParts */
165    Dmsg0(200, "dir_get_volume_info\n");
166    if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
167       Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
168    }
169    
170    for ( ;; ) {
171       /* If not polling limit retries */
172       if (!dev->poll && retry++ > 10) {
173          break;
174       }
175       dev->clear_labeled();              /* force reread of label */
176       if (job_canceled(jcr)) {
177          char ed1[50];
178          Mmsg1(dev->errmsg, _("Job %s canceled.\n"), edit_int64(jcr->JobId, ed1));
179          Jmsg(jcr, M_INFO, 0, dev->errmsg);
180          goto get_out;                /* error return */
181       }
182
183       autoload_device(dcr, 0, NULL);
184
185       /*
186        * This code ensures that the device is ready for
187        * reading. If it is a file, it opens it.
188        * If it is a tape, it checks the volume name
189        */
190       Dmsg1(100, "bstored: open vol=%s\n", dcr->VolumeName);
191       if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
192         Jmsg3(jcr, M_WARNING, 0, _("Read open device %s Volume \"%s\" failed: ERR=%s\n"),
193               dev->print_name(), dcr->VolumeName, dev->bstrerror());
194          goto default_path;
195       }
196       Dmsg1(100, "opened dev %s OK\n", dev->print_name());
197       
198       /* Read Volume Label */
199       
200       Dmsg0(200, "calling read-vol-label\n");
201       vol_label_status = read_dev_volume_label(dcr);
202       switch (vol_label_status) {
203       case VOL_OK:
204          ok = true;
205          memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
206          break;                    /* got it */
207       case VOL_IO_ERROR:
208          /*
209           * Send error message generated by read_dev_volume_label()
210           *  only we really had a tape mounted. This supresses superfluous
211           *  error messages when nothing is mounted.
212           */
213          if (tape_previously_mounted) {
214             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
215          }
216          goto default_path;
217       case VOL_NAME_ERROR:
218          if (tape_initially_mounted) {
219             tape_initially_mounted = false;
220             goto default_path;
221          }
222          /* If polling and got a previous bad name, ignore it */
223          if (dev->poll && strcmp(dev->BadVolName, dev->VolHdr.VolumeName) == 0) {
224             goto default_path;
225          } else {
226              bstrncpy(dev->BadVolName, dev->VolHdr.VolumeName, sizeof(dev->BadVolName));
227          }
228          /* Fall through */
229       default:
230          Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
231 default_path:
232          tape_previously_mounted = true;
233          
234          /*
235           * If the device requires mount, close it, so the device can be ejected.
236           */
237          if (dev->requires_mount()) {
238             dev->close();
239          }
240          
241          /* Call autochanger only once unless ask_sysop called */
242          if (try_autochanger) {
243             int stat;
244             Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
245                dcr->VolumeName, dcr->VolCatInfo.Slot);
246             stat = autoload_device(dcr, 0, NULL);
247             if (stat > 0) {
248                try_autochanger = false;
249                continue;              /* try reading volume mounted */
250             }
251          }
252          
253          /* Mount a specific volume and no other */
254          Dmsg0(200, "calling dir_ask_sysop\n");
255          if (!dir_ask_sysop_to_mount_volume(dcr)) {
256             goto get_out;             /* error return */
257          }
258          try_autochanger = true;      /* permit using autochanger again */
259          continue;                    /* try reading again */
260       } /* end switch */
261       break;
262    } /* end for loop */
263    if (!ok) {
264       Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s for reading.\n"),
265             dev->print_name());
266       goto get_out;
267    }
268
269    dev->clear_append();
270    dev->set_read();
271    set_jcr_job_status(jcr, JS_Running);
272    dir_send_job_status(jcr);
273    Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
274       dcr->VolumeName, dev->print_name());
275
276 get_out:
277    dev->dlock();
278    if (dcr->reserved_device) {
279       dev->reserved_device--;
280       Dmsg2(100, "Dec reserve=%d dev=%s\n", dev->reserved_device, dev->print_name());
281       dcr->reserved_device = false;
282    }
283    dev->dunblock(DEV_LOCKED);
284    Dmsg1(50, "jcr->dcr=%p\n", jcr->dcr);
285    return ok;
286 }
287
288
289 /*
290  * Acquire device for writing. We permit multiple writers.
291  *  If this is the first one, we read the label.
292  *
293  *  Returns: NULL if failed for any reason
294  *           dcr if successful.
295  *   Note, normally reserve_device_for_append() is called
296  *   before this routine.
297  */
298 DCR *acquire_device_for_append(DCR *dcr)
299 {
300    bool release = false;
301    bool recycle = false;
302    bool do_mount = false;
303    DEVICE *dev = dcr->dev;
304    JCR *jcr = dcr->jcr;
305
306    init_device_wait_timers(dcr);
307
308    dev->dblock(BST_DOING_ACQUIRE);
309    Dmsg1(190, "acquire_append device is %s\n", dev->is_tape()?"tape":
310         (dev->is_dvd()?"DVD":"disk"));
311
312    /*
313     * With the reservation system, this should not happen
314     */
315    if (dev->can_read()) {
316       Jmsg1(jcr, M_FATAL, 0, _("Want to append, but device %s is busy reading.\n"), dev->print_name());
317       Dmsg1(200, "Want to append but device %s is busy reading.\n", dev->print_name());
318       goto get_out;
319    }
320
321    if (dev->can_append()) {
322       Dmsg0(190, "device already in append.\n");
323       /*
324        * Device already in append mode
325        *
326        * Check if we have the right Volume mounted
327        *   OK if current volume info OK
328        *   OK if next volume matches current volume
329        *   otherwise mount desired volume obtained from
330        *    dir_find_next_appendable_volume
331        *  dev->VolHdr.VolumeName is what is in the drive
332        *  dcr->VolumeName is what we pass into the routines, or
333        *    get back from the subroutines.
334        */
335       bstrncpy(dcr->VolumeName, dev->VolHdr.VolumeName, sizeof(dcr->VolumeName));
336       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
337           !(dir_find_next_appendable_volume(dcr) &&
338             strcmp(dev->VolHdr.VolumeName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
339          Dmsg2(190, "Wrong tape mounted: %s. wants:%s\n", dev->VolHdr.VolumeName,
340             dcr->VolumeName);
341          /* Release volume reserved by dir_find_next_appendable_volume() */
342          if (dcr->VolumeName[0]) {
343             volume_unused(dcr);
344          }
345          if (dev->num_writers != 0) {
346             Jmsg3(jcr, M_FATAL, 0, _("Wanted to append to Volume \"%s\", but device %s is busy writing on \"%s\" .\n"), 
347                  dcr->VolumeName, dev->print_name(), dev->VolHdr.VolumeName);
348             Dmsg3(200, "Wanted to append to Volume \"%s\", but device %s is busy writing on \"%s\" .\n",  
349                  dcr->VolumeName, dev->print_name(), dev->VolHdr.VolumeName);
350             goto get_out;
351          }
352          /* Wrong tape mounted, release it, then fall through to get correct one */
353          Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
354          release = true;
355          do_mount = true;
356       } else {
357          /*
358           * At this point, the correct tape is already mounted, so
359           *   we do not need to do mount_next_write_volume(), unless
360           *   we need to recycle the tape.
361           */
362           recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
363           Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
364           if (recycle && dev->num_writers != 0) {
365              Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
366                   " on device %s because it is in use by another job.\n"),
367                   dev->VolHdr.VolumeName, dev->print_name());
368              goto get_out;
369           }
370           if (dev->num_writers == 0) {
371              memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
372           }
373
374           /*
375            *      Insanity check 
376            *
377            * Check to see if the tape position as defined by the OS is
378            *  the same as our concept.  If it is not, we bail out, because
379            *  it means the user has probably manually rewound the tape.
380            * Note, we check only if num_writers == 0, but this code will
381            *  also work fine for any number of writers. If num_writers > 0,
382            *  we probably should cancel all jobs using this device, or 
383            *  perhaps even abort the SD, or at a minimum, mark the tape
384            *  in error.  Another strategy with num_writers == 0, would be
385            *  to rewind the tape and do a new eod() request.
386            */
387           if (dev->is_tape() && dev->num_writers == 0) {
388              int32_t file = dev->get_os_tape_file();
389              if (file >= 0 && file != (int32_t)dev->get_file()) {
390                 Jmsg(jcr, M_FATAL, 0, _("Invalid tape position on volume \"%s\"" 
391                      " on device %s. Expected %d, got %d\n"), 
392                      dev->VolHdr.VolumeName, dev->print_name(), dev->get_file(), file);
393                 goto get_out;
394              }
395           }
396       }
397    } else {
398       /* Not already in append mode, so mount the device */
399       Dmsg0(190, "Not in append mode, try mount.\n");
400       ASSERT(dev->num_writers == 0);
401       do_mount = true;
402    }
403
404    if (do_mount || recycle) {
405       Dmsg0(190, "Do mount_next_write_vol\n");
406       bool mounted = mount_next_write_volume(dcr, release);
407       if (!mounted) {
408          if (!job_canceled(jcr)) {
409             /* Reduce "noise" -- don't print if job canceled */
410             Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
411                dev->print_name());
412             Dmsg1(200, "Could not ready device %s for append.\n", 
413                dev->print_name());
414          }
415          goto get_out;
416       }
417       Dmsg2(190, "Output pos=%u:%u\n", dcr->dev->file, dcr->dev->block_num);
418    }
419
420    dev->num_writers++;                /* we are now a writer */
421    if (jcr->NumWriteVolumes == 0) {
422       jcr->NumWriteVolumes = 1;
423    }
424    dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs on vol */
425    dir_update_volume_info(dcr, false);        /* send Volume info to Director */
426    dev->dlock();
427    if (dcr->reserved_device) {
428       dev->reserved_device--;
429       Dmsg2(100, "Dec reserve=%d dev=%s\n", dev->reserved_device, dev->print_name());
430       dcr->reserved_device = false;
431    }
432    dev->dunblock(DEV_LOCKED);
433    return dcr;
434
435 /*
436  * Error return
437  */
438 get_out:
439    dev->dlock();
440    if (dcr->reserved_device) {
441       dev->reserved_device--;
442       Dmsg2(100, "Dec reserve=%d dev=%s\n", dev->reserved_device, dev->print_name());
443       dcr->reserved_device = false;
444    }
445    dev->dunblock(DEV_LOCKED);
446    return NULL;
447 }
448
449
450 /*
451  * This job is done, so release the device. From a Unix standpoint,
452  *  the device remains open.
453  *
454  * Note, if we are spooling, we may enter with the device locked.
455  * However, in all cases, unlock the device when leaving.
456  *
457  */
458 bool release_device(DCR *dcr)
459 {
460    JCR *jcr = dcr->jcr;
461    DEVICE *dev = dcr->dev;
462    bool ok = true;
463
464    /* lock only if not already locked by this thread */
465    if (!dcr->is_dev_locked()) {
466       dev->r_dlock();
467    }
468    Dmsg2(100, "release_device device %s is %s\n", dev->print_name(), dev->is_tape()?"tape":"disk");
469
470    /* if device is reserved, job never started, so release the reserve here */
471    if (dcr->reserved_device) {
472       dev->reserved_device--;
473       Dmsg2(100, "Dec reserve=%d dev=%s\n", dev->reserved_device, dev->print_name());
474       dcr->reserved_device = false;
475    }
476
477    if (dev->can_read()) {
478       dev->clear_read();              /* clear read bit */
479       Dmsg0(100, "dir_update_vol_info. Release0\n");
480       dir_update_volume_info(dcr, false); /* send Volume info to Director */
481
482    } else if (dev->num_writers > 0) {
483       /* 
484        * Note if WEOT is set, we are at the end of the tape
485        *   and may not be positioned correctly, so the
486        *   job_media_record and update_vol_info have already been
487        *   done, which means we skip them here.
488        */
489       dev->num_writers--;
490       Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
491       if (dev->is_labeled()) {
492          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
493          if (!dev->at_weot() && !dir_create_jobmedia_record(dcr)) {
494             Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
495                dcr->VolCatInfo.VolCatName, jcr->Job);
496          }
497          /* If no more writers, write an EOF */
498          if (!dev->num_writers && dev->can_write() && dev->block_num > 0) {
499             dev->weof(1);
500             write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolumeName);
501          }
502          if (!dev->at_weot()) {
503             dev->VolCatInfo.VolCatFiles = dev->file;   /* set number of files */
504             /* Note! do volume update before close, which zaps VolCatInfo */
505             Dmsg0(100, "dir_update_vol_info. Release0\n");
506             dir_update_volume_info(dcr, false); /* send Volume info to Director */
507          }
508       }
509
510    } else {
511       /*                
512        * If we reach here, it is most likely because the job
513        *   has failed, since the device is not in read mode and
514        *   there are no writers. It was probably reserved.
515        */
516    }
517
518    /* If no writers, close if file or !CAP_ALWAYS_OPEN */
519    if (dev->num_writers == 0 && (!dev->is_tape() || !dev->has_cap(CAP_ALWAYSOPEN))) {
520       dvd_remove_empty_part(dcr);        /* get rid of any empty spool part */
521       dev->close();
522    }
523
524    /* Fire off Alert command and include any output */
525    if (!job_canceled(jcr) && dcr->device->alert_command) {
526       POOLMEM *alert;
527       int status = 1;
528       BPIPE *bpipe;
529       char line[MAXSTRING];
530       alert = get_pool_memory(PM_FNAME);
531       alert = edit_device_codes(dcr, alert, dcr->device->alert_command, "");
532       bpipe = open_bpipe(alert, 0, "r");
533       if (bpipe) {
534          while (fgets(line, sizeof(line), bpipe->rfd)) {
535             Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
536          }
537          status = close_bpipe(bpipe);
538       } else {
539          status = errno;
540       }
541       if (status != 0) {
542          berrno be;
543          Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
544               alert, be.bstrerror(status));
545       }
546
547       Dmsg1(400, "alert status=%d\n", status);
548       free_pool_memory(alert);
549    }
550    pthread_cond_broadcast(&dev->wait_next_vol);
551    Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
552    pthread_cond_broadcast(&wait_device_release);
553    dev->dunlock();
554    if (jcr->read_dcr == dcr) {
555       jcr->read_dcr = NULL;
556    }
557    if (jcr->dcr == dcr) {
558       jcr->dcr = NULL;
559    }
560    free_dcr(dcr);
561    Dmsg2(100, "===== Device %s released by JobId=%u\n", dev->print_name(),
562          (uint32_t)jcr->JobId);
563    return ok;
564 }
565
566 /*
567  * Create a new Device Control Record and attach
568  *   it to the device (if this is a real job).
569  * Note, this has been updated so that it can be called first 
570  *   without a DEVICE, then a second or third time with a DEVICE,
571  *   and each time, it should cleanup and point to the new device.
572  *   This should facilitate switching devices.
573  * Note, each dcr must point to the controlling job (jcr).  However,
574  *   a job can have multiple dcrs, so we must not store in the jcr's
575  *   structure as previously. The higher level routine must store
576  *   this dcr in the right place
577  *
578  */
579 DCR *new_dcr(JCR *jcr, DCR *dcr, DEVICE *dev)
580 {
581    if (!dcr) {
582       dcr = (DCR *)malloc(sizeof(DCR));
583       memset(dcr, 0, sizeof(DCR));
584       dcr->tid = pthread_self();
585       dcr->spool_fd = -1;
586    }
587    dcr->jcr = jcr;                 /* point back to jcr */
588    /* Set device information, possibly change device */
589    if (dev) {
590       if (dcr->block) {
591          free_block(dcr->block);
592       }
593       dcr->block = new_block(dev);
594       if (dcr->rec) {
595          free_record(dcr->rec);
596       }
597       dcr->rec = new_record();
598       if (dcr->attached_to_dev) {
599          detach_dcr_from_dev(dcr);
600       }
601       dcr->max_job_spool_size = dev->device->max_job_spool_size;
602       dcr->device = dev->device;
603       dcr->dev = dev;
604       attach_dcr_to_dev(dcr);
605    }
606    return dcr;
607 }
608
609 /*
610  * Search the dcrs list for the given dcr. If it is found,
611  *  as it should be, then remove it. Also zap the jcr pointer
612  *  to the dcr if it is the same one.
613  *
614  * Note, this code will be turned on when we can write to multiple
615  *  dcrs at the same time.
616  */
617 #ifdef needed
618 static void remove_dcr_from_dcrs(DCR *dcr)
619 {
620    JCR *jcr = dcr->jcr;
621    if (jcr->dcrs) {
622       int i = 0;
623       DCR *ldcr;
624       int num = jcr->dcrs->size();
625       for (i=0; i < num; i++) {
626          ldcr = (DCR *)jcr->dcrs->get(i);
627          if (ldcr == dcr) {
628             jcr->dcrs->remove(i);
629             if (jcr->dcr == dcr) {
630                jcr->dcr = NULL;
631             }
632          }
633       }
634    }
635 }
636 #endif
637
638 static void attach_dcr_to_dev(DCR *dcr)
639 {
640    DEVICE *dev = dcr->dev;
641    JCR *jcr = dcr->jcr;
642
643    if (jcr) Dmsg1(500, "JobId=%u enter attach_dcr_to_dev\n", (uint32_t)jcr->JobId);
644    if (!dcr->attached_to_dev && dev->initiated && jcr && jcr->JobType != JT_SYSTEM) {
645       dev->attached_dcrs->append(dcr);  /* attach dcr to device */
646       dcr->attached_to_dev = true;
647       Dmsg1(500, "JobId=%u attach_dcr_to_dev\n", (uint32_t)jcr->JobId);
648    }
649 }
650
651 void detach_dcr_from_dev(DCR *dcr)
652 {
653    DEVICE *dev = dcr->dev;
654    Dmsg1(500, "JobId=%u enter detach_dcr_from_dev\n", (uint32_t)dcr->jcr->JobId);
655
656    /* Detach this dcr only if attached */
657    if (dcr->attached_to_dev && dev) {
658       dev->dlock();
659       unreserve_device(dcr);
660       dcr->dev->attached_dcrs->remove(dcr);  /* detach dcr from device */
661       dcr->attached_to_dev = false;
662 //    remove_dcr_from_dcrs(dcr);      /* remove dcr from jcr list */
663       dev->dunlock();
664    }
665 }
666
667 /*
668  * Free up all aspects of the given dcr -- i.e. dechain it,
669  *  release allocated memory, zap pointers, ...
670  */
671 void free_dcr(DCR *dcr)
672 {
673    JCR *jcr = dcr->jcr;
674
675    detach_dcr_from_dev(dcr);
676
677    if (dcr->block) {
678       free_block(dcr->block);
679    }
680    if (dcr->rec) {
681       free_record(dcr->rec);
682    }
683    if (jcr && jcr->dcr == dcr) {
684       jcr->dcr = NULL;
685    }
686    free(dcr);
687 }