tape_char.c 12 KB

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