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 <linux/posix_acl.h>
28 #include <asm/uaccess.h>
30 #include "jfs_incore.h"
31 #include "jfs_filsys.h"
32 #include "jfs_inode.h"
33 #include "jfs_metapage.h"
34 #include "jfs_superblock.h"
38 #include "jfs_debug.h"
40 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
41 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
42 MODULE_LICENSE("GPL");
44 static kmem_cache_t
* jfs_inode_cachep
;
46 static struct super_operations jfs_super_operations
;
47 static struct export_operations jfs_export_operations
;
48 static struct file_system_type jfs_fs_type
;
50 #define MAX_COMMIT_THREADS 64
51 static int commit_threads
= 0;
52 module_param(commit_threads
, int, 0);
53 MODULE_PARM_DESC(commit_threads
, "Number of commit threads");
56 static pid_t jfsIOthread
;
57 static pid_t jfsCommitThread
[MAX_COMMIT_THREADS
];
58 static pid_t jfsSyncThread
;
59 DECLARE_COMPLETION(jfsIOwait
);
61 #ifdef CONFIG_JFS_DEBUG
62 int jfsloglevel
= JFS_LOGLEVEL_WARN
;
63 module_param(jfsloglevel
, int, 0644);
64 MODULE_PARM_DESC(jfsloglevel
, "Specify JFS loglevel (0, 1 or 2)");
67 static void jfs_handle_error(struct super_block
*sb
)
69 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
71 if (sb
->s_flags
& MS_RDONLY
)
74 updateSuper(sb
, FM_DIRTY
);
76 if (sbi
->flag
& JFS_ERR_PANIC
)
77 panic("JFS (device %s): panic forced after error\n",
79 else if (sbi
->flag
& JFS_ERR_REMOUNT_RO
) {
80 jfs_err("ERROR: (device %s): remounting filesystem "
83 sb
->s_flags
|= MS_RDONLY
;
86 /* nothing is done for continue beyond marking the superblock dirty */
89 void jfs_error(struct super_block
*sb
, const char * function
, ...)
91 static char error_buf
[256];
94 va_start(args
, function
);
95 vsprintf(error_buf
, function
, args
);
98 printk(KERN_ERR
"ERROR: (device %s): %s\n", sb
->s_id
, error_buf
);
100 jfs_handle_error(sb
);
103 static struct inode
*jfs_alloc_inode(struct super_block
*sb
)
105 struct jfs_inode_info
*jfs_inode
;
107 jfs_inode
= kmem_cache_alloc(jfs_inode_cachep
, GFP_NOFS
);
110 return &jfs_inode
->vfs_inode
;
113 static void jfs_destroy_inode(struct inode
*inode
)
115 struct jfs_inode_info
*ji
= JFS_IP(inode
);
117 spin_lock_irq(&ji
->ag_lock
);
118 if (ji
->active_ag
!= -1) {
119 struct bmap
*bmap
= JFS_SBI(inode
->i_sb
)->bmap
;
120 atomic_dec(&bmap
->db_active
[ji
->active_ag
]);
123 spin_unlock_irq(&ji
->ag_lock
);
125 #ifdef CONFIG_JFS_POSIX_ACL
126 if (ji
->i_acl
!= JFS_ACL_NOT_CACHED
) {
127 posix_acl_release(ji
->i_acl
);
128 ji
->i_acl
= JFS_ACL_NOT_CACHED
;
130 if (ji
->i_default_acl
!= JFS_ACL_NOT_CACHED
) {
131 posix_acl_release(ji
->i_default_acl
);
132 ji
->i_default_acl
= JFS_ACL_NOT_CACHED
;
136 kmem_cache_free(jfs_inode_cachep
, ji
);
139 static int jfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
141 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
143 struct inomap
*imap
= JFS_IP(sbi
->ipimap
)->i_imap
;
145 jfs_info("In jfs_statfs");
146 buf
->f_type
= JFS_SUPER_MAGIC
;
147 buf
->f_bsize
= sbi
->bsize
;
148 buf
->f_blocks
= sbi
->bmap
->db_mapsize
;
149 buf
->f_bfree
= sbi
->bmap
->db_nfree
;
150 buf
->f_bavail
= sbi
->bmap
->db_nfree
;
152 * If we really return the number of allocated & free inodes, some
153 * applications will fail because they won't see enough free inodes.
154 * We'll try to calculate some guess as to how may inodes we can
157 * buf->f_files = atomic_read(&imap->im_numinos);
158 * buf->f_ffree = atomic_read(&imap->im_numfree);
160 maxinodes
= min((s64
) atomic_read(&imap
->im_numinos
) +
161 ((sbi
->bmap
->db_nfree
>> imap
->im_l2nbperiext
)
162 << L2INOSPEREXT
), (s64
) 0xffffffffLL
);
163 buf
->f_files
= maxinodes
;
164 buf
->f_ffree
= maxinodes
- (atomic_read(&imap
->im_numinos
) -
165 atomic_read(&imap
->im_numfree
));
167 buf
->f_namelen
= JFS_NAME_MAX
;
171 static void jfs_put_super(struct super_block
*sb
)
173 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
176 jfs_info("In jfs_put_super");
179 jfs_err("jfs_umount failed with return code %d", rc
);
181 unload_nls(sbi
->nls_tab
);
184 truncate_inode_pages(sbi
->direct_inode
->i_mapping
, 0);
185 iput(sbi
->direct_inode
);
186 sbi
->direct_inode
= NULL
;
192 Opt_integrity
, Opt_nointegrity
, Opt_iocharset
, Opt_resize
,
193 Opt_resize_nosize
, Opt_errors
, Opt_ignore
, Opt_err
,
196 static match_table_t tokens
= {
197 {Opt_integrity
, "integrity"},
198 {Opt_nointegrity
, "nointegrity"},
199 {Opt_iocharset
, "iocharset=%s"},
200 {Opt_resize
, "resize=%u"},
201 {Opt_resize_nosize
, "resize"},
202 {Opt_errors
, "errors=%s"},
203 {Opt_ignore
, "noquota"},
204 {Opt_ignore
, "quota"},
205 {Opt_ignore
, "usrquota"},
206 {Opt_ignore
, "grpquota"},
210 static int parse_options(char *options
, struct super_block
*sb
, s64
*newLVSize
,
213 void *nls_map
= (void *)-1; /* -1: no change; NULL: none */
215 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
222 while ((p
= strsep(&options
, ",")) != NULL
) {
223 substring_t args
[MAX_OPT_ARGS
];
228 token
= match_token(p
, tokens
, args
);
231 *flag
&= ~JFS_NOINTEGRITY
;
233 case Opt_nointegrity
:
234 *flag
|= JFS_NOINTEGRITY
;
237 /* Silently ignore the quota options */
238 /* Don't do anything ;-) */
241 if (nls_map
&& nls_map
!= (void *) -1)
243 if (!strcmp(args
[0].from
, "none"))
246 nls_map
= load_nls(args
[0].from
);
249 "JFS: charset not found\n");
256 char *resize
= args
[0].from
;
257 *newLVSize
= simple_strtoull(resize
, &resize
, 0);
260 case Opt_resize_nosize
:
262 *newLVSize
= sb
->s_bdev
->bd_inode
->i_size
>>
263 sb
->s_blocksize_bits
;
266 "JFS: Cannot determine volume size\n");
271 char *errors
= args
[0].from
;
272 if (!errors
|| !*errors
)
274 if (!strcmp(errors
, "continue")) {
275 *flag
&= ~JFS_ERR_REMOUNT_RO
;
276 *flag
&= ~JFS_ERR_PANIC
;
277 *flag
|= JFS_ERR_CONTINUE
;
278 } else if (!strcmp(errors
, "remount-ro")) {
279 *flag
&= ~JFS_ERR_CONTINUE
;
280 *flag
&= ~JFS_ERR_PANIC
;
281 *flag
|= JFS_ERR_REMOUNT_RO
;
282 } else if (!strcmp(errors
, "panic")) {
283 *flag
&= ~JFS_ERR_CONTINUE
;
284 *flag
&= ~JFS_ERR_REMOUNT_RO
;
285 *flag
|= JFS_ERR_PANIC
;
288 "JFS: %s is an invalid error handler\n",
295 printk("jfs: Unrecognized mount option \"%s\" "
296 " or missing value\n", p
);
301 if (nls_map
!= (void *) -1) {
302 /* Discard old (if remount) */
304 unload_nls(sbi
->nls_tab
);
305 sbi
->nls_tab
= nls_map
;
310 if (nls_map
&& nls_map
!= (void *) -1)
315 static int jfs_remount(struct super_block
*sb
, int *flags
, char *data
)
319 int flag
= JFS_SBI(sb
)->flag
;
321 if (!parse_options(data
, sb
, &newLVSize
, &flag
)) {
325 if (sb
->s_flags
& MS_RDONLY
) {
327 "JFS: resize requires volume to be mounted read-write\n");
330 rc
= jfs_extendfs(sb
, newLVSize
, 0);
335 if ((sb
->s_flags
& MS_RDONLY
) && !(*flags
& MS_RDONLY
)) {
337 * Invalidate any previously read metadata. fsck may have
338 * changed the on-disk data since we mounted r/o
340 truncate_inode_pages(JFS_SBI(sb
)->direct_inode
->i_mapping
, 0);
342 JFS_SBI(sb
)->flag
= flag
;
343 return jfs_mount_rw(sb
, 1);
345 if ((!(sb
->s_flags
& MS_RDONLY
)) && (*flags
& MS_RDONLY
)) {
346 rc
= jfs_umount_rw(sb
);
347 JFS_SBI(sb
)->flag
= flag
;
350 if ((JFS_SBI(sb
)->flag
& JFS_NOINTEGRITY
) != (flag
& JFS_NOINTEGRITY
))
351 if (!(sb
->s_flags
& MS_RDONLY
)) {
352 rc
= jfs_umount_rw(sb
);
355 JFS_SBI(sb
)->flag
= flag
;
356 return jfs_mount_rw(sb
, 1);
358 JFS_SBI(sb
)->flag
= flag
;
363 static int jfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
365 struct jfs_sb_info
*sbi
;
371 jfs_info("In jfs_read_super: s_flags=0x%lx", sb
->s_flags
);
373 if (!new_valid_dev(sb
->s_bdev
->bd_dev
))
376 sbi
= kmalloc(sizeof (struct jfs_sb_info
), GFP_KERNEL
);
379 memset(sbi
, 0, sizeof (struct jfs_sb_info
));
383 /* initialize the mount flag and determine the default error handler */
384 flag
= JFS_ERR_REMOUNT_RO
;
386 if (!parse_options((char *) data
, sb
, &newLVSize
, &flag
)) {
392 #ifdef CONFIG_JFS_POSIX_ACL
393 sb
->s_flags
|= MS_POSIXACL
;
397 printk(KERN_ERR
"resize option for remount only\n");
402 * Initialize blocksize to 4K.
404 sb_set_blocksize(sb
, PSIZE
);
407 * Set method vectors.
409 sb
->s_op
= &jfs_super_operations
;
410 sb
->s_export_op
= &jfs_export_operations
;
413 * Initialize direct-mapping inode/address-space
415 inode
= new_inode(sb
);
420 inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
421 inode
->i_mapping
->a_ops
= &jfs_metapage_aops
;
422 mapping_set_gfp_mask(inode
->i_mapping
, GFP_NOFS
);
424 sbi
->direct_inode
= inode
;
429 jfs_err("jfs_mount failed w/return code = %d", rc
);
431 goto out_mount_failed
;
433 if (sb
->s_flags
& MS_RDONLY
)
436 rc
= jfs_mount_rw(sb
, 0);
439 jfs_err("jfs_mount_rw failed, return code = %d",
446 sb
->s_magic
= JFS_SUPER_MAGIC
;
448 inode
= iget(sb
, ROOT_I
);
449 if (!inode
|| is_bad_inode(inode
))
451 sb
->s_root
= d_alloc_root(inode
);
455 if (sbi
->mntflag
& JFS_OS2
)
456 sb
->s_root
->d_op
= &jfs_ci_dentry_operations
;
458 /* logical blocks are represented by 40 bits in pxd_t, etc. */
459 sb
->s_maxbytes
= ((u64
) sb
->s_blocksize
) << 40;
460 #if BITS_PER_LONG == 32
462 * Page cache is indexed by long.
463 * I would use MAX_LFS_FILESIZE, but it's only half as big
465 sb
->s_maxbytes
= min(((u64
) PAGE_CACHE_SIZE
<< 32) - 1, sb
->s_maxbytes
);
471 jfs_err("jfs_read_super: get root inode failed");
478 jfs_err("jfs_umount failed with return code %d", rc
);
481 filemap_fdatawrite(sbi
->direct_inode
->i_mapping
);
482 filemap_fdatawait(sbi
->direct_inode
->i_mapping
);
483 truncate_inode_pages(sbi
->direct_inode
->i_mapping
, 0);
484 make_bad_inode(sbi
->direct_inode
);
485 iput(sbi
->direct_inode
);
486 sbi
->direct_inode
= NULL
;
489 unload_nls(sbi
->nls_tab
);
494 static void jfs_write_super_lockfs(struct super_block
*sb
)
496 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
497 struct jfs_log
*log
= sbi
->log
;
499 if (!(sb
->s_flags
& MS_RDONLY
)) {
502 updateSuper(sb
, FM_CLEAN
);
506 static void jfs_unlockfs(struct super_block
*sb
)
508 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
509 struct jfs_log
*log
= sbi
->log
;
512 if (!(sb
->s_flags
& MS_RDONLY
)) {
513 updateSuper(sb
, FM_MOUNT
);
514 if ((rc
= lmLogInit(log
)))
515 jfs_err("jfs_unlock failed with return code %d", rc
);
521 static struct super_block
*jfs_get_sb(struct file_system_type
*fs_type
,
522 int flags
, const char *dev_name
, void *data
)
524 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, jfs_fill_super
);
527 static int jfs_sync_fs(struct super_block
*sb
, int wait
)
529 struct jfs_log
*log
= JFS_SBI(sb
)->log
;
531 /* log == NULL indicates read-only mount */
533 jfs_flush_journal(log
, wait
);
540 static struct super_operations jfs_super_operations
= {
541 .alloc_inode
= jfs_alloc_inode
,
542 .destroy_inode
= jfs_destroy_inode
,
543 .read_inode
= jfs_read_inode
,
544 .dirty_inode
= jfs_dirty_inode
,
545 .write_inode
= jfs_write_inode
,
546 .delete_inode
= jfs_delete_inode
,
547 .put_super
= jfs_put_super
,
548 .sync_fs
= jfs_sync_fs
,
549 .write_super_lockfs
= jfs_write_super_lockfs
,
550 .unlockfs
= jfs_unlockfs
,
551 .statfs
= jfs_statfs
,
552 .remount_fs
= jfs_remount
,
555 static struct export_operations jfs_export_operations
= {
556 .get_parent
= jfs_get_parent
,
559 static struct file_system_type jfs_fs_type
= {
560 .owner
= THIS_MODULE
,
562 .get_sb
= jfs_get_sb
,
563 .kill_sb
= kill_block_super
,
564 .fs_flags
= FS_REQUIRES_DEV
,
567 static void init_once(void *foo
, kmem_cache_t
* cachep
, unsigned long flags
)
569 struct jfs_inode_info
*jfs_ip
= (struct jfs_inode_info
*) foo
;
571 if ((flags
& (SLAB_CTOR_VERIFY
| SLAB_CTOR_CONSTRUCTOR
)) ==
572 SLAB_CTOR_CONSTRUCTOR
) {
573 memset(jfs_ip
, 0, sizeof(struct jfs_inode_info
));
574 INIT_LIST_HEAD(&jfs_ip
->anon_inode_list
);
575 init_rwsem(&jfs_ip
->rdwrlock
);
576 init_MUTEX(&jfs_ip
->commit_sem
);
577 init_rwsem(&jfs_ip
->xattr_sem
);
578 spin_lock_init(&jfs_ip
->ag_lock
);
579 jfs_ip
->active_ag
= -1;
580 #ifdef CONFIG_JFS_POSIX_ACL
581 jfs_ip
->i_acl
= JFS_ACL_NOT_CACHED
;
582 jfs_ip
->i_default_acl
= JFS_ACL_NOT_CACHED
;
584 inode_init_once(&jfs_ip
->vfs_inode
);
588 static int __init
init_jfs_fs(void)
594 kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info
), 0,
595 SLAB_RECLAIM_ACCOUNT
, init_once
, NULL
);
596 if (jfs_inode_cachep
== NULL
)
600 * Metapage initialization
602 rc
= metapage_init();
604 jfs_err("metapage_init failed w/rc = %d", rc
);
609 * Transaction Manager initialization
613 jfs_err("txInit failed w/rc = %d", rc
);
618 * I/O completion thread (endio)
620 jfsIOthread
= kernel_thread(jfsIOWait
, NULL
, CLONE_KERNEL
);
621 if (jfsIOthread
< 0) {
622 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread
);
625 wait_for_completion(&jfsIOwait
); /* Wait until thread starts */
627 if (commit_threads
< 1)
628 commit_threads
= num_online_cpus();
629 if (commit_threads
> MAX_COMMIT_THREADS
)
630 commit_threads
= MAX_COMMIT_THREADS
;
632 for (i
= 0; i
< commit_threads
; i
++) {
633 jfsCommitThread
[i
] = kernel_thread(jfs_lazycommit
, NULL
,
635 if (jfsCommitThread
[i
] < 0) {
636 jfs_err("init_jfs_fs: fork failed w/rc = %d",
639 goto kill_committask
;
641 /* Wait until thread starts */
642 wait_for_completion(&jfsIOwait
);
645 jfsSyncThread
= kernel_thread(jfs_sync
, NULL
, CLONE_KERNEL
);
646 if (jfsSyncThread
< 0) {
647 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread
);
648 goto kill_committask
;
650 wait_for_completion(&jfsIOwait
); /* Wait until thread starts */
656 return register_filesystem(&jfs_fs_type
);
659 jfs_stop_threads
= 1;
660 wake_up_all(&jfs_commit_thread_wait
);
661 for (i
= 0; i
< commit_threads
; i
++)
662 wait_for_completion(&jfsIOwait
);
664 wake_up(&jfs_IO_thread_wait
);
665 wait_for_completion(&jfsIOwait
); /* Wait for thread exit */
671 kmem_cache_destroy(jfs_inode_cachep
);
675 static void __exit
exit_jfs_fs(void)
679 jfs_info("exit_jfs_fs called");
681 jfs_stop_threads
= 1;
684 wake_up(&jfs_IO_thread_wait
);
685 wait_for_completion(&jfsIOwait
); /* Wait until IO thread exits */
686 wake_up_all(&jfs_commit_thread_wait
);
687 for (i
= 0; i
< commit_threads
; i
++)
688 wait_for_completion(&jfsIOwait
);
689 wake_up(&jfs_sync_thread_wait
);
690 wait_for_completion(&jfsIOwait
); /* Wait until Sync thread exits */
694 unregister_filesystem(&jfs_fs_type
);
695 kmem_cache_destroy(jfs_inode_cachep
);
698 module_init(init_jfs_fs
)
699 module_exit(exit_jfs_fs
)