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/compat.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. #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. #ifdef CONFIG_S390_TAPE_BLOCK
  139. /* Changes position. */
  140. device->blk_data.medium_changed = 1;
  141. #endif
  142. DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
  143. /* Let the discipline build the ccw chain. */
  144. request = device->discipline->read_block(device, block_size);
  145. if (IS_ERR(request))
  146. return PTR_ERR(request);
  147. /* Execute it. */
  148. rc = tape_do_io(device, request);
  149. if (rc == 0) {
  150. rc = block_size - request->rescnt;
  151. DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
  152. /* Copy data from idal buffer to user space. */
  153. if (idal_buffer_to_user(device->char_data.idal_buf,
  154. data, rc) != 0)
  155. rc = -EFAULT;
  156. }
  157. tape_free_request(request);
  158. return rc;
  159. }
  160. /*
  161. * Tape device write function
  162. */
  163. static ssize_t
  164. tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
  165. {
  166. struct tape_device *device;
  167. struct tape_request *request;
  168. size_t block_size;
  169. size_t written;
  170. int nblocks;
  171. int i, rc;
  172. DBF_EVENT(6, "TCHAR:write\n");
  173. device = (struct tape_device *) filp->private_data;
  174. /* Find out block size and number of blocks */
  175. if (device->char_data.block_size != 0) {
  176. if (count < device->char_data.block_size) {
  177. DBF_EVENT(3, "TCHAR:write smaller than block "
  178. "size was requested\n");
  179. return -EINVAL;
  180. }
  181. block_size = device->char_data.block_size;
  182. nblocks = count / block_size;
  183. } else {
  184. block_size = count;
  185. nblocks = 1;
  186. }
  187. rc = tapechar_check_idalbuffer(device, block_size);
  188. if (rc)
  189. return rc;
  190. #ifdef CONFIG_S390_TAPE_BLOCK
  191. /* Changes position. */
  192. device->blk_data.medium_changed = 1;
  193. #endif
  194. DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
  195. DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
  196. /* Let the discipline build the ccw chain. */
  197. request = device->discipline->write_block(device, block_size);
  198. if (IS_ERR(request))
  199. return PTR_ERR(request);
  200. rc = 0;
  201. written = 0;
  202. for (i = 0; i < nblocks; i++) {
  203. /* Copy data from user space to idal buffer. */
  204. if (idal_buffer_from_user(device->char_data.idal_buf,
  205. data, block_size)) {
  206. rc = -EFAULT;
  207. break;
  208. }
  209. rc = tape_do_io(device, request);
  210. if (rc)
  211. break;
  212. DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
  213. block_size - request->rescnt);
  214. written += block_size - request->rescnt;
  215. if (request->rescnt != 0)
  216. break;
  217. data += block_size;
  218. }
  219. tape_free_request(request);
  220. if (rc == -ENOSPC) {
  221. /*
  222. * Ok, the device has no more space. It has NOT written
  223. * the block.
  224. */
  225. if (device->discipline->process_eov)
  226. device->discipline->process_eov(device);
  227. if (written > 0)
  228. rc = 0;
  229. }
  230. /*
  231. * After doing a write we always need two tapemarks to correctly
  232. * terminate the tape (one to terminate the file, the second to
  233. * flag the end of recorded data.
  234. * Since process_eov positions the tape in front of the written
  235. * tapemark it doesn't hurt to write two marks again.
  236. */
  237. if (!rc)
  238. device->required_tapemarks = 2;
  239. return rc ? rc : written;
  240. }
  241. /*
  242. * Character frontend tape device open function.
  243. */
  244. static int
  245. tapechar_open (struct inode *inode, struct file *filp)
  246. {
  247. struct tape_device *device;
  248. int minor, rc;
  249. DBF_EVENT(6, "TCHAR:open: %i:%i\n",
  250. imajor(filp->f_path.dentry->d_inode),
  251. iminor(filp->f_path.dentry->d_inode));
  252. if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
  253. return -ENODEV;
  254. minor = iminor(filp->f_path.dentry->d_inode);
  255. device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
  256. if (IS_ERR(device)) {
  257. DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
  258. return PTR_ERR(device);
  259. }
  260. rc = tape_open(device);
  261. if (rc == 0) {
  262. filp->private_data = device;
  263. nonseekable_open(inode, filp);
  264. } else
  265. tape_put_device(device);
  266. return rc;
  267. }
  268. /*
  269. * Character frontend tape device release function.
  270. */
  271. static int
  272. tapechar_release(struct inode *inode, struct file *filp)
  273. {
  274. struct tape_device *device;
  275. DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
  276. device = (struct tape_device *) filp->private_data;
  277. /*
  278. * If this is the rewinding tape minor then rewind. In that case we
  279. * write all required tapemarks. Otherwise only one to terminate the
  280. * file.
  281. */
  282. if ((iminor(inode) & 1) != 0) {
  283. if (device->required_tapemarks)
  284. tape_std_terminate_write(device);
  285. tape_mtop(device, MTREW, 1);
  286. } else {
  287. if (device->required_tapemarks > 1) {
  288. if (tape_mtop(device, MTWEOF, 1) == 0)
  289. device->required_tapemarks--;
  290. }
  291. }
  292. if (device->char_data.idal_buf != NULL) {
  293. idal_buffer_free(device->char_data.idal_buf);
  294. device->char_data.idal_buf = NULL;
  295. }
  296. tape_release(device);
  297. filp->private_data = NULL;
  298. tape_put_device(device);
  299. return 0;
  300. }
  301. /*
  302. * Tape device io controls.
  303. */
  304. static int
  305. __tapechar_ioctl(struct tape_device *device,
  306. unsigned int no, unsigned long data)
  307. {
  308. int rc;
  309. if (no == MTIOCTOP) {
  310. struct mtop op;
  311. if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
  312. return -EFAULT;
  313. if (op.mt_count < 0)
  314. return -EINVAL;
  315. /*
  316. * Operations that change tape position should write final
  317. * tapemarks.
  318. */
  319. switch (op.mt_op) {
  320. case MTFSF:
  321. case MTBSF:
  322. case MTFSR:
  323. case MTBSR:
  324. case MTREW:
  325. case MTOFFL:
  326. case MTEOM:
  327. case MTRETEN:
  328. case MTBSFM:
  329. case MTFSFM:
  330. case MTSEEK:
  331. #ifdef CONFIG_S390_TAPE_BLOCK
  332. device->blk_data.medium_changed = 1;
  333. #endif
  334. if (device->required_tapemarks)
  335. tape_std_terminate_write(device);
  336. default:
  337. ;
  338. }
  339. rc = tape_mtop(device, op.mt_op, op.mt_count);
  340. if (op.mt_op == MTWEOF && rc == 0) {
  341. if (op.mt_count > device->required_tapemarks)
  342. device->required_tapemarks = 0;
  343. else
  344. device->required_tapemarks -= op.mt_count;
  345. }
  346. return rc;
  347. }
  348. if (no == MTIOCPOS) {
  349. /* MTIOCPOS: query the tape position. */
  350. struct mtpos pos;
  351. rc = tape_mtop(device, MTTELL, 1);
  352. if (rc < 0)
  353. return rc;
  354. pos.mt_blkno = rc;
  355. if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
  356. return -EFAULT;
  357. return 0;
  358. }
  359. if (no == MTIOCGET) {
  360. /* MTIOCGET: query the tape drive status. */
  361. struct mtget get;
  362. memset(&get, 0, sizeof(get));
  363. get.mt_type = MT_ISUNKNOWN;
  364. get.mt_resid = 0 /* device->devstat.rescnt */;
  365. get.mt_dsreg = device->tape_state;
  366. /* FIXME: mt_gstat, mt_erreg, mt_fileno */
  367. get.mt_gstat = 0;
  368. get.mt_erreg = 0;
  369. get.mt_fileno = 0;
  370. get.mt_gstat = device->tape_generic_status;
  371. if (device->medium_state == MS_LOADED) {
  372. rc = tape_mtop(device, MTTELL, 1);
  373. if (rc < 0)
  374. return rc;
  375. if (rc == 0)
  376. get.mt_gstat |= GMT_BOT(~0);
  377. get.mt_blkno = rc;
  378. }
  379. if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
  380. return -EFAULT;
  381. return 0;
  382. }
  383. /* Try the discipline ioctl function. */
  384. if (device->discipline->ioctl_fn == NULL)
  385. return -EINVAL;
  386. return device->discipline->ioctl_fn(device, no, data);
  387. }
  388. static long
  389. tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
  390. {
  391. struct tape_device *device;
  392. long rc;
  393. DBF_EVENT(6, "TCHAR:ioct\n");
  394. device = (struct tape_device *) filp->private_data;
  395. mutex_lock(&device->mutex);
  396. rc = __tapechar_ioctl(device, no, data);
  397. mutex_unlock(&device->mutex);
  398. return rc;
  399. }
  400. #ifdef CONFIG_COMPAT
  401. static long
  402. tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
  403. {
  404. struct tape_device *device = filp->private_data;
  405. int rval = -ENOIOCTLCMD;
  406. unsigned long argp;
  407. /* The 'arg' argument of any ioctl function may only be used for
  408. * pointers because of the compat pointer conversion.
  409. * Consider this when adding new ioctls.
  410. */
  411. argp = (unsigned long) compat_ptr(data);
  412. if (device->discipline->ioctl_fn) {
  413. mutex_lock(&device->mutex);
  414. rval = device->discipline->ioctl_fn(device, no, argp);
  415. mutex_unlock(&device->mutex);
  416. if (rval == -EINVAL)
  417. rval = -ENOIOCTLCMD;
  418. }
  419. return rval;
  420. }
  421. #endif /* CONFIG_COMPAT */
  422. /*
  423. * Initialize character device frontend.
  424. */
  425. int
  426. tapechar_init (void)
  427. {
  428. dev_t dev;
  429. if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
  430. return -1;
  431. tapechar_major = MAJOR(dev);
  432. return 0;
  433. }
  434. /*
  435. * cleanup
  436. */
  437. void
  438. tapechar_exit(void)
  439. {
  440. unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
  441. }