tape_char.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * drivers/s390/char/tape_char.c
  3. * character device frontend for tape device driver
  4. *
  5. * S390 and zSeries version
  6. * Copyright (C) 2001,2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Carsten Otte <cotte@de.ibm.com>
  8. * Michael Holzheu <holzheu@de.ibm.com>
  9. * Tuan Ngo-Anh <ngoanh@de.ibm.com>
  10. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/mtio.h>
  17. #include <asm/uaccess.h>
  18. #define TAPE_DBF_AREA tape_core_dbf
  19. #include "tape.h"
  20. #include "tape_std.h"
  21. #include "tape_class.h"
  22. #define PRINTK_HEADER "TAPE_CHAR: "
  23. #define TAPECHAR_MAJOR 0 /* get dynamic major */
  24. /*
  25. * file operation structure for tape character frontend
  26. */
  27. static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
  28. static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
  29. static int tapechar_open(struct inode *,struct file *);
  30. static int tapechar_release(struct inode *,struct file *);
  31. static int tapechar_ioctl(struct inode *, struct file *, unsigned int,
  32. unsigned long);
  33. static struct file_operations tape_fops =
  34. {
  35. .owner = THIS_MODULE,
  36. .read = tapechar_read,
  37. .write = tapechar_write,
  38. .ioctl = tapechar_ioctl,
  39. .open = tapechar_open,
  40. .release = tapechar_release,
  41. };
  42. static int tapechar_major = TAPECHAR_MAJOR;
  43. /*
  44. * This function is called for every new tapedevice
  45. */
  46. int
  47. tapechar_setup_device(struct tape_device * device)
  48. {
  49. char device_name[20];
  50. sprintf(device_name, "ntibm%i", device->first_minor / 2);
  51. device->nt = register_tape_dev(
  52. &device->cdev->dev,
  53. MKDEV(tapechar_major, device->first_minor),
  54. &tape_fops,
  55. device_name,
  56. "non-rewinding"
  57. );
  58. device_name[0] = 'r';
  59. device->rt = register_tape_dev(
  60. &device->cdev->dev,
  61. MKDEV(tapechar_major, device->first_minor + 1),
  62. &tape_fops,
  63. device_name,
  64. "rewinding"
  65. );
  66. return 0;
  67. }
  68. void
  69. tapechar_cleanup_device(struct tape_device *device)
  70. {
  71. unregister_tape_dev(device->rt);
  72. device->rt = NULL;
  73. unregister_tape_dev(device->nt);
  74. device->nt = NULL;
  75. }
  76. /*
  77. * Terminate write command (we write two TMs and skip backward over last)
  78. * This ensures that the tape is always correctly terminated.
  79. * When the user writes afterwards a new file, he will overwrite the
  80. * second TM and therefore one TM will remain to separate the
  81. * two files on the tape...
  82. */
  83. static inline void
  84. tapechar_terminate_write(struct tape_device *device)
  85. {
  86. if (tape_mtop(device, MTWEOF, 1) == 0 &&
  87. tape_mtop(device, MTWEOF, 1) == 0)
  88. tape_mtop(device, MTBSR, 1);
  89. }
  90. static inline int
  91. tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
  92. {
  93. struct idal_buffer *new;
  94. if (device->char_data.idal_buf != NULL &&
  95. device->char_data.idal_buf->size == block_size)
  96. return 0;
  97. if (block_size > MAX_BLOCKSIZE) {
  98. DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
  99. block_size, MAX_BLOCKSIZE);
  100. PRINT_ERR("Invalid blocksize (%zd> %d)\n",
  101. block_size, MAX_BLOCKSIZE);
  102. return -EINVAL;
  103. }
  104. /* The current idal buffer is not correct. Allocate a new one. */
  105. new = idal_buffer_alloc(block_size, 0);
  106. if (new == NULL)
  107. return -ENOMEM;
  108. if (device->char_data.idal_buf != NULL)
  109. idal_buffer_free(device->char_data.idal_buf);
  110. device->char_data.idal_buf = new;
  111. return 0;
  112. }
  113. /*
  114. * Tape device read function
  115. */
  116. ssize_t
  117. tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
  118. {
  119. struct tape_device *device;
  120. struct tape_request *request;
  121. size_t block_size;
  122. int rc;
  123. DBF_EVENT(6, "TCHAR:read\n");
  124. device = (struct tape_device *) filp->private_data;
  125. /*
  126. * If the tape isn't terminated yet, do it now. And since we then
  127. * are at the end of the tape there wouldn't be anything to read
  128. * anyways. So we return immediatly.
  129. */
  130. if(device->required_tapemarks) {
  131. return tape_std_terminate_write(device);
  132. }
  133. /* Find out block size to use */
  134. if (device->char_data.block_size != 0) {
  135. if (count < device->char_data.block_size) {
  136. DBF_EVENT(3, "TCHAR:read smaller than block "
  137. "size was requested\n");
  138. return -EINVAL;
  139. }
  140. block_size = device->char_data.block_size;
  141. } else {
  142. block_size = count;
  143. }
  144. rc = tapechar_check_idalbuffer(device, block_size);
  145. if (rc)
  146. return rc;
  147. #ifdef CONFIG_S390_TAPE_BLOCK
  148. /* Changes position. */
  149. device->blk_data.medium_changed = 1;
  150. #endif
  151. DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
  152. /* Let the discipline build the ccw chain. */
  153. request = device->discipline->read_block(device, block_size);
  154. if (IS_ERR(request))
  155. return PTR_ERR(request);
  156. /* Execute it. */
  157. rc = tape_do_io(device, request);
  158. if (rc == 0) {
  159. rc = block_size - request->rescnt;
  160. DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
  161. filp->f_pos += rc;
  162. /* Copy data from idal buffer to user space. */
  163. if (idal_buffer_to_user(device->char_data.idal_buf,
  164. data, rc) != 0)
  165. rc = -EFAULT;
  166. }
  167. tape_free_request(request);
  168. return rc;
  169. }
  170. /*
  171. * Tape device write function
  172. */
  173. ssize_t
  174. tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
  175. {
  176. struct tape_device *device;
  177. struct tape_request *request;
  178. size_t block_size;
  179. size_t written;
  180. int nblocks;
  181. int i, rc;
  182. DBF_EVENT(6, "TCHAR:write\n");
  183. device = (struct tape_device *) filp->private_data;
  184. /* Find out block size and number of blocks */
  185. if (device->char_data.block_size != 0) {
  186. if (count < device->char_data.block_size) {
  187. DBF_EVENT(3, "TCHAR:write smaller than block "
  188. "size was requested\n");
  189. return -EINVAL;
  190. }
  191. block_size = device->char_data.block_size;
  192. nblocks = count / block_size;
  193. } else {
  194. block_size = count;
  195. nblocks = 1;
  196. }
  197. rc = tapechar_check_idalbuffer(device, block_size);
  198. if (rc)
  199. return rc;
  200. #ifdef CONFIG_S390_TAPE_BLOCK
  201. /* Changes position. */
  202. device->blk_data.medium_changed = 1;
  203. #endif
  204. DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
  205. DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
  206. /* Let the discipline build the ccw chain. */
  207. request = device->discipline->write_block(device, block_size);
  208. if (IS_ERR(request))
  209. return PTR_ERR(request);
  210. rc = 0;
  211. written = 0;
  212. for (i = 0; i < nblocks; i++) {
  213. /* Copy data from user space to idal buffer. */
  214. if (idal_buffer_from_user(device->char_data.idal_buf,
  215. data, block_size)) {
  216. rc = -EFAULT;
  217. break;
  218. }
  219. rc = tape_do_io(device, request);
  220. if (rc)
  221. break;
  222. DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
  223. block_size - request->rescnt);
  224. filp->f_pos += block_size - request->rescnt;
  225. written += block_size - request->rescnt;
  226. if (request->rescnt != 0)
  227. break;
  228. data += block_size;
  229. }
  230. tape_free_request(request);
  231. if (rc == -ENOSPC) {
  232. /*
  233. * Ok, the device has no more space. It has NOT written
  234. * the block.
  235. */
  236. if (device->discipline->process_eov)
  237. device->discipline->process_eov(device);
  238. if (written > 0)
  239. rc = 0;
  240. }
  241. /*
  242. * After doing a write we always need two tapemarks to correctly
  243. * terminate the tape (one to terminate the file, the second to
  244. * flag the end of recorded data.
  245. * Since process_eov positions the tape in front of the written
  246. * tapemark it doesn't hurt to write two marks again.
  247. */
  248. if (!rc)
  249. device->required_tapemarks = 2;
  250. return rc ? rc : written;
  251. }
  252. /*
  253. * Character frontend tape device open function.
  254. */
  255. int
  256. tapechar_open (struct inode *inode, struct file *filp)
  257. {
  258. struct tape_device *device;
  259. int minor, rc;
  260. DBF_EVENT(6, "TCHAR:open: %i:%i\n",
  261. imajor(filp->f_dentry->d_inode),
  262. iminor(filp->f_dentry->d_inode));
  263. if (imajor(filp->f_dentry->d_inode) != tapechar_major)
  264. return -ENODEV;
  265. minor = iminor(filp->f_dentry->d_inode);
  266. device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
  267. if (IS_ERR(device)) {
  268. DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
  269. return PTR_ERR(device);
  270. }
  271. rc = tape_open(device);
  272. if (rc == 0) {
  273. filp->private_data = device;
  274. return nonseekable_open(inode, filp);
  275. }
  276. tape_put_device(device);
  277. return rc;
  278. }
  279. /*
  280. * Character frontend tape device release function.
  281. */
  282. int
  283. tapechar_release(struct inode *inode, struct file *filp)
  284. {
  285. struct tape_device *device;
  286. DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
  287. device = (struct tape_device *) filp->private_data;
  288. /*
  289. * If this is the rewinding tape minor then rewind. In that case we
  290. * write all required tapemarks. Otherwise only one to terminate the
  291. * file.
  292. */
  293. if ((iminor(inode) & 1) != 0) {
  294. if (device->required_tapemarks)
  295. tape_std_terminate_write(device);
  296. tape_mtop(device, MTREW, 1);
  297. } else {
  298. if (device->required_tapemarks > 1) {
  299. if (tape_mtop(device, MTWEOF, 1) == 0)
  300. device->required_tapemarks--;
  301. }
  302. }
  303. if (device->char_data.idal_buf != NULL) {
  304. idal_buffer_free(device->char_data.idal_buf);
  305. device->char_data.idal_buf = NULL;
  306. }
  307. tape_release(device);
  308. filp->private_data = tape_put_device(device);
  309. return 0;
  310. }
  311. /*
  312. * Tape device io controls.
  313. */
  314. static int
  315. tapechar_ioctl(struct inode *inp, struct file *filp,
  316. unsigned int no, unsigned long data)
  317. {
  318. struct tape_device *device;
  319. int rc;
  320. DBF_EVENT(6, "TCHAR:ioct\n");
  321. device = (struct tape_device *) filp->private_data;
  322. if (no == MTIOCTOP) {
  323. struct mtop op;
  324. if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
  325. return -EFAULT;
  326. if (op.mt_count < 0)
  327. return -EINVAL;
  328. /*
  329. * Operations that change tape position should write final
  330. * tapemarks.
  331. */
  332. switch (op.mt_op) {
  333. case MTFSF:
  334. case MTBSF:
  335. case MTFSR:
  336. case MTBSR:
  337. case MTREW:
  338. case MTOFFL:
  339. case MTEOM:
  340. case MTRETEN:
  341. case MTBSFM:
  342. case MTFSFM:
  343. case MTSEEK:
  344. #ifdef CONFIG_S390_TAPE_BLOCK
  345. device->blk_data.medium_changed = 1;
  346. #endif
  347. if (device->required_tapemarks)
  348. tape_std_terminate_write(device);
  349. default:
  350. ;
  351. }
  352. rc = tape_mtop(device, op.mt_op, op.mt_count);
  353. if (op.mt_op == MTWEOF && rc == 0) {
  354. if (op.mt_count > device->required_tapemarks)
  355. device->required_tapemarks = 0;
  356. else
  357. device->required_tapemarks -= op.mt_count;
  358. }
  359. return rc;
  360. }
  361. if (no == MTIOCPOS) {
  362. /* MTIOCPOS: query the tape position. */
  363. struct mtpos pos;
  364. rc = tape_mtop(device, MTTELL, 1);
  365. if (rc < 0)
  366. return rc;
  367. pos.mt_blkno = rc;
  368. if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
  369. return -EFAULT;
  370. return 0;
  371. }
  372. if (no == MTIOCGET) {
  373. /* MTIOCGET: query the tape drive status. */
  374. struct mtget get;
  375. memset(&get, 0, sizeof(get));
  376. get.mt_type = MT_ISUNKNOWN;
  377. get.mt_resid = 0 /* device->devstat.rescnt */;
  378. get.mt_dsreg = device->tape_state;
  379. /* FIXME: mt_gstat, mt_erreg, mt_fileno */
  380. get.mt_gstat = 0;
  381. get.mt_erreg = 0;
  382. get.mt_fileno = 0;
  383. get.mt_gstat = device->tape_generic_status;
  384. if (device->medium_state == MS_LOADED) {
  385. rc = tape_mtop(device, MTTELL, 1);
  386. if (rc < 0)
  387. return rc;
  388. if (rc == 0)
  389. get.mt_gstat |= GMT_BOT(~0);
  390. get.mt_blkno = rc;
  391. }
  392. if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
  393. return -EFAULT;
  394. return 0;
  395. }
  396. /* Try the discipline ioctl function. */
  397. if (device->discipline->ioctl_fn == NULL)
  398. return -EINVAL;
  399. return device->discipline->ioctl_fn(device, no, data);
  400. }
  401. /*
  402. * Initialize character device frontend.
  403. */
  404. int
  405. tapechar_init (void)
  406. {
  407. dev_t dev;
  408. if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
  409. return -1;
  410. tapechar_major = MAJOR(dev);
  411. PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
  412. return 0;
  413. }
  414. /*
  415. * cleanup
  416. */
  417. void
  418. tapechar_exit(void)
  419. {
  420. PRINT_INFO("tape releases major %d for character devices\n",
  421. tapechar_major);
  422. unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
  423. }