Debian Codegraph @

restore/readtape.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2024
3 * Tim Woodall. All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * SPDX short identifier: BSD-2-Clause
28 */
29
30 #include "config.h" // IWYU pragma: keep
31 #include "readtape.h"
32
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <sys/mtio.h>
36 #include "compat/include/compaterr.h" // IWYU pragma: keep
37 #include <errno.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/ioctl.h>
43 #include "compat/include/protocols/dumprestore.h"
44
45
46 #include "restore.h"
47 #include "restore_globals.h"
48 #include "tape.h"
49 #include "rmt/rmt.h"
50 #include "common/dumprmt.h"
51
52 static int mt = -1;
53 static int is_remote = 0;
54 static int magtapein = 0; /* input is from magtape */
55 static int eot = 0;
56 static off_t tapepos = 0;
57 static char* readbuffer = 0; // Data we're read from the tape but not delivered yet.
58 static size_t datalen = 0; // Number of bytes of data in readbuffer
59 static int compressed = 0; // Tape is compressed
60 static int have_blocklen = 0; // Old tapes before compression did not have blocksize recorded
61 static int useCalculatedPos = 0; // file returns the blocksize we ask for, not the blocksize that was written.
62 static int ignoreCompressionFlag = 0; // Whether to ignore the compression flag to read buggy 0.4b45 dumps
63
64 void set_ignoreCompressionFlag(void) {
65 ignoreCompressionFlag = 1;
66 }
67
68 int open_tape(int open_remote, const char* tape)
69 {
70 is_remote = open_remote;
71 magtapein = 0;
72 if (is_remote) {
73 mt = rmtopen(tape, O_RDONLY);
74 if (mt != -1)
75 magtapein = rmtseek(0, LSEEK_IS_MAGTAPE);
76 } else if (strcmp(tape, "-") == 0)
77 mt = 0;
78 else {
79 mt = open(tape, O_RDONLY, 0);
80 if (mt != -1) {
81 struct mtget mt_stat;
82 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
83 }
84 }
85 tapepos = 0;
86 datalen = 0;
87 eot = 0;
88 compressed = 0;
89
90 return mt;
91 }
92
93 void close_tape(void)
94 {
95 if (mt < 0)
96 return;
97 if (is_remote)
98 rmtclose();
99 else
100 close(mt);
101 mt = -1;
102 is_remote = 0;
103 magtapein = 0;
104 tapepos = 0;
105 }
106
107 int ioctl_tape(int cmd, int count) {
108 if (is_remote)
109 return rmtioctl(cmd, count);
110 struct mtop tcom;
111 tcom.mt_op = cmd;
112 tcom.mt_count = count;
113 return ioctl(mt, MTIOCTOP, &tcom);
114 }
115
116 int magtapein_tape(void) {
117 return magtapein;
118 }
119
120 #ifdef USE_QFA
121
122 int getpos_tape(off_t *pos) {
123 int err = 0;
124
125 if (useCalculatedPos) {
126 *pos = tapepos;
127 } else if (is_remote) {
128 *pos = rmtseek(0, LSEEK_GET_TAPEPOS);
129 err = *pos < 0;
130 } else if (magtapein) {
131 struct mtpos mtpos = {0};
132 *pos = 0;
133
134 struct mtop buf;
135 buf.mt_op = MTSETDRVBUFFER;
136 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
137 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
138
139 if (!err) {
140 err = (ioctl(mt, MTIOCPOS, &mtpos) < 0);
141 *pos = mtpos.mt_blkno;
142 } else {
143 warn("MTSETDRVBUFFER trying MTIOCGET instead\n");
144 struct mtget mt_stat;
145 err = (ioctl(mt, MTIOCGET, (char *)&mt_stat) < 0);
146 if (!err)
147 *pos = mt_stat.mt_blkno;
148 }
149 } else {
150 *pos = lseek(mt, 0, SEEK_CUR);
151 err = (*pos < 0);
152 }
153 if (err) {
154 err = errno;
155 fprintf(stderr, "[%jd] error: %d (getting tapepos: %jd)\n",
156 (intmax_t)getpid(), err, (intmax_t)*pos);
157 }
158 return err;
159 }
160
161 int setpos_tape(off_t pos) {
162 /* This will cause "odd problems" if it is called before the first block has been read so don't do that! */
163 int err = 0;
164
165 /* Drop any cached data */
166 datalen = 0;
167 if (is_remote)
168 err = rmtseek(pos, LSEEK_GO2_TAPEPOS) < 0;
169 else if (magtapein) {
170 struct mtop buf;
171 buf.mt_op = MTSETDRVBUFFER;
172 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
173 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
174
175 if(!err) {
176 buf.mt_op = MTSEEK;
177 buf.mt_count = (int) pos;
178 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
179 }
180 } else {
181 pos = lseek(mt, pos, SEEK_SET);
182 err = (pos < 0);
183 }
184 if (err) {
185 err = errno;
186 fprintf(stdout, "[%jd] error: %d (setting tapepos: %jd)\n",
187 (intmax_t)getpid(), err, (intmax_t)pos);
188 return err;
189 }
190 return err;
191 }
192 #endif
193
194 #define PREFIXSIZE sizeof(struct tapebuf)
195
196 static ssize_t read_some_data(void) {
197 // Uncompressed tapes were written with ntrec*TP_BSIZE sized blocks.
198 // Compressed tapes are written with blocks from "small" to
199 // ntrec*TP_BSIZE+PREFIXSIZE except when ntrec*TP_BSIZE is the maximum
200 // size for the tape when two blocks are written for incompressible
201 // data.
202 // Tapes that have been written from a file might have an arbitrary
203 // block size.
204 static size_t maxReadSize = -1;
205 static size_t size = 0;
206 if (size == 0)
207 size = ntrec*TP_BSIZE+PREFIXSIZE;
208
209 if (eot)
210 return 0;
211 ssize_t len;
212 do {
213 if (size > maxReadSize)
214 size = maxReadSize;
215 if (is_remote)
216 len = rmtread(readbuffer+datalen, size);
217 else
218 len = read(mt, readbuffer+datalen, size);
219 if(len == -1) {
220 if(tapepos == 0 && maxReadSize == (size_t)-1)
221 /* This might be the maximum size the tape can support */
222 maxReadSize = ntrec*TP_BSIZE;
223 else
224 break;
225 }
226 } while(size > maxReadSize);
227
228 if (len == 0)
229 eot = 1;
230 if (len > 0)
231 datalen += len;
232 return len;
233 }
234
235 static int tapebuf_getcompressed_mangled(const struct tapebuf* tb)
236 {
237 return *(const uint32_t*)tb & (1U<<31)?1:0;
238 }
239 static int tapebuf_getflags_mangled(const struct tapebuf* tb)
240 {
241 return (*(const uint32_t*)tb&0x70000000) >> 28;
242 }
243 static int tapebuf_getlength_mangled(const struct tapebuf* tb)
244 {
245 return *(const uint32_t*)tb&0x0fffffff;
246 }
247
248
249 /* Returns 0 on eot, -1 on error, bytes read on successful read */
250 int readblock_tape(char* dst, size_t maxsize, size_t* sizeread) {
251 /* This is "utterly horrible"
252 * The original uncompressed tapes expected to consist of tape blocks
253 * of ntrec sized blocks and nothing else
254 * Then compression came along and the tape blocks could be arbitrarily
255 * sized.
256 * But worst of all, the decoder, the resynching, everything, depended
257 * on the tape drive maintaining the block boundaries.
258 * This is why, for instance, you couldn't write a (compressed) dump to a
259 * file and then later write it to tape.
260 */
261
262 if (!readbuffer) {
263 // This should be twice the maximum size we can be asked to read.
264 // Compressed tapes write blocks this size when they cannot be compressed.
265 readbuffer = malloc((ntrec * TP_BSIZE + PREFIXSIZE) * 2);
266 if (!readbuffer)
267 errx(1, "Can't allocate memory for readbuffer");
268 }
269
270 size_t tapesize = 0;
271 if (datalen < sizeof(struct tapebuf)) {
272 read_some_data();
273 }
274 if (datalen < sizeof(struct tapebuf)) {
275 return eot&&!datalen?0:-1;
276 }
277 if (tapepos == 0) {
278 // First block on tape is uncompressed and does not have a header.
279 // Note that there were some differences in tapes written from CVS before the 0.4b22 release.
280 // struct tapebuf added in 4d5ed2f38cb1dce0244a05f94c8aec7f725a1f33 Wed Feb 21 16:13:04 2001 +0000
281 // Modified to a single uint32_t in 2b7475327b6a1a580f76eca13a18f68a2943a5b1 Sun Mar 18 15:35:43 2001 +0000
282 // Stopped the first block having a header in 86fab47e75fc141db053271654a2570fddcd0f46 Sat May 12 11:36:12 2001 +0000
283 // all applied before the 0.4b22 release
284
285 struct s_spcl spclpt;
286 cvtflag = 0;
287 memcpy(&spclpt, readbuffer, TP_BSIZE);
288 if (converthead(&spclpt) == GOOD)
289 goto goodheader;
290 cvtflag++;
291 if (converthead(&spclpt) == GOOD) {
292 fprintf(stderr, "Converting to new file system format.\n");
293 goto goodheader;
294 }
295 // Test if this dump was written with an initial header
296 tapesize = sizeof(struct tapebuf);
297 memcpy(&spclpt, readbuffer+tapesize, TP_BSIZE);
298 cvtflag = 0;
299 memcpy(&spclpt, readbuffer, TP_BSIZE);
300 if (converthead(&spclpt) == GOOD)
301 goto goodheader;
302 errx(1, "Tape is not a dump tape");
303 goodheader:
304 if (spclpt.c_type == TS_TAPE) {
305 if (spclpt.c_flags & DR_COMPRESSED && !ignoreCompressionFlag)
306 compressed = 1;
307 } else
308 errx(1, "First block is not TS_TAPE\n");
309
310 if (spclpt.c_ntrec == 0) // This wasn't added until compression was added.
311 tapesize += ntrec * TP_BSIZE;
312 else {
313 have_blocklen = 1;
314 tapesize += spclpt.c_ntrec * TP_BSIZE;
315 }
316 if (spclpt.c_ntrec > ntrec)
317 errx(1, "Tape blocksize is too large, use "
318 "\'-b %d\' ", spclpt.c_ntrec);
319 if (tapesize < datalen)
320 useCalculatedPos = 1;
321 if (magtapein && tapesize != datalen) {
322 #ifdef USE_QFA
323 /*
324 * We could enhance the QFA file to have tapepos=<logical-block>.<offset> for tapepos.
325 * Then, after a seek we'd have to discard offset bytes
326 */
327 if (tapeposflag || createtapeposflag)
328 errx(1, "Dump appears to have been written with a blocksize setting of -b %zu but we read %f blocks from the tape. QFA will not work",
329 tapesize/TP_BSIZE, (double)datalen/TP_BSIZE);
330 else
331 #endif
332 warnx("Dump appears to have been written with a blocksize setting of -b %zu but we read %f blocks from the tape. QFA will not work",
333 tapesize/TP_BSIZE, (double)datalen/TP_BSIZE);
334 }
335 } else if (compressed) {
336 struct tapebuf* hdr = (struct tapebuf*)readbuffer;
337 if (Bcvt)
338 swabst("i", (u_char*)hdr);
339 // Up to dump 0.4b48 tapebuffer used bitfields rather than shift and masks.
340 // Tapes written on a system where the bitfields were assigned from MSB first will not work.
341 // We apply a heuristic to try and determine which way it should be.
342 static int mangle_bitfields = -1;
343 if (mangle_bitfields == -1) {
344 /* Note that dump currently restricts max block size to 16383KB
345 * (2^24-1) so the top 4 bits of the length MUST be zero.
346 * 1MB requires 21 bits to represent (as per diagrams below)
347 * 64-127MB requires 27 bits to represent - this is the largest blocksize for which the heuristics below still work.
348 * 128-255MB requires 28 bits to represent
349 * A compressed tape with a 256MB block size and an
350 * incompressible block would overflow and could not be
351 * restored without some work to encode this length in
352 * one of the unused values.
353 */
354 // 3 2 1
355 // 1 4 4 4 read mangled read normal
356 // case 1a. buffer was not compressed. 0000000LLLLLLLLLLL0000000000fff0 -> mc=0 ml=length*16+flags*2 nc=0 nl=length
357 // 1b. (bitfield reversed) 0fff0000000LLLLLLLLLLL0000000000 -> mc=0 ml=length nc=0 nl=length/16+flags<<28
358 // case 2a. buffer was compressed. 0000000LLLLLLLLLLLLLLLLLLLLLfff1 -> mc=0 ml=length*16+flags*2+1 nc=1 nl=length
359 // 2b. (bitfield reversed) 1fff000000LLLLLLLLLLLLLLLLLLLLLL -> mc=1 ml=length nc=? nl=length/16+flags<<28+1<<31
360
361 if (tapebuf_getcompressed_mangled(hdr) == 1)
362 // Case 2b. Only way mc can be set.
363 mangle_bitfields = 1;
364 /* blocks written without compression must be exactly ntrec*TP_BSIZE in size */
365 else if (tapebuf_getlength_mangled(hdr) != ntrec*TP_BSIZE)
366 // This covers case 2a and 1a. 2a because length will be odd when read mangled, and 1a because length will be 16x too big.
367 mangle_bitfields = 0;
368 else
369 // Only case 1b remains.
370 mangle_bitfields = 1;
371 if (mangle_bitfields)
372 warnx("Tape appears to have been written with different bitfield ordering\n");
373 }
374 if (mangle_bitfields) {
375 /* rewrite the header in the expected order */
376 int l = tapebuf_getlength_mangled(hdr);
377 int f = tapebuf_getflags_mangled(hdr);
378 int c = tapebuf_getcompressed_mangled(hdr);
379 tapebuf_setlength(hdr, l);
380 tapebuf_setflags(hdr, f);
381 tapebuf_setcompressed(hdr, c);
382 }
383 tapesize = tapebuf_getlength(hdr) + sizeof(*hdr);
384 } else {
385 struct tapebuf* hdr = (struct tapebuf*)dst;
386 tapesize = ntrec * TP_BSIZE;
387 tapebuf_setlength(hdr, tapesize);
388 tapebuf_setflags(hdr, 0);
389 tapebuf_setcompressed(hdr, 0);
390 dst += sizeof(struct tapebuf);
391 maxsize -= sizeof(struct tapebuf);
392 }
393 if (tapesize > ntrec * TP_BSIZE + PREFIXSIZE)
394 errx(1, "Tape block size error. This should not happen. Try using a larger -b setting\n");
395
396 // At this point tapesize is the size that was originally written to tape
397 // This might be bigger than the data available in readbuffer - for
398 // example when reading from a file we're not following the original
399 // blocking so we might have a partial block already read.
400 while (datalen < tapesize &&
401 read_some_data() > 0) {
402 }
403 if (datalen < tapesize) {
404 if (have_blocklen) {
405 /* Archive files weren't padded to the correct size until v0.4b50 */
406 tapesize = datalen;
407 warnx("Insufficient data on tape. datalen=%zu tapelen=%zu This should not happen.\n"
408 "If this is not a pre v0.4b50 archive file then try using a different -b setting\n",
409 datalen, tapesize);
410 }
411 else { // We don't know what the original blocksize was.
412 tapesize = datalen;
413 warnx("Attempting to handle short block read on old tape. Reading blocksize probably doesn't match writing blocksize");
414 }
415 }
416 size_t len = tapesize;
417 if (len > maxsize) {
418 len = maxsize;
419 warnx("Size requested (%zu) is smaller than data on tape (%zu). Some data is lost. This should not happen\n", maxsize, tapesize);
420 }
421 memcpy(dst, readbuffer, len);
422 memmove(readbuffer, readbuffer+tapesize, datalen - tapesize);
423 if (tapepos>0 && !compressed) {
424 len += sizeof (struct tapebuf);
425 }
426 datalen -= tapesize;
427 tapepos += tapesize;
428 if (sizeread)
429 *sizeread = len;
430 return len;
431 }