| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "config.h" |
| 31 |
#include "readtape.h" |
| 32 |
|
| 33 |
#include <fcntl.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <sys/mtio.h> |
| 36 |
#include "compat/include/compaterr.h" |
| 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; |
| 55 |
static int eot = 0; |
| 56 |
static off_t tapepos = 0; |
| 57 |
static char* readbuffer = 0; |
| 58 |
static size_t datalen = 0; |
| 59 |
static int compressed = 0; |
| 60 |
static int have_blocklen = 0; |
| 61 |
static int useCalculatedPos = 0; |
| 62 |
static int ignoreCompressionFlag = 0; |
| 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 |
|
| 163 |
int err = 0; |
| 164 |
|
| 165 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 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 |
|
| 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 |
|
| 250 |
int readblock_tape(char* dst, size_t maxsize, size_t* sizeread) { |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
if (!readbuffer) { |
| 263 |
|
| 264 |
|
| 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 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 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 |
|
| 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) |
| 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 |
|
| 325 |
|
| 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 |
|
| 340 |
|
| 341 |
|
| 342 |
static int mangle_bitfields = -1; |
| 343 |
if (mangle_bitfields == -1) { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
if (tapebuf_getcompressed_mangled(hdr) == 1) |
| 362 |
|
| 363 |
mangle_bitfields = 1; |
| 364 |
|
| 365 |
else if (tapebuf_getlength_mangled(hdr) != ntrec*TP_BSIZE) |
| 366 |
|
| 367 |
mangle_bitfields = 0; |
| 368 |
else |
| 369 |
|
| 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 |
|
| 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 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
while (datalen < tapesize && |
| 401 |
read_some_data() > 0) { |
| 402 |
} |
| 403 |
if (datalen < tapesize) { |
| 404 |
if (have_blocklen) { |
| 405 |
|
| 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 { |
| 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 |
} |