tape_char.c 11 KB

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