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