2 * Copyright (C) 1996, 1997 Claus-Justus Heine.
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)
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.
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.
19 * This file contains the code that registers the zftape frontend
20 * to the ftape floppy tape driver for Linux
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/version.h>
28 #include <asm/segment.h>
29 #include <linux/kernel.h>
30 #include <linux/signal.h>
31 #include <linux/major.h>
32 #include <linux/malloc.h>
34 #include <linux/kmod.h>
36 #include <linux/fcntl.h>
37 #include <linux/wrapper.h>
39 #include <linux/zftape.h>
40 #if LINUX_VERSION_CODE >=KERNEL_VER(2,1,16)
41 #include <linux/init.h>
44 #define __initfunc(__arg) __arg
47 #include "../zftape/zftape-init.h"
48 #include "../zftape/zftape-read.h"
49 #include "../zftape/zftape-write.h"
50 #include "../zftape/zftape-ctl.h"
51 #include "../zftape/zftape-buffers.h"
52 #include "../zftape/zftape_syms.h"
54 char zft_src
[] __initdata
= "$Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-init.c,v $";
55 char zft_rev
[] __initdata
= "$Revision: 1.8 $";
56 char zft_dat
[] __initdata
= "$Date: 1997/11/06 00:48:56 $";
58 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
59 MODULE_AUTHOR("(c) 1996, 1997 Claus-Justus Heine "
60 "(claus@momo.math.rwth-aachen.de)");
61 MODULE_DESCRIPTION(ZFTAPE_VERSION
" - "
62 "VFS interface for the Linux floppy tape driver. "
63 "Support for QIC-113 compatible volume table "
64 "and builtin compression (lzrw3 algorithm)");
65 MODULE_SUPPORTED_DEVICE("char-major-27");
70 struct zft_cmpr_ops
*zft_cmpr_ops
= NULL
;
71 const ftape_info
*zft_status
;
75 static int busy_flag
= 0;
76 static sigset_t orig_sigmask
;
78 /* the interface to the kernel vfs layer
81 /* Note about llseek():
83 * st.c and tpqic.c update fp->f_pos but don't implment llseek() and
84 * initialize the llseek component of the file_ops struct with NULL.
85 * This means that the user will get the default seek, but the tape
86 * device will not respect the new position, but happily read from the
87 * old position. Think a zftape specific llseek() function would be
88 * better, returning -ESPIPE. TODO.
91 static int zft_open (struct inode
*ino
, struct file
*filep
);
92 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
93 static int zft_close(struct inode
*ino
, struct file
*filep
);
95 static void zft_close(struct inode
*ino
, struct file
*filep
);
97 static int zft_ioctl(struct inode
*ino
, struct file
*filep
,
98 unsigned int command
, unsigned long arg
);
99 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
100 static int zft_mmap(struct file
*filep
, struct vm_area_struct
*vma
);
102 static int zft_mmap(struct inode
*ino
, struct file
*filep
,
103 struct vm_area_struct
*vma
);
105 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
106 static ssize_t
zft_read (struct file
*fp
, char *buff
,
107 size_t req_len
, loff_t
*ppos
);
108 static ssize_t
zft_write(struct file
*fp
, const char *buff
,
109 size_t req_len
, loff_t
*ppos
);
110 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
111 static long zft_read (struct inode
*ino
, struct file
*fp
, char *buff
,
112 unsigned long req_len
);
113 static long zft_write(struct inode
*ino
, struct file
*fp
, const char *buff
,
114 unsigned long req_len
);
116 static int zft_read (struct inode
*ino
, struct file
*fp
, char *buff
,
118 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
119 static int zft_write(struct inode
*ino
, struct file
*fp
, const char *buff
,
122 static int zft_write(struct inode
*ino
, struct file
*fp
, char *buff
,
127 static struct file_operations zft_cdev
=
131 zft_write
, /* write */
134 zft_ioctl
, /* ioctl */
138 zft_close
, /* release */
142 /* Open floppy tape device
144 static int zft_open(struct inode
*ino
, struct file
*filep
)
147 TRACE_FUN(ft_t_flow
);
149 #if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
151 MOD_INC_USE_COUNT
; /* lock module in memory */
154 MOD_INC_USE_COUNT
; /* sets MOD_VISITED and MOD_USED_ONCE,
155 * locking is done with can_unload()
158 TRACE(ft_t_flow
, "called for minor %d", MINOR(ino
->i_rdev
));
160 TRACE_ABORT(-EBUSY
, ft_t_warn
, "failed: already busy");
163 if ((MINOR(ino
->i_rdev
) & ~(ZFT_MINOR_OP_MASK
| FTAPE_NO_REWIND
))
167 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
169 MOD_DEC_USE_COUNT
; /* unlock module in memory */
172 TRACE_ABORT(-ENXIO
, ft_t_err
, "failed: illegal unit nr");
174 orig_sigmask
= current
->blocked
;
175 sigfillset(¤t
->blocked
);
176 result
= _zft_open(MINOR(ino
->i_rdev
), filep
->f_flags
& O_ACCMODE
);
178 current
->blocked
= orig_sigmask
; /* restore mask */
180 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
182 MOD_DEC_USE_COUNT
; /* unlock module in memory */
185 TRACE_ABORT(result
, ft_t_err
, "_ftape_open failed");
187 /* Mask signals that will disturb proper operation of the
188 * program that is calling.
190 current
->blocked
= orig_sigmask
;
191 sigaddsetmask (¤t
->blocked
, _DO_BLOCK
);
196 /* Close floppy tape device
198 static int zft_close(struct inode
*ino
, struct file
*filep
)
201 TRACE_FUN(ft_t_flow
);
203 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
) {
204 TRACE(ft_t_err
, "failed: not busy or wrong unit");
205 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
208 TRACE_EXIT
; /* keep busy_flag !(?) */
211 sigfillset(¤t
->blocked
);
212 result
= _zft_close();
214 TRACE(ft_t_err
, "_zft_close failed");
216 current
->blocked
= orig_sigmask
; /* restore before open state */
218 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
220 MOD_DEC_USE_COUNT
; /* unlock module in memory */
223 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
230 /* Ioctl for floppy tape device
232 static int zft_ioctl(struct inode
*ino
, struct file
*filep
,
233 unsigned int command
, unsigned long arg
)
236 sigset_t old_sigmask
;
237 TRACE_FUN(ft_t_flow
);
239 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
|| ft_failure
) {
240 TRACE_ABORT(-EIO
, ft_t_err
,
241 "failed: not busy, failure or wrong unit");
243 old_sigmask
= current
->blocked
; /* save mask */
244 sigfillset(¤t
->blocked
);
245 /* This will work as long as sizeof(void *) == sizeof(long) */
246 result
= _zft_ioctl(command
, (void *) arg
);
247 current
->blocked
= old_sigmask
; /* restore mask */
251 /* Ioctl for floppy tape device
253 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
254 static int zft_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
256 static int zft_mmap(struct inode
*ino
,
258 struct vm_area_struct
*vma
)
262 sigset_t old_sigmask
;
263 TRACE_FUN(ft_t_flow
);
266 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
267 MINOR(filep
->f_dentry
->d_inode
->i_rdev
) != zft_unit
||
269 MINOR(ino
->i_rdev
) != zft_unit
||
273 TRACE_ABORT(-EIO
, ft_t_err
,
274 "failed: not busy, failure or wrong unit");
276 old_sigmask
= current
->blocked
; /* save mask */
277 sigfillset(¤t
->blocked
);
278 if ((result
= ftape_mmap(vma
)) >= 0) {
279 #ifndef MSYNC_BUG_WAS_FIXED
280 static struct vm_operations_struct dummy
= { NULL
, };
281 vma
->vm_ops
= &dummy
;
283 vma
->vm_file
= filep
;
286 current
->blocked
= old_sigmask
; /* restore mask */
290 /* Read from floppy tape device
292 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
293 static ssize_t
zft_read(struct file
*fp
, char *buff
,
294 size_t req_len
, loff_t
*ppos
)
295 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
296 static long zft_read(struct inode
*ino
, struct file
*fp
, char *buff
,
297 unsigned long req_len
)
299 static int zft_read(struct inode
*ino
, struct file
*fp
, char *buff
,
304 sigset_t old_sigmask
;
305 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
306 struct inode
*ino
= fp
->f_dentry
->d_inode
;
308 TRACE_FUN(ft_t_flow
);
310 TRACE(ft_t_data_flow
, "called with count: %ld", (unsigned long)req_len
);
311 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
|| ft_failure
) {
312 TRACE_ABORT(-EIO
, ft_t_err
,
313 "failed: not busy, failure or wrong unit");
315 old_sigmask
= current
->blocked
; /* save mask */
316 sigfillset(¤t
->blocked
);
317 result
= _zft_read(buff
, req_len
);
318 current
->blocked
= old_sigmask
; /* restore mask */
319 TRACE(ft_t_data_flow
, "return with count: %d", result
);
323 /* Write to tape device
325 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
326 static ssize_t
zft_write(struct file
*fp
, const char *buff
,
327 size_t req_len
, loff_t
*ppos
)
328 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
329 static long zft_write(struct inode
*ino
, struct file
*fp
, const char *buff
,
330 unsigned long req_len
)
331 #elif LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
332 static int zft_write(struct inode
*ino
, struct file
*fp
, const char *buff
,
335 static int zft_write(struct inode
*ino
, struct file
*fp
, char *buff
,
340 sigset_t old_sigmask
;
341 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
342 struct inode
*ino
= fp
->f_dentry
->d_inode
;
344 TRACE_FUN(ft_t_flow
);
346 TRACE(ft_t_flow
, "called with count: %ld", (unsigned long)req_len
);
347 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
|| ft_failure
) {
348 TRACE_ABORT(-EIO
, ft_t_err
,
349 "failed: not busy, failure or wrong unit");
351 old_sigmask
= current
->blocked
; /* save mask */
352 sigfillset(¤t
->blocked
);
353 result
= _zft_write(buff
, req_len
);
354 current
->blocked
= old_sigmask
; /* restore mask */
355 TRACE(ft_t_data_flow
, "return with count: %d", result
);
359 /* END OF VFS INTERFACE
361 *****************************************************************************/
363 /* driver/module initialization
366 /* the compression module has to call this function to hook into the zftape
369 int zft_cmpr_register(struct zft_cmpr_ops
*new_ops
)
371 TRACE_FUN(ft_t_flow
);
373 if (zft_cmpr_ops
!= NULL
) {
376 zft_cmpr_ops
= new_ops
;
381 struct zft_cmpr_ops
*zft_cmpr_unregister(void)
383 struct zft_cmpr_ops
*old_ops
= zft_cmpr_ops
;
384 TRACE_FUN(ft_t_flow
);
390 /* lock the zft-compressor() module.
392 int zft_cmpr_lock(int try_to_load
)
394 if (zft_cmpr_ops
== NULL
) {
397 request_module("zft-compressor");
398 if (zft_cmpr_ops
== NULL
) {
408 (*zft_cmpr_ops
->lock
)();
412 #ifdef CONFIG_ZFT_COMPRESSOR
413 extern int zft_compressor_init(void);
416 /* Called by modules package when installing the driver or by kernel
417 * during the initialization phase
419 __initfunc(int zft_init(void))
421 TRACE_FUN(ft_t_flow
);
424 printk(KERN_INFO ZFTAPE_VERSION
"\n");
425 if (TRACE_LEVEL
>= ft_t_info
) {
428 "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
430 "vfs interface for ftape floppy tape driver.\n"
432 "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
434 "and builtin compression (lzrw3 algorithm).\n"
436 "Compiled for Linux version %s"
438 " with versioned symbols"
443 /* print a short no-nonsense boot message */
444 printk(KERN_INFO ZFTAPE_VERSION
" for Linux " UTS_RELEASE
"\n");
446 TRACE(ft_t_info
, "zft_init @ 0x%p", zft_init
);
448 "installing zftape VFS interface for ftape driver ...");
449 TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR
, "zft", &zft_cdev
),);
450 #if LINUX_VERSION_CODE >= KERNEL_VER(1,2,0)
451 # if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
452 register_symtab(&zft_symbol_table
); /* add global zftape symbols */
455 #ifdef CONFIG_ZFT_COMPRESSOR
456 (void)zft_compressor_init();
458 zft_status
= ftape_get_status(); /* fetch global data of ftape
466 #if LINUX_VERSION_CODE <= KERNEL_VER(1,2,13) && defined(MODULE)
467 char kernel_version
[] = UTS_RELEASE
;
469 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
470 /* Called by modules package before trying to unload the module
472 static int can_unload(void)
474 return (zft_dirty() || busy_flag
) ? -EBUSY
: 0;
477 /* Called by modules package when installing the driver
479 int init_module(void)
481 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
482 if (!mod_member_present(&__this_module
, can_unload
)) {
485 __this_module
.can_unload
= can_unload
;
490 /* Called by modules package when removing the driver
492 void cleanup_module(void)
494 TRACE_FUN(ft_t_flow
);
496 if (unregister_chrdev(QIC117_TAPE_MAJOR
, "zft") != 0) {
497 TRACE(ft_t_warn
, "failed");
499 TRACE(ft_t_info
, "successful");
501 zft_uninit_mem(); /* release remaining memory, if any */
502 printk(KERN_INFO
"zftape successfully unloaded.\n");