ftape-write.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *      Copyright (C) 1993-1995 Bas Laarhoven,
  3.  *                (C) 1996-1997 Claus-Justus Heine.
  4.  This program is free software; you can redistribute it and/or modify
  5.  it under the terms of the GNU General Public License as published by
  6.  the Free Software Foundation; either version 2, or (at your option)
  7.  any later version.
  8.  This program is distributed in the hope that it will be useful,
  9.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  GNU General Public License for more details.
  12.  You should have received a copy of the GNU General Public License
  13.  along with this program; see the file COPYING.  If not, write to
  14.  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  *
  16.  * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-write.c,v $
  17.  * $Revision: 1.3.4.1 $
  18.  * $Date: 1997/11/14 18:07:04 $
  19.  *
  20.  *      This file contains the writing code
  21.  *      for the QIC-117 floppy-tape driver for Linux.
  22.  */
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include <linux/mm.h>
  26. #include <asm/segment.h>
  27. #include <linux/ftape.h>
  28. #include <linux/qic117.h>
  29. #include "../lowlevel/ftape-tracing.h"
  30. #include "../lowlevel/ftape-write.h"
  31. #include "../lowlevel/ftape-read.h"
  32. #include "../lowlevel/ftape-io.h"
  33. #include "../lowlevel/ftape-ctl.h"
  34. #include "../lowlevel/ftape-rw.h"
  35. #include "../lowlevel/ftape-ecc.h"
  36. #include "../lowlevel/ftape-bsm.h"
  37. #include "../lowlevel/fdc-isr.h"
  38. /*      Global vars.
  39.  */
  40. /*      Local vars.
  41.  */
  42. static int last_write_failed;
  43. void ftape_zap_write_buffers(void)
  44. {
  45. int i;
  46. for (i = 0; i < ft_nr_buffers; ++i) {
  47. ft_buffer[i]->status = done;
  48. }
  49. ftape_reset_buffer();
  50. }
  51. static int copy_and_gen_ecc(void *destination, 
  52.     const void *source,
  53.     const SectorMap bad_sector_map)
  54. {
  55. int result;
  56. struct memory_segment mseg;
  57. int bads = count_ones(bad_sector_map);
  58. TRACE_FUN(ft_t_any);
  59. if (bads > 0) {
  60. TRACE(ft_t_noise, "bad sectors in map: %d", bads);
  61. }
  62. if (bads + 3 >= FT_SECTORS_PER_SEGMENT) {
  63. TRACE(ft_t_noise, "empty segment");
  64. mseg.blocks = 0; /* skip entire segment */
  65. result = 0;      /* nothing written */
  66. } else {
  67. mseg.blocks = FT_SECTORS_PER_SEGMENT - bads;
  68. mseg.data = destination;
  69. memcpy(mseg.data, source, (mseg.blocks - 3) * FT_SECTOR_SIZE);
  70. result = ftape_ecc_set_segment_parity(&mseg);
  71. if (result < 0) {
  72. TRACE(ft_t_err, "ecc_set_segment_parity failed");
  73. } else {
  74. result = (mseg.blocks - 3) * FT_SECTOR_SIZE;
  75. }
  76. }
  77. TRACE_EXIT result;
  78. }
  79. int ftape_start_writing(const ft_write_mode_t mode)
  80. {
  81. buffer_struct *head = ftape_get_buffer(ft_queue_head);
  82. int segment_id = head->segment_id;
  83. int result;
  84. buffer_state_enum wanted_state = (mode == FT_WR_DELETE
  85.   ? deleting
  86.   : writing);
  87. TRACE_FUN(ft_t_flow);
  88. if ((ft_driver_state != wanted_state) || head->status != waiting) {
  89. TRACE_EXIT 0;
  90. }
  91. ftape_setup_new_segment(head, segment_id, 1);
  92. if (mode == FT_WR_SINGLE) {
  93. /* stop tape instead of pause */
  94. head->next_segment = 0;
  95. }
  96. ftape_calc_next_cluster(head); /* prepare */
  97. head->status = ft_driver_state; /* either writing or deleting */
  98. if (ft_runner_status == idle) {
  99. TRACE(ft_t_noise,
  100.       "starting runner for segment %d", segment_id);
  101. TRACE_CATCH(ftape_start_tape(segment_id,head->sector_offset),);
  102. } else {
  103. TRACE(ft_t_noise, "runner not idle, not starting tape");
  104. }
  105. /* go */
  106. result = fdc_setup_read_write(head, (mode == FT_WR_DELETE
  107.      ? FDC_WRITE_DELETED : FDC_WRITE));
  108. ftape_set_state(wanted_state); /* should not be necessary */
  109. TRACE_EXIT result;
  110. }
  111. /*  Wait until all data is actually written to tape.
  112.  *  
  113.  *  There is a problem: when the tape runs into logical EOT, then this
  114.  *  failes. We need to restart the runner in this case.
  115.  */
  116. int ftape_loop_until_writes_done(void)
  117. {
  118. buffer_struct *head;
  119. TRACE_FUN(ft_t_flow);
  120. while ((ft_driver_state == writing || ft_driver_state == deleting) && 
  121.        ftape_get_buffer(ft_queue_head)->status != done) {
  122. /* set the runner status to idle if at lEOT */
  123. TRACE_CATCH(ftape_handle_logical_eot(), last_write_failed = 1);
  124. /* restart the tape if necessary */
  125. if (ft_runner_status == idle) {
  126. TRACE(ft_t_noise, "runner is idle, restarting");
  127. if (ft_driver_state == deleting) {
  128. TRACE_CATCH(ftape_start_writing(FT_WR_DELETE),
  129.     last_write_failed = 1);
  130. } else {
  131. TRACE_CATCH(ftape_start_writing(FT_WR_MULTI),
  132.     last_write_failed = 1);
  133. }
  134. }
  135. TRACE(ft_t_noise, "tail: %d, head: %d", 
  136.       ftape_buffer_id(ft_queue_tail),
  137.       ftape_buffer_id(ft_queue_head));
  138. TRACE_CATCH(fdc_interrupt_wait(5 * FT_SECOND),
  139.     last_write_failed = 1);
  140. head = ftape_get_buffer(ft_queue_head);
  141. if (head->status == error) {
  142. /* Allow escape from loop when signaled !
  143.  */
  144. FT_SIGNAL_EXIT(_DONT_BLOCK);
  145. if (head->hard_error_map != 0) {
  146. /*  Implement hard write error recovery here
  147.  */
  148. }
  149. /* retry this one */
  150. head->status = waiting;
  151. if (ft_runner_status == aborting) {
  152. ftape_dumb_stop();
  153. }
  154. if (ft_runner_status != idle) {
  155. TRACE_ABORT(-EIO, ft_t_err,
  156.     "unexpected state: "
  157.     "ft_runner_status != idle");
  158. }
  159. ftape_start_writing(ft_driver_state == deleting
  160.     ? FT_WR_MULTI : FT_WR_DELETE);
  161. }
  162. TRACE(ft_t_noise, "looping until writes done");
  163. }
  164. ftape_set_state(idle);
  165. TRACE_EXIT 0;
  166. }
  167. /*      Write given segment from buffer at address to tape.
  168.  */
  169. static int write_segment(const int segment_id,
  170.  const void *address, 
  171.  const ft_write_mode_t write_mode)
  172. {
  173. int bytes_written = 0;
  174. buffer_struct *tail;
  175. buffer_state_enum wanted_state = (write_mode == FT_WR_DELETE
  176.   ? deleting : writing);
  177. TRACE_FUN(ft_t_flow);
  178. TRACE(ft_t_noise, "segment_id = %d", segment_id);
  179. if (ft_driver_state != wanted_state) {
  180. if (ft_driver_state == deleting ||
  181.     wanted_state == deleting) {
  182. TRACE_CATCH(ftape_loop_until_writes_done(),);
  183. }
  184. TRACE(ft_t_noise, "calling ftape_abort_operation");
  185. TRACE_CATCH(ftape_abort_operation(),);
  186. ftape_zap_write_buffers();
  187. ftape_set_state(wanted_state);
  188. }
  189. /*    if all buffers full we'll have to wait...
  190.  */
  191. ftape_wait_segment(wanted_state);
  192. tail = ftape_get_buffer(ft_queue_tail);
  193. switch(tail->status) {
  194. case done:
  195. ft_history.defects += count_ones(tail->hard_error_map);
  196. break;
  197. case waiting:
  198. /* this could happen with multiple EMPTY_SEGMENTs, but
  199.  * shouldn't happen any more as we re-start the runner even
  200.  * with an empty segment.
  201.  */
  202. bytes_written = -EAGAIN;
  203. break;
  204. case error:
  205. /*  setup for a retry
  206.  */
  207. tail->status = waiting;
  208. bytes_written = -EAGAIN; /* force retry */
  209. if (tail->hard_error_map != 0) {
  210. TRACE(ft_t_warn, 
  211.       "warning: %d hard error(s) in written segment",
  212.       count_ones(tail->hard_error_map));
  213. TRACE(ft_t_noise, "hard_error_map = 0x%08lx", 
  214.       (long)tail->hard_error_map);
  215. /*  Implement hard write error recovery here
  216.  */
  217. }
  218. break;
  219. default:
  220. TRACE_ABORT(-EIO, ft_t_err,
  221.     "wait for empty segment failed, tail status: %d",
  222.     tail->status);
  223. }
  224. /*    should runner stop ?
  225.  */
  226. if (ft_runner_status == aborting) {
  227. buffer_struct *head = ftape_get_buffer(ft_queue_head);
  228. if (head->status == wanted_state) {
  229. head->status = done; /* ???? */
  230. }
  231. /*  don't call abort_operation(), we don't want to zap
  232.  *  the dma buffers
  233.  */
  234. TRACE_CATCH(ftape_dumb_stop(),);
  235. } else {
  236. /*  If just passed last segment on tape: wait for BOT
  237.  *  or EOT mark. Sets ft_runner_status to idle if at lEOT
  238.  *  and successful 
  239.  */
  240. TRACE_CATCH(ftape_handle_logical_eot(),);
  241. }
  242. if (tail->status == done) {
  243. /* now at least one buffer is empty, fill it with our
  244.  * data.  skip bad sectors and generate ecc.
  245.  * copy_and_gen_ecc return nr of bytes written, range
  246.  * 0..29 Kb inclusive!  
  247.  *
  248.  * Empty segments are handled inside coyp_and_gen_ecc()
  249.  */
  250. if (write_mode != FT_WR_DELETE) {
  251. TRACE_CATCH(bytes_written = copy_and_gen_ecc(
  252. tail->address, address,
  253. ftape_get_bad_sector_entry(segment_id)),);
  254. }
  255. tail->segment_id = segment_id;
  256. tail->status = waiting;
  257. tail = ftape_next_buffer(ft_queue_tail);
  258. }
  259. /*  Start tape only if all buffers full or flush mode.
  260.  *  This will give higher probability of streaming.
  261.  */
  262. if (ft_runner_status != running && 
  263.     ((tail->status == waiting &&
  264.       ftape_get_buffer(ft_queue_head) == tail) ||
  265.      write_mode != FT_WR_ASYNC)) {
  266. TRACE_CATCH(ftape_start_writing(write_mode),);
  267. }
  268. TRACE_EXIT bytes_written;
  269. }
  270. /*  Write as much as fits from buffer to the given segment on tape
  271.  *  and handle retries.
  272.  *  Return the number of bytes written (>= 0), or:
  273.  *      -EIO          write failed
  274.  *      -EINTR        interrupted by signal
  275.  *      -ENOSPC       device full
  276.  */
  277. int ftape_write_segment(const int segment_id,
  278. const void *buffer, 
  279. const ft_write_mode_t flush)
  280. {
  281. int retry = 0;
  282. int result;
  283. TRACE_FUN(ft_t_flow);
  284. ft_history.used |= 2;
  285. if (segment_id >= ft_tracks_per_tape*ft_segments_per_track) {
  286. /* tape full */
  287. TRACE_ABORT(-ENOSPC, ft_t_err,
  288.     "invalid segment id: %d (max %d)", 
  289.     segment_id, 
  290.     ft_tracks_per_tape * ft_segments_per_track -1);
  291. }
  292. for (;;) {
  293. if ((result = write_segment(segment_id, buffer, flush)) >= 0) {
  294. if (result == 0) { /* empty segment */
  295. TRACE(ft_t_noise,
  296.       "empty segment, nothing written");
  297. }
  298. TRACE_EXIT result;
  299. }
  300. if (result == -EAGAIN) {
  301. if (++retry > 100) { /* give up */
  302. TRACE_ABORT(-EIO, ft_t_err,
  303.       "write failed, >100 retries in segment");
  304. }
  305. TRACE(ft_t_warn, "write error, retry %d (%d)",
  306.       retry,
  307.       ftape_get_buffer(ft_queue_tail)->segment_id);
  308. } else {
  309. TRACE_ABORT(result, ft_t_err,
  310.     "write_segment failed, error: %d", result);
  311. }
  312. /* Allow escape from loop when signaled !
  313.  */
  314. FT_SIGNAL_EXIT(_DONT_BLOCK);
  315. }
  316. }