3 * record.c -- tape record handling functions
5 * Kern Sibbald, April MMI
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 extern int debug_level;
37 * Convert a FileIndex into a printable
38 * ASCII string. Not reentrant.
40 char *FI_to_ascii(int fi)
44 sprintf(buf, "%d", fi);
59 sprintf(buf, "unknown: %d", fi);
66 * Convert a Stream ID into a printable
67 * ASCII string. Not reentrant.
69 char *stream_to_ascii(int stream)
73 case STREAM_UNIX_ATTRIBUTES:
75 case STREAM_FILE_DATA:
77 case STREAM_MD5_SIGNATURE:
79 case STREAM_GZIP_DATA:
82 sprintf(buf, "%d", stream);
88 * Return a new record entity
90 DEV_RECORD *new_record(void)
94 rec = (DEV_RECORD *) get_memory(sizeof(DEV_RECORD));
95 memset(rec, 0, sizeof(DEV_RECORD));
96 rec->data = get_pool_memory(PM_MESSAGE);
101 * Free the record entity
104 void free_record(DEV_RECORD *rec)
106 Dmsg0(150, "Enter free_record.\n");
107 free_pool_memory(rec->data);
108 Dmsg0(150, "Data buf is freed.\n");
109 free_pool_memory((POOLMEM *)rec);
110 Dmsg0(150, "Leave free_record.\n");
114 * Read a record from a block
115 * if necessary, read the block from the device without locking
116 * if necessary, handle getting a new Volume
118 * Returns: 0 on failure
121 int read_record(DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *record)
123 Dmsg2(90, "read_record() dev=%x state=%x\n", dev, dev->state);
125 while (!read_record_from_block(block, record)) {
126 Dmsg2(90, "!read_record_from_block data_len=%d rem=%d\n", record->data_len,
128 if (!read_block_from_dev(dev, block)) {
129 /**** ****FIXME**** handle getting a new Volume */
130 Dmsg0(200, "===== Got read block I/O error ======\n");
134 Dmsg4(90, "read_record FI=%s SessId=%d Strm=%s len=%d\n",
135 FI_to_ascii(record->FileIndex), record->VolSessionId,
136 stream_to_ascii(record->Stream), record->data_len);
142 * Write a record to block
143 * if necessary, write the block to the device with locking
144 * if necessary, handle getting a new Volume
146 * Returns: 0 on failure
149 int write_record_device(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *record)
151 Dmsg2(190, "write_device() dev=%x state=%x\n", dev, dev->state);
153 while (!write_record_to_block(block, record)) {
154 Dmsg2(190, "!write_record data_len=%d rem=%d\n", record->data_len,
156 if (!write_block_to_device(jcr, dev, block)) {
157 Dmsg0(90, "Got write_block_to_dev error.\n");
161 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
162 FI_to_ascii(record->FileIndex), record->VolSessionId,
163 stream_to_ascii(record->Stream), record->data_len);
168 * Write a Record to the block
170 * Returns: 0 on failure (none or partially written)
171 * 1 on success (all bytes written)
173 * and remainder returned in packet.
175 * We require enough room for the header, and we deal with
176 * two special cases. 1. Only part of the record may have
177 * been transferred the last time (when remainder is
178 * non-zero), and 2. The remaining bytes to write may not
179 * all fit into the block.
181 int write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
186 sm_check(__FILE__, __LINE__, False);
187 remlen = block->buf_len - block->binbuf;
189 ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
192 Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
193 rem=%d remainder=%d\n",
194 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
195 stream_to_ascii(rec->Stream), rec->data_len,
196 remlen, rec->remainder);
198 if (rec->remainder == 0) {
199 /* Require enough room to write a full header */
200 if (remlen >= RECHDR_LENGTH) {
201 ser_begin(block->bufp, RECHDR_LENGTH);
202 ser_uint32(rec->VolSessionId);
203 ser_uint32(rec->VolSessionTime);
204 ser_int32(rec->FileIndex);
205 ser_int32(rec->Stream);
206 ser_uint32(rec->data_len);
207 ASSERT(ser_length(block->bufp) == RECHDR_LENGTH);
209 block->bufp += RECHDR_LENGTH;
210 block->binbuf += RECHDR_LENGTH;
211 remlen -= RECHDR_LENGTH;
212 rec->remainder = rec->data_len;
214 rec->remainder = rec->data_len + RECHDR_LENGTH;
215 sm_check(__FILE__, __LINE__, False);
220 * We are here to write unwritten bytes from a previous
221 * time. Presumably we have a new buffer (possibly
222 * containing a volume label), so the new header
223 * should be able to fit in the block -- otherwise we have
224 * an error. Note, we may have to continue splitting the
225 * data record though.
227 * First, write the header, then write as much as
228 * possible of the data record.
230 ser_begin(block->bufp, RECHDR_LENGTH);
231 ser_uint32(rec->VolSessionId);
232 ser_uint32(rec->VolSessionTime);
233 ser_int32(rec->FileIndex);
234 if (rec->remainder > rec->data_len) {
235 ser_int32(rec->Stream); /* normal full header */
236 ser_uint32(rec->data_len);
237 rec->remainder = rec->data_len; /* must still do data record */
239 ser_int32(-rec->Stream); /* mark this as a continuation record */
240 ser_uint32(rec->remainder); /* bytes to do */
242 ASSERT(ser_length(block->bufp) == RECHDR_LENGTH);
244 /* Require enough room to write a full header */
245 ASSERT(remlen >= RECHDR_LENGTH);
247 block->bufp += RECHDR_LENGTH;
248 block->binbuf += RECHDR_LENGTH;
249 remlen -= RECHDR_LENGTH;
252 sm_check(__FILE__, __LINE__, False);
253 return 0; /* partial transfer */
256 /* Now deal with data record.
257 * Part of it may have already been transferred, and we
258 * may not have enough room to transfer the whole this time.
260 if (rec->remainder > 0) {
261 /* Write as much of data as possible */
262 if (remlen >= rec->remainder) {
263 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
265 block->bufp += rec->remainder;
266 block->binbuf += rec->remainder;
268 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
270 if (!sm_check_rtn(__FILE__, __LINE__, False)) {
271 /* We damaged a buffer */
272 Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
273 rem=%d remainder=%d\n",
274 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
275 stream_to_ascii(rec->Stream), rec->data_len,
276 remlen, rec->remainder);
277 Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
278 block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
280 Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
281 block->buf, block->bufp-block->buf);
283 Emsg0(M_ABORT, 0, "Damaged buffer\n");
286 block->bufp += remlen;
287 block->binbuf += remlen;
288 rec->remainder -= remlen;
289 return 0; /* did partial transfer */
292 rec->remainder = 0; /* did whole transfer */
293 sm_check(__FILE__, __LINE__, False);
299 * Read a Record from the block
300 * Returns: 0 on failure
303 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
310 remlen = block->binbuf;
313 * Get the header. There is always a full header,
314 * otherwise we find it in the next block.
316 if (remlen >= RECHDR_LENGTH) {
317 Dmsg3(90, "read_record_block: remlen=%d data_len=%d rem=%d\n",
318 remlen, rec->data_len, rec->remainder);
319 /* memcpy(rec->ser_buf, block->bufp, RECHDR_LENGTH); */
321 unser_begin(block->bufp, RECHDR_LENGTH);
322 unser_uint32(rec->VolSessionId);
323 unser_uint32(rec->VolSessionTime);
324 unser_int32(rec->FileIndex);
326 unser_uint32(data_bytes);
328 ASSERT(unser_length(block->bufp) == RECHDR_LENGTH);
329 block->bufp += RECHDR_LENGTH;
330 block->binbuf -= RECHDR_LENGTH;
331 remlen -= RECHDR_LENGTH;
333 if (Stream < 0) { /* continuation record? */
334 Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n",
336 /* ****FIXME**** add code to verify that this
337 * is the correct continuation of the previous
340 if (!rec->remainder) { /* if we didn't read previously */
341 rec->data_len = 0; /* simply return data */
343 rec->Stream = -Stream; /* set correct Stream */
344 } else { /* Regular record */
345 rec->Stream = Stream;
346 rec->data_len = 0; /* transfer to beginning of data */
348 Dmsg6(90, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%d\n\
349 remlen=%d data_len=%d\n",
350 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
351 stream_to_ascii(rec->Stream), data_bytes, remlen, rec->data_len);
353 Dmsg0(90, "read_record_block: nothing\n");
354 if (!rec->remainder) {
355 rec->remainder = 1; /* set to expect continuation */
356 rec->data_len = 0; /* no data transferred */
361 ASSERT(data_bytes < MAX_BLOCK_LENGTH); /* temp sanity check */
363 rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
366 * At this point, we have read the header, now we
367 * must transfer as much of the data record as
368 * possible taking into account: 1. A partial
369 * data record may have previously been transferred,
370 * 2. The current block may not contain the whole data
373 if (remlen >= data_bytes) {
374 /* Got whole record */
375 memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
376 block->bufp += data_bytes;
377 block->binbuf -= data_bytes;
378 rec->data_len += data_bytes;
381 memcpy(rec->data+rec->data_len, block->bufp, remlen);
382 block->bufp += remlen;
383 block->binbuf -= remlen;
384 rec->data_len += remlen;
385 rec->remainder = 1; /* partial record transferred */
386 Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
390 Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
391 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
392 stream_to_ascii(rec->Stream), rec->data_len);
393 return 1; /* transferred full record */