2 * Read code for Storage daemon
3 * Kern Sibbald, November MM
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
30 /* Forward referenced subroutines */
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
37 extern struct s_shm *shm; /* shared memory structure */
38 extern int FiledDataChan; /* File daemon data channel (port) */
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";
46 * Read Data and send to File Daemon
47 * Returns: 0 on failure
50 int do_read_data(JCR *jcr)
53 BSOCK *fd_sock = jcr->file_bsock;
61 Dmsg0(20, "Start read data.\n");
63 dev = jcr->device->dev;
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);
71 if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
76 Dmsg1(20, "Begin read device=%s\n", dev_name(dev));
78 block = new_block(dev);
80 /* Find out if we were passed multiple volumes */
83 /* Scan through VolumeNames terminating them and counting them */
84 for (p = jcr->VolumeName; p && *p; ) {
85 p = strchr(p, '|'); /* volume name separator */
87 *p++ = 0; /* Terminate name */
92 Dmsg1(20, "Found %d volumes names to restore.\n", jcr->NumVolumes);
95 * Ready device for reading, and read records
97 if (!acquire_device_for_read(jcr, dev, block)) {
102 memset(&rec, 0, sizeof(rec));
103 rec.data = ds->msg; /* use socket message buffer */
104 hdr = (char *) get_pool_memory(PM_MESSAGE);
107 * ****FIXME**** enhance this to look for
108 * more things than just a session.
111 DEV_RECORD *record; /* for reading label of multi-volumes */
113 if (job_cancelled(jcr)) {
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) {
123 Dmsg0(500, "Not end of record.\n");
125 Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
126 if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
128 for (p=jcr->VolumeName; *p++; ) /* skip to next volume name */
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);
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);
146 Dmsg0(90, "End of Device reached.\n");
147 break; /* End of Tape */
149 if (dev->state & ST_EOF) {
150 Dmsg0(90, "Got End of File. Trying again ...\n");
151 continue; /* End of File */
154 Emsg2(M_ABORT, 0, "Read error on Record Header %s ERR=%s\n", dev_name(dev), strerror(errno));
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 */
164 continue; /* ignore other labels */
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 */
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
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);
185 Dmsg1(30, ">filed: Hdr=%s\n", ds->msg);
188 ds->msg = rec.data; /* restore data record address */
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"),
200 /* Send end of data to FD */
201 bnet_sig(ds, BNET_EOF);
203 if (!release_device(jcr, dev, block)) {
206 free_pool_memory(hdr);
208 Dmsg0(30, "Done reading.\n");