| 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 "faketape-lib.h" |
| 32 | |
| 33 | #include <cstring> |
| 34 | #include <zlib.h> |
| 35 | #include <algorithm> |
| 36 | #include <fcntl.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <errno.h> |
| 39 | #include <zconf.h> |
| 40 | |
| 41 | #define TRACE(x) do { if (FakeTape::trace()) \ |
| 42 | DEBUG((*this), __func__ << ":" << __FILE__ << ":" << __LINE__ << " " << x << " " \ |
| 43 | " fd=" << fd << \ |
| 44 | " pos=" << lseek(fd,0,SEEK_CUR) << \ |
| 45 | " filenum=" << filenum << \ |
| 46 | " fileblock=" << fileblock << \ |
| 47 | " block=" << block << \ |
| 48 | " cbt_index=" << cbt_index << \ |
| 49 | " cbt.count=" << cbt.count << \ |
| 50 | ""); } while (0) |
| 51 | #define USE_ZLIB_COMPRESSION 1 |
| 52 | |
| 53 | uint32_t FakeTape::crc32(const BlockTable* bt) { |
| 54 | static constexpr uint32_t crcTable[256] = { |
| 55 | 0x00000000, 0xedb88320, 0x36c98560, 0xdb710640, 0x6d930ac0, 0x802b89e0, 0x5b5a8fa0, 0xb6e20c80, |
| 56 | 0xdb261580, 0x369e96a0, 0xedef90e0, 0x005713c0, 0xb6b51f40, 0x5b0d9c60, 0x807c9a20, 0x6dc41900, |
| 57 | 0x5bf4a820, 0xb64c2b00, 0x6d3d2d40, 0x8085ae60, 0x3667a2e0, 0xdbdf21c0, 0x00ae2780, 0xed16a4a0, |
| 58 | 0x80d2bda0, 0x6d6a3e80, 0xb61b38c0, 0x5ba3bbe0, 0xed41b760, 0x00f93440, 0xdb883200, 0x3630b120, |
| 59 | 0xb7e95040, 0x5a51d360, 0x8120d520, 0x6c985600, 0xda7a5a80, 0x37c2d9a0, 0xecb3dfe0, 0x010b5cc0, |
| 60 | 0x6ccf45c0, 0x8177c6e0, 0x5a06c0a0, 0xb7be4380, 0x015c4f00, 0xece4cc20, 0x3795ca60, 0xda2d4940, |
| 61 | 0xec1df860, 0x01a57b40, 0xdad47d00, 0x376cfe20, 0x818ef2a0, 0x6c367180, 0xb74777c0, 0x5afff4e0, |
| 62 | 0x373bede0, 0xda836ec0, 0x01f26880, 0xec4aeba0, 0x5aa8e720, 0xb7106400, 0x6c616240, 0x81d9e160, |
| 63 | 0x826a23a0, 0x6fd2a080, 0xb4a3a6c0, 0x591b25e0, 0xeff92960, 0x0241aa40, 0xd930ac00, 0x34882f20, |
| 64 | 0x594c3620, 0xb4f4b500, 0x6f85b340, 0x823d3060, 0x34df3ce0, 0xd967bfc0, 0x0216b980, 0xefae3aa0, |
| 65 | 0xd99e8b80, 0x342608a0, 0xef570ee0, 0x02ef8dc0, 0xb40d8140, 0x59b50260, 0x82c40420, 0x6f7c8700, |
| 66 | 0x02b89e00, 0xef001d20, 0x34711b60, 0xd9c99840, 0x6f2b94c0, 0x829317e0, 0x59e211a0, 0xb45a9280, |
| 67 | 0x358373e0, 0xd83bf0c0, 0x034af680, 0xeef275a0, 0x58107920, 0xb5a8fa00, 0x6ed9fc40, 0x83617f60, |
| 68 | 0xeea56660, 0x031de540, 0xd86ce300, 0x35d46020, 0x83366ca0, 0x6e8eef80, 0xb5ffe9c0, 0x58476ae0, |
| 69 | 0x6e77dbc0, 0x83cf58e0, 0x58be5ea0, 0xb506dd80, 0x03e4d100, 0xee5c5220, 0x352d5460, 0xd895d740, |
| 70 | 0xb551ce40, 0x58e94d60, 0x83984b20, 0x6e20c800, 0xd8c2c480, 0x357a47a0, 0xee0b41e0, 0x03b3c2c0, |
| 71 | 0xe96cc460, 0x04d44740, 0xdfa54100, 0x321dc220, 0x84ffcea0, 0x69474d80, 0xb2364bc0, 0x5f8ec8e0, |
| 72 | 0x324ad1e0, 0xdff252c0, 0x04835480, 0xe93bd7a0, 0x5fd9db20, 0xb2615800, 0x69105e40, 0x84a8dd60, |
| 73 | 0xb2986c40, 0x5f20ef60, 0x8451e920, 0x69e96a00, 0xdf0b6680, 0x32b3e5a0, 0xe9c2e3e0, 0x047a60c0, |
| 74 | 0x69be79c0, 0x8406fae0, 0x5f77fca0, 0xb2cf7f80, 0x042d7300, 0xe995f020, 0x32e4f660, 0xdf5c7540, |
| 75 | 0x5e859420, 0xb33d1700, 0x684c1140, 0x85f49260, 0x33169ee0, 0xdeae1dc0, 0x05df1b80, 0xe86798a0, |
| 76 | 0x85a381a0, 0x681b0280, 0xb36a04c0, 0x5ed287e0, 0xe8308b60, 0x05880840, 0xdef90e00, 0x33418d20, |
| 77 | 0x05713c00, 0xe8c9bf20, 0x33b8b960, 0xde003a40, 0x68e236c0, 0x855ab5e0, 0x5e2bb3a0, 0xb3933080, |
| 78 | 0xde572980, 0x33efaaa0, 0xe89eace0, 0x05262fc0, 0xb3c42340, 0x5e7ca060, 0x850da620, 0x68b52500, |
| 79 | 0x6b06e7c0, 0x86be64e0, 0x5dcf62a0, 0xb077e180, 0x0695ed00, 0xeb2d6e20, 0x305c6860, 0xdde4eb40, |
| 80 | 0xb020f240, 0x5d987160, 0x86e97720, 0x6b51f400, 0xddb3f880, 0x300b7ba0, 0xeb7a7de0, 0x06c2fec0, |
| 81 | 0x30f24fe0, 0xdd4accc0, 0x063bca80, 0xeb8349a0, 0x5d614520, 0xb0d9c600, 0x6ba8c040, 0x86104360, |
| 82 | 0xebd45a60, 0x066cd940, 0xdd1ddf00, 0x30a55c20, 0x864750a0, 0x6bffd380, 0xb08ed5c0, 0x5d3656e0, |
| 83 | 0xdcefb780, 0x315734a0, 0xea2632e0, 0x079eb1c0, 0xb17cbd40, 0x5cc43e60, 0x87b53820, 0x6a0dbb00, |
| 84 | 0x07c9a200, 0xea712120, 0x31002760, 0xdcb8a440, 0x6a5aa8c0, 0x87e22be0, 0x5c932da0, 0xb12bae80, |
| 85 | 0x871b1fa0, 0x6aa39c80, 0xb1d29ac0, 0x5c6a19e0, 0xea881560, 0x07309640, 0xdc419000, 0x31f91320, |
| 86 | 0x5c3d0a20, 0xb1858900, 0x6af48f40, 0x874c0c60, 0x31ae00e0, 0xdc1683c0, 0x07678580, 0xeadf06a0, |
| 87 | }; |
| 88 | |
| 89 | auto data = reinterpret_cast<const uint8_t*>(bt); |
| 90 | uint32_t crc = 0xffffffffu; |
| 91 | |
| 92 | for (size_t i = 0; i < sizeof(*bt); ++i) { |
| 93 | const uint32_t lookupIndex = (crc ^ data[i]) & 0xff; |
| 94 | crc = (crc >> 8) ^ crcTable[lookupIndex]; // CRCTable is an array of 256 32-bit constants |
| 95 | } |
| 96 | |
| 97 | // Finalize the CRC-32 value by inverting all the bits |
| 98 | crc ^= 0xFFFFFFFFu; |
| 99 | return crc; |
| 100 | } |
| 101 | |
| 102 | #define returnError(x) do { TRACE("DONE " << x); return Response::ERROR; } while(0) |
| 103 | |
| 104 | bool FakeTape::readBlockTable() { |
| 105 | TRACE("ENTER"); |
| 106 | if (fd == -1) |
| 107 | return false; |
| 108 | |
| 109 | int rl = read(fd, &cbt, sizeof cbt); |
| 110 | if (rl != sizeof cbt) |
| 111 | return false; |
| 112 | |
| 113 | uint32_t read_crc = cbt.crc; |
| 114 | cbt.crc = 0; |
| 115 | cbt.crc = crc32(&cbt); |
| 116 | if (cbt.crc != read_crc) |
| 117 | return false; |
| 118 | cbt_index = 0; |
| 119 | TRACE("DONE"); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | /* Seek to the blocktable for the current block */ |
| 124 | bool FakeTape::seekBlockTable() { |
| 125 | TRACE("ENTER"); |
| 126 | if (fd == -1) |
| 127 | return false; |
| 128 | |
| 129 | off_t seek = -(off_t)sizeof cbt; |
| 130 | for (uint32_t i=0; i < cbt_index; ++i) |
| 131 | seek -= cbt.disksize(i); |
| 132 | |
| 133 | if(lseek(fd, seek, SEEK_CUR) < 0) |
| 134 | return false; |
| 135 | TRACE("DONE"); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | /* This replaces the existing block table that we're using */ |
| 140 | bool FakeTape::writeBlockTable() { |
| 141 | TRACE("ENTER"); |
| 142 | if (fd == -1 || !writing) |
| 143 | return false; |
| 144 | |
| 145 | off_t curpos = lseek(fd, 0, SEEK_CUR); |
| 146 | if (curpos < 0 ) |
| 147 | return false; |
| 148 | |
| 149 | if(!seekBlockTable()) |
| 150 | return false; |
| 151 | |
| 152 | BlockTable bt = cbt; |
| 153 | bt.crc = 0; |
| 154 | bt.crc = crc32(&bt); |
| 155 | |
| 156 | if(!readBlockTable()) |
| 157 | return false; |
| 158 | cbt_index = bt.count; |
| 159 | |
| 160 | if(lseek(fd, -(off_t)sizeof cbt, SEEK_CUR) < 0) |
| 161 | return false; |
| 162 | |
| 163 | if (write(fd, &bt, sizeof bt) != sizeof bt) |
| 164 | return false; |
| 165 | |
| 166 | if(lseek(fd, curpos, SEEK_SET) < 0) |
| 167 | return false; |
| 168 | |
| 169 | cbt = bt; |
| 170 | TRACE("DONE"); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | /* This writes a new block table */ |
| 175 | bool FakeTape::writeNewBlockTable() { |
| 176 | TRACE("ENTER"); |
| 177 | if (fd == -1 || !writing) |
| 178 | return false; |
| 179 | |
| 180 | BlockTable bt; |
| 181 | if (cbt.count == cbt.maxblocks()) { |
| 182 | bt.prevCount = cbt.count; |
| 183 | bt.prevSize = sizeof bt; |
| 184 | for (uint32_t i=0; i < cbt.maxblocks(); ++i) { |
| 185 | bt.prevSize += cbt.disksize(i); |
| 186 | if(cbt.size(i) == 0) |
| 187 | bt.prevSize |= 1ULL<<63; |
| 188 | } |
| 189 | } else if (cbt.count != 0 || cbt.prevSize != 0) |
| 190 | return false; |
| 191 | bt.crc = crc32(&bt); |
| 192 | |
| 193 | if (write(fd, &bt, sizeof bt) != sizeof bt) |
| 194 | return false; |
| 195 | TRACE("wrote " << sizeof bt << " bytes to tape for new BlockTable "); |
| 196 | cbt = bt; |
| 197 | cbt_index = 0; |
| 198 | off_t curpos = lseek(fd, 0, SEEK_CUR); |
| 199 | if (curpos < 0 ) |
| 200 | return false; |
| 201 | if(ftruncate(fd, curpos) < 0) |
| 202 | return false; |
| 203 | TRACE("DONE"); |
| 204 | return true; |
| 205 | }; |
| 206 | |
| 207 | FakeTape::Response FakeTape::advanceOneBlock() { |
| 208 | TRACE("ENTER"); |
| 209 | if (fd == -1) |
| 210 | returnError("tape not open"); |
| 211 | |
| 212 | if (writing) |
| 213 | returnError("tape must be open for reading to seek"); |
| 214 | |
| 215 | if (cbt_index == cbt.count) |
| 216 | returnError("seeking past end of tape"); |
| 217 | uint32_t datasize = cbt.size(cbt_index); |
| 218 | off_t disksize = cbt.disksize(cbt_index++); |
| 219 | if (lseek(fd, disksize, SEEK_CUR) < 0) |
| 220 | return Response::FATAL; |
| 221 | if (cbt_index == cbt.maxblocks()) |
| 222 | if(!readBlockTable()) |
| 223 | return Response::FATAL; |
| 224 | block++; |
| 225 | fileblock++; |
| 226 | if (datasize == 0) { |
| 227 | filenum++; |
| 228 | fileblock=0; |
| 229 | } |
| 230 | TRACE("DONE"); |
| 231 | return Response::OK; |
| 232 | } |
| 233 | |
| 234 | FakeTape::Response FakeTape::reverseOneBlock() { |
| 235 | TRACE("ENTER"); |
| 236 | if (fd == -1) |
| 237 | returnError("tape not open"); |
| 238 | |
| 239 | if (writing) |
| 240 | returnError("tape must be open for reading to seek"); |
| 241 | |
| 242 | if (cbt_index == 0) { |
| 243 | if (cbt.prevSize == 0) |
| 244 | returnError("seeking past start of tape"); |
| 245 | off_t prevSize = cbt.prevSize & (~0ULL>>1); |
| 246 | if (lseek(fd, -prevSize - sizeof cbt, SEEK_CUR) < 0) |
| 247 | return Response::FATAL; |
| 248 | if(!readBlockTable()) |
| 249 | return Response::FATAL; |
| 250 | if (lseek(fd, prevSize - sizeof cbt, SEEK_CUR) < 0) |
| 251 | return Response::FATAL; |
| 252 | cbt_index = cbt.maxblocks(); |
| 253 | } |
| 254 | uint32_t datasize = cbt.size(--cbt_index); |
| 255 | off_t disksize = cbt.disksize(cbt_index); |
| 256 | if (lseek(fd, -disksize, SEEK_CUR) < 0) |
| 257 | return Response::FATAL; |
| 258 | block--; |
| 259 | if (datasize == 0) { |
| 260 | if (read(fd, &fileblock, disksize) != disksize) |
| 261 | return Response::FATAL; |
| 262 | if (lseek(fd, -disksize, SEEK_CUR) < 0) |
| 263 | return Response::FATAL; |
| 264 | filenum--; |
| 265 | } else |
| 266 | fileblock--; |
| 267 | |
| 268 | TRACE("DONE"); |
| 269 | return Response::OK; |
| 270 | } |
| 271 | |
| 272 | FakeTape::Response FakeTape::blockSeekFwd(size_t offset) { |
| 273 | TRACE("ENTER offset=" << offset); |
| 274 | if (fd == -1) |
| 275 | returnError("tape not open"); |
| 276 | |
| 277 | if (writing) |
| 278 | returnError("tape must be open for reading to seek"); |
| 279 | |
| 280 | bool handleEOF = false; |
| 281 | while (offset) { |
| 282 | if (cbt_index || handleEOF) { |
| 283 | if (Response r = advanceOneBlock(); r != Response::OK) |
| 284 | return r; |
| 285 | else |
| 286 | offset--; |
| 287 | handleEOF = false; |
| 288 | } else if (offset >= cbt.maxblocks()) { |
| 289 | if (cbt.count != cbt.maxblocks()) |
| 290 | returnError("seek past end of tape"); |
| 291 | off_t bs = 0; |
| 292 | for (uint32_t i=0; i < cbt.maxblocks(); ++i) { |
| 293 | if (!cbt.size(i)) |
| 294 | handleEOF = true; |
| 295 | else |
| 296 | bs += cbt.disksize(i); |
| 297 | } |
| 298 | if(!handleEOF) { |
| 299 | if (lseek(fd, bs, SEEK_CUR) < 0) |
| 300 | return Response::FATAL; |
| 301 | offset -= cbt.maxblocks(); |
| 302 | block += cbt.maxblocks(); |
| 303 | fileblock += cbt.maxblocks(); |
| 304 | if(!readBlockTable()) |
| 305 | return Response::FATAL; |
| 306 | } |
| 307 | } else |
| 308 | handleEOF = true; |
| 309 | } |
| 310 | TRACE("DONE"); |
| 311 | return Response::OK; |
| 312 | } |
| 313 | |
| 314 | FakeTape::Response FakeTape::blockSeekBwd(size_t offset) { |
| 315 | TRACE("ENTER offset=" << offset); |
| 316 | if (fd == -1) |
| 317 | returnError("tape not open"); |
| 318 | |
| 319 | if (writing) |
| 320 | returnError("tape must be open for reading to seek"); |
| 321 | |
| 322 | bool handleEOF = false; |
| 323 | while(offset) { |
| 324 | if (cbt_index || handleEOF) { |
| 325 | if (Response r = reverseOneBlock(); r != Response::OK) |
| 326 | return r; |
| 327 | else |
| 328 | offset--; |
| 329 | } else if (cbt.prevCount && offset >= cbt.prevCount) { |
| 330 | if (cbt.prevSize == 0) |
| 331 | returnError("seek past beginning of tape"); |
| 332 | if (cbt.prevSize & (1ULL<<63)) { |
| 333 | handleEOF = true; |
| 334 | continue; |
| 335 | } |
| 336 | off_t seek = -cbt.prevSize - sizeof cbt; |
| 337 | if (lseek(fd, seek, SEEK_CUR) < 0) |
| 338 | return Response::FATAL; |
| 339 | if(!readBlockTable()) |
| 340 | return Response::FATAL; |
| 341 | |
| 342 | offset -= cbt.maxblocks(); |
| 343 | block -= cbt.maxblocks(); |
| 344 | } else |
| 345 | handleEOF = true; |
| 346 | } |
| 347 | TRACE("DONE"); |
| 348 | return Response::OK; |
| 349 | } |
| 350 | |
| 351 | FakeTape::Response FakeTape::closefd(const char* wmsg) { |
| 352 | TRACE("ENTER"); |
| 353 | |
| 354 | if(wmsg) |
| 355 | DEBUG((*this), wmsg << " errno=" << errno << ":" << strerror(errno)); |
| 356 | |
| 357 | if (fd != -1) |
| 358 | ::close(fd); |
| 359 | fd = -1; |
| 360 | |
| 361 | for(size_t i = 0; i< maxWorkers; ++i) { |
| 362 | context[i].in.clear(); |
| 363 | context[i].out.clear(); |
| 364 | } |
| 365 | |
| 366 | TRACE("DONE"); |
| 367 | return Response::FATAL; |
| 368 | } |
| 369 | |
| 370 | FakeTape::Response FakeTape::writeDataImpl(const uint8_t* data, size_t size, bool async) { |
| 371 | CalcTime ct(*writeDataImpl_time); |
| 372 | TRACE("ENTER " << (async?"ASYNC":"SYNC") << " size=" << size); |
| 373 | |
| 374 | if (fd == -1 || !writing) |
| 375 | returnError("tape not open"); |
| 376 | |
| 377 | if (size > max_block_size) { |
| 378 | errno = EIO; |
| 379 | returnError("block too big"); |
| 380 | } |
| 381 | |
| 382 | /* inbufidx, writebufidx | joinidx inbufidx | |
| 383 | * 0 0 | 0 1 | |
| 384 | * 1 0 | 0 2 | |
| 385 | * 2 0 | 0 3 | |
| 386 | * 2 0 | ---------------- | wait for writebufidx to change |
| 387 | * 3 1 | 1 4 | |
| 388 | */ |
| 389 | // We do not need a lock here unless we're waiting on all of the workers. |
| 390 | if (inBufIdx == writeBufIdx + maxWorkers) { |
| 391 | CalcTime ct(*writeDataImpl_wait_time); |
| 392 | std::unique_lock lk(filemutex); |
| 393 | block_written.wait(lk, [this]() { return inBufIdx < writeBufIdx + maxWorkers; } ); |
| 394 | } |
| 395 | |
| 396 | if (join() != Response::OK) |
| 397 | /* We hold the write lock here so we can safely close */ |
| 398 | return closefd("Fatal error during async writes"); |
| 399 | |
| 400 | size_t idx = inBufIdx++; |
| 401 | worker_context& ctxt = context[idx % maxWorkers]; |
| 402 | |
| 403 | if (ctxt.in.size() < size) { |
| 404 | ctxt.in.resize(size); |
| 405 | ctxt.out.resize(compressBound(size)); |
| 406 | } |
| 407 | ctxt.size = size; |
| 408 | memcpy(ctxt.in.data(), data, size); |
| 409 | written = true; |
| 410 | |
| 411 | ctxt.t = std::thread(&FakeTape::worker, this, idx); |
| 412 | Response r = Response::OK; |
| 413 | if (!async) { |
| 414 | ctxt.t.join(); |
| 415 | joinIdx++; |
| 416 | r = ctxt.response; |
| 417 | } |
| 418 | |
| 419 | TRACE("DONE"); |
| 420 | return r; |
| 421 | } |
| 422 | |
| 423 | FakeTape::Response FakeTape::join() { |
| 424 | TRACE("ENTER"); |
| 425 | Response r = Response::OK; |
| 426 | while (joinIdx < writeBufIdx) { |
| 427 | worker_context& jctxt = context[joinIdx++%maxWorkers]; |
| 428 | jctxt.t.join(); |
| 429 | if(r == Response::OK) |
| 430 | r = jctxt.response; |
| 431 | else if (jctxt.response == Response::FATAL) |
| 432 | r = Response::FATAL; |
| 433 | } |
| 434 | TRACE("DONE"); |
| 435 | return r; |
| 436 | } |
| 437 | |
| 438 | void FakeTape::worker(size_t idx) { |
| 439 | TRACE("ENTER idx=" << idx); |
| 440 | std::unique_lock lk(filemutex); |
| 441 | |
| 442 | bool zeroblock = false; /* This flags an all zero buffer */ |
| 443 | worker_context& ctxt = context[idx%maxWorkers]; |
| 444 | size_t size = ctxt.size; |
| 445 | uint8_t* indata = ctxt.in.data(); |
| 446 | uLongf cmprlen = size; |
| 447 | |
| 448 | if (size == 0) { |
| 449 | /* End of file marker */ |
| 450 | zeroblock = false; |
| 451 | ctxt.block_written.wait(lk, [this, &idx]() { return writeBufIdx == idx; }); |
| 452 | TRACE("GOT LOCK writeBufIdx=" << writeBufIdx << " idx=" << idx); |
| 453 | |
| 454 | if(write(fd, &fileblock, sizeof fileblock) != sizeof(uint64_t)) { |
| 455 | ctxt.response = closefd("write of fileblock failed in writeData"); |
| 456 | return; |
| 457 | } |
| 458 | TRACE("wrote " << sizeof(uint64_t) << " bytes to tape for fileblock " << fileblock); |
| 459 | filenum++; |
| 460 | fileblock = 0; |
| 461 | } else { |
| 462 | zeroblock = size>1 && std::find_if(indata, indata+size, [](uint8_t b){ return b!=0; }) == indata+size; |
| 463 | if(!zeroblock) { |
| 464 | #if USE_ZLIB_COMPRESSION |
| 465 | cmprlen = ctxt.out.size(); |
| 466 | uint8_t* outdata = ctxt.out.data(); |
| 467 | int status = compress2(outdata, &cmprlen, indata, size, 2); |
| 468 | switch (status) { |
| 469 | case Z_OK: break; |
| 470 | case Z_MEM_ERROR: |
| 471 | DEBUG((*this), "Z_MEM_ERROR from compress"); |
| 472 | ctxt.response = Response::FATAL; |
| 473 | return; |
| 474 | case Z_BUF_ERROR: |
| 475 | DEBUG((*this), "Z_BUF_ERROR from compress"); |
| 476 | ctxt.response = Response::FATAL; |
| 477 | return; |
| 478 | case Z_STREAM_ERROR: |
| 479 | DEBUG((*this), "Z_STREAM_ERROR from compress"); |
| 480 | ctxt.response = Response::FATAL; |
| 481 | return; |
| 482 | default: |
| 483 | DEBUG((*this), "Unexpected return " << status << " from compress2"); |
| 484 | ctxt.response = Response::FATAL; |
| 485 | return; |
| 486 | } |
| 487 | ctxt.block_written.wait(lk, [this, &idx]() { return writeBufIdx == idx; }); |
| 488 | if (write(fd, outdata, cmprlen) != (ssize_t)cmprlen) { |
| 489 | ctxt.response = closefd("write failed in worker"); |
| 490 | return; |
| 491 | } |
| 492 | TRACE("wrote " << cmprlen << " bytes to tape for fileblock " << fileblock); |
| 493 | #else |
| 494 | ctxt.block_written.wait(lk, [this, &idx]() { return writeBufIdx == idx; }); |
| 495 | if (write(fd, indata, cmprlen) != (ssize_t)cmprlen) { |
| 496 | ctxt.response = closefd("write failed in writeData"); |
| 497 | return; |
| 498 | } |
| 499 | TRACE("wrote " << cmprlen << " bytes to tape for fileblock " << fileblock); |
| 500 | #endif |
| 501 | } else { |
| 502 | ctxt.block_written.wait(lk, [this, &idx]() { return writeBufIdx == idx; }); |
| 503 | } |
| 504 | fileblock++; |
| 505 | } |
| 506 | |
| 507 | cbt.setsize(cbt_index++, cmprlen, zeroblock); |
| 508 | cbt.count = cbt_index; |
| 509 | if (cbt.count == cbt.maxblocks()) { |
| 510 | if (!writeBlockTable()) { |
| 511 | ctxt.response = closefd("writeBlockTable failed"); |
| 512 | return; |
| 513 | } |
| 514 | if (!writeNewBlockTable()) { |
| 515 | ctxt.response = closefd("writeNewBlockTable failed"); |
| 516 | return; |
| 517 | } |
| 518 | } |
| 519 | block++; |
| 520 | writeBufIdx++; |
| 521 | lk.unlock(); |
| 522 | // Tell the main thread that there's space available. |
| 523 | block_written.notify_one(); |
| 524 | // Tell the next block to start writing. |
| 525 | context[(idx + 1) % maxWorkers].block_written.notify_one(); |
| 526 | ctxt.response = Response::OK; |
| 527 | TRACE("DONE idx=" << idx); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* public functions below here */ |
| 532 | FakeTape::Response FakeTape::open(const std::string& filename, int flags) { |
| 533 | TRACE("ENTER filename=" << filename << " flags=" << flags); |
| 534 | |
| 535 | errno = 0; |
| 536 | if (fd != -1) |
| 537 | returnError("tape already open"); |
| 538 | if (flags & ~(O_ACCMODE|O_CREAT|O_TRUNC)) { |
| 539 | errno = EINVAL; |
| 540 | returnError("invalid flags: only ACCMODE, CREAT and TRUNC supported"); |
| 541 | } |
| 542 | bool open_for_writing; |
| 543 | switch (flags & O_ACCMODE) { |
| 544 | case O_WRONLY: |
| 545 | open_for_writing = true; |
| 546 | break; |
| 547 | case O_RDONLY: |
| 548 | open_for_writing = false; |
| 549 | break; |
| 550 | default: |
| 551 | errno = EINVAL; |
| 552 | returnError("invalid flags: only O_WRONLY or O_RDONLY supported for ACCMODE"); |
| 553 | } |
| 554 | writing = false; |
| 555 | written = false; |
| 556 | |
| 557 | // We always open for RDWR due to having to save the tape position when we close |
| 558 | flags &= O_CREAT; |
| 559 | flags |= O_RDWR; |
| 560 | |
| 561 | cbt = BlockTable{}; |
| 562 | cbt_index = 0; |
| 563 | block = 0; |
| 564 | filenum = 0; |
| 565 | fileblock = 0; |
| 566 | |
| 567 | struct stat s; |
| 568 | if(stat(filename.c_str(), &s) == -1) { |
| 569 | if(!(flags&O_CREAT)) |
| 570 | returnError("tape does not exist"); |
| 571 | } else if(!S_ISREG(s.st_mode)) |
| 572 | returnError("not a valid tape"); |
| 573 | |
| 574 | fd = ::open(filename.c_str(), flags, 0600); |
| 575 | if (fd == -1) |
| 576 | returnError("Can't open tape"); |
| 577 | |
| 578 | if(fstat(fd, &s) == -1) { |
| 579 | return closefd("stat on fd failed"); |
| 580 | } |
| 581 | |
| 582 | for(size_t i = 0; i< maxWorkers; ++i) { |
| 583 | context[i].in.resize(max_block_size); |
| 584 | context[i].out.resize(compressBound(max_block_size)); |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * If the file didn't exist or was empty we'll create a new tape. If |
| 589 | * the file contained data then it must be a valid taper |
| 590 | */ |
| 591 | if(s.st_size == 0) { |
| 592 | writing = open_for_writing; |
| 593 | |
| 594 | if(!writing) |
| 595 | return closefd("Empty file but not opened for writing. Bug?"); |
| 596 | |
| 597 | if(!writeNewBlockTable()) |
| 598 | return closefd("Can't write initial block"); |
| 599 | } else { |
| 600 | BlockTable bt; |
| 601 | if(read(fd, &bt, sizeof bt) != sizeof bt) |
| 602 | return closefd("Can't read initial BlockTable"); |
| 603 | |
| 604 | uint32_t read_crc = bt.crc; |
| 605 | bt.crc = 0; |
| 606 | if(read_crc != crc32(&bt)) |
| 607 | return closefd("Initial crc corrupt"); |
| 608 | |
| 609 | off_t blockTablePos; |
| 610 | uint32_t offset; |
| 611 | offset = bt.recdata.firstblock.cbt_index; |
| 612 | block = bt.recdata.firstblock.blockno; |
| 613 | filenum = bt.recdata.firstblock.filenum; |
| 614 | fileblock = bt.recdata.firstblock.fileblock; |
| 615 | blockTablePos = bt.recdata.firstblock.blockTablePos; |
| 616 | if(lseek(fd, blockTablePos, SEEK_SET) != blockTablePos) |
| 617 | return closefd("Corrupted tape. Cannot seek to restored position"); |
| 618 | if (!readBlockTable()) |
| 619 | return closefd("Corrupted tape. Cannot seek to restored position"); |
| 620 | if(blockSeekFwd(offset) != Response::OK) |
| 621 | return closefd("Corrupted tape. Cannot seek to restored position"); |
| 622 | writing = open_for_writing; |
| 623 | if(writing) { |
| 624 | cbt.count = cbt_index; |
| 625 | for(uint32_t i=cbt_index; i<cbt.maxblocks(); ++i) |
| 626 | cbt.setsize(i, 0, false); |
| 627 | } |
| 628 | } |
| 629 | TRACE("DONE"); |
| 630 | writeDataImpl_time = std::make_unique<FakeTapeTimer>(*this, "writeDataImpl_time"); |
| 631 | writeDataImpl_wait_time = std::make_unique<FakeTapeTimer>(*this, "writeDataImpl_wait_time"); |
| 632 | return Response::OK; |
| 633 | } |
| 634 | |
| 635 | void FakeTape::close() { |
| 636 | TRACE("ENTER"); |
| 637 | |
| 638 | std::unique_lock lk(filemutex); |
| 639 | block_written.wait(lk, [this]() { return inBufIdx == writeBufIdx; } ); |
| 640 | |
| 641 | /* All the writers have finished */ |
| 642 | if (join() != Response::OK) |
| 643 | closefd("Fatal error during async writes"); |
| 644 | |
| 645 | const char* close_with_err = "Error while closing tape"; |
| 646 | if (fd != -1) { |
| 647 | lk.unlock(); |
| 648 | BlockTable bt; |
| 649 | off_t blockpos; |
| 650 | uint32_t save_cbt_index; |
| 651 | if (written == true) { |
| 652 | if (writeData(nullptr, 0) != Response::OK) |
| 653 | goto close; |
| 654 | if (sync() != Response::OK) |
| 655 | goto close; |
| 656 | if (!writeBlockTable()) |
| 657 | goto close; |
| 658 | blockpos = lseek(fd, 0, SEEK_CUR); |
| 659 | if (blockpos == -1) |
| 660 | goto close; |
| 661 | if (ftruncate(fd, blockpos) == -1) |
| 662 | goto close; |
| 663 | } |
| 664 | save_cbt_index = cbt_index; |
| 665 | writing = false; |
| 666 | if (blockSeekBwd(cbt_index) != Response::OK) |
| 667 | goto close; |
| 668 | blockpos = lseek(fd, 0, SEEK_CUR) - sizeof bt; |
| 669 | if (blockpos == -1) |
| 670 | goto close; |
| 671 | if(lseek(fd, 0, SEEK_SET) != 0) |
| 672 | goto close; |
| 673 | if(read(fd, &bt, sizeof bt) != sizeof bt) |
| 674 | goto close; |
| 675 | if(lseek(fd, 0, SEEK_SET) != 0) |
| 676 | goto close; |
| 677 | bt.recdata.firstblock.cbt_index = save_cbt_index; |
| 678 | bt.recdata.firstblock.blockno = block; |
| 679 | bt.recdata.firstblock.filenum = filenum; |
| 680 | bt.recdata.firstblock.fileblock = fileblock; |
| 681 | bt.recdata.firstblock.blockTablePos = blockpos; |
| 682 | bt.crc = 0; |
| 683 | bt.crc = crc32(&bt); |
| 684 | if(write(fd, &bt, sizeof bt) == sizeof bt) |
| 685 | close_with_err = nullptr; |
| 686 | close: |
| 687 | closefd(close_with_err); |
| 688 | } |
| 689 | fd = -1; |
| 690 | |
| 691 | TRACE("DONE"); |
| 692 | writeDataImpl_time.reset(); |
| 693 | writeDataImpl_wait_time.reset(); |
| 694 | } |
| 695 | |
| 696 | FakeTape::Response FakeTape::rewind() { |
| 697 | TRACE("ENTER"); |
| 698 | |
| 699 | if (fd == -1) |
| 700 | returnError("tape not open"); |
| 701 | |
| 702 | if(writing) |
| 703 | returnError("tape must be open for reading to seek"); |
| 704 | |
| 705 | if(lseek(fd, 0, SEEK_SET) != 0) |
| 706 | return closefd("Corrupted tape. Cannot seek to start of tape"); |
| 707 | block = 0; |
| 708 | fileblock = 0; |
| 709 | filenum = 0; |
| 710 | |
| 711 | if (!readBlockTable()) |
| 712 | return closefd("Corrupted tape. Cannot read start of tape"); |
| 713 | |
| 714 | TRACE("DONE"); |
| 715 | return Response::OK; |
| 716 | } |
| 717 | |
| 718 | FakeTape::Response FakeTape::erase() { |
| 719 | TRACE("ENTER"); |
| 720 | |
| 721 | if (fd == -1) |
| 722 | returnError("tape not open"); |
| 723 | |
| 724 | if(!writing || written) |
| 725 | returnError("tape not open for writing or has been modified"); |
| 726 | |
| 727 | if(lseek(fd, 0, SEEK_SET) != 0) |
| 728 | return closefd("Corrupted tape. Cannot seek to start of tape"); |
| 729 | |
| 730 | block = 0; |
| 731 | fileblock = 0; |
| 732 | filenum = 0; |
| 733 | |
| 734 | if (!readBlockTable()) |
| 735 | return closefd("Corrupted tape. Cannot read start of tape"); |
| 736 | |
| 737 | if (ftruncate(fd, sizeof(BlockTable)) == -1) |
| 738 | return closefd("truncate failed"); |
| 739 | TRACE("DONE"); |
| 740 | return Response::OK; |
| 741 | } |
| 742 | |
| 743 | FakeTape::Response FakeTape::blockSeek(size_t offset) { |
| 744 | TRACE("ENTER offset=" << offset); |
| 745 | if (fd == -1) |
| 746 | returnError("tape not open"); |
| 747 | |
| 748 | if (writing) |
| 749 | returnError("tape must be open for reading to seek"); |
| 750 | |
| 751 | Response status; |
| 752 | if (offset == block) |
| 753 | status = Response::OK; |
| 754 | else if (offset > block) |
| 755 | status = blockSeekFwd(offset - block); |
| 756 | else |
| 757 | status = blockSeekBwd(block - offset); |
| 758 | if (status == Response::FATAL) |
| 759 | closefd("corrupted tape while seeking"); |
| 760 | return status; |
| 761 | } |
| 762 | |
| 763 | FakeTape::Response FakeTape::writeData(const uint8_t* data, size_t size) { |
| 764 | return writeDataImpl(data, size, false); |
| 765 | } |
| 766 | |
| 767 | FakeTape::Response FakeTape::writeDataAsync(const uint8_t* data, size_t size) { |
| 768 | return writeDataImpl(data, size, true); |
| 769 | } |
| 770 | |
| 771 | FakeTape::Response FakeTape::sync() { |
| 772 | TRACE("ENTER"); |
| 773 | std::unique_lock lk(filemutex); |
| 774 | block_written.wait(lk, [this]() { return inBufIdx == writeBufIdx; } ); |
| 775 | Response r = Response::OK; |
| 776 | while (joinIdx < writeBufIdx) { |
| 777 | worker_context& jctxt = context[joinIdx++%maxWorkers]; |
| 778 | jctxt.t.join(); |
| 779 | if(r == Response::OK) |
| 780 | r = jctxt.response; |
| 781 | else if (jctxt.response == Response::FATAL) |
| 782 | r = Response::FATAL; |
| 783 | } |
| 784 | TRACE("DONE"); |
| 785 | return r; |
| 786 | } |
| 787 | |
| 788 | FakeTape::Response FakeTape::readData(uint8_t* data, size_t maxsize, size_t* readsize) { |
| 789 | TRACE("ENTER maxsize=" << maxsize); |
| 790 | if (fd == -1 || writing) |
| 791 | returnError("tape not open for reading"); |
| 792 | |
| 793 | if (maxsize > max_block_size) { |
| 794 | errno = EIO; |
| 795 | returnError("Block too big"); |
| 796 | } |
| 797 | |
| 798 | if (cbt_index == cbt.count) { |
| 799 | *readsize = 0; |
| 800 | return Response::OK; |
| 801 | } |
| 802 | |
| 803 | uint32_t disksize = cbt.disksize(cbt_index); |
| 804 | bool zeroblock = cbt.zeroblock(cbt_index); |
| 805 | uint32_t blocksize = cbt.size(cbt_index++); |
| 806 | |
| 807 | if(zeroblock) { |
| 808 | if (blocksize < maxsize) |
| 809 | maxsize = blocksize; |
| 810 | std::memset(data, 0, maxsize); |
| 811 | if (lseek(fd, disksize, SEEK_CUR) < 0) { |
| 812 | return closefd("Error seeking to end of block"); |
| 813 | } |
| 814 | } else if (blocksize == 0) { |
| 815 | maxsize = 0; |
| 816 | } else { |
| 817 | #if USE_ZLIB_COMPRESSION |
| 818 | static std::vector<uint8_t> cmprbuf; |
| 819 | if (cmprbuf.size() < disksize) |
| 820 | cmprbuf.resize(disksize); |
| 821 | if (read(fd, cmprbuf.data(), disksize) != (ssize_t)disksize) |
| 822 | return closefd("Error reading block from tape"); |
| 823 | TRACE("read " << disksize << " bytes from tape for fileblock " << fileblock); |
| 824 | uLongf decmprlen = maxsize; |
| 825 | int status = uncompress(data, &decmprlen, cmprbuf.data(), disksize); |
| 826 | switch (status) { |
| 827 | case Z_OK: break; |
| 828 | case Z_MEM_ERROR: return closefd("Memory error while decompressing"); |
| 829 | case Z_BUF_ERROR: break; |
| 830 | case Z_DATA_ERROR: return closefd("Corrupt compressed data while decompressing"); |
| 831 | default: |
| 832 | DEBUG((*this), "Unexpected return " << status << " from uncompress"); |
| 833 | return closefd("Unexpected return from uncompress"); |
| 834 | } |
| 835 | TRACE("Decompressed to " << decmprlen << " bytes"); |
| 836 | if (decmprlen < maxsize) |
| 837 | maxsize = decmprlen; |
| 838 | /* In the case where there is not enough room, uncompress() |
| 839 | * will fill the output buffer with the uncompressed data up to |
| 840 | * that point. |
| 841 | * */ |
| 842 | #else |
| 843 | if (blocksize < maxsize) |
| 844 | maxsize = blocksize; |
| 845 | if (read(fd, data, maxsize) != (ssize_t)maxsize) |
| 846 | return closefd("Error reading block from tape"); |
| 847 | if (lseek(fd, disksize - maxsize, SEEK_CUR) < 0) |
| 848 | return closefd("Error seeking to end of block"); |
| 849 | #endif |
| 850 | } |
| 851 | *readsize = maxsize; |
| 852 | if (cbt_index == cbt.maxblocks()) |
| 853 | if(!readBlockTable()) { |
| 854 | return Response::FATAL; |
| 855 | } |
| 856 | block++; |
| 857 | fileblock++; |
| 858 | if (blocksize == 0) { |
| 859 | filenum++; |
| 860 | fileblock=0; |
| 861 | if (lseek(fd, disksize, SEEK_CUR) < 0) |
| 862 | return closefd("Error seeking past fileblock"); |
| 863 | } |
| 864 | TRACE("DONE"); |
| 865 | return Response::OK; |
| 866 | } |
| 867 | |
| 868 | FakeTape::Response FakeTape::fsr(size_t offset) { |
| 869 | TRACE("ENTER offset=" << offset); |
| 870 | if (fd == -1) |
| 871 | returnError("tape not open"); |
| 872 | |
| 873 | if (writing) |
| 874 | returnError("tape must be open for reading to seek"); |
| 875 | |
| 876 | return blockSeekFwd(offset); |
| 877 | } |
| 878 | |
| 879 | FakeTape::Response FakeTape::bsr(size_t offset) { |
| 880 | TRACE("ENTER offset=" << offset); |
| 881 | if (fd == -1) |
| 882 | returnError("tape not open"); |
| 883 | |
| 884 | if (writing) |
| 885 | returnError("tape must be open for reading to seek"); |
| 886 | |
| 887 | return blockSeekBwd(offset); |
| 888 | } |
| 889 | |
| 890 | size_t FakeTape::max_block_size = 16*1024*1024; |
| 891 | |
| 892 | /* vim: set sw=8 sts=8 ts=8 noexpandtab: */ |