tape_char.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <linux/compat.h>
  20. #include <asm/uaccess.h>
  21. #define TAPE_DBF_AREA tape_core_dbf
  22. #include "tape.h"
  23. #include "tape_std.h"
  24. #include "tape_class.h"
  25. #define TAPECHAR_MAJOR 0 /* get dynamic major */
  26. /*
  27. * file operation structure for tape character frontend
  28. */
  29. static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
  30. static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
  31. static int tapechar_open(struct inode *,struct file *);
  32. static int tapechar_release(struct inode *,struct file *);
  33. static long tapechar_ioctl(struct file *, unsigned int, unsigned long);
  34. #ifdef CONFIG_COMPAT
  35. static long tapechar_compat_ioctl(struct file *, unsigned int, unsigned long);
  36. #endif
  37. static const struct file_operations tape_fops =
  38. {
  39. .owner = THIS_MODULE,
  40. .read = tapechar_read,
  41. .write = tapechar_write,
  42. .unlocked_ioctl = tapechar_ioctl,
  43. #ifdef CONFIG_COMPAT
  44. .compat_ioctl = tapechar_compat_ioctl,
  45. #endif
  46. .open = tapechar_open,
  47. .release = tapechar_release,
  48. .llseek = no_llseek,
  49. };
  50. static int tapechar_major = TAPECHAR_MAJOR;
  51. /*
  52. * This function is called for every new tapedevice
  53. */
  54. int
  55. tapechar_setup_device(struct tape_device * device)
  56. {
  57. char device_name[20];
  58. sprintf(device_name, "ntibm%i", device->first_minor / 2);
  59. device->nt = register_tape_dev(
  60. &device->cdev->dev,
  61. MKDEV(tapechar_major, device->first_minor),
  62. &tape_fops,
  63. device_name,
  64. "non-rewinding"
  65. );
  66. device_name[0] = 'r';
  67. device->rt = register_tape_dev(
  68. &device->cdev->dev,
  69. MKDEV(tapechar_major, device->first_minor + 1),
  70. &tape_fops,
  71. device_name,
  72. "rewinding"
  73. );
  74. return 0;
  75. }
  76. void
  77. tapechar_cleanup_device(struct tape_device *device)
  78. {
  79. unregister_tape_dev(&device->cdev->dev, device->rt);
  80. device->rt = NULL;
  81. unregister_tape_dev(&device->cdev->dev, device->nt);
  82. device->nt = NULL;
  83. }
  84. static int
  85. tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
  86. {
  87. struct idal_buffer *new;
  88. if (device->char_data.idal_buf != NULL &&
  89. device->char_data.idal_buf->size == block_size)
  90. return 0;
  91. if (block_size > MAX_BLOCKSIZE) {
  92. DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
  93. block_size, MAX_BLOCKSIZE);
  94. return -EINVAL;
  95. }
  96. /* The current idal buffer is not correct. Allocate a new one. */
  97. new = idal_buffer_alloc(block_size, 0);
  98. if (IS_ERR(new))
  99. return -ENOMEM;
  100. if (device->char_data.idal_buf != NULL)
  101. idal_buffer_free(device->char_data.idal_buf);
  102. device->char_data.idal_buf = new;
  103. return 0;
  104. }
  105. /*
  106. * Tape device read function
  107. */
  108. static ssize_t
  109. tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
  110. {
  111. struct tape_device *device;
  112. struct tape_request *request;
  113. size_t block_size;
  114. int rc;
  115. DBF_EVENT(6, "TCHAR:read\n");
  116. device = (struct tape_device *) filp->private_data;
  117. /*
  118. * If the tape isn't terminated yet, do it now. And since we then
  119. * are at the end of the tape there wouldn't be anything to read
  120. * anyways. So we return immediatly.
  121. */
  122. if(device->required_tapemarks) {
  123. return tape_std_terminate_write(device);
  124. }
  125. /* Find out block size to use */
  126. if (device->char_data.block_size != 0) {
  127. if (count < device->char_data.block_size) {
  128. DBF_EVENT(3, "TCHAR:read smaller than block "
  129. "size was requested\n");
  130. return -EINVAL;
  131. }
  132. block_size = device->char_data.block_size;
  133. } else {
  134. block_size = count;
  135. }
  136. rc = tapechar_check_idalbuffer(device, block_size);
  137. if (rc)
  138. return rc;
  139. #ifdef CONFIG_S390_TAPE_BLOCK
  140. /* Changes position. */
  141. device->blk_data.medium_changed = 1;
  142. #endif
  143. DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
  144. /* Let the discipline build the ccw chain. */
  145. request = device->discipline->read_block(device, block_size);
  146. if (IS_ERR(request))
  147. return PTR_ERR(request);
  148. /* Execute it. */
  149. rc = tape_do_io(device, request);
  150. if (rc == 0) {
  151. rc = block_size - request->rescnt;
  152. DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
  153. /* Copy data from idal buffer to user space. */
  154. if (idal_buffer_to_user(device->char_data.idal_buf,
  155. data, rc) != 0)
  156. rc = -EFAULT;
  157. }
  158. tape_free_request(request);
  159. return rc;
  160. }
  161. /*
  162. * Tape device write function
  163. */
  164. static ssize_t
  165. tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
  166. {
  167. struct tape_device *device;
  168. struct tape_request *request;
  169. size_t block_size;
  170. size_t written;
  171. int nblocks;
  172. int i, rc;
  173. DBF_EVENT(6, "TCHAR:write\n");
  174. device = (struct tape_device *) filp->private_data;
  175. /* Find out block size and number of blocks */
  176. if (device->char_data.block_size != 0) {
  177. if (count < device->char_data.block_size) {
  178. DBF_EVENT(3, "TCHAR:write smaller than block "
  179. "size was requested\n");
  180. return -EINVAL;
  181. }
  182. block_size = device->char_data.block_size;
  183. nblocks = count / block_size;
  184. } else {
  185. block_size = count;
  186. nblocks = 1;
  187. }
  188. rc = tapechar_check_idalbuffer(device, block_size);
  189. if (rc)
  190. return rc;
  191. #ifdef CONFIG_S390_TAPE_BLOCK
  192. /* Changes position. */
  193. device->blk_data.medium_changed = 1;
  194. #endif
  195. DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
  196. DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
  197. /* Let the discipline build the ccw chain. */
  198. request = device->discipline->write_block(device, block_size);
  199. if (IS_ERR(request))
  200. return PTR_ERR(request);
  201. rc = 0;
  202. written = 0;
  203. for (i = 0; i < nblocks; i++) {
  204. /* Copy data from user space to idal buffer. */
  205. if (idal_buffer_from_user(device->char_data.idal_buf,
  206. data, block_size)) {
  207. rc = -EFAULT;
  208. break;
  209. }
  210. rc = tape_do_io(device, request);
  211. if (rc)
  212. break;
  213. DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
  214. block_size - request->rescnt);
  215. written += block_size - request->rescnt;
  216. if (request->rescnt != 0)
  217. break;
  218. data += block_size;
  219. }
  220. tape_free_request(request);
  221. if (rc == -ENOSPC) {
  222. /*
  223. * Ok, the device has no more space. It has NOT written
  224. * the block.
  225. */
  226. if (device->discipline->process_eov)
  227. device->discipline->process_eov(device);
  228. if (written > 0)
  229. rc = 0;
  230. }
  231. /*
  232. * After doing a write we always need two tapemarks to correctly
  233. * terminate the tape (one to terminate the file, the second to
  234. * flag the end of recorded data.
  235. * Since process_eov positions the tape in front of the written
  236. * tapemark it doesn't hurt to write two marks again.
  237. */
  238. if (!rc)
  239. device->required_tapemarks = 2;
  240. return rc ? rc : written;
  241. }
  242. /*
  243. * Character frontend tape device open function.
  244. */
  245. static int
  246. tapechar_open (struct inode *inode, struct file *filp)
  247. {
  248. struct tape_device *device;
  249. int minor, rc;
  250. DBF_EVENT(6, "TCHAR:open: %i:%i\n",
  251. imajor(filp->f_path.dentry->d_inode),
  252. iminor(filp->f_path.dentry->d_inode));
  253. if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
  254. return -ENODEV;
  255. minor = iminor(filp->f_path.dentry->d_inode);
  256. device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
  257. if (IS_ERR(device)) {
  258. DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
  259. return PTR_ERR(device);
  260. }
  261. rc = tape_open(device);
  262. if (rc == 0) {
  263. filp->private_data = device;
  264. nonseekable_open(inode, filp);
  265. } else
  266. tape_put_device(device);
  267. return rc;
  268. }
  269. /*
  270. * Character frontend tape device release function.
  271. */
  272. static int
  273. tapechar_release(struct inode *inode, struct file *filp)
  274. {
  275. struct tape_device *device;
  276. DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
  277. device = (struct tape_device *) filp->private_data;
  278. /*
  279. * If this is the rewinding tape minor then rewind. In that case we
  280. * write all required tapemarks. Otherwise only one to terminate the
  281. * file.
  282. */
  283. if ((iminor(inode) & 1) != 0) {
  284. if (device->required_tapemarks)
  285. tape_std_terminate_write(device);
  286. tape_mtop(device, MTREW, 1);
  287. } else {
  288. if (device->required_tapemarks > 1) {
  289. if (tape_mtop(device, MTWEOF, 1) == 0)
  290. device->required_tapemarks--;
  291. }
  292. }
  293. if (device->char_data.idal_buf != NULL) {
  294. idal_buffer_free(device->char_data.idal_buf);
  295. device->char_data.idal_buf = NULL;
  296. }
  297. tape_release(device);
  298. filp->private_data = NULL;
  299. tape_put_device(device);
  300. return 0;
  301. }
  302. /*
  303. * Tape device io controls.
  304. */
  305. static int
  306. __tapechar_ioctl(struct tape_device *device,
  307. unsigned int no, unsigned long data)
  308. {
  309. int rc;
  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_ioctl(struct file *filp, unsigned int no, unsigned long data)
  391. {
  392. struct tape_device *device;
  393. long rc;
  394. DBF_EVENT(6, "TCHAR:ioct\n");
  395. device = (struct tape_device *) filp->private_data;
  396. mutex_lock(&device->mutex);
  397. rc = __tapechar_ioctl(device, no, data);
  398. mutex_unlock(&device->mutex);
  399. return rc;
  400. }
  401. #ifdef CONFIG_COMPAT
  402. static long
  403. tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
  404. {
  405. struct tape_device *device = filp->private_data;
  406. int rval = -ENOIOCTLCMD;
  407. unsigned long argp;
  408. /* The 'arg' argument of any ioctl function may only be used for
  409. * pointers because of the compat pointer conversion.
  410. * Consider this when adding new ioctls.
  411. */
  412. argp = (unsigned long) compat_ptr(data);
  413. if (device->discipline->ioctl_fn) {
  414. mutex_lock(&device->mutex);
  415. rval = device->discipline->ioctl_fn(device, no, argp);
  416. mutex_unlock(&device->mutex);
  417. if (rval == -EINVAL)
  418. rval = -ENOIOCTLCMD;
  419. }
  420. return rval;
  421. }
  422. #endif /* CONFIG_COMPAT */
  423. /*
  424. * Initialize character device frontend.
  425. */
  426. int
  427. tapechar_init (void)
  428. {
  429. dev_t dev;
  430. if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
  431. return -1;
  432. tapechar_major = MAJOR(dev);
  433. return 0;
  434. }
  435. /*
  436. * cleanup
  437. */
  438. void
  439. tapechar_exit(void)
  440. {
  441. unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
  442. }