2 * Copyright (C) International Business Machines Corp., 2000-2004
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
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/parser.h>
24 #include <linux/completion.h>
25 #include <linux/vfs.h>
26 #include <linux/moduleparam.h>
27 #include <asm/uaccess.h>
29 #include "jfs_incore.h"
30 #include "jfs_filsys.h"
31 #include "jfs_inode.h"
32 #include "jfs_metapage.h"
33 #include "jfs_superblock.h"
37 #include "jfs_debug.h"
39 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
40 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
41 MODULE_LICENSE("GPL");
43 static kmem_cache_t * jfs_inode_cachep;
45 static struct super_operations jfs_super_operations;
46 static struct export_operations jfs_export_operations;
47 static struct file_system_type jfs_fs_type;
49 #define MAX_COMMIT_THREADS 64
50 static int commit_threads = 0;
51 module_param(commit_threads, int, 0);
52 MODULE_PARM_DESC(commit_threads, "Number of commit threads");
55 static pid_t jfsIOthread;
56 static pid_t jfsCommitThread[MAX_COMMIT_THREADS];
57 static pid_t jfsSyncThread;
58 DECLARE_COMPLETION(jfsIOwait);
60 #ifdef CONFIG_JFS_DEBUG
61 int jfsloglevel = JFS_LOGLEVEL_WARN;
62 module_param(jfsloglevel, int, 0644);
63 MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
66 static void jfs_handle_error(struct super_block *sb)
68 struct jfs_sb_info *sbi = JFS_SBI(sb);
70 if (sb->s_flags & MS_RDONLY)
73 updateSuper(sb, FM_DIRTY);
75 if (sbi->flag & JFS_ERR_PANIC)
76 panic("JFS (device %s): panic forced after error\n",
78 else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
79 jfs_err("ERROR: (device %s): remounting filesystem "
82 sb->s_flags |= MS_RDONLY;
85 /* nothing is done for continue beyond marking the superblock dirty */
88 void jfs_error(struct super_block *sb, const char * function, ...)
90 static char error_buf[256];
93 va_start(args, function);
94 vsprintf(error_buf, function, args);
97 printk(KERN_ERR "ERROR: (device %s): %s\n", sb->s_id, error_buf);
102 static struct inode *jfs_alloc_inode(struct super_block *sb)
104 struct jfs_inode_info *jfs_inode;
106 jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
109 return &jfs_inode->vfs_inode;
112 static void jfs_destroy_inode(struct inode *inode)
114 struct jfs_inode_info *ji = JFS_IP(inode);
116 spin_lock_irq(&ji->ag_lock);
117 if (ji->active_ag != -1) {
118 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
119 atomic_dec(&bmap->db_active[ji->active_ag]);
122 spin_unlock_irq(&ji->ag_lock);
124 #ifdef CONFIG_JFS_POSIX_ACL
125 if (ji->i_acl != JFS_ACL_NOT_CACHED) {
126 posix_acl_release(ji->i_acl);
127 ji->i_acl = JFS_ACL_NOT_CACHED;
129 if (ji->i_default_acl != JFS_ACL_NOT_CACHED) {
130 posix_acl_release(ji->i_default_acl);
131 ji->i_default_acl = JFS_ACL_NOT_CACHED;
135 kmem_cache_free(jfs_inode_cachep, ji);
138 static int jfs_statfs(struct super_block *sb, struct kstatfs *buf)
140 struct jfs_sb_info *sbi = JFS_SBI(sb);
142 struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
144 jfs_info("In jfs_statfs");
145 buf->f_type = JFS_SUPER_MAGIC;
146 buf->f_bsize = sbi->bsize;
147 buf->f_blocks = sbi->bmap->db_mapsize;
148 buf->f_bfree = sbi->bmap->db_nfree;
149 buf->f_bavail = sbi->bmap->db_nfree;
151 * If we really return the number of allocated & free inodes, some
152 * applications will fail because they won't see enough free inodes.
153 * We'll try to calculate some guess as to how may inodes we can
156 * buf->f_files = atomic_read(&imap->im_numinos);
157 * buf->f_ffree = atomic_read(&imap->im_numfree);
159 maxinodes = min((s64) atomic_read(&imap->im_numinos) +
160 ((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
161 << L2INOSPEREXT), (s64) 0xffffffffLL);
162 buf->f_files = maxinodes;
163 buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
164 atomic_read(&imap->im_numfree));
166 buf->f_namelen = JFS_NAME_MAX;
170 static void jfs_put_super(struct super_block *sb)
172 struct jfs_sb_info *sbi = JFS_SBI(sb);
175 jfs_info("In jfs_put_super");
178 jfs_err("jfs_umount failed with return code %d", rc);
180 unload_nls(sbi->nls_tab);
183 truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
184 iput(sbi->direct_inode);
185 sbi->direct_inode = NULL;
191 Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
192 Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err,
195 static match_table_t tokens = {
196 {Opt_integrity, "integrity"},
197 {Opt_nointegrity, "nointegrity"},
198 {Opt_iocharset, "iocharset=%s"},
199 {Opt_resize, "resize=%u"},
200 {Opt_resize_nosize, "resize"},
201 {Opt_errors, "errors=%s"},
202 {Opt_ignore, "noquota"},
203 {Opt_ignore, "quota"},
204 {Opt_ignore, "usrquota"},
205 {Opt_ignore, "grpquota"},
209 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
212 void *nls_map = (void *)-1; /* -1: no change; NULL: none */
214 struct jfs_sb_info *sbi = JFS_SBI(sb);
221 while ((p = strsep(&options, ",")) != NULL) {
222 substring_t args[MAX_OPT_ARGS];
227 token = match_token(p, tokens, args);
230 *flag &= ~JFS_NOINTEGRITY;
232 case Opt_nointegrity:
233 *flag |= JFS_NOINTEGRITY;
236 /* Silently ignore the quota options */
237 /* Don't do anything ;-) */
240 if (nls_map && nls_map != (void *) -1)
242 if (!strcmp(args[0].from, "none"))
245 nls_map = load_nls(args[0].from);
248 "JFS: charset not found\n");
255 char *resize = args[0].from;
256 *newLVSize = simple_strtoull(resize, &resize, 0);
259 case Opt_resize_nosize:
261 *newLVSize = sb->s_bdev->bd_inode->i_size >>
262 sb->s_blocksize_bits;
265 "JFS: Cannot determine volume size\n");
270 char *errors = args[0].from;
271 if (!errors || !*errors)
273 if (!strcmp(errors, "continue")) {
274 *flag &= ~JFS_ERR_REMOUNT_RO;
275 *flag &= ~JFS_ERR_PANIC;
276 *flag |= JFS_ERR_CONTINUE;
277 } else if (!strcmp(errors, "remount-ro")) {
278 *flag &= ~JFS_ERR_CONTINUE;
279 *flag &= ~JFS_ERR_PANIC;
280 *flag |= JFS_ERR_REMOUNT_RO;
281 } else if (!strcmp(errors, "panic")) {
282 *flag &= ~JFS_ERR_CONTINUE;
283 *flag &= ~JFS_ERR_REMOUNT_RO;
284 *flag |= JFS_ERR_PANIC;
287 "JFS: %s is an invalid error handler\n",
294 printk("jfs: Unrecognized mount option \"%s\" "
295 " or missing value\n", p);
300 if (nls_map != (void *) -1) {
301 /* Discard old (if remount) */
303 unload_nls(sbi->nls_tab);
304 sbi->nls_tab = nls_map;
309 if (nls_map && nls_map != (void *) -1)
314 static int jfs_remount(struct super_block *sb, int *flags, char *data)
318 int flag = JFS_SBI(sb)->flag;
320 if (!parse_options(data, sb, &newLVSize, &flag)) {
324 if (sb->s_flags & MS_RDONLY) {
326 "JFS: resize requires volume to be mounted read-write\n");
329 rc = jfs_extendfs(sb, newLVSize, 0);
334 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
336 * Invalidate any previously read metadata. fsck may have
337 * changed the on-disk data since we mounted r/o
339 truncate_inode_pages(JFS_SBI(sb)->direct_inode->i_mapping, 0);
341 JFS_SBI(sb)->flag = flag;
342 return jfs_mount_rw(sb, 1);
344 if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
345 rc = jfs_umount_rw(sb);
346 JFS_SBI(sb)->flag = flag;
349 if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
350 if (!(sb->s_flags & MS_RDONLY)) {
351 rc = jfs_umount_rw(sb);
354 JFS_SBI(sb)->flag = flag;
355 return jfs_mount_rw(sb, 1);
357 JFS_SBI(sb)->flag = flag;
362 static int jfs_fill_super(struct super_block *sb, void *data, int silent)
364 struct jfs_sb_info *sbi;
370 jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
372 if (!new_valid_dev(sb->s_bdev->bd_dev))
375 sbi = kmalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
378 memset(sbi, 0, sizeof (struct jfs_sb_info));
382 /* initialize the mount flag and determine the default error handler */
383 flag = JFS_ERR_REMOUNT_RO;
385 if (!parse_options((char *) data, sb, &newLVSize, &flag)) {
391 #ifdef CONFIG_JFS_POSIX_ACL
392 sb->s_flags |= MS_POSIXACL;
396 printk(KERN_ERR "resize option for remount only\n");
401 * Initialize blocksize to 4K.
403 sb_set_blocksize(sb, PSIZE);
406 * Set method vectors.
408 sb->s_op = &jfs_super_operations;
409 sb->s_export_op = &jfs_export_operations;
412 * Initialize direct-mapping inode/address-space
414 inode = new_inode(sb);
419 inode->i_size = sb->s_bdev->bd_inode->i_size;
420 inode->i_mapping->a_ops = &jfs_metapage_aops;
421 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
423 sbi->direct_inode = inode;
428 jfs_err("jfs_mount failed w/return code = %d", rc);
430 goto out_mount_failed;
432 if (sb->s_flags & MS_RDONLY)
435 rc = jfs_mount_rw(sb, 0);
438 jfs_err("jfs_mount_rw failed, return code = %d",
445 sb->s_magic = JFS_SUPER_MAGIC;
447 inode = iget(sb, ROOT_I);
448 if (!inode || is_bad_inode(inode))
450 sb->s_root = d_alloc_root(inode);
454 if (sbi->mntflag & JFS_OS2)
455 sb->s_root->d_op = &jfs_ci_dentry_operations;
457 /* logical blocks are represented by 40 bits in pxd_t, etc. */
458 sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
459 #if BITS_PER_LONG == 32
461 * Page cache is indexed by long.
462 * I would use MAX_LFS_FILESIZE, but it's only half as big
464 sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1, sb->s_maxbytes);
470 jfs_err("jfs_read_super: get root inode failed");
477 jfs_err("jfs_umount failed with return code %d", rc);
480 filemap_fdatawrite(sbi->direct_inode->i_mapping);
481 filemap_fdatawait(sbi->direct_inode->i_mapping);
482 truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
483 make_bad_inode(sbi->direct_inode);
484 iput(sbi->direct_inode);
485 sbi->direct_inode = NULL;
488 unload_nls(sbi->nls_tab);
493 static void jfs_write_super_lockfs(struct super_block *sb)
495 struct jfs_sb_info *sbi = JFS_SBI(sb);
496 struct jfs_log *log = sbi->log;
498 if (!(sb->s_flags & MS_RDONLY)) {
501 updateSuper(sb, FM_CLEAN);
505 static void jfs_unlockfs(struct super_block *sb)
507 struct jfs_sb_info *sbi = JFS_SBI(sb);
508 struct jfs_log *log = sbi->log;
511 if (!(sb->s_flags & MS_RDONLY)) {
512 updateSuper(sb, FM_MOUNT);
513 if ((rc = lmLogInit(log)))
514 jfs_err("jfs_unlock failed with return code %d", rc);
520 static struct super_block *jfs_get_sb(struct file_system_type *fs_type,
521 int flags, const char *dev_name, void *data)
523 return get_sb_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
526 static int jfs_sync_fs(struct super_block *sb, int wait)
528 struct jfs_log *log = JFS_SBI(sb)->log;
530 /* log == NULL indicates read-only mount */
532 jfs_flush_journal(log, wait);
539 static struct super_operations jfs_super_operations = {
540 .alloc_inode = jfs_alloc_inode,
541 .destroy_inode = jfs_destroy_inode,
542 .read_inode = jfs_read_inode,
543 .dirty_inode = jfs_dirty_inode,
544 .write_inode = jfs_write_inode,
545 .delete_inode = jfs_delete_inode,
546 .put_super = jfs_put_super,
547 .sync_fs = jfs_sync_fs,
548 .write_super_lockfs = jfs_write_super_lockfs,
549 .unlockfs = jfs_unlockfs,
550 .statfs = jfs_statfs,
551 .remount_fs = jfs_remount,
554 static struct export_operations jfs_export_operations = {
555 .get_parent = jfs_get_parent,
558 static struct file_system_type jfs_fs_type = {
559 .owner = THIS_MODULE,
561 .get_sb = jfs_get_sb,
562 .kill_sb = kill_block_super,
563 .fs_flags = FS_REQUIRES_DEV,
566 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
568 struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
570 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
571 SLAB_CTOR_CONSTRUCTOR) {
572 memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
573 INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
574 init_rwsem(&jfs_ip->rdwrlock);
575 init_MUTEX(&jfs_ip->commit_sem);
576 init_rwsem(&jfs_ip->xattr_sem);
577 spin_lock_init(&jfs_ip->ag_lock);
578 jfs_ip->active_ag = -1;
579 #ifdef CONFIG_JFS_POSIX_ACL
580 jfs_ip->i_acl = JFS_ACL_NOT_CACHED;
581 jfs_ip->i_default_acl = JFS_ACL_NOT_CACHED;
583 inode_init_once(&jfs_ip->vfs_inode);
587 static int __init init_jfs_fs(void)
593 kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info), 0,
594 SLAB_RECLAIM_ACCOUNT, init_once, NULL);
595 if (jfs_inode_cachep == NULL)
599 * Metapage initialization
601 rc = metapage_init();
603 jfs_err("metapage_init failed w/rc = %d", rc);
608 * Transaction Manager initialization
612 jfs_err("txInit failed w/rc = %d", rc);
617 * I/O completion thread (endio)
619 jfsIOthread = kernel_thread(jfsIOWait, NULL, CLONE_KERNEL);
620 if (jfsIOthread < 0) {
621 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread);
624 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
626 if (commit_threads < 1)
627 commit_threads = num_online_cpus();
628 if (commit_threads > MAX_COMMIT_THREADS)
629 commit_threads = MAX_COMMIT_THREADS;
631 for (i = 0; i < commit_threads; i++) {
632 jfsCommitThread[i] = kernel_thread(jfs_lazycommit, NULL,
634 if (jfsCommitThread[i] < 0) {
635 jfs_err("init_jfs_fs: fork failed w/rc = %d",
638 goto kill_committask;
640 /* Wait until thread starts */
641 wait_for_completion(&jfsIOwait);
644 jfsSyncThread = kernel_thread(jfs_sync, NULL, CLONE_KERNEL);
645 if (jfsSyncThread < 0) {
646 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread);
647 goto kill_committask;
649 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
655 return register_filesystem(&jfs_fs_type);
658 jfs_stop_threads = 1;
659 wake_up_all(&jfs_commit_thread_wait);
660 for (i = 0; i < commit_threads; i++)
661 wait_for_completion(&jfsIOwait);
663 wake_up(&jfs_IO_thread_wait);
664 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
670 kmem_cache_destroy(jfs_inode_cachep);
674 static void __exit exit_jfs_fs(void)
678 jfs_info("exit_jfs_fs called");
680 jfs_stop_threads = 1;
683 wake_up(&jfs_IO_thread_wait);
684 wait_for_completion(&jfsIOwait); /* Wait until IO thread exits */
685 wake_up_all(&jfs_commit_thread_wait);
686 for (i = 0; i < commit_threads; i++)
687 wait_for_completion(&jfsIOwait);
688 wake_up(&jfs_sync_thread_wait);
689 wait_for_completion(&jfsIOwait); /* Wait until Sync thread exits */
693 unregister_filesystem(&jfs_fs_type);
694 kmem_cache_destroy(jfs_inode_cachep);
697 module_init(init_jfs_fs)
698 module_exit(exit_jfs_fs)