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 */
112 SESSION_LABEL sessrec; /* session record */
114 if (job_cancelled(jcr)) {
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) {
124 Dmsg0(500, "Not end of record.\n");
126 Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
127 if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
129 for (p=jcr->VolumeName; *p++; ) /* skip to next volume name */
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);
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);
147 Dmsg0(90, "End of Device reached.\n");
148 break; /* End of Tape */
150 if (dev->state & ST_EOF) {
151 Dmsg0(90, "Got End of File. Trying again ...\n");
152 continue; /* End of File */
155 Emsg2(M_ABORT, 0, "Read error on Record Header %s ERR=%s\n", dev_name(dev), strerror(errno));
158 /* Some sort of label? */
159 if (rec.FileIndex < 0) {
161 switch (rec.FileIndex) {
163 rtype = "Fresh Volume Label";
166 rtype = "Volume Label";
167 unser_volume_label(dev, &rec);
170 rtype = "Begin Session";
171 unser_session_label(&sessrec, &rec);
174 rtype = "End Session";
177 rtype = "End of Media";
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);
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 */
193 continue; /* ignore other labels */
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");
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 */
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
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);
220 Dmsg1(30, ">filed: Hdr=%s\n", ds->msg);
223 ds->msg = rec.data; /* restore data record address */
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"),
235 /* Send end of data to FD */
236 bnet_sig(ds, BNET_EOF);
238 if (!release_device(jcr, dev, block)) {
241 free_pool_memory(hdr);
243 Dmsg0(30, "Done reading.\n");