zftape-read.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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-read.c,v $
  16. * $Revision: 1.2 $
  17. * $Date: 1997/10/05 19:19:06 $
  18. *
  19. * This file contains the high level reading code
  20. * for the QIC-117 floppy-tape driver for Linux.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/mm.h>
  24. #include <linux/zftape.h>
  25. #include <asm/uaccess.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. int zft_just_before_eof;
  36. /* Local vars.
  37. */
  38. static int buf_len_rd;
  39. void zft_zap_read_buffers(void)
  40. {
  41. buf_len_rd = 0;
  42. }
  43. int zft_read_header_segments(void)
  44. {
  45. TRACE_FUN(ft_t_flow);
  46. zft_header_read = 0;
  47. TRACE_CATCH(zft_vmalloc_once(&zft_hseg_buf, FT_SEGMENT_SIZE),);
  48. TRACE_CATCH(ftape_read_header_segment(zft_hseg_buf),);
  49. TRACE(ft_t_info, "Segments written since first format: %d",
  50. (int)GET4(zft_hseg_buf, FT_SEG_CNT));
  51. zft_qic113 = (ft_format_code != fmt_normal &&
  52. ft_format_code != fmt_1100ft &&
  53. ft_format_code != fmt_425ft);
  54. TRACE(ft_t_info, "ft_first_data_segment: %d, ft_last_data_segment: %d",
  55. ft_first_data_segment, ft_last_data_segment);
  56. zft_capacity = zft_get_capacity();
  57. zft_old_ftape = zft_ftape_validate_label(&zft_hseg_buf[FT_LABEL]);
  58. if (zft_old_ftape) {
  59. TRACE(ft_t_info,
  60. "Found old ftaped tape, emulating eof marks, entering read-only mode");
  61. zft_ftape_extract_file_marks(zft_hseg_buf);
  62. TRACE_CATCH(zft_fake_volume_headers(zft_eof_map,
  63. zft_nr_eof_marks),);
  64. } else {
  65. /* the specs say that the volume table must be
  66. * initialized with zeroes during formatting, so it
  67. * MUST be readable, i.e. contain vaid ECC
  68. * information.
  69. */
  70. TRACE_CATCH(ftape_read_segment(ft_first_data_segment,
  71. zft_deblock_buf,
  72. FT_RD_SINGLE),);
  73. TRACE_CATCH(zft_extract_volume_headers(zft_deblock_buf),);
  74. }
  75. zft_header_read = 1;
  76. zft_set_flags(zft_unit);
  77. zft_reset_position(&zft_pos);
  78. TRACE_EXIT 0;
  79. }
  80. int zft_fetch_segment_fraction(const unsigned int segment, void *buffer,
  81. const ft_read_mode_t read_mode,
  82. const unsigned int start,
  83. const unsigned int size)
  84. {
  85. int seg_sz;
  86. TRACE_FUN(ft_t_flow);
  87. if (segment == zft_deblock_segment) {
  88. TRACE(ft_t_data_flow,
  89. "re-using segment %d already in deblock buffer",
  90. segment);
  91. seg_sz = zft_get_seg_sz(segment);
  92. if (start > seg_sz) {
  93. TRACE_ABORT(-EINVAL, ft_t_bug,
  94. "trying to read beyond end of segment:\n"
  95. KERN_INFO "seg_sz : %d\n"
  96. KERN_INFO "start : %d\n"
  97. KERN_INFO "segment: %d",
  98. seg_sz, start, segment);
  99. }
  100. if ((start + size) > seg_sz) {
  101. TRACE_EXIT seg_sz - start;
  102. }
  103. TRACE_EXIT size;
  104. }
  105. seg_sz = ftape_read_segment_fraction(segment, buffer, read_mode,
  106. start, size);
  107. TRACE(ft_t_data_flow, "segment %d, result %d", segment, seg_sz);
  108. if ((int)seg_sz >= 0 && start == 0 && size == FT_SEGMENT_SIZE) {
  109. /* this implicitly assumes that we are always called with
  110. * buffer == zft_deblock_buf
  111. */
  112. zft_deblock_segment = segment;
  113. } else {
  114. zft_deblock_segment = -1;
  115. }
  116. TRACE_EXIT seg_sz;
  117. }
  118. /*
  119. * out:
  120. *
  121. * int *read_cnt: the number of bytes we removed from the
  122. * zft_deblock_buf (result)
  123. *
  124. * int *to_do : the remaining size of the read-request. Is changed.
  125. *
  126. * in:
  127. *
  128. * char *buff : buff is the address of the upper part of the user
  129. * buffer, that hasn't been filled with data yet.
  130. * int buf_pos_read: copy of buf_pos_rd
  131. * int buf_len_read: copy of buf_len_rd
  132. * char *zft_deblock_buf: ftape_zft_deblock_buf
  133. *
  134. * returns the amount of data actually copied to the user-buffer
  135. *
  136. * to_do MUST NOT SHRINK except to indicate an EOT. In this case to_do
  137. * has to be set to 0. We cannot return -ENOSPC, because we return the
  138. * amount of data actually * copied to the user-buffer
  139. */
  140. static int zft_simple_read (int *read_cnt,
  141. __u8 __user *dst_buf,
  142. const int to_do,
  143. const __u8 *src_buf,
  144. const int seg_sz,
  145. const zft_position *pos,
  146. const zft_volinfo *volume)
  147. {
  148. TRACE_FUN(ft_t_flow);
  149. if (seg_sz - pos->seg_byte_pos < to_do) {
  150. *read_cnt = seg_sz - pos->seg_byte_pos;
  151. } else {
  152. *read_cnt = to_do;
  153. }
  154. if (copy_to_user(dst_buf,
  155. src_buf + pos->seg_byte_pos, *read_cnt) != 0) {
  156. TRACE_EXIT -EFAULT;
  157. }
  158. TRACE(ft_t_noise, "nr bytes just read: %d", *read_cnt);
  159. TRACE_EXIT *read_cnt;
  160. }
  161. /* req_len: gets clipped due to EOT of EOF.
  162. * req_clipped: is a flag indicating whether req_len was clipped or not
  163. * volume: contains information on current volume (blk_sz etc.)
  164. */
  165. static int check_read_access(int *req_len,
  166. const zft_volinfo **volume,
  167. int *req_clipped,
  168. const zft_position *pos)
  169. {
  170. static __s64 remaining;
  171. static int eod;
  172. TRACE_FUN(ft_t_flow);
  173. if (zft_io_state != zft_reading) {
  174. if (zft_offline) { /* offline includes no_tape */
  175. TRACE_ABORT(-ENXIO, ft_t_warn,
  176. "tape is offline or no cartridge");
  177. }
  178. if (!ft_formatted) {
  179. TRACE_ABORT(-EACCES,
  180. ft_t_warn, "tape is not formatted");
  181. }
  182. /* now enter defined state, read header segment if not
  183. * already done and flush write buffers
  184. */
  185. TRACE_CATCH(zft_def_idle_state(),);
  186. zft_io_state = zft_reading;
  187. if (zft_tape_at_eod(pos)) {
  188. eod = 1;
  189. TRACE_EXIT 1;
  190. }
  191. eod = 0;
  192. *volume = zft_find_volume(pos->seg_pos);
  193. /* get the space left until EOF */
  194. remaining = zft_check_for_eof(*volume, pos);
  195. buf_len_rd = 0;
  196. TRACE(ft_t_noise, "remaining: " LL_X ", vol_no: %d",
  197. LL(remaining), (*volume)->count);
  198. } else if (zft_tape_at_eod(pos)) {
  199. if (++eod > 2) {
  200. TRACE_EXIT -EIO; /* st.c also returns -EIO */
  201. } else {
  202. TRACE_EXIT 1;
  203. }
  204. }
  205. if ((*req_len % (*volume)->blk_sz) != 0) {
  206. /* this message is informational only. The user gets the
  207. * proper return value
  208. */
  209. TRACE_ABORT(-EINVAL, ft_t_info,
  210. "req_len %d not a multiple of block size %d",
  211. *req_len, (*volume)->blk_sz);
  212. }
  213. /* As GNU tar doesn't accept partial read counts when the
  214. * multiple volume flag is set, we make sure to return the
  215. * requested amount of data. Except, of course, at the end of
  216. * the tape or file mark.
  217. */
  218. remaining -= *req_len;
  219. if (remaining <= 0) {
  220. TRACE(ft_t_noise,
  221. "clipped request from %d to %d.",
  222. *req_len, (int)(*req_len + remaining));
  223. *req_len += remaining;
  224. *req_clipped = 1;
  225. } else {
  226. *req_clipped = 0;
  227. }
  228. TRACE_EXIT 0;
  229. }
  230. /* this_segs_size: the current segment's size.
  231. * buff: the USER-SPACE buffer provided by the calling function.
  232. * req_len: how much data should be read at most.
  233. * volume: contains information on current volume (blk_sz etc.)
  234. */
  235. static int empty_deblock_buf(__u8 __user *usr_buf, const int req_len,
  236. const __u8 *src_buf, const int seg_sz,
  237. zft_position *pos,
  238. const zft_volinfo *volume)
  239. {
  240. int cnt;
  241. int result = 0;
  242. TRACE_FUN(ft_t_flow);
  243. TRACE(ft_t_data_flow, "this_segs_size: %d", seg_sz);
  244. if (zft_use_compression && volume->use_compression) {
  245. TRACE_CATCH(zft_cmpr_lock(1 /* try to load */),);
  246. TRACE_CATCH(result= (*zft_cmpr_ops->read)(&cnt,
  247. usr_buf, req_len,
  248. src_buf, seg_sz,
  249. pos, volume),);
  250. } else {
  251. TRACE_CATCH(result= zft_simple_read (&cnt,
  252. usr_buf, req_len,
  253. src_buf, seg_sz,
  254. pos, volume),);
  255. }
  256. pos->volume_pos += result;
  257. pos->tape_pos += cnt;
  258. pos->seg_byte_pos += cnt;
  259. buf_len_rd -= cnt; /* remaining bytes in buffer */
  260. TRACE(ft_t_data_flow, "buf_len_rd: %d, cnt: %d", buf_len_rd, cnt);
  261. if(pos->seg_byte_pos >= seg_sz) {
  262. pos->seg_pos++;
  263. pos->seg_byte_pos = 0;
  264. }
  265. TRACE(ft_t_data_flow, "bytes moved out of deblock-buffer: %d", cnt);
  266. TRACE_EXIT result;
  267. }
  268. /* note: we store the segment id of the segment that is inside the
  269. * deblock buffer. This spares a lot of ftape_read_segment()s when we
  270. * use small block-sizes. The block-size may be 1kb (SECTOR_SIZE). In
  271. * this case a MTFSR 28 maybe still inside the same segment.
  272. */
  273. int _zft_read(char __user *buff, int req_len)
  274. {
  275. int req_clipped;
  276. int result = 0;
  277. int bytes_read = 0;
  278. static unsigned int seg_sz = 0;
  279. static const zft_volinfo *volume = NULL;
  280. TRACE_FUN(ft_t_flow);
  281. zft_resid = req_len;
  282. result = check_read_access(&req_len, &volume,
  283. &req_clipped, &zft_pos);
  284. switch(result) {
  285. case 0:
  286. break; /* nothing special */
  287. case 1:
  288. TRACE(ft_t_noise, "EOD reached");
  289. TRACE_EXIT 0; /* EOD */
  290. default:
  291. TRACE_ABORT(result, ft_t_noise,
  292. "check_read_access() failed with result %d",
  293. result);
  294. TRACE_EXIT result;
  295. }
  296. while (req_len > 0) {
  297. /* Allow escape from this loop on signal !
  298. */
  299. FT_SIGNAL_EXIT(_DONT_BLOCK);
  300. /* buf_len_rd == 0 means that we need to read a new
  301. * segment.
  302. */
  303. if (buf_len_rd == 0) {
  304. while((result = zft_fetch_segment(zft_pos.seg_pos,
  305. zft_deblock_buf,
  306. FT_RD_AHEAD)) == 0) {
  307. zft_pos.seg_pos ++;
  308. zft_pos.seg_byte_pos = 0;
  309. }
  310. if (result < 0) {
  311. zft_resid -= bytes_read;
  312. TRACE_ABORT(result, ft_t_noise,
  313. "zft_fetch_segment(): %d",
  314. result);
  315. }
  316. seg_sz = result;
  317. buf_len_rd = seg_sz - zft_pos.seg_byte_pos;
  318. }
  319. TRACE_CATCH(result = empty_deblock_buf(buff,
  320. req_len,
  321. zft_deblock_buf,
  322. seg_sz,
  323. &zft_pos,
  324. volume),
  325. zft_resid -= bytes_read);
  326. TRACE(ft_t_data_flow, "bytes just read: %d", result);
  327. bytes_read += result; /* what we got so far */
  328. buff += result; /* index in user-buffer */
  329. req_len -= result; /* what's left from req_len */
  330. } /* while (req_len > 0) */
  331. if (req_clipped) {
  332. TRACE(ft_t_data_flow,
  333. "maybe partial count because of eof mark");
  334. if (zft_just_before_eof && bytes_read == 0) {
  335. /* req_len was > 0, but user didn't get
  336. * anything the user has read in the eof-mark
  337. */
  338. zft_move_past_eof(&zft_pos);
  339. ftape_abort_operation();
  340. } else {
  341. /* don't skip to the next file before the user
  342. * tried to read a second time past EOF Just
  343. * mark that we are at EOF and maybe decrement
  344. * zft_seg_pos to stay in the same volume;
  345. */
  346. zft_just_before_eof = 1;
  347. zft_position_before_eof(&zft_pos, volume);
  348. TRACE(ft_t_noise, "just before eof");
  349. }
  350. }
  351. zft_resid -= result; /* for MTSTATUS */
  352. TRACE_EXIT bytes_read;
  353. }