tape_char.c 11 KB

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