zftape-vtbl.c 22 KB

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