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