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