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 | /* * Copyright (C) 1993-1995 Bas Laarhoven, * (C) 1996-1997 Claus-Justus 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/lowlevel/ftape-write.c,v $ * $Revision: 1.3.4.1 $ * $Date: 1997/11/14 18:07:04 $ * * This file contains the writing code * for the QIC-117 floppy-tape driver for Linux. */ #include <linux/string.h> #include <linux/errno.h> #include <linux/mm.h> #include <linux/ftape.h> #include <linux/qic117.h> #include "../lowlevel/ftape-tracing.h" #include "../lowlevel/ftape-write.h" #include "../lowlevel/ftape-read.h" #include "../lowlevel/ftape-io.h" #include "../lowlevel/ftape-ctl.h" #include "../lowlevel/ftape-rw.h" #include "../lowlevel/ftape-ecc.h" #include "../lowlevel/ftape-bsm.h" #include "../lowlevel/fdc-isr.h" /* Global vars. */ /* Local vars. */ static int last_write_failed; void ftape_zap_write_buffers(void) { int i; for (i = 0; i < ft_nr_buffers; ++i) { ft_buffer[i]->status = done; } ftape_reset_buffer(); } static int copy_and_gen_ecc(void *destination, const void *source, const SectorMap bad_sector_map) { int result; struct memory_segment mseg; int bads = count_ones(bad_sector_map); TRACE_FUN(ft_t_any); if (bads > 0) { TRACE(ft_t_noise, "bad sectors in map: %d", bads); } if (bads + 3 >= FT_SECTORS_PER_SEGMENT) { TRACE(ft_t_noise, "empty segment"); mseg.blocks = 0; /* skip entire segment */ result = 0; /* nothing written */ } else { mseg.blocks = FT_SECTORS_PER_SEGMENT - bads; mseg.data = destination; memcpy(mseg.data, source, (mseg.blocks - 3) * FT_SECTOR_SIZE); result = ftape_ecc_set_segment_parity(&mseg); if (result < 0) { TRACE(ft_t_err, "ecc_set_segment_parity failed"); } else { result = (mseg.blocks - 3) * FT_SECTOR_SIZE; } } TRACE_EXIT result; } int ftape_start_writing(const ft_write_mode_t mode) { buffer_struct *head = ftape_get_buffer(ft_queue_head); int segment_id = head->segment_id; int result; buffer_state_enum wanted_state = (mode == FT_WR_DELETE ? deleting : writing); TRACE_FUN(ft_t_flow); if ((ft_driver_state != wanted_state) || head->status != waiting) { TRACE_EXIT 0; } ftape_setup_new_segment(head, segment_id, 1); if (mode == FT_WR_SINGLE) { /* stop tape instead of pause */ head->next_segment = 0; } ftape_calc_next_cluster(head); /* prepare */ head->status = ft_driver_state; /* either writing or deleting */ if (ft_runner_status == idle) { TRACE(ft_t_noise, "starting runner for segment %d", segment_id); TRACE_CATCH(ftape_start_tape(segment_id,head->sector_offset),); } else { TRACE(ft_t_noise, "runner not idle, not starting tape"); } /* go */ result = fdc_setup_read_write(head, (mode == FT_WR_DELETE ? FDC_WRITE_DELETED : FDC_WRITE)); ftape_set_state(wanted_state); /* should not be necessary */ TRACE_EXIT result; } /* Wait until all data is actually written to tape. * * There is a problem: when the tape runs into logical EOT, then this * failes. We need to restart the runner in this case. */ int ftape_loop_until_writes_done(void) { buffer_struct *head; TRACE_FUN(ft_t_flow); while ((ft_driver_state == writing || ft_driver_state == deleting) && ftape_get_buffer(ft_queue_head)->status != done) { /* set the runner status to idle if at lEOT */ TRACE_CATCH(ftape_handle_logical_eot(), last_write_failed = 1); /* restart the tape if necessary */ if (ft_runner_status == idle) { TRACE(ft_t_noise, "runner is idle, restarting"); if (ft_driver_state == deleting) { TRACE_CATCH(ftape_start_writing(FT_WR_DELETE), last_write_failed = 1); } else { TRACE_CATCH(ftape_start_writing(FT_WR_MULTI), last_write_failed = 1); } } TRACE(ft_t_noise, "tail: %d, head: %d", ftape_buffer_id(ft_queue_tail), ftape_buffer_id(ft_queue_head)); TRACE_CATCH(fdc_interrupt_wait(5 * FT_SECOND), last_write_failed = 1); head = ftape_get_buffer(ft_queue_head); if (head->status == error) { /* Allow escape from loop when signaled ! */ FT_SIGNAL_EXIT(_DONT_BLOCK); if (head->hard_error_map != 0) { /* Implement hard write error recovery here */ } /* retry this one */ head->status = waiting; if (ft_runner_status == aborting) { ftape_dumb_stop(); } if (ft_runner_status != idle) { TRACE_ABORT(-EIO, ft_t_err, "unexpected state: " "ft_runner_status != idle"); } ftape_start_writing(ft_driver_state == deleting ? FT_WR_MULTI : FT_WR_DELETE); } TRACE(ft_t_noise, "looping until writes done"); } ftape_set_state(idle); TRACE_EXIT 0; } /* Write given segment from buffer at address to tape. */ static int write_segment(const int segment_id, const void *address, const ft_write_mode_t write_mode) { int bytes_written = 0; buffer_struct *tail; buffer_state_enum wanted_state = (write_mode == FT_WR_DELETE ? deleting : writing); TRACE_FUN(ft_t_flow); TRACE(ft_t_noise, "segment_id = %d", segment_id); if (ft_driver_state != wanted_state) { if (ft_driver_state == deleting || wanted_state == deleting) { TRACE_CATCH(ftape_loop_until_writes_done(),); } TRACE(ft_t_noise, "calling ftape_abort_operation"); TRACE_CATCH(ftape_abort_operation(),); ftape_zap_write_buffers(); ftape_set_state(wanted_state); } /* if all buffers full we'll have to wait... */ ftape_wait_segment(wanted_state); tail = ftape_get_buffer(ft_queue_tail); switch(tail->status) { case done: ft_history.defects += count_ones(tail->hard_error_map); break; case waiting: /* this could happen with multiple EMPTY_SEGMENTs, but * shouldn't happen any more as we re-start the runner even * with an empty segment. */ bytes_written = -EAGAIN; break; case error: /* setup for a retry */ tail->status = waiting; bytes_written = -EAGAIN; /* force retry */ if (tail->hard_error_map != 0) { TRACE(ft_t_warn, "warning: %d hard error(s) in written segment", count_ones(tail->hard_error_map)); TRACE(ft_t_noise, "hard_error_map = 0x%08lx", (long)tail->hard_error_map); /* Implement hard write error recovery here */ } break; default: TRACE_ABORT(-EIO, ft_t_err, "wait for empty segment failed, tail status: %d", tail->status); } /* should runner stop ? */ if (ft_runner_status == aborting) { buffer_struct *head = ftape_get_buffer(ft_queue_head); if (head->status == wanted_state) { head->status = done; /* ???? */ } /* don't call abort_operation(), we don't want to zap * the dma buffers */ TRACE_CATCH(ftape_dumb_stop(),); } else { /* If just passed last segment on tape: wait for BOT * or EOT mark. Sets ft_runner_status to idle if at lEOT * and successful */ TRACE_CATCH(ftape_handle_logical_eot(),); } if (tail->status == done) { /* now at least one buffer is empty, fill it with our * data. skip bad sectors and generate ecc. * copy_and_gen_ecc return nr of bytes written, range * 0..29 Kb inclusive! * * Empty segments are handled inside coyp_and_gen_ecc() */ if (write_mode != FT_WR_DELETE) { TRACE_CATCH(bytes_written = copy_and_gen_ecc( tail->address, address, ftape_get_bad_sector_entry(segment_id)),); } tail->segment_id = segment_id; tail->status = waiting; tail = ftape_next_buffer(ft_queue_tail); } /* Start tape only if all buffers full or flush mode. * This will give higher probability of streaming. */ if (ft_runner_status != running && ((tail->status == waiting && ftape_get_buffer(ft_queue_head) == tail) || write_mode != FT_WR_ASYNC)) { TRACE_CATCH(ftape_start_writing(write_mode),); } TRACE_EXIT bytes_written; } /* Write as much as fits from buffer to the given segment on tape * and handle retries. * Return the number of bytes written (>= 0), or: * -EIO write failed * -EINTR interrupted by signal * -ENOSPC device full */ int ftape_write_segment(const int segment_id, const void *buffer, const ft_write_mode_t flush) { int retry = 0; int result; TRACE_FUN(ft_t_flow); ft_history.used |= 2; if (segment_id >= ft_tracks_per_tape*ft_segments_per_track) { /* tape full */ TRACE_ABORT(-ENOSPC, ft_t_err, "invalid segment id: %d (max %d)", segment_id, ft_tracks_per_tape * ft_segments_per_track -1); } for (;;) { if ((result = write_segment(segment_id, buffer, flush)) >= 0) { if (result == 0) { /* empty segment */ TRACE(ft_t_noise, "empty segment, nothing written"); } TRACE_EXIT result; } if (result == -EAGAIN) { if (++retry > 100) { /* give up */ TRACE_ABORT(-EIO, ft_t_err, "write failed, >100 retries in segment"); } TRACE(ft_t_warn, "write error, retry %d (%d)", retry, ftape_get_buffer(ft_queue_tail)->segment_id); } else { TRACE_ABORT(result, ft_t_err, "write_segment failed, error: %d", result); } /* Allow escape from loop when signaled ! */ FT_SIGNAL_EXIT(_DONT_BLOCK); } } |