Merge with 2.5.75.
[linux-2.6/linux-mips.git] / fs / jfs / super.c
blobd733da4a2b7c0d1a0625da4fe76d8e8d26c6c29a
1 /*
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.
9 *
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/fs.h>
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/completion.h>
24 #include <linux/vfs.h>
25 #include <asm/uaccess.h>
27 #include "jfs_incore.h"
28 #include "jfs_filsys.h"
29 #include "jfs_metapage.h"
30 #include "jfs_superblock.h"
31 #include "jfs_dmap.h"
32 #include "jfs_imap.h"
33 #include "jfs_acl.h"
34 #include "jfs_debug.h"
36 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
37 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
38 MODULE_LICENSE("GPL");
40 static kmem_cache_t * jfs_inode_cachep;
42 static struct super_operations jfs_super_operations;
43 static struct export_operations jfs_export_operations;
44 static struct file_system_type jfs_fs_type;
46 int jfs_stop_threads;
47 static pid_t jfsIOthread;
48 static pid_t jfsCommitThread;
49 static pid_t jfsSyncThread;
50 DECLARE_COMPLETION(jfsIOwait);
52 #ifdef CONFIG_JFS_DEBUG
53 int jfsloglevel = JFS_LOGLEVEL_WARN;
54 MODULE_PARM(jfsloglevel, "i");
55 MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
56 #endif
59 * External declarations
61 extern int jfs_mount(struct super_block *);
62 extern int jfs_mount_rw(struct super_block *, int);
63 extern int jfs_umount(struct super_block *);
64 extern int jfs_umount_rw(struct super_block *);
66 extern int jfsIOWait(void *);
67 extern int jfs_lazycommit(void *);
68 extern int jfs_sync(void *);
70 extern void jfs_read_inode(struct inode *inode);
71 extern void jfs_dirty_inode(struct inode *inode);
72 extern void jfs_delete_inode(struct inode *inode);
73 extern void jfs_write_inode(struct inode *inode, int wait);
75 extern struct dentry *jfs_get_parent(struct dentry *dentry);
76 extern int jfs_extendfs(struct super_block *, s64, int);
78 #ifdef PROC_FS_JFS /* see jfs_debug.h */
79 extern void jfs_proc_init(void);
80 extern void jfs_proc_clean(void);
81 #endif
83 extern wait_queue_head_t jfs_IO_thread_wait;
84 extern wait_queue_head_t jfs_commit_thread_wait;
85 extern wait_queue_head_t jfs_sync_thread_wait;
87 static struct inode *jfs_alloc_inode(struct super_block *sb)
89 struct jfs_inode_info *jfs_inode;
91 jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
92 if (!jfs_inode)
93 return NULL;
94 return &jfs_inode->vfs_inode;
97 static void jfs_destroy_inode(struct inode *inode)
99 struct jfs_inode_info *ji = JFS_IP(inode);
101 if (ji->active_ag != -1) {
102 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
103 atomic_dec(&bmap->db_active[ji->active_ag]);
106 #ifdef CONFIG_JFS_POSIX_ACL
107 if (ji->i_acl != JFS_ACL_NOT_CACHED) {
108 posix_acl_release(ji->i_acl);
109 ji->i_acl = JFS_ACL_NOT_CACHED;
111 if (ji->i_default_acl != JFS_ACL_NOT_CACHED) {
112 posix_acl_release(ji->i_default_acl);
113 ji->i_default_acl = JFS_ACL_NOT_CACHED;
115 #endif
117 kmem_cache_free(jfs_inode_cachep, ji);
120 static int jfs_statfs(struct super_block *sb, struct kstatfs *buf)
122 struct jfs_sb_info *sbi = JFS_SBI(sb);
123 s64 maxinodes;
124 struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
126 jfs_info("In jfs_statfs");
127 buf->f_type = JFS_SUPER_MAGIC;
128 buf->f_bsize = sbi->bsize;
129 buf->f_blocks = sbi->bmap->db_mapsize;
130 buf->f_bfree = sbi->bmap->db_nfree;
131 buf->f_bavail = sbi->bmap->db_nfree;
133 * If we really return the number of allocated & free inodes, some
134 * applications will fail because they won't see enough free inodes.
135 * We'll try to calculate some guess as to how may inodes we can
136 * really allocate
138 * buf->f_files = atomic_read(&imap->im_numinos);
139 * buf->f_ffree = atomic_read(&imap->im_numfree);
141 maxinodes = min((s64) atomic_read(&imap->im_numinos) +
142 ((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
143 << L2INOSPEREXT), (s64) 0xffffffffLL);
144 buf->f_files = maxinodes;
145 buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
146 atomic_read(&imap->im_numfree));
148 buf->f_namelen = JFS_NAME_MAX;
149 return 0;
152 static void jfs_put_super(struct super_block *sb)
154 struct jfs_sb_info *sbi = JFS_SBI(sb);
155 int rc;
157 jfs_info("In jfs_put_super");
158 rc = jfs_umount(sb);
159 if (rc)
160 jfs_err("jfs_umount failed with return code %d", rc);
161 unload_nls(sbi->nls_tab);
162 sbi->nls_tab = NULL;
164 kfree(sbi);
167 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
168 int *flag)
170 void *nls_map = NULL;
171 char *this_char;
172 char *value;
173 struct jfs_sb_info *sbi = JFS_SBI(sb);
175 *newLVSize = 0;
177 if (!options)
178 return 1;
179 while ((this_char = strsep(&options, ",")) != NULL) {
180 if (!*this_char)
181 continue;
182 if ((value = strchr(this_char, '=')) != NULL)
183 *value++ = 0;
184 if (!strcmp(this_char, "integrity")) {
185 *flag &= ~JFS_NOINTEGRITY;
186 } else if (!strcmp(this_char, "nointegrity")) {
187 *flag |= JFS_NOINTEGRITY;
188 } else if (!strcmp(this_char, "iocharset")) {
189 if (!value || !*value)
190 goto needs_arg;
191 if (nls_map) /* specified iocharset twice! */
192 unload_nls(nls_map);
193 nls_map = load_nls(value);
194 if (!nls_map) {
195 printk(KERN_ERR "JFS: charset not found\n");
196 goto cleanup;
198 } else if (!strcmp(this_char, "resize")) {
199 if (!value || !*value) {
200 *newLVSize = sb->s_bdev->bd_inode->i_size >>
201 sb->s_blocksize_bits;
202 if (*newLVSize == 0)
203 printk(KERN_ERR
204 "JFS: Cannot determine volume size\n");
205 } else
206 *newLVSize = simple_strtoull(value, &value, 0);
208 /* Silently ignore the quota options */
209 } else if (!strcmp(this_char, "grpquota")
210 || !strcmp(this_char, "noquota")
211 || !strcmp(this_char, "quota")
212 || !strcmp(this_char, "usrquota"))
213 /* Don't do anything ;-) */ ;
214 else {
215 printk("jfs: Unrecognized mount option %s\n",
216 this_char);
217 goto cleanup;
220 if (nls_map) {
221 /* Discard old (if remount) */
222 if (sbi->nls_tab)
223 unload_nls(sbi->nls_tab);
224 sbi->nls_tab = nls_map;
226 return 1;
227 needs_arg:
228 printk(KERN_ERR "JFS: %s needs an argument\n", this_char);
229 cleanup:
230 if (nls_map)
231 unload_nls(nls_map);
232 return 0;
235 int jfs_remount(struct super_block *sb, int *flags, char *data)
237 s64 newLVSize = 0;
238 int rc = 0;
239 int flag = JFS_SBI(sb)->flag;
241 if (!parse_options(data, sb, &newLVSize, &flag)) {
242 return -EINVAL;
244 if (newLVSize) {
245 if (sb->s_flags & MS_RDONLY) {
246 printk(KERN_ERR
247 "JFS: resize requires volume to be mounted read-write\n");
248 return -EROFS;
250 rc = jfs_extendfs(sb, newLVSize, 0);
251 if (rc)
252 return rc;
255 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
256 JFS_SBI(sb)->flag = flag;
257 return jfs_mount_rw(sb, 1);
259 if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
260 rc = jfs_umount_rw(sb);
261 JFS_SBI(sb)->flag = flag;
262 return rc;
264 if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
265 if (!(sb->s_flags & MS_RDONLY)) {
266 rc = jfs_umount_rw(sb);
267 if (rc)
268 return rc;
269 JFS_SBI(sb)->flag = flag;
270 return jfs_mount_rw(sb, 1);
272 JFS_SBI(sb)->flag = flag;
274 return 0;
277 static int jfs_fill_super(struct super_block *sb, void *data, int silent)
279 struct jfs_sb_info *sbi;
280 struct inode *inode;
281 int rc;
282 s64 newLVSize = 0;
283 int flag;
285 jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
287 sbi = kmalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
288 if (!sbi)
289 return -ENOSPC;
290 memset(sbi, 0, sizeof (struct jfs_sb_info));
291 sb->s_fs_info = sbi;
293 flag = 0;
294 if (!parse_options((char *) data, sb, &newLVSize, &flag)) {
295 kfree(sbi);
296 return -EINVAL;
298 sbi->flag = flag;
300 if (newLVSize) {
301 printk(KERN_ERR "resize option for remount only\n");
302 return -EINVAL;
306 * Initialize blocksize to 4K.
308 sb_set_blocksize(sb, PSIZE);
311 * Set method vectors.
313 sb->s_op = &jfs_super_operations;
314 sb->s_export_op = &jfs_export_operations;
316 rc = jfs_mount(sb);
317 if (rc) {
318 if (!silent) {
319 jfs_err("jfs_mount failed w/return code = %d", rc);
321 goto out_kfree;
323 if (sb->s_flags & MS_RDONLY)
324 sbi->log = 0;
325 else {
326 rc = jfs_mount_rw(sb, 0);
327 if (rc) {
328 if (!silent) {
329 jfs_err("jfs_mount_rw failed, return code = %d",
330 rc);
332 goto out_no_rw;
336 sb->s_magic = JFS_SUPER_MAGIC;
338 inode = iget(sb, ROOT_I);
339 if (!inode || is_bad_inode(inode))
340 goto out_no_root;
341 sb->s_root = d_alloc_root(inode);
342 if (!sb->s_root)
343 goto out_no_root;
345 if (!sbi->nls_tab)
346 sbi->nls_tab = load_nls_default();
348 /* logical blocks are represented by 40 bits in pxd_t, etc. */
349 sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
350 #if BITS_PER_LONG == 32
352 * Page cache is indexed by long.
353 * I would use MAX_LFS_FILESIZE, but it's only half as big
355 sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1, sb->s_maxbytes);
356 #endif
358 return 0;
360 out_no_root:
361 jfs_err("jfs_read_super: get root inode failed");
362 if (inode)
363 iput(inode);
365 out_no_rw:
366 rc = jfs_umount(sb);
367 if (rc) {
368 jfs_err("jfs_umount failed with return code %d", rc);
370 out_kfree:
371 if (sbi->nls_tab)
372 unload_nls(sbi->nls_tab);
373 kfree(sbi);
374 return -EINVAL;
377 static void jfs_write_super_lockfs(struct super_block *sb)
379 struct jfs_sb_info *sbi = JFS_SBI(sb);
380 struct jfs_log *log = sbi->log;
382 if (!(sb->s_flags & MS_RDONLY)) {
383 txQuiesce(sb);
384 lmLogShutdown(log);
388 static void jfs_unlockfs(struct super_block *sb)
390 struct jfs_sb_info *sbi = JFS_SBI(sb);
391 struct jfs_log *log = sbi->log;
392 int rc = 0;
394 if (!(sb->s_flags & MS_RDONLY)) {
395 if ((rc = lmLogInit(log)))
396 jfs_err("jfs_unlock failed with return code %d", rc);
397 else
398 txResume(sb);
402 static struct super_block *jfs_get_sb(struct file_system_type *fs_type,
403 int flags, const char *dev_name, void *data)
405 return get_sb_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
408 static int jfs_sync_fs(struct super_block *sb, int wait)
410 struct jfs_log *log = JFS_SBI(sb)->log;
412 /* log == NULL indicates read-only mount */
413 if (log)
414 jfs_flush_journal(log, wait);
416 return 0;
419 static struct super_operations jfs_super_operations = {
420 .alloc_inode = jfs_alloc_inode,
421 .destroy_inode = jfs_destroy_inode,
422 .read_inode = jfs_read_inode,
423 .dirty_inode = jfs_dirty_inode,
424 .write_inode = jfs_write_inode,
425 .delete_inode = jfs_delete_inode,
426 .put_super = jfs_put_super,
427 .sync_fs = jfs_sync_fs,
428 .write_super_lockfs = jfs_write_super_lockfs,
429 .unlockfs = jfs_unlockfs,
430 .statfs = jfs_statfs,
431 .remount_fs = jfs_remount,
434 static struct export_operations jfs_export_operations = {
435 .get_parent = jfs_get_parent,
438 static struct file_system_type jfs_fs_type = {
439 .owner = THIS_MODULE,
440 .name = "jfs",
441 .get_sb = jfs_get_sb,
442 .kill_sb = kill_block_super,
443 .fs_flags = FS_REQUIRES_DEV,
446 extern int metapage_init(void);
447 extern int txInit(void);
448 extern void txExit(void);
449 extern void metapage_exit(void);
451 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
453 struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
455 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
456 SLAB_CTOR_CONSTRUCTOR) {
457 INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
458 init_rwsem(&jfs_ip->rdwrlock);
459 init_MUTEX(&jfs_ip->commit_sem);
460 jfs_ip->atlhead = 0;
461 jfs_ip->active_ag = -1;
462 #ifdef CONFIG_JFS_POSIX_ACL
463 jfs_ip->i_acl = JFS_ACL_NOT_CACHED;
464 jfs_ip->i_default_acl = JFS_ACL_NOT_CACHED;
465 #endif
466 inode_init_once(&jfs_ip->vfs_inode);
470 static int __init init_jfs_fs(void)
472 int rc;
474 jfs_inode_cachep =
475 kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info), 0,
476 SLAB_RECLAIM_ACCOUNT, init_once, NULL);
477 if (jfs_inode_cachep == NULL)
478 return -ENOMEM;
481 * Metapage initialization
483 rc = metapage_init();
484 if (rc) {
485 jfs_err("metapage_init failed w/rc = %d", rc);
486 goto free_slab;
490 * Transaction Manager initialization
492 rc = txInit();
493 if (rc) {
494 jfs_err("txInit failed w/rc = %d", rc);
495 goto free_metapage;
499 * I/O completion thread (endio)
501 jfsIOthread = kernel_thread(jfsIOWait, 0,
502 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
503 if (jfsIOthread < 0) {
504 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread);
505 goto end_txmngr;
507 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
509 jfsCommitThread = kernel_thread(jfs_lazycommit, 0,
510 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
511 if (jfsCommitThread < 0) {
512 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsCommitThread);
513 goto kill_iotask;
515 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
517 jfsSyncThread = kernel_thread(jfs_sync, 0,
518 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
519 if (jfsSyncThread < 0) {
520 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread);
521 goto kill_committask;
523 wait_for_completion(&jfsIOwait); /* Wait until thread starts */
525 #ifdef PROC_FS_JFS
526 jfs_proc_init();
527 #endif
529 return register_filesystem(&jfs_fs_type);
531 kill_committask:
532 jfs_stop_threads = 1;
533 wake_up(&jfs_commit_thread_wait);
534 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
535 kill_iotask:
536 jfs_stop_threads = 1;
537 wake_up(&jfs_IO_thread_wait);
538 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
539 end_txmngr:
540 txExit();
541 free_metapage:
542 metapage_exit();
543 free_slab:
544 kmem_cache_destroy(jfs_inode_cachep);
545 return rc;
548 static void __exit exit_jfs_fs(void)
550 jfs_info("exit_jfs_fs called");
552 jfs_stop_threads = 1;
553 txExit();
554 metapage_exit();
555 wake_up(&jfs_IO_thread_wait);
556 wait_for_completion(&jfsIOwait); /* Wait until IO thread exits */
557 wake_up(&jfs_commit_thread_wait);
558 wait_for_completion(&jfsIOwait); /* Wait until Commit thread exits */
559 wake_up(&jfs_sync_thread_wait);
560 wait_for_completion(&jfsIOwait); /* Wait until Sync thread exits */
561 #ifdef PROC_FS_JFS
562 jfs_proc_clean();
563 #endif
564 unregister_filesystem(&jfs_fs_type);
565 kmem_cache_destroy(jfs_inode_cachep);
568 module_init(init_jfs_fs)
569 module_exit(exit_jfs_fs)