tape_char.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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/compat.h>
  19. #include <linux/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. #ifdef CONFIG_COMPAT
  34. static long tapechar_compat_ioctl(struct file *, unsigned int, unsigned long);
  35. #endif
  36. static const struct file_operations tape_fops =
  37. {
  38. .owner = THIS_MODULE,
  39. .read = tapechar_read,
  40. .write = tapechar_write,
  41. .unlocked_ioctl = tapechar_ioctl,
  42. #ifdef CONFIG_COMPAT
  43. .compat_ioctl = tapechar_compat_ioctl,
  44. #endif
  45. .open = tapechar_open,
  46. .release = tapechar_release,
  47. .llseek = no_llseek,
  48. };
  49. static int tapechar_major = TAPECHAR_MAJOR;
  50. /*
  51. * This function is called for every new tapedevice
  52. */
  53. int
  54. tapechar_setup_device(struct tape_device * device)
  55. {
  56. char device_name[20];
  57. sprintf(device_name, "ntibm%i", device->first_minor / 2);
  58. device->nt = register_tape_dev(
  59. &device->cdev->dev,
  60. MKDEV(tapechar_major, device->first_minor),
  61. &tape_fops,
  62. device_name,
  63. "non-rewinding"
  64. );
  65. device_name[0] = 'r';
  66. device->rt = register_tape_dev(
  67. &device->cdev->dev,
  68. MKDEV(tapechar_major, device->first_minor + 1),
  69. &tape_fops,
  70. device_name,
  71. "rewinding"
  72. );
  73. return 0;
  74. }
  75. void
  76. tapechar_cleanup_device(struct tape_device *device)
  77. {
  78. unregister_tape_dev(&device->cdev->dev, device->rt);
  79. device->rt = NULL;
  80. unregister_tape_dev(&device->cdev->dev, device->nt);
  81. device->nt = NULL;
  82. }
  83. static int
  84. tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
  85. {
  86. struct idal_buffer *new;
  87. if (device->char_data.idal_buf != NULL &&
  88. device->char_data.idal_buf->size == block_size)
  89. return 0;
  90. if (block_size > MAX_BLOCKSIZE) {
  91. DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
  92. block_size, MAX_BLOCKSIZE);
  93. return -EINVAL;
  94. }
  95. /* The current idal buffer is not correct. Allocate a new one. */
  96. new = idal_buffer_alloc(block_size, 0);
  97. if (IS_ERR(new))
  98. return -ENOMEM;
  99. if (device->char_data.idal_buf != NULL)
  100. idal_buffer_free(device->char_data.idal_buf);
  101. device->char_data.idal_buf = new;
  102. return 0;
  103. }
  104. /*
  105. * Tape device read function
  106. */
  107. static ssize_t
  108. tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
  109. {
  110. struct tape_device *device;
  111. struct tape_request *request;
  112. size_t block_size;
  113. int rc;
  114. DBF_EVENT(6, "TCHAR:read\n");
  115. device = (struct tape_device *) filp->private_data;
  116. /*
  117. * If the tape isn't terminated yet, do it now. And since we then
  118. * are at the end of the tape there wouldn't be anything to read
  119. * anyways. So we return immediately.
  120. */
  121. if(device->required_tapemarks) {
  122. return tape_std_terminate_write(device);
  123. }
  124. /* Find out block size to use */
  125. if (device->char_data.block_size != 0) {
  126. if (count < device->char_data.block_size) {
  127. DBF_EVENT(3, "TCHAR:read smaller than block "
  128. "size was requested\n");
  129. return -EINVAL;
  130. }
  131. block_size = device->char_data.block_size;
  132. } else {
  133. block_size = count;
  134. }
  135. rc = tapechar_check_idalbuffer(device, block_size);
  136. if (rc)
  137. return rc;
  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. DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
  187. DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
  188. /* Let the discipline build the ccw chain. */
  189. request = device->discipline->write_block(device, block_size);
  190. if (IS_ERR(request))
  191. return PTR_ERR(request);
  192. rc = 0;
  193. written = 0;
  194. for (i = 0; i < nblocks; i++) {
  195. /* Copy data from user space to idal buffer. */
  196. if (idal_buffer_from_user(device->char_data.idal_buf,
  197. data, block_size)) {
  198. rc = -EFAULT;
  199. break;
  200. }
  201. rc = tape_do_io(device, request);
  202. if (rc)
  203. break;
  204. DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
  205. block_size - request->rescnt);
  206. written += block_size - request->rescnt;
  207. if (request->rescnt != 0)
  208. break;
  209. data += block_size;
  210. }
  211. tape_free_request(request);
  212. if (rc == -ENOSPC) {
  213. /*
  214. * Ok, the device has no more space. It has NOT written
  215. * the block.
  216. */
  217. if (device->discipline->process_eov)
  218. device->discipline->process_eov(device);
  219. if (written > 0)
  220. rc = 0;
  221. }
  222. /*
  223. * After doing a write we always need two tapemarks to correctly
  224. * terminate the tape (one to terminate the file, the second to
  225. * flag the end of recorded data.
  226. * Since process_eov positions the tape in front of the written
  227. * tapemark it doesn't hurt to write two marks again.
  228. */
  229. if (!rc)
  230. device->required_tapemarks = 2;
  231. return rc ? rc : written;
  232. }
  233. /*
  234. * Character frontend tape device open function.
  235. */
  236. static int
  237. tapechar_open (struct inode *inode, struct file *filp)
  238. {
  239. struct tape_device *device;
  240. int minor, rc;
  241. DBF_EVENT(6, "TCHAR:open: %i:%i\n",
  242. imajor(file_inode(filp)),
  243. iminor(file_inode(filp)));
  244. if (imajor(file_inode(filp)) != tapechar_major)
  245. return -ENODEV;
  246. minor = iminor(file_inode(filp));
  247. device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
  248. if (IS_ERR(device)) {
  249. DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
  250. return PTR_ERR(device);
  251. }
  252. rc = tape_open(device);
  253. if (rc == 0) {
  254. filp->private_data = device;
  255. nonseekable_open(inode, filp);
  256. } else
  257. tape_put_device(device);
  258. return rc;
  259. }
  260. /*
  261. * Character frontend tape device release function.
  262. */
  263. static int
  264. tapechar_release(struct inode *inode, struct file *filp)
  265. {
  266. struct tape_device *device;
  267. DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
  268. device = (struct tape_device *) filp->private_data;
  269. /*
  270. * If this is the rewinding tape minor then rewind. In that case we
  271. * write all required tapemarks. Otherwise only one to terminate the
  272. * file.
  273. */
  274. if ((iminor(inode) & 1) != 0) {
  275. if (device->required_tapemarks)
  276. tape_std_terminate_write(device);
  277. tape_mtop(device, MTREW, 1);
  278. } else {
  279. if (device->required_tapemarks > 1) {
  280. if (tape_mtop(device, MTWEOF, 1) == 0)
  281. device->required_tapemarks--;
  282. }
  283. }
  284. if (device->char_data.idal_buf != NULL) {
  285. idal_buffer_free(device->char_data.idal_buf);
  286. device->char_data.idal_buf = NULL;
  287. }
  288. tape_release(device);
  289. filp->private_data = NULL;
  290. tape_put_device(device);
  291. return 0;
  292. }
  293. /*
  294. * Tape device io controls.
  295. */
  296. static int
  297. __tapechar_ioctl(struct tape_device *device,
  298. unsigned int no, unsigned long data)
  299. {
  300. int rc;
  301. if (no == MTIOCTOP) {
  302. struct mtop op;
  303. if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
  304. return -EFAULT;
  305. if (op.mt_count < 0)
  306. return -EINVAL;
  307. /*
  308. * Operations that change tape position should write final
  309. * tapemarks.
  310. */
  311. switch (op.mt_op) {
  312. case MTFSF:
  313. case MTBSF:
  314. case MTFSR:
  315. case MTBSR:
  316. case MTREW:
  317. case MTOFFL:
  318. case MTEOM:
  319. case MTRETEN:
  320. case MTBSFM:
  321. case MTFSFM:
  322. case MTSEEK:
  323. if (device->required_tapemarks)
  324. tape_std_terminate_write(device);
  325. default:
  326. ;
  327. }
  328. rc = tape_mtop(device, op.mt_op, op.mt_count);
  329. if (op.mt_op == MTWEOF && rc == 0) {
  330. if (op.mt_count > device->required_tapemarks)
  331. device->required_tapemarks = 0;
  332. else
  333. device->required_tapemarks -= op.mt_count;
  334. }
  335. return rc;
  336. }
  337. if (no == MTIOCPOS) {
  338. /* MTIOCPOS: query the tape position. */
  339. struct mtpos pos;
  340. rc = tape_mtop(device, MTTELL, 1);
  341. if (rc < 0)
  342. return rc;
  343. pos.mt_blkno = rc;
  344. if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
  345. return -EFAULT;
  346. return 0;
  347. }
  348. if (no == MTIOCGET) {
  349. /* MTIOCGET: query the tape drive status. */
  350. struct mtget get;
  351. memset(&get, 0, sizeof(get));
  352. get.mt_type = MT_ISUNKNOWN;
  353. get.mt_resid = 0 /* device->devstat.rescnt */;
  354. get.mt_dsreg =
  355. ((device->char_data.block_size << MT_ST_BLKSIZE_SHIFT)
  356. & MT_ST_BLKSIZE_MASK);
  357. /* FIXME: mt_gstat, mt_erreg, mt_fileno */
  358. get.mt_gstat = 0;
  359. get.mt_erreg = 0;
  360. get.mt_fileno = 0;
  361. get.mt_gstat = device->tape_generic_status;
  362. if (device->medium_state == MS_LOADED) {
  363. rc = tape_mtop(device, MTTELL, 1);
  364. if (rc < 0)
  365. return rc;
  366. if (rc == 0)
  367. get.mt_gstat |= GMT_BOT(~0);
  368. get.mt_blkno = rc;
  369. }
  370. if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
  371. return -EFAULT;
  372. return 0;
  373. }
  374. /* Try the discipline ioctl function. */
  375. if (device->discipline->ioctl_fn == NULL)
  376. return -EINVAL;
  377. return device->discipline->ioctl_fn(device, no, data);
  378. }
  379. static long
  380. tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
  381. {
  382. struct tape_device *device;
  383. long rc;
  384. DBF_EVENT(6, "TCHAR:ioct\n");
  385. device = (struct tape_device *) filp->private_data;
  386. mutex_lock(&device->mutex);
  387. rc = __tapechar_ioctl(device, no, data);
  388. mutex_unlock(&device->mutex);
  389. return rc;
  390. }
  391. #ifdef CONFIG_COMPAT
  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. unsigned long argp;
  398. /* The 'arg' argument of any ioctl function may only be used for
  399. * pointers because of the compat pointer conversion.
  400. * Consider this when adding new ioctls.
  401. */
  402. argp = (unsigned long) compat_ptr(data);
  403. if (device->discipline->ioctl_fn) {
  404. mutex_lock(&device->mutex);
  405. rval = device->discipline->ioctl_fn(device, no, argp);
  406. mutex_unlock(&device->mutex);
  407. if (rval == -EINVAL)
  408. rval = -ENOIOCTLCMD;
  409. }
  410. return rval;
  411. }
  412. #endif /* CONFIG_COMPAT */
  413. /*
  414. * Initialize character device frontend.
  415. */
  416. int
  417. tapechar_init (void)
  418. {
  419. dev_t dev;
  420. if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
  421. return -1;
  422. tapechar_major = MAJOR(dev);
  423. return 0;
  424. }
  425. /*
  426. * cleanup
  427. */
  428. void
  429. tapechar_exit(void)
  430. {
  431. unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
  432. }