]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
d956494a103b38e51941b6fbc879b0966358bac0
[bacula/bacula] / bacula / src / stored / read.c
1 /*
2  * Read code for Storage daemon
3  *     Kern Sibbald, November MM
4  *
5  *   Version $Id$
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30 /* Forward referenced subroutines */
31
32 /* Variables used by Child process */
33 /* Global statistics */
34 /* Note, these probably should be in shared memory so that 
35  * they are truly global for all processes
36  */
37 extern struct s_shm *shm;             /* shared memory structure */
38 extern int  FiledDataChan;            /* File daemon data channel (port) */
39
40
41 /* Responses sent to the File daemon */
42 static char OK_data[]    = "3000 OK data\n";
43 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
44
45 /* 
46  *  Read Data and send to File Daemon
47  *   Returns: 0 on failure
48  *            1 on success
49  */
50 int do_read_data(JCR *jcr) 
51 {
52    BSOCK *ds;
53    BSOCK *fd_sock = jcr->file_bsock;
54    int ok = TRUE;
55    DEVICE *dev;
56    DEV_RECORD rec;
57    DEV_BLOCK *block;
58    char *hdr, *p;
59
60    
61    Dmsg0(20, "Start read data.\n");
62
63    dev = jcr->device->dev;
64
65    /* Tell File daemon we will send data */
66    bnet_fsend(fd_sock, OK_data);
67    Dmsg1(10, "bstored>filed: %s\n", fd_sock->msg);
68
69    ds = fd_sock;
70
71    if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
72       return 0;
73    }
74
75
76    Dmsg1(20, "Begin read device=%s\n", dev_name(dev));
77
78    block = new_block(dev);
79
80    /* Find out if we were passed multiple volumes */
81    jcr->NumVolumes = 1;
82    jcr->CurVolume = 1;
83    /* Scan through VolumeNames terminating them and counting them */
84    for (p = jcr->VolumeName; p && *p; ) {
85       p = strchr(p, '|');             /* volume name separator */
86       if (p) {
87          *p++ = 0;                    /* Terminate name */
88          jcr->NumVolumes++;
89       }
90    }
91
92    Dmsg1(20, "Found %d volumes names to restore.\n", jcr->NumVolumes);
93
94    /* 
95     * Ready device for reading, and read records
96     */
97    if (!acquire_device_for_read(jcr, dev, block)) {
98       free_block(block);
99       return 0;
100    }
101
102    memset(&rec, 0, sizeof(rec));
103    rec.data = ds->msg;                /* use socket message buffer */
104    hdr = (char *) get_pool_memory(PM_MESSAGE);
105
106    /*
107     * ****FIXME**** enhance this to look for 
108     *               more things than just a session.
109     */
110    for ( ;ok; ) {
111       DEV_RECORD *record;             /* for reading label of multi-volumes */
112       SESSION_LABEL sessrec;           /* session record */
113
114       if (job_cancelled(jcr)) {
115          ok = FALSE;
116          break;
117       }
118       /* Read Record */
119       Dmsg1(500, "Main read_record. rem=%d\n", rec.remainder);
120       if (!read_record(dev, block, &rec)) {
121          Dmsg1(500, "Main read record failed. rem=%d\n", rec.remainder);
122          if (dev->state & ST_EOT) {
123             if (rec.remainder) {
124                Dmsg0(500, "Not end of record.\n");
125             }
126             Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
127             if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
128                close_dev(dev);
129                for (p=jcr->VolumeName; *p++; ) /* skip to next volume name */
130                   { }
131                jcr->CurVolume++;
132                Dmsg1(20, "There is another volume %s.\n", p);
133                strcpy(jcr->VolumeName, p);
134                dev->state &= ~ST_READ; 
135                if (!acquire_device_for_read(jcr, dev, block)) {
136                   Emsg2(M_ERROR, 0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), p);
137                   ok = FALSE;
138                   break;
139                }
140                record = new_record();
141                Dmsg1(500, "read record after new tape. rem=%d\n", record->remainder);
142                read_record(dev, block, record); /* read vol label */
143                dump_label_record(dev, record, 0);
144                free_record(record);
145                continue;
146             }
147             Dmsg0(90, "End of Device reached.\n");
148             break;                    /* End of Tape */
149          }
150          if (dev->state & ST_EOF) {
151             Dmsg0(90, "Got End of File. Trying again ...\n");
152             continue;                 /* End of File */
153          }
154
155          Emsg2(M_ABORT, 0, "Read error on Record Header %s ERR=%s\n", dev_name(dev), strerror(errno));
156       }
157
158       /* Some sort of label? */ 
159       if (rec.FileIndex < 0) {
160          char *rtype;
161          switch (rec.FileIndex) {
162             case PRE_LABEL:
163                rtype = "Fresh Volume Label";   
164                break;
165             case VOL_LABEL:
166                rtype = "Volume Label";
167                unser_volume_label(dev, &rec);
168                break;
169             case SOS_LABEL:
170                rtype = "Begin Session";
171                unser_session_label(&sessrec, &rec);
172                break;
173             case EOS_LABEL:
174                rtype = "End Session";
175                break;
176             case EOM_LABEL:
177                rtype = "End of Media";
178                break;
179             default:
180                rtype = "Unknown";
181                break;
182          }
183          if (debug_level > 0) {
184             printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
185                rtype, rec.VolSessionId, rec.VolSessionTime, rec.Stream, rec.data_len);
186          }
187
188          Dmsg1(40, "Got label = %d\n", rec.FileIndex);
189          if (rec.FileIndex == EOM_LABEL) { /* end of tape? */
190             Dmsg0(40, "Get EOM LABEL\n");
191             break;                         /* yes, get out */
192          }
193          continue;                         /* ignore other labels */
194       }
195
196       /* ****FIXME***** make sure we REALLY have a session record */
197       if (jcr->bsr && !match_bsr(jcr->bsr, &rec, &dev->VolHdr, &sessrec)) {
198          Dmsg0(50, "BSR rejected record\n");
199          continue;
200       }
201
202       if (rec.VolSessionId != jcr->read_VolSessionId ||
203           rec.VolSessionTime != jcr->read_VolSessionTime) {
204          Dmsg0(50, "Ignore record ids not equal\n");
205          continue;                    /* ignore */
206       }
207        
208       /* Generate Header parameters and send to File daemon
209        * Note, we build header in hdr buffer to avoid wiping
210        * out the data record
211        */
212       ds->msg = hdr;
213       if (!bnet_fsend(ds, rec_header, rec.VolSessionId, rec.VolSessionTime,
214              rec.FileIndex, rec.Stream, rec.data_len)) {
215          Dmsg1(30, ">filed: Error Hdr=%s\n", ds->msg);
216          ds->msg = rec.data;
217          ok = FALSE;
218          break;
219       } else {
220          Dmsg1(30, ">filed: Hdr=%s\n", ds->msg);
221       }
222
223       ds->msg = rec.data;             /* restore data record address */
224
225       /* Send data record to File daemon */
226       ds->msglen = rec.data_len;
227       Dmsg1(40, ">filed: send %d bytes data.\n", ds->msglen);
228       if (!bnet_send(ds)) {
229          Dmsg0(0, "Error sending to FD\n");
230          Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
231             bnet_strerror(ds));
232          ok = FALSE;
233       }
234    }
235    /* Send end of data to FD */
236    bnet_sig(ds, BNET_EOF);
237
238    if (!release_device(jcr, dev, block)) {
239       ok = FALSE;
240    }
241    free_pool_memory(hdr);
242    free_block(block);
243    Dmsg0(30, "Done reading.\n");
244    return ok ? 1 : 0;
245 }