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