2 * Copyright (C) International Business Machines Corp., 2000-2003
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 <asm/uaccess.h>
28 #include "jfs_incore.h"
29 #include "jfs_filsys.h"
30 #include "jfs_metapage.h"
31 #include "jfs_superblock.h"
35 #include "jfs_debug.h"
37 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
38 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
39 MODULE_LICENSE("GPL");
41 static kmem_cache_t
* jfs_inode_cachep
;
43 static struct super_operations jfs_super_operations
;
44 static struct export_operations jfs_export_operations
;
45 static struct file_system_type jfs_fs_type
;
48 static pid_t jfsIOthread
;
49 static pid_t jfsCommitThread
;
50 static pid_t jfsSyncThread
;
51 DECLARE_COMPLETION(jfsIOwait
);
53 #ifdef CONFIG_JFS_DEBUG
54 int jfsloglevel
= JFS_LOGLEVEL_WARN
;
55 MODULE_PARM(jfsloglevel
, "i");
56 MODULE_PARM_DESC(jfsloglevel
, "Specify JFS loglevel (0, 1 or 2)");
60 * External declarations
62 extern int jfs_mount(struct super_block
*);
63 extern int jfs_mount_rw(struct super_block
*, int);
64 extern int jfs_umount(struct super_block
*);
65 extern int jfs_umount_rw(struct super_block
*);
67 extern int jfsIOWait(void *);
68 extern int jfs_lazycommit(void *);
69 extern int jfs_sync(void *);
71 extern void jfs_read_inode(struct inode
*inode
);
72 extern void jfs_dirty_inode(struct inode
*inode
);
73 extern void jfs_delete_inode(struct inode
*inode
);
74 extern void jfs_write_inode(struct inode
*inode
, int wait
);
76 extern struct dentry
*jfs_get_parent(struct dentry
*dentry
);
77 extern int jfs_extendfs(struct super_block
*, s64
, int);
79 #ifdef PROC_FS_JFS /* see jfs_debug.h */
80 extern void jfs_proc_init(void);
81 extern void jfs_proc_clean(void);
84 extern wait_queue_head_t jfs_IO_thread_wait
;
85 extern wait_queue_head_t jfs_commit_thread_wait
;
86 extern wait_queue_head_t jfs_sync_thread_wait
;
88 static void jfs_handle_error(struct super_block
*sb
)
90 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
92 if (sb
->s_flags
& MS_RDONLY
)
95 updateSuper(sb
, FM_DIRTY
);
97 if (sbi
->flag
& JFS_ERR_PANIC
)
98 panic("JFS (device %s): panic forced after error\n",
100 else if (sbi
->flag
& JFS_ERR_REMOUNT_RO
) {
101 jfs_err("ERROR: (device %s): remounting filesystem "
104 sb
->s_flags
|= MS_RDONLY
;
107 /* nothing is done for continue beyond marking the superblock dirty */
110 void jfs_error(struct super_block
*sb
, const char * function
, ...)
112 static char error_buf
[256];
115 va_start(args
, function
);
116 vsprintf(error_buf
, function
, args
);
119 printk(KERN_ERR
"ERROR: (device %s): %s\n", sb
->s_id
, error_buf
);
121 jfs_handle_error(sb
);
124 static struct inode
*jfs_alloc_inode(struct super_block
*sb
)
126 struct jfs_inode_info
*jfs_inode
;
128 jfs_inode
= kmem_cache_alloc(jfs_inode_cachep
, GFP_NOFS
);
131 return &jfs_inode
->vfs_inode
;
134 static void jfs_destroy_inode(struct inode
*inode
)
136 struct jfs_inode_info
*ji
= JFS_IP(inode
);
138 if (ji
->active_ag
!= -1) {
139 struct bmap
*bmap
= JFS_SBI(inode
->i_sb
)->bmap
;
140 atomic_dec(&bmap
->db_active
[ji
->active_ag
]);
143 #ifdef CONFIG_JFS_POSIX_ACL
144 if (ji
->i_acl
!= JFS_ACL_NOT_CACHED
) {
145 posix_acl_release(ji
->i_acl
);
146 ji
->i_acl
= JFS_ACL_NOT_CACHED
;
148 if (ji
->i_default_acl
!= JFS_ACL_NOT_CACHED
) {
149 posix_acl_release(ji
->i_default_acl
);
150 ji
->i_default_acl
= JFS_ACL_NOT_CACHED
;
154 kmem_cache_free(jfs_inode_cachep
, ji
);
157 static int jfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
159 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
161 struct inomap
*imap
= JFS_IP(sbi
->ipimap
)->i_imap
;
163 jfs_info("In jfs_statfs");
164 buf
->f_type
= JFS_SUPER_MAGIC
;
165 buf
->f_bsize
= sbi
->bsize
;
166 buf
->f_blocks
= sbi
->bmap
->db_mapsize
;
167 buf
->f_bfree
= sbi
->bmap
->db_nfree
;
168 buf
->f_bavail
= sbi
->bmap
->db_nfree
;
170 * If we really return the number of allocated & free inodes, some
171 * applications will fail because they won't see enough free inodes.
172 * We'll try to calculate some guess as to how may inodes we can
175 * buf->f_files = atomic_read(&imap->im_numinos);
176 * buf->f_ffree = atomic_read(&imap->im_numfree);
178 maxinodes
= min((s64
) atomic_read(&imap
->im_numinos
) +
179 ((sbi
->bmap
->db_nfree
>> imap
->im_l2nbperiext
)
180 << L2INOSPEREXT
), (s64
) 0xffffffffLL
);
181 buf
->f_files
= maxinodes
;
182 buf
->f_ffree
= maxinodes
- (atomic_read(&imap
->im_numinos
) -
183 atomic_read(&imap
->im_numfree
));
185 buf
->f_namelen
= JFS_NAME_MAX
;
189 static void jfs_put_super(struct super_block
*sb
)
191 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
194 jfs_info("In jfs_put_super");
197 jfs_err("jfs_umount failed with return code %d", rc
);
198 unload_nls(sbi
->nls_tab
);
205 Opt_integrity
, Opt_nointegrity
, Opt_iocharset
, Opt_resize
,
206 Opt_errors
, Opt_ignore
, Opt_err
,
209 static match_table_t tokens
= {
210 {Opt_integrity
, "integrity"},
211 {Opt_nointegrity
, "nointegrity"},
212 {Opt_iocharset
, "iocharset=%s"},
213 {Opt_resize
, "resize=%u"},
214 {Opt_errors
, "errors=%s"},
215 {Opt_ignore
, "noquota"},
216 {Opt_ignore
, "quota"},
217 {Opt_ignore
, "usrquota"},
218 {Opt_ignore
, "grpquota"},
222 static int parse_options(char *options
, struct super_block
*sb
, s64
*newLVSize
,
225 void *nls_map
= NULL
;
227 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
234 while ((p
= strsep(&options
, ",")) != NULL
) {
235 substring_t args
[MAX_OPT_ARGS
];
240 token
= match_token(p
, tokens
, args
);
243 *flag
&= ~JFS_NOINTEGRITY
;
245 case Opt_nointegrity
:
246 *flag
|= JFS_NOINTEGRITY
;
249 /* Silently ignore the quota options */
250 /* Don't do anything ;-) */
253 if (nls_map
) /* specified iocharset twice! */
255 nls_map
= load_nls(args
[0].from
);
257 printk(KERN_ERR
"JFS: charset not found\n");
263 char *resize
= args
[0].from
;
264 if (!resize
|| !*resize
) {
265 *newLVSize
= sb
->s_bdev
->bd_inode
->i_size
>>
266 sb
->s_blocksize_bits
;
269 "JFS: Cannot determine volume size\n");
271 *newLVSize
= simple_strtoull(resize
, &resize
, 0);
276 char *errors
= args
[0].from
;
277 if (!errors
|| !*errors
)
279 if (!strcmp(errors
, "continue")) {
280 *flag
&= ~JFS_ERR_REMOUNT_RO
;
281 *flag
&= ~JFS_ERR_PANIC
;
282 *flag
|= JFS_ERR_CONTINUE
;
283 } else if (!strcmp(errors
, "remount-ro")) {
284 *flag
&= ~JFS_ERR_CONTINUE
;
285 *flag
&= ~JFS_ERR_PANIC
;
286 *flag
|= JFS_ERR_REMOUNT_RO
;
287 } else if (!strcmp(errors
, "panic")) {
288 *flag
&= ~JFS_ERR_CONTINUE
;
289 *flag
&= ~JFS_ERR_REMOUNT_RO
;
290 *flag
|= JFS_ERR_PANIC
;
293 "JFS: %s is an invalid error handler\n",
300 printk("jfs: Unrecognized mount option \"%s\" "
301 " or missing value\n", p
);
307 /* Discard old (if remount) */
309 unload_nls(sbi
->nls_tab
);
310 sbi
->nls_tab
= nls_map
;
320 int jfs_remount(struct super_block
*sb
, int *flags
, char *data
)
324 int flag
= JFS_SBI(sb
)->flag
;
326 if (!parse_options(data
, sb
, &newLVSize
, &flag
)) {
330 if (sb
->s_flags
& MS_RDONLY
) {
332 "JFS: resize requires volume to be mounted read-write\n");
335 rc
= jfs_extendfs(sb
, newLVSize
, 0);
340 if ((sb
->s_flags
& MS_RDONLY
) && !(*flags
& MS_RDONLY
)) {
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
));
381 /* initialize the mount flag and determine the default error handler */
382 flag
= JFS_ERR_REMOUNT_RO
;
384 if (!parse_options((char *) data
, sb
, &newLVSize
, &flag
)) {
391 printk(KERN_ERR
"resize option for remount only\n");
396 * Initialize blocksize to 4K.
398 sb_set_blocksize(sb
, PSIZE
);
401 * Set method vectors.
403 sb
->s_op
= &jfs_super_operations
;
404 sb
->s_export_op
= &jfs_export_operations
;
409 jfs_err("jfs_mount failed w/return code = %d", rc
);
413 if (sb
->s_flags
& MS_RDONLY
)
416 rc
= jfs_mount_rw(sb
, 0);
419 jfs_err("jfs_mount_rw failed, return code = %d",
426 sb
->s_magic
= JFS_SUPER_MAGIC
;
428 inode
= iget(sb
, ROOT_I
);
429 if (!inode
|| is_bad_inode(inode
))
431 sb
->s_root
= d_alloc_root(inode
);
436 sbi
->nls_tab
= load_nls_default();
438 /* logical blocks are represented by 40 bits in pxd_t, etc. */
439 sb
->s_maxbytes
= ((u64
) sb
->s_blocksize
) << 40;
440 #if BITS_PER_LONG == 32
442 * Page cache is indexed by long.
443 * I would use MAX_LFS_FILESIZE, but it's only half as big
445 sb
->s_maxbytes
= min(((u64
) PAGE_CACHE_SIZE
<< 32) - 1, sb
->s_maxbytes
);
451 jfs_err("jfs_read_super: get root inode failed");
458 jfs_err("jfs_umount failed with return code %d", rc
);
462 unload_nls(sbi
->nls_tab
);
467 static void jfs_write_super_lockfs(struct super_block
*sb
)
469 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
470 struct jfs_log
*log
= sbi
->log
;
472 if (!(sb
->s_flags
& MS_RDONLY
)) {
475 updateSuper(sb
, FM_CLEAN
);
479 static void jfs_unlockfs(struct super_block
*sb
)
481 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
482 struct jfs_log
*log
= sbi
->log
;
485 if (!(sb
->s_flags
& MS_RDONLY
)) {
486 updateSuper(sb
, FM_MOUNT
);
487 if ((rc
= lmLogInit(log
)))
488 jfs_err("jfs_unlock failed with return code %d", rc
);
494 static struct super_block
*jfs_get_sb(struct file_system_type
*fs_type
,
495 int flags
, const char *dev_name
, void *data
)
497 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, jfs_fill_super
);
500 static int jfs_sync_fs(struct super_block
*sb
, int wait
)
502 struct jfs_log
*log
= JFS_SBI(sb
)->log
;
504 /* log == NULL indicates read-only mount */
506 jfs_flush_journal(log
, wait
);
511 static struct super_operations jfs_super_operations
= {
512 .alloc_inode
= jfs_alloc_inode
,
513 .destroy_inode
= jfs_destroy_inode
,
514 .read_inode
= jfs_read_inode
,
515 .dirty_inode
= jfs_dirty_inode
,
516 .write_inode
= jfs_write_inode
,
517 .delete_inode
= jfs_delete_inode
,
518 .put_super
= jfs_put_super
,
519 .sync_fs
= jfs_sync_fs
,
520 .write_super_lockfs
= jfs_write_super_lockfs
,
521 .unlockfs
= jfs_unlockfs
,
522 .statfs
= jfs_statfs
,
523 .remount_fs
= jfs_remount
,
526 static struct export_operations jfs_export_operations
= {
527 .get_parent
= jfs_get_parent
,
530 static struct file_system_type jfs_fs_type
= {
531 .owner
= THIS_MODULE
,
533 .get_sb
= jfs_get_sb
,
534 .kill_sb
= kill_block_super
,
535 .fs_flags
= FS_REQUIRES_DEV
,
538 extern int metapage_init(void);
539 extern int txInit(void);
540 extern void txExit(void);
541 extern void metapage_exit(void);
543 static void init_once(void *foo
, kmem_cache_t
* cachep
, unsigned long flags
)
545 struct jfs_inode_info
*jfs_ip
= (struct jfs_inode_info
*) foo
;
547 if ((flags
& (SLAB_CTOR_VERIFY
| SLAB_CTOR_CONSTRUCTOR
)) ==
548 SLAB_CTOR_CONSTRUCTOR
) {
549 INIT_LIST_HEAD(&jfs_ip
->anon_inode_list
);
550 init_rwsem(&jfs_ip
->rdwrlock
);
551 init_MUTEX(&jfs_ip
->commit_sem
);
552 init_rwsem(&jfs_ip
->xattr_sem
);
554 jfs_ip
->active_ag
= -1;
555 #ifdef CONFIG_JFS_POSIX_ACL
556 jfs_ip
->i_acl
= JFS_ACL_NOT_CACHED
;
557 jfs_ip
->i_default_acl
= JFS_ACL_NOT_CACHED
;
559 inode_init_once(&jfs_ip
->vfs_inode
);
563 static int __init
init_jfs_fs(void)
568 kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info
), 0,
569 SLAB_RECLAIM_ACCOUNT
, init_once
, NULL
);
570 if (jfs_inode_cachep
== NULL
)
574 * Metapage initialization
576 rc
= metapage_init();
578 jfs_err("metapage_init failed w/rc = %d", rc
);
583 * Transaction Manager initialization
587 jfs_err("txInit failed w/rc = %d", rc
);
592 * I/O completion thread (endio)
594 jfsIOthread
= kernel_thread(jfsIOWait
, 0, CLONE_KERNEL
);
595 if (jfsIOthread
< 0) {
596 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread
);
599 wait_for_completion(&jfsIOwait
); /* Wait until thread starts */
601 jfsCommitThread
= kernel_thread(jfs_lazycommit
, 0, CLONE_KERNEL
);
602 if (jfsCommitThread
< 0) {
603 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsCommitThread
);
606 wait_for_completion(&jfsIOwait
); /* Wait until thread starts */
608 jfsSyncThread
= kernel_thread(jfs_sync
, 0, CLONE_KERNEL
);
609 if (jfsSyncThread
< 0) {
610 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread
);
611 goto kill_committask
;
613 wait_for_completion(&jfsIOwait
); /* Wait until thread starts */
619 return register_filesystem(&jfs_fs_type
);
622 jfs_stop_threads
= 1;
623 wake_up(&jfs_commit_thread_wait
);
624 wait_for_completion(&jfsIOwait
); /* Wait for thread exit */
626 jfs_stop_threads
= 1;
627 wake_up(&jfs_IO_thread_wait
);
628 wait_for_completion(&jfsIOwait
); /* Wait for thread exit */
634 kmem_cache_destroy(jfs_inode_cachep
);
638 static void __exit
exit_jfs_fs(void)
640 jfs_info("exit_jfs_fs called");
642 jfs_stop_threads
= 1;
645 wake_up(&jfs_IO_thread_wait
);
646 wait_for_completion(&jfsIOwait
); /* Wait until IO thread exits */
647 wake_up(&jfs_commit_thread_wait
);
648 wait_for_completion(&jfsIOwait
); /* Wait until Commit thread exits */
649 wake_up(&jfs_sync_thread_wait
);
650 wait_for_completion(&jfsIOwait
); /* Wait until Sync thread exits */
654 unregister_filesystem(&jfs_fs_type
);
655 kmem_cache_destroy(jfs_inode_cachep
);
658 module_init(init_jfs_fs
)
659 module_exit(exit_jfs_fs
)