[PATCH] Fix bug in saa7146 analog tv i2c-handling
[linux-2.6/history.git] / fs / jfs / super.c
blob578a67b98155408d54a17ccc1b33b726d741d117
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/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"
32 #include "jfs_dmap.h"
33 #include "jfs_imap.h"
34 #include "jfs_acl.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;
47 int jfs_stop_threads;
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)");
57 #endif
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);
82 #endif
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)
93 return;
95 updateSuper(sb, FM_DIRTY);
97 if (sbi->flag & JFS_ERR_PANIC)
98 panic("JFS (device %s): panic forced after error\n",
99 sb->s_id);
100 else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
101 jfs_err("ERROR: (device %s): remounting filesystem "
102 "as read-only\n",
103 sb->s_id);
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];
113 va_list args;
115 va_start(args, function);
116 vsprintf(error_buf, function, args);
117 va_end(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);
129 if (!jfs_inode)
130 return NULL;
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;
152 #endif
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);
160 s64 maxinodes;
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
173 * really allocate
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;
186 return 0;
189 static void jfs_put_super(struct super_block *sb)
191 struct jfs_sb_info *sbi = JFS_SBI(sb);
192 int rc;
194 jfs_info("In jfs_put_super");
195 rc = jfs_umount(sb);
196 if (rc)
197 jfs_err("jfs_umount failed with return code %d", rc);
198 unload_nls(sbi->nls_tab);
199 sbi->nls_tab = NULL;
201 kfree(sbi);
204 enum {
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"},
219 {Opt_err, NULL}
222 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
223 int *flag)
225 void *nls_map = NULL;
226 char *p;
227 struct jfs_sb_info *sbi = JFS_SBI(sb);
229 *newLVSize = 0;
231 if (!options)
232 return 1;
234 while ((p = strsep(&options, ",")) != NULL) {
235 substring_t args[MAX_OPT_ARGS];
236 int token;
237 if (!*p)
238 continue;
240 token = match_token(p, tokens, args);
241 switch (token) {
242 case Opt_integrity:
243 *flag &= ~JFS_NOINTEGRITY;
244 break;
245 case Opt_nointegrity:
246 *flag |= JFS_NOINTEGRITY;
247 break;
248 case Opt_ignore:
249 /* Silently ignore the quota options */
250 /* Don't do anything ;-) */
251 break;
252 case Opt_iocharset:
253 if (nls_map) /* specified iocharset twice! */
254 unload_nls(nls_map);
255 nls_map = load_nls(args[0].from);
256 if (!nls_map) {
257 printk(KERN_ERR "JFS: charset not found\n");
258 goto cleanup;
260 break;
261 case Opt_resize:
263 char *resize = args[0].from;
264 if (!resize || !*resize) {
265 *newLVSize = sb->s_bdev->bd_inode->i_size >>
266 sb->s_blocksize_bits;
267 if (*newLVSize == 0)
268 printk(KERN_ERR
269 "JFS: Cannot determine volume size\n");
270 } else
271 *newLVSize = simple_strtoull(resize, &resize, 0);
272 break;
274 case Opt_errors:
276 char *errors = args[0].from;
277 if (!errors || !*errors)
278 goto cleanup;
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;
291 } else {
292 printk(KERN_ERR
293 "JFS: %s is an invalid error handler\n",
294 errors);
295 goto cleanup;
297 break;
299 default:
300 printk("jfs: Unrecognized mount option \"%s\" "
301 " or missing value\n", p);
302 goto cleanup;
306 if (nls_map) {
307 /* Discard old (if remount) */
308 if (sbi->nls_tab)
309 unload_nls(sbi->nls_tab);
310 sbi->nls_tab = nls_map;
312 return 1;
314 cleanup:
315 if (nls_map)
316 unload_nls(nls_map);
317 return 0;
320 int jfs_remount(struct super_block *sb, int *flags, char *data)
322 s64 newLVSize = 0;
323 int rc = 0;
324 int flag = JFS_SBI(sb)->flag;
326 if (!parse_options(data, sb, &newLVSize, &flag)) {
327 return -EINVAL;
329 if (newLVSize) {
330 if (sb->s_flags & MS_RDONLY) {
331 printk(KERN_ERR
332 "JFS: resize requires volume to be mounted read-write\n");
333 return -EROFS;
335 rc = jfs_extendfs(sb, newLVSize, 0);
336 if (rc)
337 return rc;
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;
347 return rc;
349 if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
350 if (!(sb->s_flags & MS_RDONLY)) {
351 rc = jfs_umount_rw(sb);
352 if (rc)
353 return rc;
354 JFS_SBI(sb)->flag = flag;
355 return jfs_mount_rw(sb, 1);
357 JFS_SBI(sb)->flag = flag;
359 return 0;
362 static int jfs_fill_super(struct super_block *sb, void *data, int silent)
364 struct jfs_sb_info *sbi;
365 struct inode *inode;
366 int rc;
367 s64 newLVSize = 0;
368 int flag;
370 jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
372 if (!new_valid_dev(sb->s_bdev->bd_dev))
373 return -EOVERFLOW;
375 sbi = kmalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
376 if (!sbi)
377 return -ENOSPC;
378 memset(sbi, 0, sizeof (struct jfs_sb_info));
379 sb->s_fs_info = sbi;
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)) {
385 kfree(sbi);
386 return -EINVAL;
388 sbi->flag = flag;
390 if (newLVSize) {
391 printk(KERN_ERR "resize option for remount only\n");
392 return -EINVAL;
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;
406 rc = jfs_mount(sb);
407 if (rc) {
408 if (!silent) {
409 jfs_err("jfs_mount failed w/return code = %d", rc);
411 goto out_kfree;
413 if (sb->s_flags & MS_RDONLY)
414 sbi->log = 0;
415 else {
416 rc = jfs_mount_rw(sb, 0);
417 if (rc) {
418 if (!silent) {
419 jfs_err("jfs_mount_rw failed, return code = %d",
420 rc);
422 goto out_no_rw;
426 sb->s_magic = JFS_SUPER_MAGIC;
428 inode = iget(sb, ROOT_I);
429 if (!inode || is_bad_inode(inode))
430 goto out_no_root;
431 sb->s_root = d_alloc_root(inode);
432 if (!sb->s_root)
433 goto out_no_root;
435 if (!sbi->nls_tab)
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);
446 #endif
448 return 0;
450 out_no_root:
451 jfs_err("jfs_read_super: get root inode failed");
452 if (inode)
453 iput(inode);
455 out_no_rw:
456 rc = jfs_umount(sb);
457 if (rc) {
458 jfs_err("jfs_umount failed with return code %d", rc);
460 out_kfree:
461 if (sbi->nls_tab)
462 unload_nls(sbi->nls_tab);
463 kfree(sbi);
464 return -EINVAL;
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)) {
473 txQuiesce(sb);
474 lmLogShutdown(log);
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;
483 int rc = 0;
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);
489 else
490 txResume(sb);
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 */
505 if (log)
506 jfs_flush_journal(log, wait);
508 return 0;
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,
532 .name = "jfs",
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);
553 jfs_ip->atlhead = 0;
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;
558 #endif
559 inode_init_once(&jfs_ip->vfs_inode);
563 static int __init init_jfs_fs(void)
565 int rc;
567 jfs_inode_cachep =
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)
571 return -ENOMEM;
574 * Metapage initialization
576 rc = metapage_init();
577 if (rc) {
578 jfs_err("metapage_init failed w/rc = %d", rc);
579 goto free_slab;
583 * Transaction Manager initialization
585 rc = txInit();
586 if (rc) {
587 jfs_err("txInit failed w/rc = %d", rc);
588 goto free_metapage;
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);
597 goto end_txmngr;
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);
604 goto kill_iotask;
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 */
615 #ifdef PROC_FS_JFS
616 jfs_proc_init();
617 #endif
619 return register_filesystem(&jfs_fs_type);
621 kill_committask:
622 jfs_stop_threads = 1;
623 wake_up(&jfs_commit_thread_wait);
624 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
625 kill_iotask:
626 jfs_stop_threads = 1;
627 wake_up(&jfs_IO_thread_wait);
628 wait_for_completion(&jfsIOwait); /* Wait for thread exit */
629 end_txmngr:
630 txExit();
631 free_metapage:
632 metapage_exit();
633 free_slab:
634 kmem_cache_destroy(jfs_inode_cachep);
635 return rc;
638 static void __exit exit_jfs_fs(void)
640 jfs_info("exit_jfs_fs called");
642 jfs_stop_threads = 1;
643 txExit();
644 metapage_exit();
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 */
651 #ifdef PROC_FS_JFS
652 jfs_proc_clean();
653 #endif
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)