zftape-rw.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (C) 1996, 1997 Claus-Justus Heine
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING. If not, write to
  13. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14. *
  15. * $Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-rw.c,v $
  16. * $Revision: 1.2 $
  17. * $Date: 1997/10/05 19:19:08 $
  18. *
  19. * This file contains some common code for the r/w code for
  20. * zftape.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/mm.h>
  24. #include <linux/zftape.h>
  25. #include "../zftape/zftape-init.h"
  26. #include "../zftape/zftape-eof.h"
  27. #include "../zftape/zftape-ctl.h"
  28. #include "../zftape/zftape-write.h"
  29. #include "../zftape/zftape-read.h"
  30. #include "../zftape/zftape-rw.h"
  31. #include "../zftape/zftape-vtbl.h"
  32. /* Global vars.
  33. */
  34. __u8 *zft_deblock_buf;
  35. __u8 *zft_hseg_buf;
  36. int zft_deblock_segment = -1;
  37. zft_status_enum zft_io_state = zft_idle;
  38. int zft_header_changed;
  39. int zft_qic113; /* conform to old specs. and old zftape */
  40. int zft_use_compression;
  41. zft_position zft_pos = {
  42. -1, /* seg_pos */
  43. 0, /* seg_byte_pos */
  44. 0, /* tape_pos */
  45. 0 /* volume_pos */
  46. };
  47. unsigned int zft_blk_sz = CONFIG_ZFT_DFLT_BLK_SZ;
  48. __s64 zft_capacity;
  49. unsigned int zft_written_segments;
  50. int zft_label_changed;
  51. /* Local vars.
  52. */
  53. unsigned int zft_get_seg_sz(unsigned int segment)
  54. {
  55. int size;
  56. TRACE_FUN(ft_t_any);
  57. size = FT_SEGMENT_SIZE -
  58. count_ones(ftape_get_bad_sector_entry(segment))*FT_SECTOR_SIZE;
  59. if (size > 0) {
  60. TRACE_EXIT (unsigned)size;
  61. } else {
  62. TRACE_EXIT 0;
  63. }
  64. }
  65. /* ftape_set_flags(). Claus-Justus Heine, 1994/1995
  66. */
  67. void zft_set_flags(unsigned minor_unit)
  68. {
  69. TRACE_FUN(ft_t_flow);
  70. zft_use_compression = zft_qic_mode = 0;
  71. switch (minor_unit & ZFT_MINOR_OP_MASK) {
  72. case (ZFT_Q80_MODE | ZFT_ZIP_MODE):
  73. case ZFT_ZIP_MODE:
  74. zft_use_compression = 1;
  75. case 0:
  76. case ZFT_Q80_MODE:
  77. zft_qic_mode = 1;
  78. if (zft_mt_compression) { /* override the default */
  79. zft_use_compression = 1;
  80. }
  81. break;
  82. case ZFT_RAW_MODE:
  83. TRACE(ft_t_noise, "switching to raw mode");
  84. break;
  85. default:
  86. TRACE(ft_t_warn, "Warning:\n"
  87. KERN_INFO "Wrong combination of minor device bits.\n"
  88. KERN_INFO "Switching to raw read-only mode.");
  89. zft_write_protected = 1;
  90. break;
  91. }
  92. TRACE_EXIT;
  93. }
  94. /* computes the segment and byte offset inside the segment
  95. * corresponding to tape_pos.
  96. *
  97. * tape_pos gives the offset in bytes from the beginning of the
  98. * ft_first_data_segment *seg_byte_pos is the offset in the current
  99. * segment in bytes
  100. *
  101. * Of, if this routine was called often one should cache the last data
  102. * pos it was called with, but actually this is only needed in
  103. * ftape_seek_block(), that is, almost never.
  104. */
  105. int zft_calc_seg_byte_coord(int *seg_byte_pos, __s64 tape_pos)
  106. {
  107. int segment;
  108. int seg_sz;
  109. TRACE_FUN(ft_t_flow);
  110. if (tape_pos == 0) {
  111. *seg_byte_pos = 0;
  112. segment = ft_first_data_segment;
  113. } else {
  114. seg_sz = 0;
  115. for (segment = ft_first_data_segment;
  116. ((tape_pos > 0) && (segment <= ft_last_data_segment));
  117. segment++) {
  118. seg_sz = zft_get_seg_sz(segment);
  119. tape_pos -= seg_sz;
  120. }
  121. if(tape_pos >= 0) {
  122. /* the case tape_pos > != 0 means that the
  123. * argument tape_pos lies beyond the EOT.
  124. */
  125. *seg_byte_pos= 0;
  126. } else { /* tape_pos < 0 */
  127. segment--;
  128. *seg_byte_pos= tape_pos + seg_sz;
  129. }
  130. }
  131. TRACE_EXIT(segment);
  132. }
  133. /* ftape_calc_tape_pos().
  134. *
  135. * computes the offset in bytes from the beginning of the
  136. * ft_first_data_segment inverse to ftape_calc_seg_byte_coord
  137. *
  138. * We should do some caching. But how:
  139. *
  140. * Each time the header segments are read in, this routine is called
  141. * with ft_tracks_per_tape*segments_per_track argumnet. So this should be
  142. * the time to reset the cache.
  143. *
  144. * Also, it might be in the future that the bad sector map gets
  145. * changed. -> reset the cache
  146. */
  147. static int seg_pos;
  148. static __s64 tape_pos;
  149. __s64 zft_get_capacity(void)
  150. {
  151. seg_pos = ft_first_data_segment;
  152. tape_pos = 0;
  153. while (seg_pos <= ft_last_data_segment) {
  154. tape_pos += zft_get_seg_sz(seg_pos ++);
  155. }
  156. return tape_pos;
  157. }
  158. __s64 zft_calc_tape_pos(int segment)
  159. {
  160. int d1, d2, d3;
  161. TRACE_FUN(ft_t_any);
  162. if (segment > ft_last_data_segment) {
  163. TRACE_EXIT zft_capacity;
  164. }
  165. if (segment < ft_first_data_segment) {
  166. TRACE_EXIT 0;
  167. }
  168. d2 = segment - seg_pos;
  169. if (-d2 > 10) {
  170. d1 = segment - ft_first_data_segment;
  171. if (-d2 > d1) {
  172. tape_pos = 0;
  173. seg_pos = ft_first_data_segment;
  174. d2 = d1;
  175. }
  176. }
  177. if (d2 > 10) {
  178. d3 = ft_last_data_segment - segment;
  179. if (d2 > d3) {
  180. tape_pos = zft_capacity;
  181. seg_pos = ft_last_data_segment + 1;
  182. d2 = -d3;
  183. }
  184. }
  185. if (d2 > 0) {
  186. while (seg_pos < segment) {
  187. tape_pos += zft_get_seg_sz(seg_pos++);
  188. }
  189. } else {
  190. while (seg_pos > segment) {
  191. tape_pos -= zft_get_seg_sz(--seg_pos);
  192. }
  193. }
  194. TRACE(ft_t_noise, "new cached pos: %d", seg_pos);
  195. TRACE_EXIT tape_pos;
  196. }
  197. /* copy Z-label string to buffer, keeps track of the correct offset in
  198. * `buffer'
  199. */
  200. void zft_update_label(__u8 *buffer)
  201. {
  202. TRACE_FUN(ft_t_flow);
  203. if (strncmp(&buffer[FT_LABEL], ZFTAPE_LABEL,
  204. sizeof(ZFTAPE_LABEL)-1) != 0) {
  205. TRACE(ft_t_info, "updating label from \"%s\" to \"%s\"",
  206. &buffer[FT_LABEL], ZFTAPE_LABEL);
  207. strcpy(&buffer[FT_LABEL], ZFTAPE_LABEL);
  208. memset(&buffer[FT_LABEL] + sizeof(ZFTAPE_LABEL) - 1, ' ',
  209. FT_LABEL_SZ - sizeof(ZFTAPE_LABEL + 1));
  210. PUT4(buffer, FT_LABEL_DATE, 0);
  211. zft_label_changed = zft_header_changed = 1; /* changed */
  212. }
  213. TRACE_EXIT;
  214. }
  215. int zft_verify_write_segments(unsigned int segment,
  216. __u8 *data, size_t size,
  217. __u8 *buffer)
  218. {
  219. int result;
  220. __u8 *write_buf;
  221. __u8 *src_buf;
  222. int single;
  223. int seg_pos;
  224. int seg_sz;
  225. int remaining;
  226. ft_write_mode_t write_mode;
  227. TRACE_FUN(ft_t_flow);
  228. seg_pos = segment;
  229. seg_sz = zft_get_seg_sz(seg_pos);
  230. src_buf = data;
  231. single = size <= seg_sz;
  232. remaining = size;
  233. do {
  234. TRACE(ft_t_noise, "\n"
  235. KERN_INFO "remaining: %d\n"
  236. KERN_INFO "seg_sz : %d\n"
  237. KERN_INFO "segment : %d",
  238. remaining, seg_sz, seg_pos);
  239. if (remaining == seg_sz) {
  240. write_buf = src_buf;
  241. write_mode = single ? FT_WR_SINGLE : FT_WR_MULTI;
  242. remaining = 0;
  243. } else if (remaining > seg_sz) {
  244. write_buf = src_buf;
  245. write_mode = FT_WR_ASYNC; /* don't start tape */
  246. remaining -= seg_sz;
  247. } else { /* remaining < seg_sz */
  248. write_buf = buffer;
  249. memcpy(write_buf, src_buf, remaining);
  250. memset(&write_buf[remaining],'\0',seg_sz-remaining);
  251. write_mode = single ? FT_WR_SINGLE : FT_WR_MULTI;
  252. remaining = 0;
  253. }
  254. if ((result = ftape_write_segment(seg_pos,
  255. write_buf,
  256. write_mode)) != seg_sz) {
  257. TRACE(ft_t_err, "Error: "
  258. "Couldn't write segment %d", seg_pos);
  259. TRACE_EXIT result < 0 ? result : -EIO; /* bail out */
  260. }
  261. zft_written_segments ++;
  262. seg_sz = zft_get_seg_sz(++seg_pos);
  263. src_buf += result;
  264. } while (remaining > 0);
  265. if (ftape_get_status()->fti_state == writing) {
  266. TRACE_CATCH(ftape_loop_until_writes_done(),);
  267. TRACE_CATCH(ftape_abort_operation(),);
  268. zft_prevent_flush();
  269. }
  270. seg_pos = segment;
  271. src_buf = data;
  272. remaining = size;
  273. do {
  274. TRACE_CATCH(result = ftape_read_segment(seg_pos, buffer,
  275. single ? FT_RD_SINGLE
  276. : FT_RD_AHEAD),);
  277. if (memcmp(src_buf, buffer,
  278. remaining > result ? result : remaining) != 0) {
  279. TRACE_ABORT(-EIO, ft_t_err,
  280. "Failed to verify written segment %d",
  281. seg_pos);
  282. }
  283. remaining -= result;
  284. TRACE(ft_t_noise, "verify successful:\n"
  285. KERN_INFO "segment : %d\n"
  286. KERN_INFO "segsize : %d\n"
  287. KERN_INFO "remaining: %d",
  288. seg_pos, result, remaining);
  289. src_buf += seg_sz;
  290. seg_pos++;
  291. } while (remaining > 0);
  292. TRACE_EXIT size;
  293. }
  294. /* zft_erase(). implemented compression-handling
  295. *
  296. * calculate the first data-segment when using/not using compression.
  297. *
  298. * update header-segment and compression-map-segment.
  299. */
  300. int zft_erase(void)
  301. {
  302. int result = 0;
  303. TRACE_FUN(ft_t_flow);
  304. if (!zft_header_read) {
  305. TRACE_CATCH(zft_vmalloc_once((void **)&zft_hseg_buf,
  306. FT_SEGMENT_SIZE),);
  307. /* no need to read the vtbl and compression map */
  308. TRACE_CATCH(ftape_read_header_segment(zft_hseg_buf),);
  309. if ((zft_old_ftape =
  310. zft_ftape_validate_label(&zft_hseg_buf[FT_LABEL]))) {
  311. zft_ftape_extract_file_marks(zft_hseg_buf);
  312. }
  313. TRACE(ft_t_noise,
  314. "ft_first_data_segment: %d, ft_last_data_segment: %d",
  315. ft_first_data_segment, ft_last_data_segment);
  316. zft_qic113 = (ft_format_code != fmt_normal &&
  317. ft_format_code != fmt_1100ft &&
  318. ft_format_code != fmt_425ft);
  319. }
  320. if (zft_old_ftape) {
  321. zft_clear_ftape_file_marks();
  322. zft_old_ftape = 0; /* no longer old ftape */
  323. }
  324. PUT2(zft_hseg_buf, FT_CMAP_START, 0);
  325. zft_volume_table_changed = 1;
  326. zft_capacity = zft_get_capacity();
  327. zft_init_vtbl();
  328. /* the rest must be done in ftape_update_header_segments
  329. */
  330. zft_header_read = 1;
  331. zft_header_changed = 1; /* force update of timestamp */
  332. result = zft_update_header_segments();
  333. ftape_abort_operation();
  334. zft_reset_position(&zft_pos);
  335. zft_set_flags (zft_unit);
  336. TRACE_EXIT result;
  337. }
  338. unsigned int zft_get_time(void)
  339. {
  340. unsigned int date = FT_TIME_STAMP(2097, 11, 30, 23, 59, 59); /* fun */
  341. return date;
  342. }