tape_char.c 11 KB

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