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