Loading...
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | /* * Copyright (C) 1996, 1997 Claus Heine This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * * $Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-write.c,v $ * $Revision: 1.3 $ * $Date: 1997/11/06 00:50:29 $ * * This file contains the writing code * for the QIC-117 floppy-tape driver for Linux. */ #include <linux/errno.h> #include <linux/mm.h> #include <linux/zftape.h> #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,6) #include <asm/uaccess.h> #else #include <asm/segment.h> #endif #include "../zftape/zftape-init.h" #include "../zftape/zftape-eof.h" #include "../zftape/zftape-ctl.h" #include "../zftape/zftape-write.h" #include "../zftape/zftape-read.h" #include "../zftape/zftape-rw.h" #include "../zftape/zftape-vtbl.h" /* Global vars. */ /* Local vars. */ static int last_write_failed = 0; static int need_flush = 0; void zft_prevent_flush(void) { need_flush = 0; } static int zft_write_header_segments(__u8* buffer) { int header_1_ok = 0; int header_2_ok = 0; unsigned int time_stamp; TRACE_FUN(ft_t_noise); TRACE_CATCH(ftape_abort_operation(),); ftape_seek_to_bot(); /* prevents extra rewind */ if (GET4(buffer, 0) != FT_HSEG_MAGIC) { TRACE_ABORT(-EIO, ft_t_err, "wrong header signature found, aborting"); } /* Be optimistic: */ PUT4(buffer, FT_SEG_CNT, zft_written_segments + GET4(buffer, FT_SEG_CNT) + 2); if ((time_stamp = zft_get_time()) != 0) { PUT4(buffer, FT_WR_DATE, time_stamp); if (zft_label_changed) { PUT4(buffer, FT_LABEL_DATE, time_stamp); } } TRACE(ft_t_noise, "writing first header segment %d", ft_header_segment_1); header_1_ok = zft_verify_write_segments(ft_header_segment_1, buffer, FT_SEGMENT_SIZE, zft_deblock_buf) >= 0; TRACE(ft_t_noise, "writing second header segment %d", ft_header_segment_2); header_2_ok = zft_verify_write_segments(ft_header_segment_2, buffer, FT_SEGMENT_SIZE, zft_deblock_buf) >= 0; if (!header_1_ok) { TRACE(ft_t_warn, "Warning: " "update of first header segment failed"); } if (!header_2_ok) { TRACE(ft_t_warn, "Warning: " "update of second header segment failed"); } if (!header_1_ok && !header_2_ok) { TRACE_ABORT(-EIO, ft_t_err, "Error: " "update of both header segments failed."); } TRACE_EXIT 0; } int zft_update_header_segments(void) { TRACE_FUN(ft_t_noise); /* must NOT use zft_write_protected, as it also includes the * file access mode. But we also want to update when soft * write protection is enabled (O_RDONLY) */ if (ft_write_protected || zft_old_ftape) { TRACE_ABORT(0, ft_t_noise, "Tape set read-only: no update"); } if (!zft_header_read) { TRACE_ABORT(0, ft_t_noise, "Nothing to update"); } if (!zft_header_changed) { zft_header_changed = zft_written_segments > 0; } if (!zft_header_changed && !zft_volume_table_changed) { TRACE_ABORT(0, ft_t_noise, "Nothing to update"); } TRACE(ft_t_noise, "Updating header segments"); if (ftape_get_status()->fti_state == writing) { TRACE_CATCH(ftape_loop_until_writes_done(),); } TRACE_CATCH(ftape_abort_operation(),); zft_deblock_segment = -1; /* invalidate the cache */ if (zft_header_changed) { TRACE_CATCH(zft_write_header_segments(zft_hseg_buf),); } if (zft_volume_table_changed) { TRACE_CATCH(zft_update_volume_table(ft_first_data_segment),); } zft_header_changed = zft_volume_table_changed = zft_label_changed = zft_written_segments = 0; TRACE_CATCH(ftape_abort_operation(),); ftape_seek_to_bot(); TRACE_EXIT 0; } static int read_merge_buffer(int seg_pos, __u8 *buffer, int offset, int seg_sz) { int result = 0; const ft_trace_t old_tracing = TRACE_LEVEL; TRACE_FUN(ft_t_flow); if (zft_qic_mode) { /* writing in the middle of a volume is NOT allowed * */ TRACE(ft_t_noise, "No need to read a segment"); memset(buffer + offset, 0, seg_sz - offset); TRACE_EXIT 0; } TRACE(ft_t_any, "waiting"); ftape_start_writing(FT_WR_MULTI); TRACE_CATCH(ftape_loop_until_writes_done(),); TRACE(ft_t_noise, "trying to read segment %d from offset %d", seg_pos, offset); SET_TRACE_LEVEL(ft_t_bug); result = zft_fetch_segment_fraction(seg_pos, buffer, FT_RD_SINGLE, offset, seg_sz - offset); SET_TRACE_LEVEL(old_tracing); if (result != (seg_sz - offset)) { TRACE(ft_t_noise, "Ignore error: read_segment() result: %d", result); memset(buffer + offset, 0, seg_sz - offset); } TRACE_EXIT 0; } /* flush the write buffer to tape and write an eof-marker at the * current position if not in raw mode. This function always * positions the tape before the eof-marker. _ftape_close() should * then advance to the next segment. * * the parameter "finish_volume" describes whether to position before * or after the possibly created file-mark. We always position after * the file-mark when called from ftape_close() and a flush was needed * (that is ftape_write() was the last tape operation before calling * ftape_flush) But we always position before the file-mark when this * function get's called from outside ftape_close() */ int zft_flush_buffers(void) { int result; int data_remaining; int this_segs_size; TRACE_FUN(ft_t_flow); TRACE(ft_t_data_flow, "entered, ftape_state = %d", ftape_get_status()->fti_state); if (ftape_get_status()->fti_state != writing && !need_flush) { TRACE_ABORT(0, ft_t_noise, "no need for flush"); } zft_io_state = zft_idle; /* triggers some initializations for the * read and write routines */ if (last_write_failed) { ftape_abort_operation(); TRACE_EXIT -EIO; } TRACE(ft_t_noise, "flushing write buffers"); this_segs_size = zft_get_seg_sz(zft_pos.seg_pos); if (this_segs_size == zft_pos.seg_byte_pos) { zft_pos.seg_pos ++; data_remaining = zft_pos.seg_byte_pos = 0; } else { data_remaining = zft_pos.seg_byte_pos; } /* If there is any data not written to tape yet, append zero's * up to the end of the sector (if using compression) or merge * it with the data existing on the tape Then write the * segment(s) to tape. */ TRACE(ft_t_noise, "Position:\n" KERN_INFO "seg_pos : %d\n" KERN_INFO "byte pos : %d\n" KERN_INFO "remaining: %d", zft_pos.seg_pos, zft_pos.seg_byte_pos, data_remaining); if (data_remaining > 0) { do { this_segs_size = zft_get_seg_sz(zft_pos.seg_pos); if (this_segs_size > data_remaining) { TRACE_CATCH(read_merge_buffer(zft_pos.seg_pos, zft_deblock_buf, data_remaining, this_segs_size), last_write_failed = 1); } result = ftape_write_segment(zft_pos.seg_pos, zft_deblock_buf, FT_WR_MULTI); if (result != this_segs_size) { TRACE(ft_t_err, "flush buffers failed"); zft_pos.tape_pos -= zft_pos.seg_byte_pos; zft_pos.seg_byte_pos = 0; last_write_failed = 1; TRACE_EXIT result; } zft_written_segments ++; TRACE(ft_t_data_flow, "flush, moved out buffer: %d", result); /* need next segment for more data (empty segments?) */ if (result < data_remaining) { if (result > 0) { /* move remainder to buffer beginning */ memmove(zft_deblock_buf, zft_deblock_buf + result, FT_SEGMENT_SIZE - result); } } data_remaining -= result; zft_pos.seg_pos ++; } while (data_remaining > 0); TRACE(ft_t_any, "result: %d", result); zft_deblock_segment = --zft_pos.seg_pos; if (data_remaining == 0) { /* first byte next segment */ zft_pos.seg_byte_pos = this_segs_size; } else { /* after data previous segment, data_remaining < 0 */ zft_pos.seg_byte_pos = data_remaining + result; } } else { TRACE(ft_t_noise, "zft_deblock_buf empty"); zft_pos.seg_pos --; zft_pos.seg_byte_pos = zft_get_seg_sz (zft_pos.seg_pos); ftape_start_writing(FT_WR_MULTI); } TRACE(ft_t_any, "waiting"); if ((result = ftape_loop_until_writes_done()) < 0) { /* that's really bad. What to to with zft_tape_pos? */ TRACE(ft_t_err, "flush buffers failed"); } TRACE(ft_t_any, "zft_seg_pos: %d, zft_seg_byte_pos: %d", zft_pos.seg_pos, zft_pos.seg_byte_pos); last_write_failed = need_flush = 0; TRACE_EXIT result; } /* return-value: the number of bytes removed from the user-buffer * * out: * int *write_cnt: how much actually has been moved to the * zft_deblock_buf * int req_len : MUST NOT BE CHANGED, except at EOT, in * which case it may be adjusted * in : * char *buff : the user buffer * int buf_pos_write : copy of buf_len_wr int * this_segs_size : the size in bytes of the actual segment * char * *zft_deblock_buf : zft_deblock_buf */ static int zft_simple_write(int *cnt, __u8 *dst_buf, const int seg_sz, const __u8 *src_buf, const int req_len, const zft_position *pos,const zft_volinfo *volume) { int space_left; TRACE_FUN(ft_t_flow); /* volume->size holds the tape capacity while volume is open */ if (pos->tape_pos + volume->blk_sz > volume->size) { TRACE_EXIT -ENOSPC; } /* remaining space in this segment, NOT zft_deblock_buf */ space_left = seg_sz - pos->seg_byte_pos; *cnt = req_len < space_left ? req_len : space_left; #if LINUX_VERSION_CODE > KERNEL_VER(2,1,3) if (copy_from_user(dst_buf + pos->seg_byte_pos, src_buf, *cnt) != 0) { TRACE_EXIT -EFAULT; } #else TRACE_CATCH(verify_area(VERIFY_READ, src_buf, *cnt),); memcpy_fromfs(dst_buf + pos->seg_byte_pos, src_buf, *cnt); #endif TRACE_EXIT *cnt; } static int check_write_access(int req_len, const zft_volinfo **volume, zft_position *pos, const unsigned int blk_sz) { int result; TRACE_FUN(ft_t_flow); if ((req_len % zft_blk_sz) != 0) { TRACE_ABORT(-EINVAL, ft_t_info, "write-count %d must be multiple of block-size %d", req_len, blk_sz); } if (zft_io_state == zft_writing) { /* all other error conditions have been checked earlier */ TRACE_EXIT 0; } zft_io_state = zft_idle; TRACE_CATCH(zft_check_write_access(pos),); /* If we haven't read the header segment yet, do it now. * This will verify the configuration, get the bad sector * table and read the volume table segment */ if (!zft_header_read) { TRACE_CATCH(zft_read_header_segments(),); } /* fine. Now the tape is either at BOT or at EOD, * Write start of volume now */ TRACE_CATCH(zft_open_volume(pos, blk_sz, zft_use_compression),); *volume = zft_find_volume(pos->seg_pos); DUMP_VOLINFO(ft_t_noise, "", *volume); zft_just_before_eof = 0; /* now merge with old data if neccessary */ if (!zft_qic_mode && pos->seg_byte_pos != 0){ result = zft_fetch_segment(pos->seg_pos, zft_deblock_buf, FT_RD_SINGLE); if (result < 0) { if (result == -EINTR || result == -ENOSPC) { TRACE_EXIT result; } TRACE(ft_t_noise, "ftape_read_segment() result: %d. " "This might be normal when using " "a newly\nformatted tape", result); memset(zft_deblock_buf, '\0', pos->seg_byte_pos); } } zft_io_state = zft_writing; TRACE_EXIT 0; } static int fill_deblock_buf(__u8 *dst_buf, const int seg_sz, zft_position *pos, const zft_volinfo *volume, const char *usr_buf, const int req_len) { int cnt = 0; int result = 0; TRACE_FUN(ft_t_flow); if (seg_sz == 0) { TRACE_ABORT(0, ft_t_data_flow, "empty segment"); } TRACE(ft_t_data_flow, "\n" KERN_INFO "remaining req_len: %d\n" KERN_INFO " buf_pos: %d", req_len, pos->seg_byte_pos); /* zft_deblock_buf will not contain a valid segment any longer */ zft_deblock_segment = -1; if (zft_use_compression) { TRACE_CATCH(zft_cmpr_lock(1 /* try to load */),); TRACE_CATCH(result= (*zft_cmpr_ops->write)(&cnt, dst_buf, seg_sz, usr_buf, req_len, pos, volume),); } else { TRACE_CATCH(result= zft_simple_write(&cnt, dst_buf, seg_sz, usr_buf, req_len, pos, volume),); } pos->volume_pos += result; pos->seg_byte_pos += cnt; pos->tape_pos += cnt; TRACE(ft_t_data_flow, "\n" KERN_INFO "removed from user-buffer : %d bytes.\n" KERN_INFO "copied to zft_deblock_buf: %d bytes.\n" KERN_INFO "zft_tape_pos : " LL_X " bytes.", result, cnt, LL(pos->tape_pos)); TRACE_EXIT result; } /* called by the kernel-interface routine "zft_write()" */ int _zft_write(const char* buff, int req_len) { int result = 0; int written = 0; int write_cnt; int seg_sz; static const zft_volinfo *volume = NULL; TRACE_FUN(ft_t_flow); zft_resid = req_len; last_write_failed = 1; /* reset to 0 when successful */ /* check if write is allowed */ TRACE_CATCH(check_write_access(req_len, &volume,&zft_pos,zft_blk_sz),); while (req_len > 0) { /* Allow us to escape from this loop with a signal ! */ FT_SIGNAL_EXIT(_DONT_BLOCK); seg_sz = zft_get_seg_sz(zft_pos.seg_pos); if ((write_cnt = fill_deblock_buf(zft_deblock_buf, seg_sz, &zft_pos, volume, buff, req_len)) < 0) { zft_resid -= written; if (write_cnt == -ENOSPC) { /* leave the remainder to flush_buffers() */ TRACE(ft_t_info, "No space left on device"); last_write_failed = 0; if (!need_flush) { need_flush = written > 0; } TRACE_EXIT written > 0 ? written : -ENOSPC; } else { TRACE_EXIT result; } } if (zft_pos.seg_byte_pos == seg_sz) { TRACE_CATCH(ftape_write_segment(zft_pos.seg_pos, zft_deblock_buf, FT_WR_ASYNC), zft_resid -= written); zft_written_segments ++; zft_pos.seg_byte_pos = 0; zft_deblock_segment = zft_pos.seg_pos; ++zft_pos.seg_pos; } written += write_cnt; buff += write_cnt; req_len -= write_cnt; } /* while (req_len > 0) */ TRACE(ft_t_data_flow, "remaining in blocking buffer: %d", zft_pos.seg_byte_pos); TRACE(ft_t_data_flow, "just written bytes: %d", written); last_write_failed = 0; zft_resid -= written; need_flush = need_flush || written > 0; TRACE_EXIT written; /* bytes written */ } |