tape_char.c 11 KB

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