tape_char.c 11 KB

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