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