tape_char.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. #define KMSG_COMPONENT "tape"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/mtio.h>
  18. #include <linux/smp_lock.h>
  19. #include <asm/uaccess.h>
  20. #define TAPE_DBF_AREA tape_core_dbf
  21. #include "tape.h"
  22. #include "tape_std.h"
  23. #include "tape_class.h"
  24. #define TAPECHAR_MAJOR 0 /* get dynamic major */
  25. /*
  26. * file operation structure for tape character frontend
  27. */
  28. static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
  29. static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
  30. static int tapechar_open(struct inode *,struct file *);
  31. static int tapechar_release(struct inode *,struct file *);
  32. static long tapechar_ioctl(struct file *, unsigned int, 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. .unlocked_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. return -EINVAL;
  90. }
  91. /* The current idal buffer is not correct. Allocate a new one. */
  92. new = idal_buffer_alloc(block_size, 0);
  93. if (IS_ERR(new))
  94. return -ENOMEM;
  95. if (device->char_data.idal_buf != NULL)
  96. idal_buffer_free(device->char_data.idal_buf);
  97. device->char_data.idal_buf = new;
  98. return 0;
  99. }
  100. /*
  101. * Tape device read function
  102. */
  103. static ssize_t
  104. tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
  105. {
  106. struct tape_device *device;
  107. struct tape_request *request;
  108. size_t block_size;
  109. int rc;
  110. DBF_EVENT(6, "TCHAR:read\n");
  111. device = (struct tape_device *) filp->private_data;
  112. /*
  113. * If the tape isn't terminated yet, do it now. And since we then
  114. * are at the end of the tape there wouldn't be anything to read
  115. * anyways. So we return immediatly.
  116. */
  117. if(device->required_tapemarks) {
  118. return tape_std_terminate_write(device);
  119. }
  120. /* Find out block size to use */
  121. if (device->char_data.block_size != 0) {
  122. if (count < device->char_data.block_size) {
  123. DBF_EVENT(3, "TCHAR:read smaller than block "
  124. "size was requested\n");
  125. return -EINVAL;
  126. }
  127. block_size = device->char_data.block_size;
  128. } else {
  129. block_size = count;
  130. }
  131. rc = tapechar_check_idalbuffer(device, block_size);
  132. if (rc)
  133. return rc;
  134. #ifdef CONFIG_S390_TAPE_BLOCK
  135. /* Changes position. */
  136. device->blk_data.medium_changed = 1;
  137. #endif
  138. DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
  139. /* Let the discipline build the ccw chain. */
  140. request = device->discipline->read_block(device, block_size);
  141. if (IS_ERR(request))
  142. return PTR_ERR(request);
  143. /* Execute it. */
  144. rc = tape_do_io(device, request);
  145. if (rc == 0) {
  146. rc = block_size - request->rescnt;
  147. DBF_EVENT(6, "TCHAR:rbytes: %x\n", 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. written += block_size - request->rescnt;
  211. if (request->rescnt != 0)
  212. break;
  213. data += block_size;
  214. }
  215. tape_free_request(request);
  216. if (rc == -ENOSPC) {
  217. /*
  218. * Ok, the device has no more space. It has NOT written
  219. * the block.
  220. */
  221. if (device->discipline->process_eov)
  222. device->discipline->process_eov(device);
  223. if (written > 0)
  224. rc = 0;
  225. }
  226. /*
  227. * After doing a write we always need two tapemarks to correctly
  228. * terminate the tape (one to terminate the file, the second to
  229. * flag the end of recorded data.
  230. * Since process_eov positions the tape in front of the written
  231. * tapemark it doesn't hurt to write two marks again.
  232. */
  233. if (!rc)
  234. device->required_tapemarks = 2;
  235. return rc ? rc : written;
  236. }
  237. /*
  238. * Character frontend tape device open function.
  239. */
  240. static int
  241. tapechar_open (struct inode *inode, struct file *filp)
  242. {
  243. struct tape_device *device;
  244. int minor, rc;
  245. DBF_EVENT(6, "TCHAR:open: %i:%i\n",
  246. imajor(filp->f_path.dentry->d_inode),
  247. iminor(filp->f_path.dentry->d_inode));
  248. if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
  249. return -ENODEV;
  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. return PTR_ERR(device);
  255. }
  256. rc = tape_open(device);
  257. if (rc == 0) {
  258. filp->private_data = device;
  259. nonseekable_open(inode, filp);
  260. } else
  261. tape_put_device(device);
  262. return rc;
  263. }
  264. /*
  265. * Character frontend tape device release function.
  266. */
  267. static int
  268. tapechar_release(struct inode *inode, struct file *filp)
  269. {
  270. struct tape_device *device;
  271. DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
  272. device = (struct tape_device *) filp->private_data;
  273. /*
  274. * If this is the rewinding tape minor then rewind. In that case we
  275. * write all required tapemarks. Otherwise only one to terminate the
  276. * file.
  277. */
  278. if ((iminor(inode) & 1) != 0) {
  279. if (device->required_tapemarks)
  280. tape_std_terminate_write(device);
  281. tape_mtop(device, MTREW, 1);
  282. } else {
  283. if (device->required_tapemarks > 1) {
  284. if (tape_mtop(device, MTWEOF, 1) == 0)
  285. device->required_tapemarks--;
  286. }
  287. }
  288. if (device->char_data.idal_buf != NULL) {
  289. idal_buffer_free(device->char_data.idal_buf);
  290. device->char_data.idal_buf = NULL;
  291. }
  292. tape_release(device);
  293. filp->private_data = NULL;
  294. tape_put_device(device);
  295. return 0;
  296. }
  297. /*
  298. * Tape device io controls.
  299. */
  300. static int
  301. __tapechar_ioctl(struct tape_device *device,
  302. unsigned int no, unsigned long data)
  303. {
  304. int rc;
  305. if (no == MTIOCTOP) {
  306. struct mtop op;
  307. if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
  308. return -EFAULT;
  309. if (op.mt_count < 0)
  310. return -EINVAL;
  311. /*
  312. * Operations that change tape position should write final
  313. * tapemarks.
  314. */
  315. switch (op.mt_op) {
  316. case MTFSF:
  317. case MTBSF:
  318. case MTFSR:
  319. case MTBSR:
  320. case MTREW:
  321. case MTOFFL:
  322. case MTEOM:
  323. case MTRETEN:
  324. case MTBSFM:
  325. case MTFSFM:
  326. case MTSEEK:
  327. #ifdef CONFIG_S390_TAPE_BLOCK
  328. device->blk_data.medium_changed = 1;
  329. #endif
  330. if (device->required_tapemarks)
  331. tape_std_terminate_write(device);
  332. default:
  333. ;
  334. }
  335. rc = tape_mtop(device, op.mt_op, op.mt_count);
  336. if (op.mt_op == MTWEOF && rc == 0) {
  337. if (op.mt_count > device->required_tapemarks)
  338. device->required_tapemarks = 0;
  339. else
  340. device->required_tapemarks -= op.mt_count;
  341. }
  342. return rc;
  343. }
  344. if (no == MTIOCPOS) {
  345. /* MTIOCPOS: query the tape position. */
  346. struct mtpos pos;
  347. rc = tape_mtop(device, MTTELL, 1);
  348. if (rc < 0)
  349. return rc;
  350. pos.mt_blkno = rc;
  351. if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
  352. return -EFAULT;
  353. return 0;
  354. }
  355. if (no == MTIOCGET) {
  356. /* MTIOCGET: query the tape drive status. */
  357. struct mtget get;
  358. memset(&get, 0, sizeof(get));
  359. get.mt_type = MT_ISUNKNOWN;
  360. get.mt_resid = 0 /* device->devstat.rescnt */;
  361. get.mt_dsreg = device->tape_state;
  362. /* FIXME: mt_gstat, mt_erreg, mt_fileno */
  363. get.mt_gstat = 0;
  364. get.mt_erreg = 0;
  365. get.mt_fileno = 0;
  366. get.mt_gstat = device->tape_generic_status;
  367. if (device->medium_state == MS_LOADED) {
  368. rc = tape_mtop(device, MTTELL, 1);
  369. if (rc < 0)
  370. return rc;
  371. if (rc == 0)
  372. get.mt_gstat |= GMT_BOT(~0);
  373. get.mt_blkno = rc;
  374. }
  375. if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
  376. return -EFAULT;
  377. return 0;
  378. }
  379. /* Try the discipline ioctl function. */
  380. if (device->discipline->ioctl_fn == NULL)
  381. return -EINVAL;
  382. return device->discipline->ioctl_fn(device, no, data);
  383. }
  384. static long
  385. tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
  386. {
  387. struct tape_device *device;
  388. long rc;
  389. DBF_EVENT(6, "TCHAR:ioct\n");
  390. device = (struct tape_device *) filp->private_data;
  391. mutex_lock(&device->mutex);
  392. rc = __tapechar_ioctl(device, no, data);
  393. mutex_unlock(&device->mutex);
  394. return rc;
  395. }
  396. static long
  397. tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
  398. {
  399. struct tape_device *device = filp->private_data;
  400. int rval = -ENOIOCTLCMD;
  401. if (device->discipline->ioctl_fn) {
  402. mutex_lock(&device->mutex);
  403. rval = device->discipline->ioctl_fn(device, no, data);
  404. mutex_unlock(&device->mutex);
  405. if (rval == -EINVAL)
  406. rval = -ENOIOCTLCMD;
  407. }
  408. return rval;
  409. }
  410. /*
  411. * Initialize character device frontend.
  412. */
  413. int
  414. tapechar_init (void)
  415. {
  416. dev_t dev;
  417. if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
  418. return -1;
  419. tapechar_major = MAJOR(dev);
  420. return 0;
  421. }
  422. /*
  423. * cleanup
  424. */
  425. void
  426. tapechar_exit(void)
  427. {
  428. unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
  429. }