2 * Copyright (C) International Business Machines Corp., 2000-2005
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/completion.h>
25 #include <asm/uaccess.h>
26 #include "jfs_incore.h"
27 #include "jfs_filsys.h"
28 #include "jfs_metapage.h"
29 #include "jfs_superblock.h"
32 #include "jfs_debug.h"
34 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
35 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
36 MODULE_LICENSE("GPL");
38 static struct super_operations jfs_super_operations;
39 static struct file_system_type jfs_fs_type;
42 static pid_t jfsIOthread;
43 static pid_t jfsCommitThread;
44 static pid_t jfsSyncThread;
45 DECLARE_COMPLETION(jfsIOwait);
47 #ifdef CONFIG_JFS_DEBUG
48 int jfsloglevel = JFS_LOGLEVEL_WARN;
49 MODULE_PARM(jfsloglevel, "i");
50 MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
54 * External declarations
56 extern int jfs_mount(struct super_block *);
57 extern int jfs_mount_rw(struct super_block *, int);
58 extern int jfs_umount(struct super_block *);
59 extern int jfs_umount_rw(struct super_block *);
61 extern int jfsIOWait(void *);
62 extern int jfs_lazycommit(void *);
63 extern int jfs_sync(void *);
64 extern void jfs_clear_inode(struct inode *inode);
65 extern void jfs_read_inode(struct inode *inode);
66 extern void jfs_dirty_inode(struct inode *inode);
67 extern void jfs_delete_inode(struct inode *inode);
68 extern void jfs_write_inode(struct inode *inode, int wait);
69 extern int jfs_extendfs(struct super_block *, s64, int);
71 #ifdef PROC_FS_JFS /* see jfs_debug.h */
72 extern void jfs_proc_init(void);
73 extern void jfs_proc_clean(void);
76 extern wait_queue_head_t jfs_IO_thread_wait;
77 extern wait_queue_head_t jfs_commit_thread_wait;
78 extern wait_queue_head_t jfs_sync_thread_wait;
80 static void jfs_handle_error(struct super_block *sb)
82 struct jfs_sb_info *sbi = JFS_SBI(sb);
84 if (sb->s_flags & MS_RDONLY)
87 updateSuper(sb, FM_DIRTY);
89 if (sbi->flag & JFS_ERR_PANIC)
90 panic("JFS (device %s): panic forced after error\n",
92 else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
93 jfs_err("ERROR: (device %s): remounting filesystem "
96 sb->s_flags |= MS_RDONLY;
99 /* nothing is done for continue beyond marking the superblock dirty */
102 void jfs_error(struct super_block *sb, const char * function, ...)
104 static char error_buf[256];
107 va_start(args, function);
108 vsprintf(error_buf, function, args);
111 printk(KERN_ERR "ERROR: (device %s): %s\n", bdevname(sb->s_dev),
114 jfs_handle_error(sb);
117 static int jfs_statfs(struct super_block *sb, struct statfs *buf)
119 struct jfs_sb_info *sbi = JFS_SBI(sb);
121 struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
123 jfs_info("In jfs_statfs");
124 buf->f_type = JFS_SUPER_MAGIC;
125 buf->f_bsize = sbi->bsize;
126 buf->f_blocks = sbi->bmap->db_mapsize;
127 buf->f_bfree = sbi->bmap->db_nfree;
128 buf->f_bavail = sbi->bmap->db_nfree;
130 * If we really return the number of allocated & free inodes, some
131 * applications will fail because they won't see enough free inodes.
132 * We'll try to calculate some guess as to how may inodes we can
135 * buf->f_files = atomic_read(&imap->im_numinos);
136 * buf->f_ffree = atomic_read(&imap->im_numfree);
138 maxinodes = min((s64) atomic_read(&imap->im_numinos) +
139 ((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
140 << L2INOSPEREXT), (s64) 0xffffffffLL);
141 buf->f_files = maxinodes;
142 buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
143 atomic_read(&imap->im_numfree));
145 buf->f_namelen = JFS_NAME_MAX;
149 static void jfs_put_super(struct super_block *sb)
151 struct jfs_sb_info *sbi = JFS_SBI(sb);
154 jfs_info("In jfs_put_super");
157 jfs_err("jfs_umount failed with return code %d", rc);
159 unload_nls(sbi->nls_tab);
166 s64 jfs_get_volume_size(struct super_block *sb)
170 kdev_t dev = sb->s_dev;
171 int major = MAJOR(dev);
172 int minor = MINOR(dev);
174 if (blk_size[major]) {
175 blocks = blk_size[major][minor];
177 bytes = ((s64)blocks) << BLOCK_SIZE_BITS;
178 return bytes >> sb->s_blocksize_bits;
184 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
187 void *nls_map = (void *)-1; /* -1: no change; NULL: none */
190 struct jfs_sb_info *sbi = JFS_SBI(sb);
196 while ((this_char = strsep(&options, ",")) != NULL) {
199 if ((value = strchr(this_char, '=')) != NULL)
201 if (!strcmp(this_char, "errors")) {
202 if (!value || !*value)
204 if (!strcmp(value, "continue")) {
205 *flag &= ~JFS_ERR_REMOUNT_RO;
206 *flag &= ~JFS_ERR_PANIC;
207 *flag |= JFS_ERR_CONTINUE;
208 } else if (!strcmp(value, "remount-ro")) {
209 *flag &= ~JFS_ERR_CONTINUE;
210 *flag &= ~JFS_ERR_PANIC;
211 *flag |= JFS_ERR_REMOUNT_RO;
212 } else if (!strcmp(value, "panic")) {
213 *flag &= ~JFS_ERR_CONTINUE;
214 *flag &= ~JFS_ERR_REMOUNT_RO;
215 *flag |= JFS_ERR_PANIC;
217 printk(KERN_ERR "JFS: %s is an invalid error handler\n", value);
220 } else if (!strcmp(this_char, "integrity")) {
221 *flag &= ~JFS_NOINTEGRITY;
222 } else if (!strcmp(this_char, "nointegrity")) {
223 *flag |= JFS_NOINTEGRITY;
224 } else if (!strcmp(this_char, "iocharset")) {
225 if (!value || !*value)
227 if (nls_map && nls_map != (void *) -1)
229 if (!strcmp(value, "none"))
232 nls_map = load_nls(value);
233 } else if (!strcmp(this_char, "resize")) {
234 if (!value || !*value) {
235 *newLVSize = jfs_get_volume_size(sb);
238 "JFS: Cannot determine volume size\n");
240 *newLVSize = simple_strtoull(value, &value, 0);
242 /* Silently ignore the quota options */
243 } else if (!strcmp(this_char, "grpquota")
244 || !strcmp(this_char, "noquota")
245 || !strcmp(this_char, "quota")
246 || !strcmp(this_char, "usrquota"))
247 /* Don't do anything ;-) */ ;
249 printk("jfs: Unrecognized mount option %s\n",
254 if (nls_map != (void *) -1) {
255 /* Discard old (if remount) */
256 if (sbi->nls_tab && sbi->nls_tab != (void *) -1)
257 unload_nls(sbi->nls_tab);
258 sbi->nls_tab = nls_map;
262 printk(KERN_ERR "JFS: %s needs an argument\n", this_char);
264 if (nls_map && nls_map != (void *) -1)
269 static int jfs_remount(struct super_block *sb, int *flags, char *data)
273 int flag = JFS_SBI(sb)->flag;
275 if (!parse_options(data, sb, &newLVSize, &flag)) {
279 if (sb->s_flags & MS_RDONLY) {
281 "JFS: resize requires volume to be mounted read-write\n");
284 rc = jfs_extendfs(sb, newLVSize, 0);
289 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
290 JFS_SBI(sb)->flag = flag;
291 return jfs_mount_rw(sb, 1);
293 if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
294 rc = jfs_umount_rw(sb);
295 JFS_SBI(sb)->flag = flag;
298 if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
299 if (!(sb->s_flags & MS_RDONLY)) {
300 rc = jfs_umount_rw(sb);
303 JFS_SBI(sb)->flag = flag;
304 return jfs_mount_rw(sb, 1);
306 JFS_SBI(sb)->flag = flag;
311 static struct super_block *jfs_read_super(struct super_block *sb,
312 void *data, int silent)
314 struct jfs_sb_info *sbi;
320 jfs_info("In jfs_read_super s_dev=0x%x s_flags=0x%lx", sb->s_dev,
323 sbi = kmalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
326 memset(sbi, 0, sizeof (struct jfs_sb_info));
327 sb->u.generic_sbp = sbi;
329 /* initialize the mount flag and determine the default error handler */
330 flag = JFS_ERR_REMOUNT_RO;
332 /* nls_tab will be set to NULL if no character mapping is requested */
333 sbi->nls_tab = (void *) -1;
334 if (!parse_options((char *) data, sb, &newLVSize, &flag)) {
341 printk(KERN_ERR "resize option for remount only\n");
346 * Initialize blocksize to 4K.
348 sb_set_blocksize(sb, PSIZE);
351 * Set method vectors.
353 sb->s_op = &jfs_super_operations;
358 jfs_err("jfs_mount failed w/return code = %d", rc);
362 if (sb->s_flags & MS_RDONLY)
365 rc = jfs_mount_rw(sb, 0);
368 jfs_err("jfs_mount_rw failed, return code = %d",
375 sb->s_magic = JFS_SUPER_MAGIC;
377 inode = iget(sb, ROOT_I);
378 if (!inode || is_bad_inode(inode))
380 sb->s_root = d_alloc_root(inode);
384 if (sbi->nls_tab == (void *) -1)
385 sbi->nls_tab = load_nls_default();
387 /* logical blocks are represented by 40 bits in pxd_t, etc. */
388 sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
389 #if BITS_PER_LONG == 32
391 * Page cache is indexed by long.
392 * I would use MAX_LFS_FILESIZE, but it's only half as big
394 sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1, sb->s_maxbytes);
400 jfs_err("jfs_read_super: get root inode failed");
407 jfs_err("jfs_umount failed with return code %d", rc);
410 if (sbi->nls_tab && sbi->nls_tab != (void *) -1)
411 unload_nls(sbi->nls_tab);
416 static void jfs_write_super_lockfs(struct super_block *sb)
418 struct jfs_sb_info *sbi = JFS_SBI(sb);
419 struct jfs_log *log = sbi->log;
421 if (!(sb->s_flags & MS_RDONLY)) {
424 updateSuper(sb, FM_CLEAN);
428 static void jfs_unlockfs(struct super_block *sb)
430 struct jfs_sb_info *sbi = JFS_SBI(sb);
431 struct jfs_log *log = sbi->log;
434 if (!(sb->s_flags & MS_RDONLY)) {
435 updateSuper(sb, FM_MOUNT);
436 if ((rc = lmLogInit(log)))
437 jfs_err("jfs_unlock failed with return code %d", rc);
444 static int jfs_sync_fs(struct super_block *sb)
446 struct jfs_log *log = JFS_SBI(sb)->log;
448 /* log == NULL indicates read-only mount */
450 jfs_flush_journal(log, 1);
455 static struct super_operations jfs_super_operations = {
456 .read_inode = jfs_read_inode,
457 .dirty_inode = jfs_dirty_inode,
458 .write_inode = jfs_write_inode,
459 .clear_inode = jfs_clear_inode,
460 .delete_inode = jfs_delete_inode,
461 .put_super = jfs_put_super,
462 .sync_fs = jfs_sync_fs,
463 .write_super_lockfs = jfs_write_super_lockfs,
464 .unlockfs = jfs_unlockfs,
465 .statfs = jfs_statfs,
466 .remount_fs = jfs_remount,
469 static struct file_system_type jfs_fs_type = {
470 .owner = THIS_MODULE,
472 .read_super = jfs_read_super,
473 .fs_flags = FS_REQUIRES_DEV,
476 extern int metapage_init(void);
477 extern int txInit(void);
478 extern void txExit(void);
479 extern void metapage_exit(void);
481 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
483 struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
485 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
486 SLAB_CTOR_CONSTRUCTOR) {
487 memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
488 INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
489 init_rwsem(&jfs_ip->rdwrlock);
490 init_MUTEX(&jfs_ip->commit_sem);
491 spin_lock_init(&jfs_ip->ag_lock);
492 jfs_ip->active_ag = -1;
496 static int __init init_jfs_fs(void)
501 kmem_cache_create("jfs_ip", sizeof (struct jfs_inode_info), 0, 0,
503 if (jfs_inode_cachep == NULL)
507 * Metapage initialization
509 rc = metapage_init();
511 jfs_err("metapage_init failed w/rc = %d", rc);
516 * Transaction Manager initialization
520 jfs_err("txInit failed w/rc = %d", rc);
525 * I/O completion thread (endio)
527 jfsIOthread = kernel_thread(jfsIOWait, 0,
528 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
529 if (jfsIOthread < 0) {
530 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread);
533 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
535 jfsCommitThread = kernel_thread(jfs_lazycommit, 0,
536 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
537 if (jfsCommitThread < 0) {
538 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsCommitThread);
541 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
543 jfsSyncThread = kernel_thread(jfs_sync, 0,
544 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
545 if (jfsSyncThread < 0) {
546 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread);
547 goto kill_committask;
549 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
555 return register_filesystem(&jfs_fs_type);
558 jfs_stop_threads = 1;
559 wake_up(&jfs_commit_thread_wait);
560 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
562 jfs_stop_threads = 1;
563 wake_up(&jfs_IO_thread_wait);
564 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
570 kmem_cache_destroy(jfs_inode_cachep);
574 static void __exit exit_jfs_fs(void)
576 jfs_info("exit_jfs_fs called");
578 jfs_stop_threads = 1;
581 wake_up(&jfs_IO_thread_wait);
582 wait_for_completion(&jfsIOwait); /* Wait for IO thread exit */
583 wake_up(&jfs_commit_thread_wait);
584 wait_for_completion(&jfsIOwait); /* Wait for Commit thread exit */
585 wake_up(&jfs_sync_thread_wait);
586 wait_for_completion(&jfsIOwait); /* Wait for Sync thread exit */
590 unregister_filesystem(&jfs_fs_type);
591 kmem_cache_destroy(jfs_inode_cachep);
596 module_init(init_jfs_fs)
597 module_exit(exit_jfs_fs)