zftape-init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (C) 1996, 1997 Claus-Justus Heine.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING. If not, write to
  13. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14. *
  15. * This file contains the code that registers the zftape frontend
  16. * to the ftape floppy tape driver for Linux
  17. */
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/kernel.h>
  22. #include <linux/signal.h>
  23. #include <linux/major.h>
  24. #include <linux/slab.h>
  25. #ifdef CONFIG_KMOD
  26. #include <linux/kmod.h>
  27. #endif
  28. #include <linux/fcntl.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/zftape.h>
  31. #include <linux/init.h>
  32. #include <linux/device.h>
  33. #include "../zftape/zftape-init.h"
  34. #include "../zftape/zftape-read.h"
  35. #include "../zftape/zftape-write.h"
  36. #include "../zftape/zftape-ctl.h"
  37. #include "../zftape/zftape-buffers.h"
  38. MODULE_AUTHOR("(c) 1996, 1997 Claus-Justus Heine "
  39. "(claus@momo.math.rwth-aachen.de)");
  40. MODULE_DESCRIPTION(ZFTAPE_VERSION " - "
  41. "VFS interface for the Linux floppy tape driver. "
  42. "Support for QIC-113 compatible volume table "
  43. "and builtin compression (lzrw3 algorithm)");
  44. MODULE_SUPPORTED_DEVICE("char-major-27");
  45. MODULE_LICENSE("GPL");
  46. /* Global vars.
  47. */
  48. struct zft_cmpr_ops *zft_cmpr_ops = NULL;
  49. const ftape_info *zft_status;
  50. /* Local vars.
  51. */
  52. static unsigned long busy_flag;
  53. static sigset_t orig_sigmask;
  54. /* the interface to the kernel vfs layer
  55. */
  56. /* Note about llseek():
  57. *
  58. * st.c and tpqic.c update fp->f_pos but don't implment llseek() and
  59. * initialize the llseek component of the file_ops struct with NULL.
  60. * This means that the user will get the default seek, but the tape
  61. * device will not respect the new position, but happily read from the
  62. * old position. Think a zftape specific llseek() function would be
  63. * better, returning -ESPIPE. TODO.
  64. */
  65. static int zft_open (struct inode *ino, struct file *filep);
  66. static int zft_close(struct inode *ino, struct file *filep);
  67. static int zft_ioctl(struct inode *ino, struct file *filep,
  68. unsigned int command, unsigned long arg);
  69. static int zft_mmap(struct file *filep, struct vm_area_struct *vma);
  70. static ssize_t zft_read (struct file *fp, char __user *buff,
  71. size_t req_len, loff_t *ppos);
  72. static ssize_t zft_write(struct file *fp, const char __user *buff,
  73. size_t req_len, loff_t *ppos);
  74. static const struct file_operations zft_cdev =
  75. {
  76. .owner = THIS_MODULE,
  77. .read = zft_read,
  78. .write = zft_write,
  79. .ioctl = zft_ioctl,
  80. .mmap = zft_mmap,
  81. .open = zft_open,
  82. .release = zft_close,
  83. };
  84. static struct class *zft_class;
  85. /* Open floppy tape device
  86. */
  87. static int zft_open(struct inode *ino, struct file *filep)
  88. {
  89. int result;
  90. TRACE_FUN(ft_t_flow);
  91. nonseekable_open(ino, filep);
  92. TRACE(ft_t_flow, "called for minor %d", iminor(ino));
  93. if ( test_and_set_bit(0,&busy_flag) ) {
  94. TRACE_ABORT(-EBUSY, ft_t_warn, "failed: already busy");
  95. }
  96. if ((iminor(ino) & ~(ZFT_MINOR_OP_MASK | FTAPE_NO_REWIND))
  97. >
  98. FTAPE_SEL_D) {
  99. clear_bit(0,&busy_flag);
  100. TRACE_ABORT(-ENXIO, ft_t_err, "failed: invalid unit nr");
  101. }
  102. orig_sigmask = current->blocked;
  103. sigfillset(&current->blocked);
  104. result = _zft_open(iminor(ino), filep->f_flags & O_ACCMODE);
  105. if (result < 0) {
  106. current->blocked = orig_sigmask; /* restore mask */
  107. clear_bit(0,&busy_flag);
  108. TRACE_ABORT(result, ft_t_err, "_ftape_open failed");
  109. } else {
  110. /* Mask signals that will disturb proper operation of the
  111. * program that is calling.
  112. */
  113. current->blocked = orig_sigmask;
  114. sigaddsetmask (&current->blocked, _DO_BLOCK);
  115. TRACE_EXIT 0;
  116. }
  117. }
  118. /* Close floppy tape device
  119. */
  120. static int zft_close(struct inode *ino, struct file *filep)
  121. {
  122. int result;
  123. TRACE_FUN(ft_t_flow);
  124. if ( !test_bit(0,&busy_flag) || iminor(ino) != zft_unit) {
  125. TRACE(ft_t_err, "failed: not busy or wrong unit");
  126. TRACE_EXIT 0;
  127. }
  128. sigfillset(&current->blocked);
  129. result = _zft_close();
  130. if (result < 0) {
  131. TRACE(ft_t_err, "_zft_close failed");
  132. }
  133. current->blocked = orig_sigmask; /* restore before open state */
  134. clear_bit(0,&busy_flag);
  135. TRACE_EXIT 0;
  136. }
  137. /* Ioctl for floppy tape device
  138. */
  139. static int zft_ioctl(struct inode *ino, struct file *filep,
  140. unsigned int command, unsigned long arg)
  141. {
  142. int result = -EIO;
  143. sigset_t old_sigmask;
  144. TRACE_FUN(ft_t_flow);
  145. if ( !test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
  146. TRACE_ABORT(-EIO, ft_t_err,
  147. "failed: not busy, failure or wrong unit");
  148. }
  149. old_sigmask = current->blocked; /* save mask */
  150. sigfillset(&current->blocked);
  151. /* This will work as long as sizeof(void *) == sizeof(long) */
  152. result = _zft_ioctl(command, (void __user *) arg);
  153. current->blocked = old_sigmask; /* restore mask */
  154. TRACE_EXIT result;
  155. }
  156. /* Ioctl for floppy tape device
  157. */
  158. static int zft_mmap(struct file *filep, struct vm_area_struct *vma)
  159. {
  160. int result = -EIO;
  161. sigset_t old_sigmask;
  162. TRACE_FUN(ft_t_flow);
  163. if ( !test_bit(0,&busy_flag) ||
  164. iminor(filep->f_dentry->d_inode) != zft_unit ||
  165. ft_failure)
  166. {
  167. TRACE_ABORT(-EIO, ft_t_err,
  168. "failed: not busy, failure or wrong unit");
  169. }
  170. old_sigmask = current->blocked; /* save mask */
  171. sigfillset(&current->blocked);
  172. if ((result = ftape_mmap(vma)) >= 0) {
  173. #ifndef MSYNC_BUG_WAS_FIXED
  174. static struct vm_operations_struct dummy = { NULL, };
  175. vma->vm_ops = &dummy;
  176. #endif
  177. }
  178. current->blocked = old_sigmask; /* restore mask */
  179. TRACE_EXIT result;
  180. }
  181. /* Read from floppy tape device
  182. */
  183. static ssize_t zft_read(struct file *fp, char __user *buff,
  184. size_t req_len, loff_t *ppos)
  185. {
  186. int result = -EIO;
  187. sigset_t old_sigmask;
  188. struct inode *ino = fp->f_dentry->d_inode;
  189. TRACE_FUN(ft_t_flow);
  190. TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
  191. if (!test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
  192. TRACE_ABORT(-EIO, ft_t_err,
  193. "failed: not busy, failure or wrong unit");
  194. }
  195. old_sigmask = current->blocked; /* save mask */
  196. sigfillset(&current->blocked);
  197. result = _zft_read(buff, req_len);
  198. current->blocked = old_sigmask; /* restore mask */
  199. TRACE(ft_t_data_flow, "return with count: %d", result);
  200. TRACE_EXIT result;
  201. }
  202. /* Write to tape device
  203. */
  204. static ssize_t zft_write(struct file *fp, const char __user *buff,
  205. size_t req_len, loff_t *ppos)
  206. {
  207. int result = -EIO;
  208. sigset_t old_sigmask;
  209. struct inode *ino = fp->f_dentry->d_inode;
  210. TRACE_FUN(ft_t_flow);
  211. TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
  212. if (!test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
  213. TRACE_ABORT(-EIO, ft_t_err,
  214. "failed: not busy, failure or wrong unit");
  215. }
  216. old_sigmask = current->blocked; /* save mask */
  217. sigfillset(&current->blocked);
  218. result = _zft_write(buff, req_len);
  219. current->blocked = old_sigmask; /* restore mask */
  220. TRACE(ft_t_data_flow, "return with count: %d", result);
  221. TRACE_EXIT result;
  222. }
  223. /* END OF VFS INTERFACE
  224. *
  225. *****************************************************************************/
  226. /* driver/module initialization
  227. */
  228. /* the compression module has to call this function to hook into the zftape
  229. * code
  230. */
  231. int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
  232. {
  233. TRACE_FUN(ft_t_flow);
  234. if (zft_cmpr_ops != NULL) {
  235. TRACE_EXIT -EBUSY;
  236. } else {
  237. zft_cmpr_ops = new_ops;
  238. TRACE_EXIT 0;
  239. }
  240. }
  241. /* lock the zft-compressor() module.
  242. */
  243. int zft_cmpr_lock(int try_to_load)
  244. {
  245. if (zft_cmpr_ops == NULL) {
  246. #ifdef CONFIG_KMOD
  247. if (try_to_load) {
  248. request_module("zft-compressor");
  249. if (zft_cmpr_ops == NULL) {
  250. return -ENOSYS;
  251. }
  252. } else {
  253. return -ENOSYS;
  254. }
  255. #else
  256. return -ENOSYS;
  257. #endif
  258. }
  259. (*zft_cmpr_ops->lock)();
  260. return 0;
  261. }
  262. #ifdef CONFIG_ZFT_COMPRESSOR
  263. extern int zft_compressor_init(void);
  264. #endif
  265. /* Called by modules package when installing the driver or by kernel
  266. * during the initialization phase
  267. */
  268. int __init zft_init(void)
  269. {
  270. int i;
  271. TRACE_FUN(ft_t_flow);
  272. #ifdef MODULE
  273. printk(KERN_INFO ZFTAPE_VERSION "\n");
  274. if (TRACE_LEVEL >= ft_t_info) {
  275. printk(
  276. KERN_INFO
  277. "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
  278. KERN_INFO
  279. "vfs interface for ftape floppy tape driver.\n"
  280. KERN_INFO
  281. "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
  282. KERN_INFO
  283. "and builtin compression (lzrw3 algorithm).\n");
  284. }
  285. #else /* !MODULE */
  286. /* print a short no-nonsense boot message */
  287. printk(KERN_INFO ZFTAPE_VERSION "\n");
  288. #endif /* MODULE */
  289. TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
  290. TRACE(ft_t_info,
  291. "installing zftape VFS interface for ftape driver ...");
  292. TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
  293. zft_class = class_create(THIS_MODULE, "zft");
  294. for (i = 0; i < 4; i++) {
  295. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i), NULL, "qft%i", i);
  296. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 4), NULL, "nqft%i", i);
  297. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 16), NULL, "zqft%i", i);
  298. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 20), NULL, "nzqft%i", i);
  299. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 32), NULL, "rawqft%i", i);
  300. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 36), NULL, "nrawrawqft%i", i);
  301. }
  302. #ifdef CONFIG_ZFT_COMPRESSOR
  303. (void)zft_compressor_init();
  304. #endif
  305. zft_status = ftape_get_status(); /* fetch global data of ftape
  306. * hardware driver
  307. */
  308. TRACE_EXIT 0;
  309. }
  310. /* Called by modules package when removing the driver
  311. */
  312. static void zft_exit(void)
  313. {
  314. int i;
  315. TRACE_FUN(ft_t_flow);
  316. if (unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
  317. TRACE(ft_t_warn, "failed");
  318. } else {
  319. TRACE(ft_t_info, "successful");
  320. }
  321. for (i = 0; i < 4; i++) {
  322. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i));
  323. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 4));
  324. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 16));
  325. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 20));
  326. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 32));
  327. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 36));
  328. }
  329. class_destroy(zft_class);
  330. zft_uninit_mem(); /* release remaining memory, if any */
  331. printk(KERN_INFO "zftape successfully unloaded.\n");
  332. TRACE_EXIT;
  333. }
  334. module_init(zft_init);
  335. module_exit(zft_exit);