zftape-rw.c 9.6 KB

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