]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
8d3c719de873be4a0963d2291605b7b4f86eeabb
[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
113       if (job_cancelled(jcr)) {
114          ok = FALSE;
115          break;
116       }
117       /* Read Record */
118       Dmsg1(500, "Main read_record. rem=%d\n", rec.remainder);
119       if (!read_record(dev, block, &rec)) {
120          Dmsg1(500, "Main read record failed. rem=%d\n", rec.remainder);
121          if (dev->state & ST_EOT) {
122             if (rec.remainder) {
123                Dmsg0(500, "Not end of record.\n");
124             }
125             Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
126             if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
127                close_dev(dev);
128                for (p=jcr->VolumeName; *p++; ) /* skip to next volume name */
129                   { }
130                jcr->CurVolume++;
131                Dmsg1(20, "There is another volume %s.\n", p);
132                strcpy(jcr->VolumeName, p);
133                dev->state &= ~ST_READ; 
134                if (!acquire_device_for_read(jcr, dev, block)) {
135                   Emsg2(M_ERROR, 0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), p);
136                   ok = FALSE;
137                   break;
138                }
139                record = new_record();
140                Dmsg1(500, "read record after new tape. rem=%d\n", record->remainder);
141                read_record(dev, block, record); /* read vol label */
142                dump_label_record(dev, record, 0);
143                free_record(record);
144                continue;
145             }
146             Dmsg0(90, "End of Device reached.\n");
147             break;                    /* End of Tape */
148          }
149          if (dev->state & ST_EOF) {
150             Dmsg0(90, "Got End of File. Trying again ...\n");
151             continue;                 /* End of File */
152          }
153
154          Emsg2(M_ABORT, 0, "Read error on Record Header %s ERR=%s\n", dev_name(dev), strerror(errno));
155       }
156
157       /* Some sort of label? */ 
158       if (rec.FileIndex < 0) {
159          Dmsg1(40, "Got label = %d\n", rec.FileIndex);
160          if (rec.FileIndex == EOM_LABEL) { /* end of tape? */
161             Dmsg0(40, "Get EOM LABEL\n");
162             break;                         /* yes, get out */
163          }
164          continue;                         /* ignore other labels */
165       }
166
167       if (rec.VolSessionId != jcr->read_VolSessionId ||
168           rec.VolSessionTime != jcr->read_VolSessionTime) {
169          Dmsg0(50, "Ignore record ids not equal\n");
170          continue;                    /* ignore */
171       }
172        
173       /* Generate Header parameters and send to File daemon
174        * Note, we build header in hdr buffer to avoid wiping
175        * out the data record
176        */
177       ds->msg = hdr;
178       if (!bnet_fsend(ds, rec_header, rec.VolSessionId, rec.VolSessionTime,
179              rec.FileIndex, rec.Stream, rec.data_len)) {
180          Dmsg1(30, ">filed: Error Hdr=%s\n", ds->msg);
181          ds->msg = rec.data;
182          ok = FALSE;
183          break;
184       } else {
185          Dmsg1(30, ">filed: Hdr=%s\n", ds->msg);
186       }
187
188       ds->msg = rec.data;             /* restore data record address */
189
190       /* Send data record to File daemon */
191       ds->msglen = rec.data_len;
192       Dmsg1(40, ">filed: send %d bytes data.\n", ds->msglen);
193       if (!bnet_send(ds)) {
194          Dmsg0(0, "Error sending to FD\n");
195          Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
196             bnet_strerror(ds));
197          ok = FALSE;
198       }
199    }
200    /* Send end of data to FD */
201    bnet_sig(ds, BNET_EOF);
202
203    if (!release_device(jcr, dev, block)) {
204       ok = FALSE;
205    }
206    free_pool_memory(hdr);
207    free_block(block);
208    Dmsg0(30, "Done reading.\n");
209    return ok ? 1 : 0;
210 }