[GFS2] Speed up scanning of glocks
[pandora-kernel.git] / fs / gfs2 / ops_fstype.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License v.2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/vmalloc.h>
16 #include <linux/blkdev.h>
17 #include <linux/kthread.h>
18 #include <linux/gfs2_ondisk.h>
19
20 #include "gfs2.h"
21 #include "lm_interface.h"
22 #include "incore.h"
23 #include "daemon.h"
24 #include "glock.h"
25 #include "glops.h"
26 #include "inode.h"
27 #include "lm.h"
28 #include "mount.h"
29 #include "ops_export.h"
30 #include "ops_fstype.h"
31 #include "ops_super.h"
32 #include "recovery.h"
33 #include "rgrp.h"
34 #include "super.h"
35 #include "sys.h"
36 #include "util.h"
37
38 #define DO 0
39 #define UNDO 1
40
41 extern struct dentry_operations gfs2_dops;
42
43 static struct gfs2_sbd *init_sbd(struct super_block *sb)
44 {
45         struct gfs2_sbd *sdp;
46         unsigned int x;
47
48         sdp = vmalloc(sizeof(struct gfs2_sbd));
49         if (!sdp)
50                 return NULL;
51
52         memset(sdp, 0, sizeof(struct gfs2_sbd));
53
54         sb->s_fs_info = sdp;
55         sdp->sd_vfs = sb;
56
57         gfs2_tune_init(&sdp->sd_tune);
58
59         for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
60                 rwlock_init(&sdp->sd_gl_hash[x].hb_lock);
61                 INIT_LIST_HEAD(&sdp->sd_gl_hash[x].hb_list);
62         }
63         INIT_LIST_HEAD(&sdp->sd_reclaim_list);
64         spin_lock_init(&sdp->sd_reclaim_lock);
65         init_waitqueue_head(&sdp->sd_reclaim_wq);
66
67         mutex_init(&sdp->sd_inum_mutex);
68         spin_lock_init(&sdp->sd_statfs_spin);
69         mutex_init(&sdp->sd_statfs_mutex);
70
71         spin_lock_init(&sdp->sd_rindex_spin);
72         mutex_init(&sdp->sd_rindex_mutex);
73         INIT_LIST_HEAD(&sdp->sd_rindex_list);
74         INIT_LIST_HEAD(&sdp->sd_rindex_mru_list);
75         INIT_LIST_HEAD(&sdp->sd_rindex_recent_list);
76
77         INIT_LIST_HEAD(&sdp->sd_jindex_list);
78         spin_lock_init(&sdp->sd_jindex_spin);
79         mutex_init(&sdp->sd_jindex_mutex);
80
81         INIT_LIST_HEAD(&sdp->sd_quota_list);
82         spin_lock_init(&sdp->sd_quota_spin);
83         mutex_init(&sdp->sd_quota_mutex);
84
85         spin_lock_init(&sdp->sd_log_lock);
86
87         INIT_LIST_HEAD(&sdp->sd_log_le_gl);
88         INIT_LIST_HEAD(&sdp->sd_log_le_buf);
89         INIT_LIST_HEAD(&sdp->sd_log_le_revoke);
90         INIT_LIST_HEAD(&sdp->sd_log_le_rg);
91         INIT_LIST_HEAD(&sdp->sd_log_le_databuf);
92
93         mutex_init(&sdp->sd_log_reserve_mutex);
94         INIT_LIST_HEAD(&sdp->sd_ail1_list);
95         INIT_LIST_HEAD(&sdp->sd_ail2_list);
96
97         init_rwsem(&sdp->sd_log_flush_lock);
98         INIT_LIST_HEAD(&sdp->sd_log_flush_list);
99
100         INIT_LIST_HEAD(&sdp->sd_revoke_list);
101
102         mutex_init(&sdp->sd_freeze_lock);
103
104         return sdp;
105 }
106
107 static void init_vfs(struct super_block *sb, unsigned noatime)
108 {
109         struct gfs2_sbd *sdp = sb->s_fs_info;
110
111         sb->s_magic = GFS2_MAGIC;
112         sb->s_op = &gfs2_super_ops;
113         sb->s_export_op = &gfs2_export_ops;
114         sb->s_maxbytes = MAX_LFS_FILESIZE;
115
116         if (sb->s_flags & (MS_NOATIME | MS_NODIRATIME))
117                 set_bit(noatime, &sdp->sd_flags);
118
119         /* Don't let the VFS update atimes.  GFS2 handles this itself. */
120         sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
121 }
122
123 static int init_names(struct gfs2_sbd *sdp, int silent)
124 {
125         struct gfs2_sb *sb = NULL;
126         char *proto, *table;
127         int error = 0;
128
129         proto = sdp->sd_args.ar_lockproto;
130         table = sdp->sd_args.ar_locktable;
131
132         /*  Try to autodetect  */
133
134         if (!proto[0] || !table[0]) {
135                 struct buffer_head *bh;
136                 bh = sb_getblk(sdp->sd_vfs,
137                                GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
138                 lock_buffer(bh);
139                 clear_buffer_uptodate(bh);
140                 clear_buffer_dirty(bh);
141                 unlock_buffer(bh);
142                 ll_rw_block(READ, 1, &bh);
143                 wait_on_buffer(bh);
144
145                 if (!buffer_uptodate(bh)) {
146                         brelse(bh);
147                         return -EIO;
148                 }
149
150                 sb = kmalloc(sizeof(struct gfs2_sb), GFP_KERNEL);
151                 if (!sb) {
152                         brelse(bh);
153                         return -ENOMEM;
154                 }
155                 gfs2_sb_in(sb, bh->b_data); 
156                 brelse(bh);
157
158                 error = gfs2_check_sb(sdp, sb, silent);
159                 if (error)
160                         goto out;
161
162                 if (!proto[0])
163                         proto = sb->sb_lockproto;
164                 if (!table[0])
165                         table = sb->sb_locktable;
166         }
167
168         if (!table[0])
169                 table = sdp->sd_vfs->s_id;
170
171         snprintf(sdp->sd_proto_name, GFS2_FSNAME_LEN, "%s", proto);
172         snprintf(sdp->sd_table_name, GFS2_FSNAME_LEN, "%s", table);
173
174  out:
175         kfree(sb);
176
177         return error;
178 }
179
180 static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
181                         int undo)
182 {
183         struct task_struct *p;
184         int error = 0;
185
186         if (undo)
187                 goto fail_trans;
188
189         p = kthread_run(gfs2_scand, sdp, "gfs2_scand");
190         error = IS_ERR(p);
191         if (error) {
192                 fs_err(sdp, "can't start scand thread: %d\n", error);
193                 return error;
194         }
195         sdp->sd_scand_process = p;
196
197         for (sdp->sd_glockd_num = 0;
198              sdp->sd_glockd_num < sdp->sd_args.ar_num_glockd;
199              sdp->sd_glockd_num++) {
200                 p = kthread_run(gfs2_glockd, sdp, "gfs2_glockd");
201                 error = IS_ERR(p);
202                 if (error) {
203                         fs_err(sdp, "can't start glockd thread: %d\n", error);
204                         goto fail;
205                 }
206                 sdp->sd_glockd_process[sdp->sd_glockd_num] = p;
207         }
208
209         error = gfs2_glock_nq_num(sdp,
210                                   GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
211                                   LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
212                                   mount_gh);
213         if (error) {
214                 fs_err(sdp, "can't acquire mount glock: %d\n", error);
215                 goto fail;
216         }
217
218         error = gfs2_glock_nq_num(sdp,
219                                   GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
220                                   LM_ST_SHARED,
221                                   LM_FLAG_NOEXP | GL_EXACT,
222                                   &sdp->sd_live_gh);
223         if (error) {
224                 fs_err(sdp, "can't acquire live glock: %d\n", error);
225                 goto fail_mount;
226         }
227
228         error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
229                                CREATE, &sdp->sd_rename_gl);
230         if (error) {
231                 fs_err(sdp, "can't create rename glock: %d\n", error);
232                 goto fail_live;
233         }
234
235         error = gfs2_glock_get(sdp, GFS2_TRANS_LOCK, &gfs2_trans_glops,
236                                CREATE, &sdp->sd_trans_gl);
237         if (error) {
238                 fs_err(sdp, "can't create transaction glock: %d\n", error);
239                 goto fail_rename;
240         }
241         set_bit(GLF_STICKY, &sdp->sd_trans_gl->gl_flags);
242
243         return 0;
244
245 fail_trans:
246         gfs2_glock_put(sdp->sd_trans_gl);
247
248 fail_rename:
249         gfs2_glock_put(sdp->sd_rename_gl);
250
251 fail_live:
252         gfs2_glock_dq_uninit(&sdp->sd_live_gh);
253
254 fail_mount:
255         gfs2_glock_dq_uninit(mount_gh);
256
257 fail:
258         while (sdp->sd_glockd_num--)
259                 kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
260
261         kthread_stop(sdp->sd_scand_process);
262
263         return error;
264 }
265
266 static struct inode *gfs2_lookup_root(struct super_block *sb,
267                                       struct gfs2_inum *inum)
268 {
269         return gfs2_inode_lookup(sb, inum, DT_DIR);
270 }
271
272 static int init_sb(struct gfs2_sbd *sdp, int silent, int undo)
273 {
274         struct super_block *sb = sdp->sd_vfs;
275         struct gfs2_holder sb_gh;
276         struct gfs2_inum *inum;
277         struct inode *inode;
278         int error = 0;
279
280         if (undo) {
281                 if (sb->s_root) {
282                         dput(sb->s_root);
283                         sb->s_root = NULL;
284                 }
285                 return 0;
286         }
287         
288         error = gfs2_glock_nq_num(sdp, GFS2_SB_LOCK, &gfs2_meta_glops,
289                                  LM_ST_SHARED, 0, &sb_gh);
290         if (error) {
291                 fs_err(sdp, "can't acquire superblock glock: %d\n", error);
292                 return error;
293         }
294
295         error = gfs2_read_sb(sdp, sb_gh.gh_gl, silent);
296         if (error) {
297                 fs_err(sdp, "can't read superblock: %d\n", error);
298                 goto out;
299         }
300
301         /* Set up the buffer cache and SB for real */
302         if (sdp->sd_sb.sb_bsize < bdev_hardsect_size(sb->s_bdev)) {
303                 error = -EINVAL;
304                 fs_err(sdp, "FS block size (%u) is too small for device "
305                        "block size (%u)\n",
306                        sdp->sd_sb.sb_bsize, bdev_hardsect_size(sb->s_bdev));
307                 goto out;
308         }
309         if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
310                 error = -EINVAL;
311                 fs_err(sdp, "FS block size (%u) is too big for machine "
312                        "page size (%u)\n",
313                        sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
314                 goto out;
315         }
316         sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
317
318         /* Get the root inode */
319         inum = &sdp->sd_sb.sb_root_dir;
320         if (sb->s_type == &gfs2meta_fs_type)
321                 inum = &sdp->sd_sb.sb_master_dir;
322         inode = gfs2_lookup_root(sb, inum);
323         if (IS_ERR(inode)) {
324                 error = PTR_ERR(inode);
325                 fs_err(sdp, "can't read in root inode: %d\n", error);
326                 goto out;
327         }
328
329         sb->s_root = d_alloc_root(inode);
330         if (!sb->s_root) {
331                 fs_err(sdp, "can't get root dentry\n");
332                 error = -ENOMEM;
333                 iput(inode);
334         }
335         sb->s_root->d_op = &gfs2_dops;
336 out:
337         gfs2_glock_dq_uninit(&sb_gh);
338         return error;
339 }
340
341 static int init_journal(struct gfs2_sbd *sdp, int undo)
342 {
343         struct gfs2_holder ji_gh;
344         struct task_struct *p;
345         struct gfs2_inode *ip;
346         int jindex = 1;
347         int error = 0;
348
349         if (undo) {
350                 jindex = 0;
351                 goto fail_recoverd;
352         }
353
354         sdp->sd_jindex = gfs2_lookup_simple(sdp->sd_master_dir, "jindex");
355         if (IS_ERR(sdp->sd_jindex)) {
356                 fs_err(sdp, "can't lookup journal index: %d\n", error);
357                 return PTR_ERR(sdp->sd_jindex);
358         }
359         ip = GFS2_I(sdp->sd_jindex);
360         set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
361
362         /* Load in the journal index special file */
363
364         error = gfs2_jindex_hold(sdp, &ji_gh);
365         if (error) {
366                 fs_err(sdp, "can't read journal index: %d\n", error);
367                 goto fail;
368         }
369
370         error = -EINVAL;
371         if (!gfs2_jindex_size(sdp)) {
372                 fs_err(sdp, "no journals!\n");
373                 goto fail_jindex;               
374         }
375
376         if (sdp->sd_args.ar_spectator) {
377                 sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
378                 sdp->sd_log_blks_free = sdp->sd_jdesc->jd_blocks;
379         } else {
380                 if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
381                         fs_err(sdp, "can't mount journal #%u\n",
382                                sdp->sd_lockstruct.ls_jid);
383                         fs_err(sdp, "there are only %u journals (0 - %u)\n",
384                                gfs2_jindex_size(sdp),
385                                gfs2_jindex_size(sdp) - 1);
386                         goto fail_jindex;
387                 }
388                 sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
389
390                 error = gfs2_glock_nq_num(sdp, sdp->sd_lockstruct.ls_jid,
391                                           &gfs2_journal_glops,
392                                           LM_ST_EXCLUSIVE, LM_FLAG_NOEXP,
393                                           &sdp->sd_journal_gh);
394                 if (error) {
395                         fs_err(sdp, "can't acquire journal glock: %d\n", error);
396                         goto fail_jindex;
397                 }
398
399                 ip = GFS2_I(sdp->sd_jdesc->jd_inode);
400                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
401                                            LM_FLAG_NOEXP | GL_EXACT,
402                                            &sdp->sd_jinode_gh);
403                 if (error) {
404                         fs_err(sdp, "can't acquire journal inode glock: %d\n",
405                                error);
406                         goto fail_journal_gh;
407                 }
408
409                 error = gfs2_jdesc_check(sdp->sd_jdesc);
410                 if (error) {
411                         fs_err(sdp, "my journal (%u) is bad: %d\n",
412                                sdp->sd_jdesc->jd_jid, error);
413                         goto fail_jinode_gh;
414                 }
415                 sdp->sd_log_blks_free = sdp->sd_jdesc->jd_blocks;
416         }
417
418         if (sdp->sd_lockstruct.ls_first) {
419                 unsigned int x;
420                 for (x = 0; x < sdp->sd_journals; x++) {
421                         error = gfs2_recover_journal(gfs2_jdesc_find(sdp, x));
422                         if (error) {
423                                 fs_err(sdp, "error recovering journal %u: %d\n",
424                                        x, error);
425                                 goto fail_jinode_gh;
426                         }
427                 }
428
429                 gfs2_lm_others_may_mount(sdp);
430         } else if (!sdp->sd_args.ar_spectator) {
431                 error = gfs2_recover_journal(sdp->sd_jdesc);
432                 if (error) {
433                         fs_err(sdp, "error recovering my journal: %d\n", error);
434                         goto fail_jinode_gh;
435                 }
436         }
437
438         set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
439         gfs2_glock_dq_uninit(&ji_gh);
440         jindex = 0;
441
442         p = kthread_run(gfs2_recoverd, sdp, "gfs2_recoverd");
443         error = IS_ERR(p);
444         if (error) {
445                 fs_err(sdp, "can't start recoverd thread: %d\n", error);
446                 goto fail_jinode_gh;
447         }
448         sdp->sd_recoverd_process = p;
449
450         return 0;
451
452  fail_recoverd:
453         kthread_stop(sdp->sd_recoverd_process);
454
455  fail_jinode_gh:
456         if (!sdp->sd_args.ar_spectator)
457                 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
458
459  fail_journal_gh:
460         if (!sdp->sd_args.ar_spectator)
461                 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
462
463  fail_jindex:
464         gfs2_jindex_free(sdp);
465         if (jindex)
466                 gfs2_glock_dq_uninit(&ji_gh);
467
468  fail:
469         iput(sdp->sd_jindex);
470
471         return error;
472 }
473
474
475 static int init_inodes(struct gfs2_sbd *sdp, int undo)
476 {
477         int error = 0;
478         struct gfs2_inode *ip;
479         struct inode *inode;
480
481         if (undo)
482                 goto fail_qinode;
483
484         inode = gfs2_lookup_root(sdp->sd_vfs, &sdp->sd_sb.sb_master_dir);
485         if (IS_ERR(inode)) {
486                 error = PTR_ERR(inode);
487                 fs_err(sdp, "can't read in master directory: %d\n", error);
488                 goto fail;
489         }
490         sdp->sd_master_dir = inode;
491
492         error = init_journal(sdp, undo);
493         if (error)
494                 goto fail_master;
495
496         /* Read in the master inode number inode */
497         sdp->sd_inum_inode = gfs2_lookup_simple(sdp->sd_master_dir, "inum");
498         if (IS_ERR(sdp->sd_inum_inode)) {
499                 error = PTR_ERR(sdp->sd_inum_inode);
500                 fs_err(sdp, "can't read in inum inode: %d\n", error);
501                 goto fail_journal;
502         }
503
504
505         /* Read in the master statfs inode */
506         sdp->sd_statfs_inode = gfs2_lookup_simple(sdp->sd_master_dir, "statfs");
507         if (IS_ERR(sdp->sd_statfs_inode)) {
508                 error = PTR_ERR(sdp->sd_statfs_inode);
509                 fs_err(sdp, "can't read in statfs inode: %d\n", error);
510                 goto fail_inum;
511         }
512
513         /* Read in the resource index inode */
514         sdp->sd_rindex = gfs2_lookup_simple(sdp->sd_master_dir, "rindex");
515         if (IS_ERR(sdp->sd_rindex)) {
516                 error = PTR_ERR(sdp->sd_rindex);
517                 fs_err(sdp, "can't get resource index inode: %d\n", error);
518                 goto fail_statfs;
519         }
520         ip = GFS2_I(sdp->sd_rindex);
521         set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
522         sdp->sd_rindex_vn = ip->i_gl->gl_vn - 1;
523
524         /* Read in the quota inode */
525         sdp->sd_quota_inode = gfs2_lookup_simple(sdp->sd_master_dir, "quota");
526         if (IS_ERR(sdp->sd_quota_inode)) {
527                 error = PTR_ERR(sdp->sd_quota_inode);
528                 fs_err(sdp, "can't get quota file inode: %d\n", error);
529                 goto fail_rindex;
530         }
531         return 0;
532
533 fail_qinode:
534         iput(sdp->sd_quota_inode);
535
536 fail_rindex:
537         gfs2_clear_rgrpd(sdp);
538         iput(sdp->sd_rindex);
539
540 fail_statfs:
541         iput(sdp->sd_statfs_inode);
542
543 fail_inum:
544         iput(sdp->sd_inum_inode);
545 fail_journal:
546         init_journal(sdp, UNDO);
547 fail_master:
548         iput(sdp->sd_master_dir);
549 fail:
550         return error;
551 }
552
553 static int init_per_node(struct gfs2_sbd *sdp, int undo)
554 {
555         struct inode *pn = NULL;
556         char buf[30];
557         int error = 0;
558         struct gfs2_inode *ip;
559
560         if (sdp->sd_args.ar_spectator)
561                 return 0;
562
563         if (undo)
564                 goto fail_qc_gh;
565
566         pn = gfs2_lookup_simple(sdp->sd_master_dir, "per_node");
567         if (IS_ERR(pn)) {
568                 error = PTR_ERR(pn);
569                 fs_err(sdp, "can't find per_node directory: %d\n", error);
570                 return error;
571         }
572
573         sprintf(buf, "inum_range%u", sdp->sd_jdesc->jd_jid);
574         sdp->sd_ir_inode = gfs2_lookup_simple(pn, buf);
575         if (IS_ERR(sdp->sd_ir_inode)) {
576                 error = PTR_ERR(sdp->sd_ir_inode);
577                 fs_err(sdp, "can't find local \"ir\" file: %d\n", error);
578                 goto fail;
579         }
580
581         sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
582         sdp->sd_sc_inode = gfs2_lookup_simple(pn, buf);
583         if (IS_ERR(sdp->sd_sc_inode)) {
584                 error = PTR_ERR(sdp->sd_sc_inode);
585                 fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
586                 goto fail_ir_i;
587         }
588
589         sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
590         sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
591         if (IS_ERR(sdp->sd_qc_inode)) {
592                 error = PTR_ERR(sdp->sd_qc_inode);
593                 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
594                 goto fail_ut_i;
595         }
596
597         iput(pn);
598         pn = NULL;
599
600         ip = GFS2_I(sdp->sd_ir_inode);
601         error = gfs2_glock_nq_init(ip->i_gl,
602                                    LM_ST_EXCLUSIVE, 0,
603                                    &sdp->sd_ir_gh);
604         if (error) {
605                 fs_err(sdp, "can't lock local \"ir\" file: %d\n", error);
606                 goto fail_qc_i;
607         }
608
609         ip = GFS2_I(sdp->sd_sc_inode);
610         error = gfs2_glock_nq_init(ip->i_gl,
611                                    LM_ST_EXCLUSIVE, 0,
612                                    &sdp->sd_sc_gh);
613         if (error) {
614                 fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
615                 goto fail_ir_gh;
616         }
617
618         ip = GFS2_I(sdp->sd_qc_inode);
619         error = gfs2_glock_nq_init(ip->i_gl,
620                                    LM_ST_EXCLUSIVE, 0,
621                                    &sdp->sd_qc_gh);
622         if (error) {
623                 fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
624                 goto fail_ut_gh;
625         }
626
627         return 0;
628
629  fail_qc_gh:
630         gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
631
632  fail_ut_gh:
633
634         gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
635
636  fail_ir_gh:
637         gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
638
639  fail_qc_i:
640         iput(sdp->sd_qc_inode);
641
642  fail_ut_i:
643
644         iput(sdp->sd_sc_inode);
645
646  fail_ir_i:
647         iput(sdp->sd_ir_inode);
648
649  fail:
650         if (pn)
651                 iput(pn);
652         return error;
653 }
654
655 static int init_threads(struct gfs2_sbd *sdp, int undo)
656 {
657         struct task_struct *p;
658         int error = 0;
659
660         if (undo)
661                 goto fail_quotad;
662
663         sdp->sd_log_flush_time = jiffies;
664         sdp->sd_jindex_refresh_time = jiffies;
665
666         p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
667         error = IS_ERR(p);
668         if (error) {
669                 fs_err(sdp, "can't start logd thread: %d\n", error);
670                 return error;
671         }
672         sdp->sd_logd_process = p;
673
674         sdp->sd_statfs_sync_time = jiffies;
675         sdp->sd_quota_sync_time = jiffies;
676
677         p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
678         error = IS_ERR(p);
679         if (error) {
680                 fs_err(sdp, "can't start quotad thread: %d\n", error);
681                 goto fail;
682         }
683         sdp->sd_quotad_process = p;
684
685         return 0;
686
687
688 fail_quotad:
689         kthread_stop(sdp->sd_quotad_process);
690 fail:
691         kthread_stop(sdp->sd_logd_process);
692         return error;
693 }
694
695 /**
696  * fill_super - Read in superblock
697  * @sb: The VFS superblock
698  * @data: Mount options
699  * @silent: Don't complain if it's not a GFS2 filesystem
700  *
701  * Returns: errno
702  */
703
704 static int fill_super(struct super_block *sb, void *data, int silent)
705 {
706         struct gfs2_sbd *sdp;
707         struct gfs2_holder mount_gh;
708         int error;
709
710         sdp = init_sbd(sb);
711         if (!sdp) {
712                 printk(KERN_WARNING "GFS2: can't alloc struct gfs2_sbd\n");
713                 return -ENOMEM;
714         }
715
716         error = gfs2_mount_args(sdp, (char *)data, 0);
717         if (error) {
718                 printk(KERN_WARNING "GFS2: can't parse mount arguments\n");
719                 goto fail;
720         }
721
722         init_vfs(sb, SDF_NOATIME);
723
724         /* Set up the buffer cache and fill in some fake block size values
725            to allow us to read-in the on-disk superblock. */
726         sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
727         sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
728         sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
729                                GFS2_BASIC_BLOCK_SHIFT;
730         sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
731
732         error = init_names(sdp, silent);
733         if (error)
734                 goto fail;
735
736         error = gfs2_sys_fs_add(sdp);
737         if (error)
738                 goto fail;
739
740         error = gfs2_lm_mount(sdp, silent);
741         if (error)
742                 goto fail_sys;
743
744         error = init_locking(sdp, &mount_gh, DO);
745         if (error)
746                 goto fail_lm;
747
748         error = init_sb(sdp, silent, DO);
749         if (error)
750                 goto fail_locking;
751
752         error = init_inodes(sdp, DO);
753         if (error)
754                 goto fail_sb;
755
756         error = init_per_node(sdp, DO);
757         if (error)
758                 goto fail_inodes;
759
760         error = gfs2_statfs_init(sdp);
761         if (error) {
762                 fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
763                 goto fail_per_node;
764         }
765
766         error = init_threads(sdp, DO);
767         if (error)
768                 goto fail_per_node;
769
770         if (!(sb->s_flags & MS_RDONLY)) {
771                 error = gfs2_make_fs_rw(sdp);
772                 if (error) {
773                         fs_err(sdp, "can't make FS RW: %d\n", error);
774                         goto fail_threads;
775                 }
776         }
777
778         gfs2_glock_dq_uninit(&mount_gh);
779
780         return 0;
781
782  fail_threads:
783         init_threads(sdp, UNDO);
784
785  fail_per_node:
786         init_per_node(sdp, UNDO);
787
788  fail_inodes:
789         init_inodes(sdp, UNDO);
790
791  fail_sb:
792         init_sb(sdp, 0, UNDO);
793
794  fail_locking:
795         init_locking(sdp, &mount_gh, UNDO);
796
797  fail_lm:
798         gfs2_gl_hash_clear(sdp, WAIT);
799         gfs2_lm_unmount(sdp);
800         while (invalidate_inodes(sb))
801                 yield();
802
803  fail_sys:
804         gfs2_sys_fs_del(sdp);
805
806  fail:
807         vfree(sdp);
808         sb->s_fs_info = NULL;
809
810         return error;
811 }
812
813 static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
814                 const char *dev_name, void *data, struct vfsmount *mnt)
815 {
816         return get_sb_bdev(fs_type, flags, dev_name, data, fill_super, mnt);
817 }
818
819 static void gfs2_kill_sb(struct super_block *sb)
820 {
821         kill_block_super(sb);
822 }
823
824 struct file_system_type gfs2_fs_type = {
825         .name = "gfs2",
826         .fs_flags = FS_REQUIRES_DEV,
827         .get_sb = gfs2_get_sb,
828         .kill_sb = gfs2_kill_sb,
829         .owner = THIS_MODULE,
830 };
831
832 struct file_system_type gfs2meta_fs_type = {
833         .name = "gfs2meta",
834         .fs_flags = FS_REQUIRES_DEV,
835         .get_sb = gfs2_get_sb,
836         .kill_sb = gfs2_kill_sb,
837         .owner = THIS_MODULE,
838 };
839