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