tape_char.c 12 KB

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