fedora core 6 1.2949 + vserver 2.2.0
[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/module.h>
22 #include <linux/parser.h>
23 #include <linux/completion.h>
24 #include <linux/vfs.h>
25 #include <linux/mount.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kthread.h>
28 #include <linux/posix_acl.h>
29 #include <linux/buffer_head.h>
30 #include <asm/uaccess.h>
31 #include <linux/seq_file.h>
32
33 #include "jfs_incore.h"
34 #include "jfs_filsys.h"
35 #include "jfs_inode.h"
36 #include "jfs_metapage.h"
37 #include "jfs_superblock.h"
38 #include "jfs_dmap.h"
39 #include "jfs_imap.h"
40 #include "jfs_acl.h"
41 #include "jfs_debug.h"
42
43 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
44 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
45 MODULE_LICENSE("GPL");
46
47 static struct kmem_cache * jfs_inode_cachep;
48
49 static struct super_operations jfs_super_operations;
50 static struct export_operations jfs_export_operations;
51 static struct file_system_type jfs_fs_type;
52
53 #define MAX_COMMIT_THREADS 64
54 static int commit_threads = 0;
55 module_param(commit_threads, int, 0);
56 MODULE_PARM_DESC(commit_threads, "Number of commit threads");
57
58 static struct task_struct *jfsCommitThread[MAX_COMMIT_THREADS];
59 struct task_struct *jfsIOthread;
60 struct task_struct *jfsSyncThread;
61
62 #ifdef CONFIG_JFS_DEBUG
63 int jfsloglevel = JFS_LOGLEVEL_WARN;
64 module_param(jfsloglevel, int, 0644);
65 MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
66 #endif
67
68 static void jfs_handle_error(struct super_block *sb)
69 {
70         struct jfs_sb_info *sbi = JFS_SBI(sb);
71
72         if (sb->s_flags & MS_RDONLY)
73                 return;
74
75         updateSuper(sb, FM_DIRTY);
76
77         if (sbi->flag & JFS_ERR_PANIC)
78                 panic("JFS (device %s): panic forced after error\n",
79                         sb->s_id);
80         else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
81                 jfs_err("ERROR: (device %s): remounting filesystem "
82                         "as read-only\n",
83                         sb->s_id);
84                 sb->s_flags |= MS_RDONLY;
85         }
86
87         /* nothing is done for continue beyond marking the superblock dirty */
88 }
89
90 void jfs_error(struct super_block *sb, const char * function, ...)
91 {
92         static char error_buf[256];
93         va_list args;
94
95         va_start(args, function);
96         vsnprintf(error_buf, sizeof(error_buf), function, args);
97         va_end(args);
98
99         printk(KERN_ERR "ERROR: (device %s): %s\n", sb->s_id, error_buf);
100
101         jfs_handle_error(sb);
102 }
103
104 static struct inode *jfs_alloc_inode(struct super_block *sb)
105 {
106         struct jfs_inode_info *jfs_inode;
107
108         jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
109         if (!jfs_inode)
110                 return NULL;
111         return &jfs_inode->vfs_inode;
112 }
113
114 static void jfs_destroy_inode(struct inode *inode)
115 {
116         struct jfs_inode_info *ji = JFS_IP(inode);
117
118         BUG_ON(!list_empty(&ji->anon_inode_list));
119
120         spin_lock_irq(&ji->ag_lock);
121         if (ji->active_ag != -1) {
122                 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
123                 atomic_dec(&bmap->db_active[ji->active_ag]);
124                 ji->active_ag = -1;
125         }
126         spin_unlock_irq(&ji->ag_lock);
127
128 #ifdef CONFIG_JFS_POSIX_ACL
129         if (ji->i_acl != JFS_ACL_NOT_CACHED) {
130                 posix_acl_release(ji->i_acl);
131                 ji->i_acl = JFS_ACL_NOT_CACHED;
132         }
133         if (ji->i_default_acl != JFS_ACL_NOT_CACHED) {
134                 posix_acl_release(ji->i_default_acl);
135                 ji->i_default_acl = JFS_ACL_NOT_CACHED;
136         }
137 #endif
138
139         kmem_cache_free(jfs_inode_cachep, ji);
140 }
141
142 static int jfs_statfs(struct dentry *dentry, struct kstatfs *buf)
143 {
144         struct jfs_sb_info *sbi = JFS_SBI(dentry->d_sb);
145         s64 maxinodes;
146         struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
147
148         jfs_info("In jfs_statfs");
149         buf->f_type = JFS_SUPER_MAGIC;
150         buf->f_bsize = sbi->bsize;
151         buf->f_blocks = sbi->bmap->db_mapsize;
152         buf->f_bfree = sbi->bmap->db_nfree;
153         buf->f_bavail = sbi->bmap->db_nfree;
154         /*
155          * If we really return the number of allocated & free inodes, some
156          * applications will fail because they won't see enough free inodes.
157          * We'll try to calculate some guess as to how may inodes we can
158          * really allocate
159          *
160          * buf->f_files = atomic_read(&imap->im_numinos);
161          * buf->f_ffree = atomic_read(&imap->im_numfree);
162          */
163         maxinodes = min((s64) atomic_read(&imap->im_numinos) +
164                         ((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
165                          << L2INOSPEREXT), (s64) 0xffffffffLL);
166         buf->f_files = maxinodes;
167         buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
168                                     atomic_read(&imap->im_numfree));
169
170         buf->f_namelen = JFS_NAME_MAX;
171         return 0;
172 }
173
174 static void jfs_put_super(struct super_block *sb)
175 {
176         struct jfs_sb_info *sbi = JFS_SBI(sb);
177         int rc;
178
179         jfs_info("In jfs_put_super");
180         rc = jfs_umount(sb);
181         if (rc)
182                 jfs_err("jfs_umount failed with return code %d", rc);
183         if (sbi->nls_tab)
184                 unload_nls(sbi->nls_tab);
185         sbi->nls_tab = NULL;
186
187         truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
188         iput(sbi->direct_inode);
189         sbi->direct_inode = NULL;
190
191         kfree(sbi);
192 }
193
194 enum {
195         Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
196         Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err, Opt_quota,
197         Opt_usrquota, Opt_grpquota, Opt_uid, Opt_gid, Opt_umask,
198         Opt_tag, Opt_notag, Opt_tagid
199 };
200
201 static match_table_t tokens = {
202         {Opt_integrity, "integrity"},
203         {Opt_nointegrity, "nointegrity"},
204         {Opt_iocharset, "iocharset=%s"},
205         {Opt_resize, "resize=%u"},
206         {Opt_resize_nosize, "resize"},
207         {Opt_errors, "errors=%s"},
208         {Opt_tag, "tag"},
209         {Opt_notag, "notag"},
210         {Opt_tagid, "tagid=%u"},
211         {Opt_tag, "tagxid"},
212         {Opt_ignore, "noquota"},
213         {Opt_ignore, "quota"},
214         {Opt_usrquota, "usrquota"},
215         {Opt_grpquota, "grpquota"},
216         {Opt_uid, "uid=%u"},
217         {Opt_gid, "gid=%u"},
218         {Opt_umask, "umask=%u"},
219         {Opt_err, NULL}
220 };
221
222 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
223                          int *flag)
224 {
225         void *nls_map = (void *)-1;     /* -1: no change;  NULL: none */
226         char *p;
227         struct jfs_sb_info *sbi = JFS_SBI(sb);
228
229         *newLVSize = 0;
230
231         if (!options)
232                 return 1;
233
234         while ((p = strsep(&options, ",")) != NULL) {
235                 substring_t args[MAX_OPT_ARGS];
236                 int token;
237                 if (!*p)
238                         continue;
239
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 && nls_map != (void *) -1)
254                                 unload_nls(nls_map);
255                         if (!strcmp(args[0].from, "none"))
256                                 nls_map = NULL;
257                         else {
258                                 nls_map = load_nls(args[0].from);
259                                 if (!nls_map) {
260                                         printk(KERN_ERR
261                                                "JFS: charset not found\n");
262                                         goto cleanup;
263                                 }
264                         }
265                         break;
266                 case Opt_resize:
267                 {
268                         char *resize = args[0].from;
269                         *newLVSize = simple_strtoull(resize, &resize, 0);
270                         break;
271                 }
272                 case Opt_resize_nosize:
273                 {
274                         *newLVSize = sb->s_bdev->bd_inode->i_size >>
275                                 sb->s_blocksize_bits;
276                         if (*newLVSize == 0)
277                                 printk(KERN_ERR
278                                        "JFS: Cannot determine volume size\n");
279                         break;
280                 }
281                 case Opt_errors:
282                 {
283                         char *errors = args[0].from;
284                         if (!errors || !*errors)
285                                 goto cleanup;
286                         if (!strcmp(errors, "continue")) {
287                                 *flag &= ~JFS_ERR_REMOUNT_RO;
288                                 *flag &= ~JFS_ERR_PANIC;
289                                 *flag |= JFS_ERR_CONTINUE;
290                         } else if (!strcmp(errors, "remount-ro")) {
291                                 *flag &= ~JFS_ERR_CONTINUE;
292                                 *flag &= ~JFS_ERR_PANIC;
293                                 *flag |= JFS_ERR_REMOUNT_RO;
294                         } else if (!strcmp(errors, "panic")) {
295                                 *flag &= ~JFS_ERR_CONTINUE;
296                                 *flag &= ~JFS_ERR_REMOUNT_RO;
297                                 *flag |= JFS_ERR_PANIC;
298                         } else {
299                                 printk(KERN_ERR
300                                        "JFS: %s is an invalid error handler\n",
301                                        errors);
302                                 goto cleanup;
303                         }
304                         break;
305                 }
306
307 #ifdef CONFIG_QUOTA
308                 case Opt_quota:
309                 case Opt_usrquota:
310                         *flag |= JFS_USRQUOTA;
311                         break;
312                 case Opt_grpquota:
313                         *flag |= JFS_GRPQUOTA;
314                         break;
315 #else
316                 case Opt_usrquota:
317                 case Opt_grpquota:
318                 case Opt_quota:
319                         printk(KERN_ERR
320                                "JFS: quota operations not supported\n");
321                         break;
322 #endif
323                 case Opt_uid:
324                 {
325                         char *uid = args[0].from;
326                         sbi->uid = simple_strtoul(uid, &uid, 0);
327                         break;
328                 }
329                 case Opt_gid:
330                 {
331                         char *gid = args[0].from;
332                         sbi->gid = simple_strtoul(gid, &gid, 0);
333                         break;
334                 }
335                 case Opt_umask:
336                 {
337                         char *umask = args[0].from;
338                         sbi->umask = simple_strtoul(umask, &umask, 8);
339                         if (sbi->umask & ~0777) {
340                                 printk(KERN_ERR
341                                        "JFS: Invalid value of umask\n");
342                                 goto cleanup;
343                         }
344                         break;
345                 }
346 #ifndef CONFIG_TAGGING_NONE
347                 case Opt_tag:
348                         *flag |= JFS_TAGGED;
349                         break;
350                 case Opt_notag:
351                         *flag &= JFS_TAGGED;
352                         break;
353 #endif
354 #ifdef CONFIG_PROPAGATE
355                 case Opt_tagid:
356                         /* use args[0] */
357                         *flag |= JFS_TAGGED;
358                         break;
359 #endif
360                 default:
361                         printk("jfs: Unrecognized mount option \"%s\" "
362                                         " or missing value\n", p);
363                         goto cleanup;
364                 }
365         }
366
367         if (nls_map != (void *) -1) {
368                 /* Discard old (if remount) */
369                 if (sbi->nls_tab)
370                         unload_nls(sbi->nls_tab);
371                 sbi->nls_tab = nls_map;
372         }
373         return 1;
374
375 cleanup:
376         if (nls_map && nls_map != (void *) -1)
377                 unload_nls(nls_map);
378         return 0;
379 }
380
381 static int jfs_remount(struct super_block *sb, int *flags, char *data)
382 {
383         s64 newLVSize = 0;
384         int rc = 0;
385         int flag = JFS_SBI(sb)->flag;
386
387         if (!parse_options(data, sb, &newLVSize, &flag)) {
388                 return -EINVAL;
389         }
390
391         if ((flag & JFS_TAGGED) && !(sb->s_flags & MS_TAGGED)) {
392                 printk(KERN_ERR "JFS: %s: tagging not permitted on remount.\n",
393                         sb->s_id);
394                 return -EINVAL;
395         }
396
397         if (newLVSize) {
398                 if (sb->s_flags & MS_RDONLY) {
399                         printk(KERN_ERR
400                   "JFS: resize requires volume to be mounted read-write\n");
401                         return -EROFS;
402                 }
403                 rc = jfs_extendfs(sb, newLVSize, 0);
404                 if (rc)
405                         return rc;
406         }
407
408         if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
409                 /*
410                  * Invalidate any previously read metadata.  fsck may have
411                  * changed the on-disk data since we mounted r/o
412                  */
413                 truncate_inode_pages(JFS_SBI(sb)->direct_inode->i_mapping, 0);
414
415                 JFS_SBI(sb)->flag = flag;
416                 return jfs_mount_rw(sb, 1);
417         }
418         if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
419                 rc = jfs_umount_rw(sb);
420                 JFS_SBI(sb)->flag = flag;
421                 return rc;
422         }
423         if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
424                 if (!(sb->s_flags & MS_RDONLY)) {
425                         rc = jfs_umount_rw(sb);
426                         if (rc)
427                                 return rc;
428                         JFS_SBI(sb)->flag = flag;
429                         return jfs_mount_rw(sb, 1);
430                 }
431         JFS_SBI(sb)->flag = flag;
432
433         return 0;
434 }
435
436 static int jfs_fill_super(struct super_block *sb, void *data, int silent)
437 {
438         struct jfs_sb_info *sbi;
439         struct inode *inode;
440         int rc;
441         s64 newLVSize = 0;
442         int flag;
443
444         jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
445
446         if (!new_valid_dev(sb->s_bdev->bd_dev))
447                 return -EOVERFLOW;
448
449         sbi = kzalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
450         if (!sbi)
451                 return -ENOMEM;
452         sb->s_fs_info = sbi;
453         sbi->sb = sb;
454         sbi->uid = sbi->gid = sbi->umask = -1;
455
456         /* initialize the mount flag and determine the default error handler */
457         flag = JFS_ERR_REMOUNT_RO;
458
459         if (!parse_options((char *) data, sb, &newLVSize, &flag)) {
460                 kfree(sbi);
461                 return -EINVAL;
462         }
463         sbi->flag = flag;
464
465 #ifdef CONFIG_JFS_POSIX_ACL
466         sb->s_flags |= MS_POSIXACL;
467 #endif
468         /* map mount option tagxid */
469         if (sbi->flag & JFS_TAGGED)
470                 sb->s_flags |= MS_TAGGED;
471
472         if (newLVSize) {
473                 printk(KERN_ERR "resize option for remount only\n");
474                 return -EINVAL;
475         }
476
477         /*
478          * Initialize blocksize to 4K.
479          */
480         sb_set_blocksize(sb, PSIZE);
481
482         /*
483          * Set method vectors.
484          */
485         sb->s_op = &jfs_super_operations;
486         sb->s_export_op = &jfs_export_operations;
487
488         /*
489          * Initialize direct-mapping inode/address-space
490          */
491         inode = new_inode(sb);
492         if (inode == NULL)
493                 goto out_kfree;
494         inode->i_ino = 0;
495         inode->i_nlink = 1;
496         inode->i_size = sb->s_bdev->bd_inode->i_size;
497         inode->i_mapping->a_ops = &jfs_metapage_aops;
498         insert_inode_hash(inode);
499         mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
500
501         sbi->direct_inode = inode;
502
503         rc = jfs_mount(sb);
504         if (rc) {
505                 if (!silent) {
506                         jfs_err("jfs_mount failed w/return code = %d", rc);
507                 }
508                 goto out_mount_failed;
509         }
510         if (sb->s_flags & MS_RDONLY)
511                 sbi->log = NULL;
512         else {
513                 rc = jfs_mount_rw(sb, 0);
514                 if (rc) {
515                         if (!silent) {
516                                 jfs_err("jfs_mount_rw failed, return code = %d",
517                                         rc);
518                         }
519                         goto out_no_rw;
520                 }
521         }
522
523         sb->s_magic = JFS_SUPER_MAGIC;
524
525         inode = iget(sb, ROOT_I);
526         if (!inode || is_bad_inode(inode))
527                 goto out_no_root;
528         sb->s_root = d_alloc_root(inode);
529         if (!sb->s_root)
530                 goto out_no_root;
531
532         if (sbi->mntflag & JFS_OS2)
533                 sb->s_root->d_op = &jfs_ci_dentry_operations;
534
535         /* logical blocks are represented by 40 bits in pxd_t, etc. */
536         sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
537 #if BITS_PER_LONG == 32
538         /*
539          * Page cache is indexed by long.
540          * I would use MAX_LFS_FILESIZE, but it's only half as big
541          */
542         sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1, sb->s_maxbytes);
543 #endif
544         sb->s_time_gran = 1;
545         return 0;
546
547 out_no_root:
548         jfs_err("jfs_read_super: get root inode failed");
549         if (inode)
550                 iput(inode);
551
552 out_no_rw:
553         rc = jfs_umount(sb);
554         if (rc) {
555                 jfs_err("jfs_umount failed with return code %d", rc);
556         }
557 out_mount_failed:
558         filemap_write_and_wait(sbi->direct_inode->i_mapping);
559         truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
560         make_bad_inode(sbi->direct_inode);
561         iput(sbi->direct_inode);
562         sbi->direct_inode = NULL;
563 out_kfree:
564         if (sbi->nls_tab)
565                 unload_nls(sbi->nls_tab);
566         kfree(sbi);
567         return -EINVAL;
568 }
569
570 static void jfs_write_super_lockfs(struct super_block *sb)
571 {
572         struct jfs_sb_info *sbi = JFS_SBI(sb);
573         struct jfs_log *log = sbi->log;
574
575         if (!(sb->s_flags & MS_RDONLY)) {
576                 txQuiesce(sb);
577                 lmLogShutdown(log);
578                 updateSuper(sb, FM_CLEAN);
579         }
580 }
581
582 static void jfs_unlockfs(struct super_block *sb)
583 {
584         struct jfs_sb_info *sbi = JFS_SBI(sb);
585         struct jfs_log *log = sbi->log;
586         int rc = 0;
587
588         if (!(sb->s_flags & MS_RDONLY)) {
589                 updateSuper(sb, FM_MOUNT);
590                 if ((rc = lmLogInit(log)))
591                         jfs_err("jfs_unlock failed with return code %d", rc);
592                 else
593                         txResume(sb);
594         }
595 }
596
597 static int jfs_get_sb(struct file_system_type *fs_type,
598         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
599 {
600         return get_sb_bdev(fs_type, flags, dev_name, data, jfs_fill_super,
601                            mnt);
602 }
603
604 static int jfs_sync_fs(struct super_block *sb, int wait)
605 {
606         struct jfs_log *log = JFS_SBI(sb)->log;
607
608         /* log == NULL indicates read-only mount */
609         if (log) {
610                 jfs_flush_journal(log, wait);
611                 jfs_syncpt(log, 0);
612         }
613
614         return 0;
615 }
616
617 static int jfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
618 {
619         struct jfs_sb_info *sbi = JFS_SBI(vfs->mnt_sb);
620
621         if (sbi->uid != -1)
622                 seq_printf(seq, ",uid=%d", sbi->uid);
623         if (sbi->gid != -1)
624                 seq_printf(seq, ",gid=%d", sbi->gid);
625         if (sbi->umask != -1)
626                 seq_printf(seq, ",umask=%03o", sbi->umask);
627         if (sbi->flag & JFS_NOINTEGRITY)
628                 seq_puts(seq, ",nointegrity");
629
630 #ifdef CONFIG_QUOTA
631         if (sbi->flag & JFS_USRQUOTA)
632                 seq_puts(seq, ",usrquota");
633
634         if (sbi->flag & JFS_GRPQUOTA)
635                 seq_puts(seq, ",grpquota");
636 #endif
637
638         return 0;
639 }
640
641 #ifdef CONFIG_QUOTA
642
643 /* Read data from quotafile - avoid pagecache and such because we cannot afford
644  * acquiring the locks... As quota files are never truncated and quota code
645  * itself serializes the operations (and noone else should touch the files)
646  * we don't have to be afraid of races */
647 static ssize_t jfs_quota_read(struct super_block *sb, int type, char *data,
648                               size_t len, loff_t off)
649 {
650         struct inode *inode = sb_dqopt(sb)->files[type];
651         sector_t blk = off >> sb->s_blocksize_bits;
652         int err = 0;
653         int offset = off & (sb->s_blocksize - 1);
654         int tocopy;
655         size_t toread;
656         struct buffer_head tmp_bh;
657         struct buffer_head *bh;
658         loff_t i_size = i_size_read(inode);
659
660         if (off > i_size)
661                 return 0;
662         if (off+len > i_size)
663                 len = i_size-off;
664         toread = len;
665         while (toread > 0) {
666                 tocopy = sb->s_blocksize - offset < toread ?
667                                 sb->s_blocksize - offset : toread;
668
669                 tmp_bh.b_state = 0;
670                 tmp_bh.b_size = 1 << inode->i_blkbits;
671                 err = jfs_get_block(inode, blk, &tmp_bh, 0);
672                 if (err)
673                         return err;
674                 if (!buffer_mapped(&tmp_bh))    /* A hole? */
675                         memset(data, 0, tocopy);
676                 else {
677                         bh = sb_bread(sb, tmp_bh.b_blocknr);
678                         if (!bh)
679                                 return -EIO;
680                         memcpy(data, bh->b_data+offset, tocopy);
681                         brelse(bh);
682                 }
683                 offset = 0;
684                 toread -= tocopy;
685                 data += tocopy;
686                 blk++;
687         }
688         return len;
689 }
690
691 /* Write to quotafile */
692 static ssize_t jfs_quota_write(struct super_block *sb, int type,
693                                const char *data, size_t len, loff_t off)
694 {
695         struct inode *inode = sb_dqopt(sb)->files[type];
696         sector_t blk = off >> sb->s_blocksize_bits;
697         int err = 0;
698         int offset = off & (sb->s_blocksize - 1);
699         int tocopy;
700         size_t towrite = len;
701         struct buffer_head tmp_bh;
702         struct buffer_head *bh;
703
704         mutex_lock(&inode->i_mutex);
705         while (towrite > 0) {
706                 tocopy = sb->s_blocksize - offset < towrite ?
707                                 sb->s_blocksize - offset : towrite;
708
709                 tmp_bh.b_state = 0;
710                 tmp_bh.b_size = 1 << inode->i_blkbits;
711                 err = jfs_get_block(inode, blk, &tmp_bh, 1);
712                 if (err)
713                         goto out;
714                 if (offset || tocopy != sb->s_blocksize)
715                         bh = sb_bread(sb, tmp_bh.b_blocknr);
716                 else
717                         bh = sb_getblk(sb, tmp_bh.b_blocknr);
718                 if (!bh) {
719                         err = -EIO;
720                         goto out;
721                 }
722                 lock_buffer(bh);
723                 memcpy(bh->b_data+offset, data, tocopy);
724                 flush_dcache_page(bh->b_page);
725                 set_buffer_uptodate(bh);
726                 mark_buffer_dirty(bh);
727                 unlock_buffer(bh);
728                 brelse(bh);
729                 offset = 0;
730                 towrite -= tocopy;
731                 data += tocopy;
732                 blk++;
733         }
734 out:
735         if (len == towrite)
736                 return err;
737         if (inode->i_size < off+len-towrite)
738                 i_size_write(inode, off+len-towrite);
739         inode->i_version++;
740         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
741         mark_inode_dirty(inode);
742         mutex_unlock(&inode->i_mutex);
743         return len - towrite;
744 }
745
746 #endif
747
748 static struct super_operations jfs_super_operations = {
749         .alloc_inode    = jfs_alloc_inode,
750         .destroy_inode  = jfs_destroy_inode,
751         .read_inode     = jfs_read_inode,
752         .dirty_inode    = jfs_dirty_inode,
753         .write_inode    = jfs_write_inode,
754         .delete_inode   = jfs_delete_inode,
755         .put_super      = jfs_put_super,
756         .sync_fs        = jfs_sync_fs,
757         .write_super_lockfs = jfs_write_super_lockfs,
758         .unlockfs       = jfs_unlockfs,
759         .statfs         = jfs_statfs,
760         .remount_fs     = jfs_remount,
761         .show_options   = jfs_show_options,
762 #ifdef CONFIG_QUOTA
763         .quota_read     = jfs_quota_read,
764         .quota_write    = jfs_quota_write,
765 #endif
766 };
767
768 static struct export_operations jfs_export_operations = {
769         .get_parent     = jfs_get_parent,
770 };
771
772 static struct file_system_type jfs_fs_type = {
773         .owner          = THIS_MODULE,
774         .name           = "jfs",
775         .get_sb         = jfs_get_sb,
776         .kill_sb        = kill_block_super,
777         .fs_flags       = FS_REQUIRES_DEV,
778 };
779
780 static void init_once(void *foo, struct kmem_cache * cachep, unsigned long flags)
781 {
782         struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
783
784         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
785             SLAB_CTOR_CONSTRUCTOR) {
786                 memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
787                 INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
788                 init_rwsem(&jfs_ip->rdwrlock);
789                 mutex_init(&jfs_ip->commit_mutex);
790                 init_rwsem(&jfs_ip->xattr_sem);
791                 spin_lock_init(&jfs_ip->ag_lock);
792                 jfs_ip->active_ag = -1;
793 #ifdef CONFIG_JFS_POSIX_ACL
794                 jfs_ip->i_acl = JFS_ACL_NOT_CACHED;
795                 jfs_ip->i_default_acl = JFS_ACL_NOT_CACHED;
796 #endif
797                 inode_init_once(&jfs_ip->vfs_inode);
798         }
799 }
800
801 static int __init init_jfs_fs(void)
802 {
803         int i;
804         int rc;
805
806         jfs_inode_cachep =
807             kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info), 0,
808                             SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
809                             init_once, NULL);
810         if (jfs_inode_cachep == NULL)
811                 return -ENOMEM;
812
813         /*
814          * Metapage initialization
815          */
816         rc = metapage_init();
817         if (rc) {
818                 jfs_err("metapage_init failed w/rc = %d", rc);
819                 goto free_slab;
820         }
821
822         /*
823          * Transaction Manager initialization
824          */
825         rc = txInit();
826         if (rc) {
827                 jfs_err("txInit failed w/rc = %d", rc);
828                 goto free_metapage;
829         }
830
831         /*
832          * I/O completion thread (endio)
833          */
834         jfsIOthread = kthread_run(jfsIOWait, NULL, "jfsIO");
835         if (IS_ERR(jfsIOthread)) {
836                 rc = PTR_ERR(jfsIOthread);
837                 jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
838                 goto end_txmngr;
839         }
840
841         if (commit_threads < 1)
842                 commit_threads = num_online_cpus();
843         if (commit_threads > MAX_COMMIT_THREADS)
844                 commit_threads = MAX_COMMIT_THREADS;
845
846         for (i = 0; i < commit_threads; i++) {
847                 jfsCommitThread[i] = kthread_run(jfs_lazycommit, NULL, "jfsCommit");
848                 if (IS_ERR(jfsCommitThread[i])) {
849                         rc = PTR_ERR(jfsCommitThread[i]);
850                         jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
851                         commit_threads = i;
852                         goto kill_committask;
853                 }
854         }
855
856         jfsSyncThread = kthread_run(jfs_sync, NULL, "jfsSync");
857         if (IS_ERR(jfsSyncThread)) {
858                 rc = PTR_ERR(jfsSyncThread);
859                 jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
860                 goto kill_committask;
861         }
862
863 #ifdef PROC_FS_JFS
864         jfs_proc_init();
865 #endif
866
867         return register_filesystem(&jfs_fs_type);
868
869 kill_committask:
870         for (i = 0; i < commit_threads; i++)
871                 kthread_stop(jfsCommitThread[i]);
872         kthread_stop(jfsIOthread);
873 end_txmngr:
874         txExit();
875 free_metapage:
876         metapage_exit();
877 free_slab:
878         kmem_cache_destroy(jfs_inode_cachep);
879         return rc;
880 }
881
882 static void __exit exit_jfs_fs(void)
883 {
884         int i;
885
886         jfs_info("exit_jfs_fs called");
887
888         txExit();
889         metapage_exit();
890
891         kthread_stop(jfsIOthread);
892         for (i = 0; i < commit_threads; i++)
893                 kthread_stop(jfsCommitThread[i]);
894         kthread_stop(jfsSyncThread);
895 #ifdef PROC_FS_JFS
896         jfs_proc_clean();
897 #endif
898         unregister_filesystem(&jfs_fs_type);
899         kmem_cache_destroy(jfs_inode_cachep);
900 }
901
902 module_init(init_jfs_fs)
903 module_exit(exit_jfs_fs)