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