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/kerneld.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 */
137 zft_close
, /* release */
141 /* Open floppy tape device
143 static int zft_open(struct inode
*ino
, struct file
*filep
)
146 TRACE_FUN(ft_t_flow
);
148 #if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
150 MOD_INC_USE_COUNT
; /* lock module in memory */
153 MOD_INC_USE_COUNT
; /* sets MOD_VISITED and MOD_USED_ONCE,
154 * locking is done with can_unload()
157 TRACE(ft_t_flow
, "called for minor %d", MINOR(ino
->i_rdev
));
159 TRACE_ABORT(-EBUSY
, ft_t_warn
, "failed: already busy");
162 if ((MINOR(ino
->i_rdev
) & ~(ZFT_MINOR_OP_MASK
| FTAPE_NO_REWIND
))
166 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
168 MOD_DEC_USE_COUNT
; /* unlock module in memory */
171 TRACE_ABORT(-ENXIO
, ft_t_err
, "failed: illegal unit nr");
173 orig_sigmask
= current
->blocked
;
174 sigfillset(¤t
->blocked
);
175 result
= _zft_open(MINOR(ino
->i_rdev
), filep
->f_flags
& O_ACCMODE
);
177 current
->blocked
= orig_sigmask
; /* restore mask */
179 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
181 MOD_DEC_USE_COUNT
; /* unlock module in memory */
184 TRACE_ABORT(result
, ft_t_err
, "_ftape_open failed");
186 /* Mask signals that will disturb proper operation of the
187 * program that is calling.
189 current
->blocked
= orig_sigmask
;
190 sigaddsetmask (¤t
->blocked
, _DO_BLOCK
);
195 /* Close floppy tape device
197 static int zft_close(struct inode
*ino
, struct file
*filep
)
200 TRACE_FUN(ft_t_flow
);
202 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
) {
203 TRACE(ft_t_err
, "failed: not busy or wrong unit");
204 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
207 TRACE_EXIT
; /* keep busy_flag !(?) */
210 sigfillset(¤t
->blocked
);
211 result
= _zft_close();
213 TRACE(ft_t_err
, "_zft_close failed");
215 current
->blocked
= orig_sigmask
; /* restore before open state */
217 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
219 MOD_DEC_USE_COUNT
; /* unlock module in memory */
222 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
229 /* Ioctl for floppy tape device
231 static int zft_ioctl(struct inode
*ino
, struct file
*filep
,
232 unsigned int command
, unsigned long arg
)
235 sigset_t old_sigmask
;
236 TRACE_FUN(ft_t_flow
);
238 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
|| ft_failure
) {
239 TRACE_ABORT(-EIO
, ft_t_err
,
240 "failed: not busy, failure or wrong unit");
242 old_sigmask
= current
->blocked
; /* save mask */
243 sigfillset(¤t
->blocked
);
244 /* This will work as long as sizeof(void *) == sizeof(long) */
245 result
= _zft_ioctl(command
, (void *) arg
);
246 current
->blocked
= old_sigmask
; /* restore mask */
250 /* Ioctl for floppy tape device
252 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
253 static int zft_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
255 static int zft_mmap(struct inode
*ino
,
257 struct vm_area_struct
*vma
)
261 sigset_t old_sigmask
;
262 TRACE_FUN(ft_t_flow
);
265 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
266 MINOR(filep
->f_dentry
->d_inode
->i_rdev
) != zft_unit
||
268 MINOR(ino
->i_rdev
) != zft_unit
||
272 TRACE_ABORT(-EIO
, ft_t_err
,
273 "failed: not busy, failure or wrong unit");
275 old_sigmask
= current
->blocked
; /* save mask */
276 sigfillset(¤t
->blocked
);
277 if ((result
= ftape_mmap(vma
)) >= 0) {
278 #ifndef MSYNC_BUG_WAS_FIXED
279 static struct vm_operations_struct dummy
= { NULL
, };
280 vma
->vm_ops
= &dummy
;
282 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,45)
283 vma
->vm_dentry
= dget(filep
->f_dentry
);
285 vma_set_inode (vma
, ino
);
286 inode_inc_count (ino
);
289 current
->blocked
= old_sigmask
; /* restore mask */
293 /* Read from floppy tape device
295 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
296 static ssize_t
zft_read(struct file
*fp
, char *buff
,
297 size_t req_len
, loff_t
*ppos
)
298 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
299 static long zft_read(struct inode
*ino
, struct file
*fp
, char *buff
,
300 unsigned long req_len
)
302 static int zft_read(struct inode
*ino
, struct file
*fp
, char *buff
,
307 sigset_t old_sigmask
;
308 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
309 struct inode
*ino
= fp
->f_dentry
->d_inode
;
311 TRACE_FUN(ft_t_flow
);
313 TRACE(ft_t_data_flow
, "called with count: %ld", (unsigned long)req_len
);
314 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
|| ft_failure
) {
315 TRACE_ABORT(-EIO
, ft_t_err
,
316 "failed: not busy, failure or wrong unit");
318 old_sigmask
= current
->blocked
; /* save mask */
319 sigfillset(¤t
->blocked
);
320 result
= _zft_read(buff
, req_len
);
321 current
->blocked
= old_sigmask
; /* restore mask */
322 TRACE(ft_t_data_flow
, "return with count: %d", result
);
326 /* Write to tape device
328 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
329 static ssize_t
zft_write(struct file
*fp
, const char *buff
,
330 size_t req_len
, loff_t
*ppos
)
331 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
332 static long zft_write(struct inode
*ino
, struct file
*fp
, const char *buff
,
333 unsigned long req_len
)
334 #elif LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
335 static int zft_write(struct inode
*ino
, struct file
*fp
, const char *buff
,
338 static int zft_write(struct inode
*ino
, struct file
*fp
, char *buff
,
343 sigset_t old_sigmask
;
344 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
345 struct inode
*ino
= fp
->f_dentry
->d_inode
;
347 TRACE_FUN(ft_t_flow
);
349 TRACE(ft_t_flow
, "called with count: %ld", (unsigned long)req_len
);
350 if (!busy_flag
|| MINOR(ino
->i_rdev
) != zft_unit
|| ft_failure
) {
351 TRACE_ABORT(-EIO
, ft_t_err
,
352 "failed: not busy, failure or wrong unit");
354 old_sigmask
= current
->blocked
; /* save mask */
355 sigfillset(¤t
->blocked
);
356 result
= _zft_write(buff
, req_len
);
357 current
->blocked
= old_sigmask
; /* restore mask */
358 TRACE(ft_t_data_flow
, "return with count: %d", result
);
362 /* END OF VFS INTERFACE
364 *****************************************************************************/
366 /* driver/module initialization
369 /* the compression module has to call this function to hook into the zftape
372 int zft_cmpr_register(struct zft_cmpr_ops
*new_ops
)
374 TRACE_FUN(ft_t_flow
);
376 if (zft_cmpr_ops
!= NULL
) {
379 zft_cmpr_ops
= new_ops
;
384 struct zft_cmpr_ops
*zft_cmpr_unregister(void)
386 struct zft_cmpr_ops
*old_ops
= zft_cmpr_ops
;
387 TRACE_FUN(ft_t_flow
);
393 /* lock the zft-compressor() module.
395 int zft_cmpr_lock(int try_to_load
)
397 if (zft_cmpr_ops
== NULL
) {
398 #ifdef CONFIG_KERNELD
400 request_module("zft-compressor");
401 if (zft_cmpr_ops
== NULL
) {
411 (*zft_cmpr_ops
->lock
)();
415 #ifdef CONFIG_ZFT_COMPRESSOR
416 extern int zft_compressor_init(void);
419 /* Called by modules package when installing the driver or by kernel
420 * during the initialization phase
422 __initfunc(int zft_init(void))
424 TRACE_FUN(ft_t_flow
);
427 printk(KERN_INFO ZFTAPE_VERSION
"\n");
428 if (TRACE_LEVEL
>= ft_t_info
) {
431 "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
433 "vfs interface for ftape floppy tape driver.\n"
435 "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
437 "and builtin compression (lzrw3 algorithm).\n"
439 "Compiled for Linux version %s"
441 " with versioned symbols"
446 /* print a short no-nonsense boot message */
447 printk(KERN_INFO ZFTAPE_VERSION
" for Linux " UTS_RELEASE
"\n");
449 TRACE(ft_t_info
, "zft_init @ 0x%p", zft_init
);
451 "installing zftape VFS interface for ftape driver ...");
452 TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR
, "zft", &zft_cdev
),);
453 #if LINUX_VERSION_CODE >= KERNEL_VER(1,2,0)
454 # if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
455 register_symtab(&zft_symbol_table
); /* add global zftape symbols */
458 #ifdef CONFIG_ZFT_COMPRESSOR
459 (void)zft_compressor_init();
461 zft_status
= ftape_get_status(); /* fetch global data of ftape
469 #if LINUX_VERSION_CODE <= KERNEL_VER(1,2,13) && defined(MODULE)
470 char kernel_version
[] = UTS_RELEASE
;
472 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
473 /* Called by modules package before trying to unload the module
475 static int can_unload(void)
477 return (zft_dirty() || busy_flag
) ? -EBUSY
: 0;
480 /* Called by modules package when installing the driver
482 int init_module(void)
484 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
485 if (!mod_member_present(&__this_module
, can_unload
)) {
488 __this_module
.can_unload
= can_unload
;
493 /* Called by modules package when removing the driver
495 void cleanup_module(void)
497 TRACE_FUN(ft_t_flow
);
499 if (unregister_chrdev(QIC117_TAPE_MAJOR
, "zft") != 0) {
500 TRACE(ft_t_warn
, "failed");
502 TRACE(ft_t_info
, "successful");
504 zft_uninit_mem(); /* release remaining memory, if any */
505 printk(KERN_INFO
"zftape successfully unloaded.\n");