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