tape_char.c 11 KB

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