1 /*
2  *      Copyright (c) 1995-1997 Claus-Justus Heine
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License as
6  published by the Free Software Foundation; either version 2, or (at
7  your option) any later version.
8 
9  This program is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  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,
17  USA.
18 
19  *
20  * $Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-vtbl.c,v $
21  * $Revision: 1.7.6.1 $
22  * $Date: 1997/11/24 13:48:31 $
23  *
24  *      This file defines a volume table as defined in various QIC
25  *      standards.
26  *
27  *      This is a minimal implementation, just allowing ordinary DOS
28  *      :( prgrams to identify the cartridge as used.
29  */
30 
31 #include <linux/errno.h>
32 #include <linux/mm.h>
33 #include <linux/slab.h>
34 #include <asm/segment.h>
35 
36 #include <linux/zftape.h>
37 #include "../zftape/zftape-init.h"
38 #include "../zftape/zftape-eof.h"
39 #include "../zftape/zftape-ctl.h"
40 #include "../zftape/zftape-write.h"
41 #include "../zftape/zftape-read.h"
42 #include "../zftape/zftape-rw.h"
43 #include "../zftape/zftape-vtbl.h"
44 
45 #define ZFT_CMAP_HACK /* leave this defined to hide the compression map */
46 
47 /*
48  *  global variables
49  */
50 int zft_qic_mode   = 1; /* use the vtbl */
51 int zft_old_ftape; /* prevents old ftaped tapes to be overwritten */
52 int zft_volume_table_changed; /* for write_header_segments() */
53 
54 /*
55  *  private variables (only exported for inline functions)
56  */
57 LIST_HEAD(zft_vtbl);
58 
59 /*  We could also allocate these dynamically when extracting the volume table
60  *  sizeof(zft_volinfo) is about 32 or something close to that
61  */
62 static zft_volinfo  tape_vtbl;
63 static zft_volinfo  eot_vtbl;
64 static zft_volinfo *cur_vtbl;
65 
zft_new_vtbl_entry(void)66 inline void zft_new_vtbl_entry(void)
67 {
68 	struct list_head *tmp = &zft_last_vtbl->node;
69 	zft_volinfo *new = zft_kmalloc(sizeof(zft_volinfo));
70 
71 	list_add(&new->node, tmp);
72 	new->count = zft_eom_vtbl->count ++;
73 }
74 
zft_free_vtbl(void)75 void zft_free_vtbl(void)
76 {
77 	for (;;) {
78 		struct list_head *tmp = zft_vtbl.prev;
79 		zft_volinfo *vtbl;
80 
81 		if (tmp == &zft_vtbl)
82 			break;
83 		list_del(tmp);
84 		vtbl = list_entry(tmp, zft_volinfo, node);
85 		zft_kfree(vtbl, sizeof(zft_volinfo));
86 	}
87 	INIT_LIST_HEAD(&zft_vtbl);
88 	cur_vtbl = NULL;
89 }
90 
91 /*  initialize vtbl, called by ftape_new_cartridge()
92  */
zft_init_vtbl(void)93 void zft_init_vtbl(void)
94 {
95 	zft_volinfo *new;
96 
97 	zft_free_vtbl();
98 
99 	/*  Create the two dummy vtbl entries
100 	 */
101 	new = zft_kmalloc(sizeof(zft_volinfo));
102 	list_add(&new->node, &zft_vtbl);
103 	new = zft_kmalloc(sizeof(zft_volinfo));
104 	list_add(&new->node, &zft_vtbl);
105 	zft_head_vtbl->end_seg   = ft_first_data_segment;
106 	zft_head_vtbl->blk_sz    = zft_blk_sz;
107 	zft_head_vtbl->count     = -1;
108 	zft_eom_vtbl->start_seg  = ft_first_data_segment + 1;
109 	zft_eom_vtbl->end_seg    = ft_last_data_segment + 1;
110 	zft_eom_vtbl->blk_sz     = zft_blk_sz;
111 	zft_eom_vtbl->count      = 0;
112 
113 	/*  Reset the pointer for zft_find_volume()
114 	 */
115 	cur_vtbl = zft_eom_vtbl;
116 
117 	/* initialize the dummy vtbl entries for zft_qic_mode == 0
118 	 */
119 	eot_vtbl.start_seg       = ft_last_data_segment + 1;
120 	eot_vtbl.end_seg         = ft_last_data_segment + 1;
121 	eot_vtbl.blk_sz          = zft_blk_sz;
122 	eot_vtbl.count           = -1;
123 	tape_vtbl.start_seg = ft_first_data_segment;
124 	tape_vtbl.end_seg   = ft_last_data_segment;
125 	tape_vtbl.blk_sz    = zft_blk_sz;
126 	tape_vtbl.size      = zft_capacity;
127 	tape_vtbl.count     = 0;
128 }
129 
130 /* check for a valid VTBL signature.
131  */
vtbl_signature_valid(__u8 signature[4])132 static int vtbl_signature_valid(__u8 signature[4])
133 {
134 	const char *vtbl_ids[] = VTBL_IDS; /* valid signatures */
135 	int j;
136 
137 	for (j = 0;
138 	     (j < NR_ITEMS(vtbl_ids)) && (memcmp(signature, vtbl_ids[j], 4) != 0);
139 	     j++);
140 	return j < NR_ITEMS(vtbl_ids);
141 }
142 
143 /* We used to store the block-size of the volume in the volume-label,
144  * using the keyword "blocksize". The blocksize written to the
145  * volume-label is in bytes.
146  *
147  * We use this now only for compatability with old zftape version. We
148  * store the blocksize directly as binary number in the vendor
149  * extension part of the volume entry.
150  */
check_volume_label(const char * label,int * blk_sz)151 static int check_volume_label(const char *label, int *blk_sz)
152 {
153 	int valid_format;
154 	char *blocksize;
155 	TRACE_FUN(ft_t_flow);
156 
157 	TRACE(ft_t_noise, "called with \"%s\" / \"%s\"", label, ZFT_VOL_NAME);
158 	if (strncmp(label, ZFT_VOL_NAME, strlen(ZFT_VOL_NAME)) != 0) {
159 		*blk_sz = 1; /* smallest block size that we allow */
160 		valid_format = 0;
161 	} else {
162 		TRACE(ft_t_noise, "got old style zftape vtbl entry");
163 		/* get the default blocksize */
164 		/* use the kernel strstr()   */
165 		blocksize= strstr(label, " blocksize ");
166 		if (blocksize) {
167 			blocksize += strlen(" blocksize ");
168 			for(*blk_sz= 0;
169 			    *blocksize >= '0' && *blocksize <= '9';
170 			    blocksize++) {
171 				*blk_sz *= 10;
172 				*blk_sz += *blocksize - '0';
173 			}
174 			if (*blk_sz > ZFT_MAX_BLK_SZ) {
175 				*blk_sz= 1;
176 				valid_format= 0;
177 			} else {
178 				valid_format = 1;
179 			}
180 		} else {
181 			*blk_sz= 1;
182 			valid_format= 0;
183 		}
184 	}
185 	TRACE_EXIT valid_format;
186 }
187 
188 /*   check for a zftape volume
189  */
check_volume(__u8 * entry,zft_volinfo * volume)190 static int check_volume(__u8 *entry, zft_volinfo *volume)
191 {
192 	TRACE_FUN(ft_t_flow);
193 
194 	if(strncmp(&entry[VTBL_EXT+EXT_ZFTAPE_SIG], ZFTAPE_SIG,
195 		   strlen(ZFTAPE_SIG)) == 0) {
196 		TRACE(ft_t_noise, "got new style zftape vtbl entry");
197 		volume->blk_sz = GET2(entry, VTBL_EXT+EXT_ZFTAPE_BLKSZ);
198 		volume->qic113 = entry[VTBL_EXT+EXT_ZFTAPE_QIC113];
199 		TRACE_EXIT 1;
200 	} else {
201 		TRACE_EXIT check_volume_label(&entry[VTBL_DESC], &volume->blk_sz);
202 	}
203 }
204 
205 
206 /* create zftape specific vtbl entry, the volume bounds are inserted
207  * in the calling function, zft_create_volume_headers()
208  */
create_zft_volume(__u8 * entry,zft_volinfo * vtbl)209 static void create_zft_volume(__u8 *entry, zft_volinfo *vtbl)
210 {
211 	TRACE_FUN(ft_t_flow);
212 
213 	memset(entry, 0, VTBL_SIZE);
214 	memcpy(&entry[VTBL_SIG], VTBL_ID, 4);
215 	sprintf(&entry[VTBL_DESC], ZFT_VOL_NAME" %03d", vtbl->count);
216 	entry[VTBL_FLAGS] = (VTBL_FL_NOT_VERIFIED | VTBL_FL_SEG_SPANNING);
217 	entry[VTBL_M_NO] = 1; /* multi_cartridge_count */
218 	strcpy(&entry[VTBL_EXT+EXT_ZFTAPE_SIG], ZFTAPE_SIG);
219 	PUT2(entry, VTBL_EXT+EXT_ZFTAPE_BLKSZ, vtbl->blk_sz);
220 	if (zft_qic113) {
221 		PUT8(entry, VTBL_DATA_SIZE, vtbl->size);
222 		entry[VTBL_CMPR] = VTBL_CMPR_UNREG;
223 		if (vtbl->use_compression) { /* use compression: */
224 			entry[VTBL_CMPR] |= VTBL_CMPR_USED;
225 		}
226 		entry[VTBL_EXT+EXT_ZFTAPE_QIC113] = 1;
227 	} else {
228 		PUT4(entry, VTBL_DATA_SIZE, vtbl->size);
229 		entry[VTBL_K_CMPR] = VTBL_CMPR_UNREG;
230 		if (vtbl->use_compression) { /* use compression: */
231 			entry[VTBL_K_CMPR] |= VTBL_CMPR_USED;
232 		}
233 	}
234 	if (ft_format_code == fmt_big) {
235 		/* SCSI like vtbl, store the number of used
236 		 * segments as 4 byte value
237 		 */
238 		PUT4(entry, VTBL_SCSI_SEGS, vtbl->end_seg-vtbl->start_seg + 1);
239 	} else {
240 		/* normal, QIC-80MC like vtbl
241 		 */
242 		PUT2(entry, VTBL_START, vtbl->start_seg);
243 		PUT2(entry, VTBL_END, vtbl->end_seg);
244 	}
245 	TRACE_EXIT;
246 }
247 
248 /* this one creates the volume headers for each volume. It is assumed
249  * that buffer already contains the old volume-table, so that vtbl
250  * entries without the zft_volume flag set can savely be ignored.
251  */
zft_create_volume_headers(__u8 * buffer)252 void zft_create_volume_headers(__u8 *buffer)
253 {
254 	__u8 *entry;
255 	struct list_head *tmp;
256 	zft_volinfo *vtbl;
257 	TRACE_FUN(ft_t_flow);
258 
259 #ifdef ZFT_CMAP_HACK
260 	if((strncmp(&buffer[VTBL_EXT+EXT_ZFTAPE_SIG], ZFTAPE_SIG,
261 		    strlen(ZFTAPE_SIG)) == 0) &&
262 	   buffer[VTBL_EXT+EXT_ZFTAPE_CMAP] != 0) {
263 		TRACE(ft_t_noise, "deleting cmap volume");
264 		memmove(buffer, buffer + VTBL_SIZE,
265 			FT_SEGMENT_SIZE - VTBL_SIZE);
266 	}
267 #endif
268 	entry = buffer;
269 	for (tmp = zft_head_vtbl->node.next;
270 	     tmp != &zft_eom_vtbl->node;
271 	     tmp = tmp->next) {
272 		vtbl = list_entry(tmp, zft_volinfo, node);
273 		/* we now fill in the values only for newly created volumes.
274 		 */
275 		if (vtbl->new_volume) {
276 			create_zft_volume(entry, vtbl);
277 			vtbl->new_volume = 0; /* clear the flag */
278 		}
279 
280 		DUMP_VOLINFO(ft_t_noise, &entry[VTBL_DESC], vtbl);
281 		entry += VTBL_SIZE;
282 	}
283 	memset(entry, 0, FT_SEGMENT_SIZE - zft_eom_vtbl->count * VTBL_SIZE);
284 	TRACE_EXIT;
285 }
286 
287 /*  write volume table to tape. Calls zft_create_volume_headers()
288  */
zft_update_volume_table(unsigned int segment)289 int zft_update_volume_table(unsigned int segment)
290 {
291 	int result = 0;
292 	__u8 *verify_buf = NULL;
293 	TRACE_FUN(ft_t_flow);
294 
295 	TRACE_CATCH(result = ftape_read_segment(ft_first_data_segment,
296 						zft_deblock_buf,
297 						FT_RD_SINGLE),);
298 	zft_create_volume_headers(zft_deblock_buf);
299 	TRACE(ft_t_noise, "writing volume table segment %d", segment);
300 	if (zft_vmalloc_once(&verify_buf, FT_SEGMENT_SIZE) == 0) {
301 		TRACE_CATCH(zft_verify_write_segments(segment,
302 						      zft_deblock_buf, result,
303 						      verify_buf),
304 			    zft_vfree(&verify_buf, FT_SEGMENT_SIZE));
305 		zft_vfree(&verify_buf, FT_SEGMENT_SIZE);
306 	} else {
307 		TRACE_CATCH(ftape_write_segment(segment, zft_deblock_buf,
308 						FT_WR_SINGLE),);
309 	}
310 	TRACE_EXIT 0;
311 }
312 
313 /* non zftape volumes are handled in raw mode. Thus we need to
314  * calculate the raw amount of data contained in those segments.
315  */
extract_alien_volume(__u8 * entry,zft_volinfo * vtbl)316 static void extract_alien_volume(__u8 *entry, zft_volinfo *vtbl)
317 {
318 	TRACE_FUN(ft_t_flow);
319 
320 	vtbl->size  = (zft_calc_tape_pos(zft_last_vtbl->end_seg+1) -
321 		       zft_calc_tape_pos(zft_last_vtbl->start_seg));
322 	vtbl->use_compression = 0;
323 	vtbl->qic113 = zft_qic113;
324 	if (vtbl->qic113) {
325 		TRACE(ft_t_noise,
326 		      "Fake alien volume's size from " LL_X " to " LL_X,
327 		      LL(GET8(entry, VTBL_DATA_SIZE)), LL(vtbl->size));
328 	} else {
329 		TRACE(ft_t_noise,
330 		      "Fake alien volume's size from %d to " LL_X,
331 		      (int)GET4(entry, VTBL_DATA_SIZE), LL(vtbl->size));
332 	}
333 	TRACE_EXIT;
334 }
335 
336 
337 /* extract an zftape specific volume
338  */
extract_zft_volume(__u8 * entry,zft_volinfo * vtbl)339 static void extract_zft_volume(__u8 *entry, zft_volinfo *vtbl)
340 {
341 	TRACE_FUN(ft_t_flow);
342 
343 	if (vtbl->qic113) {
344 		vtbl->size = GET8(entry, VTBL_DATA_SIZE);
345 		vtbl->use_compression =
346 			(entry[VTBL_CMPR] & VTBL_CMPR_USED) != 0;
347 	} else {
348 		vtbl->size = GET4(entry, VTBL_DATA_SIZE);
349 		if (entry[VTBL_K_CMPR] & VTBL_CMPR_UNREG) {
350 			vtbl->use_compression =
351 				(entry[VTBL_K_CMPR] & VTBL_CMPR_USED) != 0;
352 		} else if (entry[VTBL_CMPR] & VTBL_CMPR_UNREG) {
353 			vtbl->use_compression =
354 				(entry[VTBL_CMPR] & VTBL_CMPR_USED) != 0;
355 		} else {
356 			TRACE(ft_t_warn, "Geeh! There is something wrong:\n"
357 			      KERN_INFO "QIC compression (Rev = K): %x\n"
358 			      KERN_INFO "QIC compression (Rev > K): %x",
359 			      entry[VTBL_K_CMPR], entry[VTBL_CMPR]);
360 		}
361 	}
362 	TRACE_EXIT;
363 }
364 
365 /* extract the volume table from buffer. "buffer" must already contain
366  * the vtbl-segment
367  */
zft_extract_volume_headers(__u8 * buffer)368 int zft_extract_volume_headers(__u8 *buffer)
369 {
370         __u8 *entry;
371 	TRACE_FUN(ft_t_flow);
372 
373 	zft_init_vtbl();
374 	entry = buffer;
375 #ifdef ZFT_CMAP_HACK
376 	if ((strncmp(&entry[VTBL_EXT+EXT_ZFTAPE_SIG], ZFTAPE_SIG,
377 		     strlen(ZFTAPE_SIG)) == 0) &&
378 	    entry[VTBL_EXT+EXT_ZFTAPE_CMAP] != 0) {
379 		TRACE(ft_t_noise, "ignoring cmap volume");
380 		entry += VTBL_SIZE;
381 	}
382 #endif
383 	/* the end of the vtbl is indicated by an invalid signature
384 	 */
385 	while (vtbl_signature_valid(&entry[VTBL_SIG]) &&
386 	       (entry - buffer) < FT_SEGMENT_SIZE) {
387 		zft_new_vtbl_entry();
388 		if (ft_format_code == fmt_big) {
389 			/* SCSI like vtbl, stores only the number of
390 			 * segments used
391 			 */
392 			unsigned int num_segments= GET4(entry, VTBL_SCSI_SEGS);
393 			zft_last_vtbl->start_seg = zft_eom_vtbl->start_seg;
394 			zft_last_vtbl->end_seg =
395 				zft_last_vtbl->start_seg + num_segments - 1;
396 		} else {
397 			/* `normal', QIC-80 like vtbl
398 			 */
399 			zft_last_vtbl->start_seg = GET2(entry, VTBL_START);
400 			zft_last_vtbl->end_seg   = GET2(entry, VTBL_END);
401 		}
402 		zft_eom_vtbl->start_seg  = zft_last_vtbl->end_seg + 1;
403 		/* check if we created this volume and get the
404 		 * blk_sz
405 		 */
406 		zft_last_vtbl->zft_volume = check_volume(entry, zft_last_vtbl);
407 		if (zft_last_vtbl->zft_volume == 0) {
408 			extract_alien_volume(entry, zft_last_vtbl);
409 		} else {
410 			extract_zft_volume(entry, zft_last_vtbl);
411 		}
412 		DUMP_VOLINFO(ft_t_noise, &entry[VTBL_DESC], zft_last_vtbl);
413 		entry +=VTBL_SIZE;
414 	}
415 #if 0
416 /*
417  *  undefine to test end of tape handling
418  */
419 	zft_new_vtbl_entry();
420 	zft_last_vtbl->start_seg = zft_eom_vtbl->start_seg;
421 	zft_last_vtbl->end_seg   = ft_last_data_segment - 10;
422 	zft_last_vtbl->blk_sz          = zft_blk_sz;
423 	zft_last_vtbl->zft_volume      = 1;
424 	zft_last_vtbl->qic113          = zft_qic113;
425 	zft_last_vtbl->size = (zft_calc_tape_pos(zft_last_vtbl->end_seg+1)
426 			       - zft_calc_tape_pos(zft_last_vtbl->start_seg));
427 #endif
428 	TRACE_EXIT 0;
429 }
430 
431 /* this functions translates the failed_sector_log, misused as
432  * EOF-marker list, into a virtual volume table. The table mustn't be
433  * written to tape, because this would occupy the first data segment,
434  * which should be the volume table, but is actually the first segment
435  * that is filled with data (when using standard ftape).  We assume,
436  * that we get a non-empty failed_sector_log.
437  */
zft_fake_volume_headers(eof_mark_union * eof_map,int num_failed_sectors)438 int zft_fake_volume_headers (eof_mark_union *eof_map, int num_failed_sectors)
439 {
440 	unsigned int segment, sector;
441 	int have_eom = 0;
442 	int vol_no;
443 	TRACE_FUN(ft_t_flow);
444 
445 	if ((num_failed_sectors >= 2) &&
446 	    (GET2(&eof_map[num_failed_sectors - 1].mark.segment, 0)
447 	     ==
448 	     GET2(&eof_map[num_failed_sectors - 2].mark.segment, 0) + 1) &&
449 	    (GET2(&eof_map[num_failed_sectors - 1].mark.date, 0) == 1)) {
450 		/* this should be eom. We keep the remainder of the
451 		 * tape as another volume.
452 		 */
453 		have_eom = 1;
454 	}
455 	zft_init_vtbl();
456 	zft_eom_vtbl->start_seg = ft_first_data_segment;
457 	for(vol_no = 0; vol_no < num_failed_sectors - have_eom; vol_no ++) {
458 		zft_new_vtbl_entry();
459 
460 		segment = GET2(&eof_map[vol_no].mark.segment, 0);
461 		sector  = GET2(&eof_map[vol_no].mark.date, 0);
462 
463 		zft_last_vtbl->start_seg  = zft_eom_vtbl->start_seg;
464 		zft_last_vtbl->end_seg    = segment;
465 		zft_eom_vtbl->start_seg  = segment + 1;
466 		zft_last_vtbl->blk_sz     = 1;
467 		zft_last_vtbl->size       =
468 			(zft_calc_tape_pos(zft_last_vtbl->end_seg)
469 			 - zft_calc_tape_pos(zft_last_vtbl->start_seg)
470 			 + (sector-1) * FT_SECTOR_SIZE);
471 		TRACE(ft_t_noise,
472 		      "failed sector log: segment: %d, sector: %d",
473 		      segment, sector);
474 		DUMP_VOLINFO(ft_t_noise, "Faked volume", zft_last_vtbl);
475 	}
476 	if (!have_eom) {
477 		zft_new_vtbl_entry();
478 		zft_last_vtbl->start_seg = zft_eom_vtbl->start_seg;
479 		zft_last_vtbl->end_seg   = ft_last_data_segment;
480 		zft_eom_vtbl->start_seg  = ft_last_data_segment + 1;
481 		zft_last_vtbl->size      = zft_capacity;
482 		zft_last_vtbl->size     -= zft_calc_tape_pos(zft_last_vtbl->start_seg);
483 		zft_last_vtbl->blk_sz    = 1;
484 		DUMP_VOLINFO(ft_t_noise, "Faked volume",zft_last_vtbl);
485 	}
486 	TRACE_EXIT 0;
487 }
488 
489 /* update the internal volume table
490  *
491  * if before start of last volume: erase all following volumes if
492  * inside a volume: set end of volume to infinity
493  *
494  * this function is intended to be called every time _ftape_write() is
495  * called
496  *
497  * return: 0 if no new volume was created, 1 if a new volume was
498  * created
499  *
500  * NOTE: we don't need to check for zft_mode as ftape_write() does
501  * that already. This function gets never called without accessing
502  * zftape via the *qft* devices
503  */
504 
zft_open_volume(zft_position * pos,int blk_sz,int use_compression)505 int zft_open_volume(zft_position *pos, int blk_sz, int use_compression)
506 {
507 	TRACE_FUN(ft_t_flow);
508 
509 	if (!zft_qic_mode) {
510 		TRACE_EXIT 0;
511 	}
512 	if (zft_tape_at_lbot(pos)) {
513 		zft_init_vtbl();
514 		if(zft_old_ftape) {
515 			/* clear old ftape's eof marks */
516 			zft_clear_ftape_file_marks();
517 			zft_old_ftape = 0; /* no longer old ftape */
518 		}
519 		zft_reset_position(pos);
520 	}
521 	if (pos->seg_pos != zft_last_vtbl->end_seg + 1) {
522 		TRACE_ABORT(-EIO, ft_t_bug,
523 		      "BUG: seg_pos: %d, zft_last_vtbl->end_seg: %d",
524 		      pos->seg_pos, zft_last_vtbl->end_seg);
525 	}
526 	TRACE(ft_t_noise, "create new volume");
527 	if (zft_eom_vtbl->count >= ZFT_MAX_VOLUMES) {
528 		TRACE_ABORT(-ENOSPC, ft_t_err,
529 			    "Error: maxmimal number of volumes exhausted "
530 			    "(maxmimum is %d)", ZFT_MAX_VOLUMES);
531 	}
532 	zft_new_vtbl_entry();
533 	pos->volume_pos = pos->seg_byte_pos = 0;
534 	zft_last_vtbl->start_seg       = pos->seg_pos;
535 	zft_last_vtbl->end_seg         = ft_last_data_segment; /* infinity */
536 	zft_last_vtbl->blk_sz          = blk_sz;
537 	zft_last_vtbl->size            = zft_capacity;
538 	zft_last_vtbl->zft_volume      = 1;
539 	zft_last_vtbl->use_compression = use_compression;
540 	zft_last_vtbl->qic113          = zft_qic113;
541 	zft_last_vtbl->new_volume      = 1;
542 	zft_last_vtbl->open            = 1;
543 	zft_volume_table_changed = 1;
544 	zft_eom_vtbl->start_seg  = ft_last_data_segment + 1;
545 	TRACE_EXIT 0;
546 }
547 
548 /*  perform mtfsf, mtbsf, not allowed without zft_qic_mode
549  */
zft_skip_volumes(int count,zft_position * pos)550 int zft_skip_volumes(int count, zft_position *pos)
551 {
552 	const zft_volinfo *vtbl;
553 	TRACE_FUN(ft_t_flow);
554 
555 	TRACE(ft_t_noise, "count: %d", count);
556 
557 	vtbl= zft_find_volume(pos->seg_pos);
558 	while (count > 0 && vtbl != zft_eom_vtbl) {
559 		vtbl = list_entry(vtbl->node.next, zft_volinfo, node);
560 		count --;
561 	}
562 	while (count < 0 && vtbl != zft_first_vtbl) {
563 		vtbl = list_entry(vtbl->node.prev, zft_volinfo, node);
564 		count ++;
565 	}
566 	pos->seg_pos        = vtbl->start_seg;
567 	pos->seg_byte_pos   = 0;
568 	pos->volume_pos     = 0;
569 	pos->tape_pos       = zft_calc_tape_pos(pos->seg_pos);
570 	zft_just_before_eof = vtbl->size == 0;
571 	if (zft_cmpr_ops) {
572 		(*zft_cmpr_ops->reset)();
573 	}
574 	zft_deblock_segment = -1; /* no need to keep cache */
575 	TRACE(ft_t_noise, "repositioning to:\n"
576 	      KERN_INFO "zft_seg_pos        : %d\n"
577 	      KERN_INFO "zft_seg_byte_pos   : %d\n"
578 	      KERN_INFO "zft_tape_pos       : " LL_X "\n"
579 	      KERN_INFO "zft_volume_pos     : " LL_X "\n"
580 	      KERN_INFO "file number        : %d",
581 	      pos->seg_pos, pos->seg_byte_pos,
582 	      LL(pos->tape_pos), LL(pos->volume_pos), vtbl->count);
583 	zft_resid = count < 0 ? -count : count;
584 	TRACE_EXIT zft_resid ? -EINVAL : 0;
585 }
586 
587 /* the following simply returns the raw data position of the EOM
588  * marker, MTIOCSIZE ioctl
589  */
zft_get_eom_pos(void)590 __s64 zft_get_eom_pos(void)
591 {
592 	if (zft_qic_mode) {
593 		return zft_calc_tape_pos(zft_eom_vtbl->start_seg);
594 	} else {
595 		/* there is only one volume in raw mode */
596 		return zft_capacity;
597 	}
598 }
599 
600 /* skip to eom, used for MTEOM
601  */
zft_skip_to_eom(zft_position * pos)602 void zft_skip_to_eom(zft_position *pos)
603 {
604 	TRACE_FUN(ft_t_flow);
605 	pos->seg_pos      = zft_eom_vtbl->start_seg;
606 	pos->seg_byte_pos =
607 		pos->volume_pos     =
608 		zft_just_before_eof = 0;
609 	pos->tape_pos = zft_calc_tape_pos(pos->seg_pos);
610 	TRACE(ft_t_noise, "ftape positioned to segment %d, data pos " LL_X,
611 	      pos->seg_pos, LL(pos->tape_pos));
612 	TRACE_EXIT;
613 }
614 
615 /*  write an EOF-marker by setting zft_last_vtbl->end_seg to seg_pos.
616  *  NOTE: this function assumes that zft_last_vtbl points to a valid
617  *  vtbl entry
618  *
619  *  NOTE: this routine always positions before the EOF marker
620  */
zft_close_volume(zft_position * pos)621 int zft_close_volume(zft_position *pos)
622 {
623 	TRACE_FUN(ft_t_any);
624 
625 	if (zft_vtbl_empty || !zft_last_vtbl->open) { /* should not happen */
626 		TRACE(ft_t_noise, "There are no volumes to finish");
627 		TRACE_EXIT -EIO;
628 	}
629 	if (pos->seg_byte_pos == 0 &&
630 	    pos->seg_pos != zft_last_vtbl->start_seg) {
631 		pos->seg_pos --;
632 		pos->seg_byte_pos      = zft_get_seg_sz(pos->seg_pos);
633 	}
634 	zft_last_vtbl->end_seg   = pos->seg_pos;
635 	zft_last_vtbl->size      = pos->volume_pos;
636 	zft_volume_table_changed = 1;
637 	zft_just_before_eof      = 1;
638 	zft_eom_vtbl->start_seg  = zft_last_vtbl->end_seg + 1;
639 	zft_last_vtbl->open      = 0; /* closed */
640 	TRACE_EXIT 0;
641 }
642 
643 /* write count file-marks at current position.
644  *
645  *  The tape is positioned after the eof-marker, that is at byte 0 of
646  *  the segment following the eof-marker
647  *
648  *  this function is only allowed in zft_qic_mode
649  *
650  *  Only allowed when tape is at BOT or EOD.
651  */
zft_weof(unsigned int count,zft_position * pos)652 int zft_weof(unsigned int count, zft_position *pos)
653 {
654 
655 	TRACE_FUN(ft_t_flow);
656 
657 	if (!count) { /* write zero EOF marks should be a real no-op */
658 		TRACE_EXIT 0;
659 	}
660 	zft_volume_table_changed = 1;
661 	if (zft_tape_at_lbot(pos)) {
662 		zft_init_vtbl();
663 		if(zft_old_ftape) {
664 			/* clear old ftape's eof marks */
665 			zft_clear_ftape_file_marks();
666 			zft_old_ftape = 0;    /* no longer old ftape */
667 		}
668 	}
669 	if (zft_last_vtbl->open) {
670 		zft_close_volume(pos);
671 		zft_move_past_eof(pos);
672 		count --;
673 	}
674 	/* now it's easy, just append eof-marks, that is empty
675 	 * volumes, to the end of the already recorded media.
676 	 */
677 	while (count > 0 &&
678 	       pos->seg_pos <= ft_last_data_segment &&
679 	       zft_eom_vtbl->count < ZFT_MAX_VOLUMES) {
680 		TRACE(ft_t_noise,
681 		      "Writing zero sized file at segment %d", pos->seg_pos);
682 		zft_new_vtbl_entry();
683 		zft_last_vtbl->start_seg       = pos->seg_pos;
684 		zft_last_vtbl->end_seg         = pos->seg_pos;
685 		zft_last_vtbl->size            = 0;
686 		zft_last_vtbl->blk_sz          = zft_blk_sz;
687 		zft_last_vtbl->zft_volume      = 1;
688 		zft_last_vtbl->use_compression = 0;
689 		pos->tape_pos += zft_get_seg_sz(pos->seg_pos);
690 		zft_eom_vtbl->start_seg = ++ pos->seg_pos;
691 		count --;
692 	}
693 	if (count > 0) {
694 		/*  there are two possibilities: end of tape, or the
695 		 *  maximum number of files is exhausted.
696 		 */
697 		zft_resid = count;
698 		TRACE(ft_t_noise,"Number of marks NOT written: %d", zft_resid);
699 		if (zft_eom_vtbl->count == ZFT_MAX_VOLUMES) {
700 			TRACE_ABORT(-EINVAL, ft_t_warn,
701 				    "maximum allowed number of files "
702 				    "exhausted: %d", ZFT_MAX_VOLUMES);
703 		} else {
704 			TRACE_ABORT(-ENOSPC,
705 				    ft_t_noise, "reached end of tape");
706 		}
707 	}
708 	TRACE_EXIT 0;
709 }
710 
zft_find_volume(unsigned int seg_pos)711 const zft_volinfo *zft_find_volume(unsigned int seg_pos)
712 {
713 	TRACE_FUN(ft_t_flow);
714 
715 	TRACE(ft_t_any, "called with seg_pos %d",seg_pos);
716 	if (!zft_qic_mode) {
717 		if (seg_pos > ft_last_data_segment) {
718 			TRACE_EXIT &eot_vtbl;
719 		}
720 		tape_vtbl.blk_sz =  zft_blk_sz;
721 		TRACE_EXIT &tape_vtbl;
722 	}
723 	if (seg_pos < zft_first_vtbl->start_seg) {
724 		TRACE_EXIT (cur_vtbl = zft_first_vtbl);
725 	}
726 	while (seg_pos > cur_vtbl->end_seg) {
727 		cur_vtbl = list_entry(cur_vtbl->node.next, zft_volinfo, node);
728 		TRACE(ft_t_noise, "%d - %d", cur_vtbl->start_seg, cur_vtbl->end_seg);
729 	}
730 	while (seg_pos < cur_vtbl->start_seg) {
731 		cur_vtbl = list_entry(cur_vtbl->node.prev, zft_volinfo, node);
732 		TRACE(ft_t_noise, "%d - %d", cur_vtbl->start_seg, cur_vtbl->end_seg);
733 	}
734 	if (seg_pos > cur_vtbl->end_seg || seg_pos < cur_vtbl->start_seg) {
735 		TRACE(ft_t_bug, "This cannot happen");
736 	}
737 	DUMP_VOLINFO(ft_t_noise, "", cur_vtbl);
738 	TRACE_EXIT cur_vtbl;
739 }
740 
741 /* this function really assumes that we are just before eof
742  */
zft_move_past_eof(zft_position * pos)743 void zft_move_past_eof(zft_position *pos)
744 {
745 	TRACE_FUN(ft_t_flow);
746 
747 	TRACE(ft_t_noise, "old seg. pos: %d", pos->seg_pos);
748 	pos->tape_pos += zft_get_seg_sz(pos->seg_pos++) - pos->seg_byte_pos;
749 	pos->seg_byte_pos = 0;
750 	pos->volume_pos   = 0;
751 	if (zft_cmpr_ops) {
752 		(*zft_cmpr_ops->reset)();
753 	}
754 	zft_just_before_eof =  0;
755 	zft_deblock_segment = -1; /* no need to cache it anymore */
756 	TRACE(ft_t_noise, "new seg. pos: %d", pos->seg_pos);
757 	TRACE_EXIT;
758 }
759