ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jfs / super.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
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.
14  *
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
18  */
19
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>
27
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"
36
37 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
38 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
39 MODULE_LICENSE("GPL");
40
41 static kmem_cache_t * jfs_inode_cachep;
42
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;
46
47 int jfs_stop_threads;
48 static pid_t jfsIOthread;
49 static pid_t jfsCommitThread;
50 static pid_t jfsSyncThread;
51 DECLARE_COMPLETION(jfsIOwait);
52
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
58
59 /*
60  * External declarations
61  */
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 *);
66
67 extern int jfsIOWait(void *);
68 extern int jfs_lazycommit(void *);
69 extern int jfs_sync(void *);
70
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);
75
76 extern struct dentry *jfs_get_parent(struct dentry *dentry);
77 extern int jfs_extendfs(struct super_block *, s64, int);
78
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
83
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;
87
88 static void jfs_handle_error(struct super_block *sb)
89 {
90         struct jfs_sb_info *sbi = JFS_SBI(sb);
91
92         if (sb->s_flags & MS_RDONLY)
93                 return;
94
95         updateSuper(sb, FM_DIRTY);
96
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;
105         } 
106
107         /* nothing is done for continue beyond marking the superblock dirty */
108 }
109
110 void jfs_error(struct super_block *sb, const char * function, ...)
111 {
112         static char error_buf[256];
113         va_list args;
114
115         va_start(args, function);
116         vsprintf(error_buf, function, args);
117         va_end(args);
118
119         printk(KERN_ERR "ERROR: (device %s): %s\n", sb->s_id, error_buf);
120
121         jfs_handle_error(sb);
122 }
123
124 static struct inode *jfs_alloc_inode(struct super_block *sb)
125 {
126         struct jfs_inode_info *jfs_inode;
127
128         jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
129         if (!jfs_inode)
130                 return NULL;
131         return &jfs_inode->vfs_inode;
132 }
133
134 static void jfs_destroy_inode(struct inode *inode)
135 {
136         struct jfs_inode_info *ji = JFS_IP(inode);
137
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]);
141         }
142
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;
147         }
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;
151         }
152 #endif
153
154         kmem_cache_free(jfs_inode_cachep, ji);
155 }
156
157 static int jfs_statfs(struct super_block *sb, struct kstatfs *buf)
158 {
159         struct jfs_sb_info *sbi = JFS_SBI(sb);
160         s64 maxinodes;
161         struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
162
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;
169         /*
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
174          *
175          * buf->f_files = atomic_read(&imap->im_numinos);
176          * buf->f_ffree = atomic_read(&imap->im_numfree);
177          */
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));
184
185         buf->f_namelen = JFS_NAME_MAX;
186         return 0;
187 }
188
189 static void jfs_put_super(struct super_block *sb)
190 {
191         struct jfs_sb_info *sbi = JFS_SBI(sb);
192         int rc;
193
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         if (sbi->nls_tab)
199                 unload_nls(sbi->nls_tab);
200         sbi->nls_tab = NULL;
201
202         kfree(sbi);
203 }
204
205 enum {
206         Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
207         Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err,
208 };
209
210 static match_table_t tokens = {
211         {Opt_integrity, "integrity"},
212         {Opt_nointegrity, "nointegrity"},
213         {Opt_iocharset, "iocharset=%s"},
214         {Opt_resize, "resize=%u"},
215         {Opt_resize_nosize, "resize"},
216         {Opt_errors, "errors=%s"},
217         {Opt_ignore, "noquota"},
218         {Opt_ignore, "quota"},
219         {Opt_ignore, "usrquota"},
220         {Opt_ignore, "grpquota"},
221         {Opt_err, NULL}
222 };
223
224 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
225                          int *flag)
226 {
227         void *nls_map = NULL;
228         char *p;
229         struct jfs_sb_info *sbi = JFS_SBI(sb);
230
231         *newLVSize = 0;
232
233         if (!options)
234                 return 1;
235
236         while ((p = strsep(&options, ",")) != NULL) {
237                 substring_t args[MAX_OPT_ARGS];
238                 int token;
239                 if (!*p)
240                         continue;
241
242                 token = match_token(p, tokens, args);
243                 switch (token) {
244                 case Opt_integrity:
245                         *flag &= ~JFS_NOINTEGRITY;
246                         break;
247                 case Opt_nointegrity:
248                         *flag |= JFS_NOINTEGRITY;
249                         break;
250                 case Opt_ignore:
251                         /* Silently ignore the quota options */
252                         /* Don't do anything ;-) */
253                         break;
254                 case Opt_iocharset:
255                         if (nls_map)    /* specified iocharset twice! */
256                                 unload_nls(nls_map);
257                         nls_map = load_nls(args[0].from);
258                         if (!nls_map) {
259                                 printk(KERN_ERR "JFS: charset not found\n");
260                                 goto cleanup;
261                         }
262                         break;
263                 case Opt_resize:
264                 {
265                         char *resize = args[0].from;
266                         *newLVSize = simple_strtoull(resize, &resize, 0);
267                         break;
268                 }
269                 case Opt_resize_nosize:
270                 {
271                         *newLVSize = sb->s_bdev->bd_inode->i_size >>
272                                 sb->s_blocksize_bits;
273                         if (*newLVSize == 0)
274                                 printk(KERN_ERR
275                                        "JFS: Cannot determine volume size\n");
276                         break;
277                 }
278                 case Opt_errors:
279                 {
280                         char *errors = args[0].from;
281                         if (!errors || !*errors)
282                                 goto cleanup;
283                         if (!strcmp(errors, "continue")) {
284                                 *flag &= ~JFS_ERR_REMOUNT_RO;
285                                 *flag &= ~JFS_ERR_PANIC;
286                                 *flag |= JFS_ERR_CONTINUE;
287                         } else if (!strcmp(errors, "remount-ro")) {
288                                 *flag &= ~JFS_ERR_CONTINUE;
289                                 *flag &= ~JFS_ERR_PANIC;
290                                 *flag |= JFS_ERR_REMOUNT_RO;
291                         } else if (!strcmp(errors, "panic")) {
292                                 *flag &= ~JFS_ERR_CONTINUE;
293                                 *flag &= ~JFS_ERR_REMOUNT_RO;
294                                 *flag |= JFS_ERR_PANIC;
295                         } else {
296                                 printk(KERN_ERR
297                                        "JFS: %s is an invalid error handler\n",
298                                        errors);
299                                 goto cleanup;
300                         }
301                         break;
302                 }
303                 default:
304                         printk("jfs: Unrecognized mount option \"%s\" "
305                                         " or missing value\n", p);
306                         goto cleanup;
307                 }
308         }
309
310         if (nls_map) {
311                 /* Discard old (if remount) */
312                 if (sbi->nls_tab)
313                         unload_nls(sbi->nls_tab);
314                 sbi->nls_tab = nls_map;
315         }
316         return 1;
317
318 cleanup:
319         if (nls_map)
320                 unload_nls(nls_map);
321         return 0;
322 }
323
324 static int jfs_remount(struct super_block *sb, int *flags, char *data)
325 {
326         s64 newLVSize = 0;
327         int rc = 0;
328         int flag = JFS_SBI(sb)->flag;
329
330         if (!parse_options(data, sb, &newLVSize, &flag)) {
331                 return -EINVAL;
332         }
333         if (newLVSize) {
334                 if (sb->s_flags & MS_RDONLY) {
335                         printk(KERN_ERR
336                   "JFS: resize requires volume to be mounted read-write\n");
337                         return -EROFS;
338                 }
339                 rc = jfs_extendfs(sb, newLVSize, 0);
340                 if (rc)
341                         return rc;
342         }
343
344         if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
345                 JFS_SBI(sb)->flag = flag;
346                 return jfs_mount_rw(sb, 1);
347         }
348         if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
349                 rc = jfs_umount_rw(sb);
350                 JFS_SBI(sb)->flag = flag;
351                 return rc;
352         }
353         if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
354                 if (!(sb->s_flags & MS_RDONLY)) {
355                         rc = jfs_umount_rw(sb);
356                         if (rc)
357                                 return rc;
358                         JFS_SBI(sb)->flag = flag;
359                         return jfs_mount_rw(sb, 1);
360                 }
361         JFS_SBI(sb)->flag = flag;
362
363         return 0;
364 }
365
366 static int jfs_fill_super(struct super_block *sb, void *data, int silent)
367 {
368         struct jfs_sb_info *sbi;
369         struct inode *inode;
370         int rc;
371         s64 newLVSize = 0;
372         int flag;
373
374         jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
375
376         if (!new_valid_dev(sb->s_bdev->bd_dev))
377                 return -EOVERFLOW;
378
379         sbi = kmalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
380         if (!sbi)
381                 return -ENOSPC;
382         memset(sbi, 0, sizeof (struct jfs_sb_info));
383         sb->s_fs_info = sbi;
384         sbi->sb = sb;
385
386         /* initialize the mount flag and determine the default error handler */
387         flag = JFS_ERR_REMOUNT_RO;
388
389         if (!parse_options((char *) data, sb, &newLVSize, &flag)) {
390                 kfree(sbi);
391                 return -EINVAL;
392         }
393         sbi->flag = flag;
394
395         if (newLVSize) {
396                 printk(KERN_ERR "resize option for remount only\n");
397                 return -EINVAL;
398         }
399
400         /*
401          * Initialize blocksize to 4K.
402          */
403         sb_set_blocksize(sb, PSIZE);
404
405         /*
406          * Set method vectors.
407          */
408         sb->s_op = &jfs_super_operations;
409         sb->s_export_op = &jfs_export_operations;
410
411         rc = jfs_mount(sb);
412         if (rc) {
413                 if (!silent) {
414                         jfs_err("jfs_mount failed w/return code = %d", rc);
415                 }
416                 goto out_kfree;
417         }
418         if (sb->s_flags & MS_RDONLY)
419                 sbi->log = 0;
420         else {
421                 rc = jfs_mount_rw(sb, 0);
422                 if (rc) {
423                         if (!silent) {
424                                 jfs_err("jfs_mount_rw failed, return code = %d",
425                                         rc);
426                         }
427                         goto out_no_rw;
428                 }
429         }
430
431         sb->s_magic = JFS_SUPER_MAGIC;
432
433         inode = iget(sb, ROOT_I);
434         if (!inode || is_bad_inode(inode))
435                 goto out_no_root;
436         sb->s_root = d_alloc_root(inode);
437         if (!sb->s_root)
438                 goto out_no_root;
439
440         /* logical blocks are represented by 40 bits in pxd_t, etc. */
441         sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
442 #if BITS_PER_LONG == 32
443         /*
444          * Page cache is indexed by long.
445          * I would use MAX_LFS_FILESIZE, but it's only half as big
446          */
447         sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1, sb->s_maxbytes);
448 #endif
449
450         return 0;
451
452 out_no_root:
453         jfs_err("jfs_read_super: get root inode failed");
454         if (inode)
455                 iput(inode);
456
457 out_no_rw:
458         rc = jfs_umount(sb);
459         if (rc) {
460                 jfs_err("jfs_umount failed with return code %d", rc);
461         }
462 out_kfree:
463         if (sbi->nls_tab)
464                 unload_nls(sbi->nls_tab);
465         kfree(sbi);
466         return -EINVAL;
467 }
468
469 static void jfs_write_super_lockfs(struct super_block *sb)
470 {
471         struct jfs_sb_info *sbi = JFS_SBI(sb);
472         struct jfs_log *log = sbi->log;
473
474         if (!(sb->s_flags & MS_RDONLY)) {
475                 txQuiesce(sb);
476                 lmLogShutdown(log);
477                 updateSuper(sb, FM_CLEAN);
478         }
479 }
480
481 static void jfs_unlockfs(struct super_block *sb)
482 {
483         struct jfs_sb_info *sbi = JFS_SBI(sb);
484         struct jfs_log *log = sbi->log;
485         int rc = 0;
486
487         if (!(sb->s_flags & MS_RDONLY)) {
488                 updateSuper(sb, FM_MOUNT);
489                 if ((rc = lmLogInit(log)))
490                         jfs_err("jfs_unlock failed with return code %d", rc);
491                 else
492                         txResume(sb);
493         }
494 }
495
496 static struct super_block *jfs_get_sb(struct file_system_type *fs_type, 
497         int flags, const char *dev_name, void *data)
498 {
499         return get_sb_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
500 }
501
502 static int jfs_sync_fs(struct super_block *sb, int wait)
503 {
504         struct jfs_log *log = JFS_SBI(sb)->log;
505
506         /* log == NULL indicates read-only mount */
507         if (log)
508                 jfs_flush_journal(log, wait);
509
510         return 0;
511 }
512
513 static struct super_operations jfs_super_operations = {
514         .alloc_inode    = jfs_alloc_inode,
515         .destroy_inode  = jfs_destroy_inode,
516         .read_inode     = jfs_read_inode,
517         .dirty_inode    = jfs_dirty_inode,
518         .write_inode    = jfs_write_inode,
519         .delete_inode   = jfs_delete_inode,
520         .put_super      = jfs_put_super,
521         .sync_fs        = jfs_sync_fs,
522         .write_super_lockfs = jfs_write_super_lockfs,
523         .unlockfs       = jfs_unlockfs,
524         .statfs         = jfs_statfs,
525         .remount_fs     = jfs_remount,
526 };
527
528 static struct export_operations jfs_export_operations = {
529         .get_parent     = jfs_get_parent,
530 };
531
532 static struct file_system_type jfs_fs_type = {
533         .owner          = THIS_MODULE,
534         .name           = "jfs",
535         .get_sb         = jfs_get_sb,
536         .kill_sb        = kill_block_super,
537         .fs_flags       = FS_REQUIRES_DEV,
538 };
539
540 extern int metapage_init(void);
541 extern int txInit(void);
542 extern void txExit(void);
543 extern void metapage_exit(void);
544
545 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
546 {
547         struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
548
549         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
550             SLAB_CTOR_CONSTRUCTOR) {
551                 memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
552                 INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
553                 init_rwsem(&jfs_ip->rdwrlock);
554                 init_MUTEX(&jfs_ip->commit_sem);
555                 init_rwsem(&jfs_ip->xattr_sem);
556                 jfs_ip->active_ag = -1;
557 #ifdef CONFIG_JFS_POSIX_ACL
558                 jfs_ip->i_acl = JFS_ACL_NOT_CACHED;
559                 jfs_ip->i_default_acl = JFS_ACL_NOT_CACHED;
560 #endif
561                 inode_init_once(&jfs_ip->vfs_inode);
562         }
563 }
564
565 static int __init init_jfs_fs(void)
566 {
567         int rc;
568
569         jfs_inode_cachep =
570             kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info), 0, 
571                             SLAB_RECLAIM_ACCOUNT, init_once, NULL);
572         if (jfs_inode_cachep == NULL)
573                 return -ENOMEM;
574
575         /*
576          * Metapage initialization
577          */
578         rc = metapage_init();
579         if (rc) {
580                 jfs_err("metapage_init failed w/rc = %d", rc);
581                 goto free_slab;
582         }
583
584         /*
585          * Transaction Manager initialization
586          */
587         rc = txInit();
588         if (rc) {
589                 jfs_err("txInit failed w/rc = %d", rc);
590                 goto free_metapage;
591         }
592
593         /*
594          * I/O completion thread (endio)
595          */
596         jfsIOthread = kernel_thread(jfsIOWait, 0, CLONE_KERNEL);
597         if (jfsIOthread < 0) {
598                 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread);
599                 goto end_txmngr;
600         }
601         wait_for_completion(&jfsIOwait);        /* Wait until thread starts */
602
603         jfsCommitThread = kernel_thread(jfs_lazycommit, 0, CLONE_KERNEL);
604         if (jfsCommitThread < 0) {
605                 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsCommitThread);
606                 goto kill_iotask;
607         }
608         wait_for_completion(&jfsIOwait);        /* Wait until thread starts */
609
610         jfsSyncThread = kernel_thread(jfs_sync, 0, CLONE_KERNEL);
611         if (jfsSyncThread < 0) {
612                 jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread);
613                 goto kill_committask;
614         }
615         wait_for_completion(&jfsIOwait);        /* Wait until thread starts */
616
617 #ifdef PROC_FS_JFS
618         jfs_proc_init();
619 #endif
620
621         return register_filesystem(&jfs_fs_type);
622
623 kill_committask:
624         jfs_stop_threads = 1;
625         wake_up(&jfs_commit_thread_wait);
626         wait_for_completion(&jfsIOwait);        /* Wait for thread exit */
627 kill_iotask:
628         jfs_stop_threads = 1;
629         wake_up(&jfs_IO_thread_wait);
630         wait_for_completion(&jfsIOwait);        /* Wait for thread exit */
631 end_txmngr:
632         txExit();
633 free_metapage:
634         metapage_exit();
635 free_slab:
636         kmem_cache_destroy(jfs_inode_cachep);
637         return rc;
638 }
639
640 static void __exit exit_jfs_fs(void)
641 {
642         jfs_info("exit_jfs_fs called");
643
644         jfs_stop_threads = 1;
645         txExit();
646         metapage_exit();
647         wake_up(&jfs_IO_thread_wait);
648         wait_for_completion(&jfsIOwait);        /* Wait until IO thread exits */
649         wake_up(&jfs_commit_thread_wait);
650         wait_for_completion(&jfsIOwait);        /* Wait until Commit thread exits */
651         wake_up(&jfs_sync_thread_wait);
652         wait_for_completion(&jfsIOwait);        /* Wait until Sync thread exits */
653 #ifdef PROC_FS_JFS
654         jfs_proc_clean();
655 #endif
656         unregister_filesystem(&jfs_fs_type);
657         kmem_cache_destroy(jfs_inode_cachep);
658 }
659
660 module_init(init_jfs_fs)
661 module_exit(exit_jfs_fs)