2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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 v.2.
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/vmalloc.h>
16 #include <linux/blkdev.h>
17 #include <linux/kthread.h>
18 #include <asm/semaphore.h>
27 #include "ops_export.h"
28 #include "ops_fstype.h"
29 #include "ops_super.h"
39 static struct gfs2_sbd *init_sbd(struct super_block *sb)
44 sdp = vmalloc(sizeof(struct gfs2_sbd));
48 memset(sdp, 0, sizeof(struct gfs2_sbd));
53 gfs2_tune_init(&sdp->sd_tune);
55 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
56 sdp->sd_gl_hash[x].hb_lock = RW_LOCK_UNLOCKED;
57 INIT_LIST_HEAD(&sdp->sd_gl_hash[x].hb_list);
59 INIT_LIST_HEAD(&sdp->sd_reclaim_list);
60 spin_lock_init(&sdp->sd_reclaim_lock);
61 init_waitqueue_head(&sdp->sd_reclaim_wq);
62 init_MUTEX(&sdp->sd_invalidate_inodes_mutex);
64 init_MUTEX(&sdp->sd_inum_mutex);
65 spin_lock_init(&sdp->sd_statfs_spin);
66 init_MUTEX(&sdp->sd_statfs_mutex);
68 spin_lock_init(&sdp->sd_rindex_spin);
69 init_MUTEX(&sdp->sd_rindex_mutex);
70 INIT_LIST_HEAD(&sdp->sd_rindex_list);
71 INIT_LIST_HEAD(&sdp->sd_rindex_mru_list);
72 INIT_LIST_HEAD(&sdp->sd_rindex_recent_list);
74 INIT_LIST_HEAD(&sdp->sd_jindex_list);
75 spin_lock_init(&sdp->sd_jindex_spin);
76 init_MUTEX(&sdp->sd_jindex_mutex);
78 INIT_LIST_HEAD(&sdp->sd_unlinked_list);
79 spin_lock_init(&sdp->sd_unlinked_spin);
80 init_MUTEX(&sdp->sd_unlinked_mutex);
82 INIT_LIST_HEAD(&sdp->sd_quota_list);
83 spin_lock_init(&sdp->sd_quota_spin);
84 init_MUTEX(&sdp->sd_quota_mutex);
86 spin_lock_init(&sdp->sd_log_lock);
87 init_waitqueue_head(&sdp->sd_log_trans_wq);
88 init_waitqueue_head(&sdp->sd_log_flush_wq);
90 INIT_LIST_HEAD(&sdp->sd_log_le_gl);
91 INIT_LIST_HEAD(&sdp->sd_log_le_buf);
92 INIT_LIST_HEAD(&sdp->sd_log_le_revoke);
93 INIT_LIST_HEAD(&sdp->sd_log_le_rg);
94 INIT_LIST_HEAD(&sdp->sd_log_le_databuf);
96 INIT_LIST_HEAD(&sdp->sd_log_blks_list);
97 init_waitqueue_head(&sdp->sd_log_blks_wait);
99 INIT_LIST_HEAD(&sdp->sd_ail1_list);
100 INIT_LIST_HEAD(&sdp->sd_ail2_list);
102 init_MUTEX(&sdp->sd_log_flush_lock);
103 INIT_LIST_HEAD(&sdp->sd_log_flush_list);
105 INIT_LIST_HEAD(&sdp->sd_revoke_list);
107 init_MUTEX(&sdp->sd_freeze_lock);
112 static void init_vfs(struct gfs2_sbd *sdp)
114 struct super_block *sb = sdp->sd_vfs;
116 sb->s_magic = GFS2_MAGIC;
117 sb->s_op = &gfs2_super_ops;
118 sb->s_export_op = &gfs2_export_ops;
119 sb->s_maxbytes = MAX_LFS_FILESIZE;
121 if (sb->s_flags & (MS_NOATIME | MS_NODIRATIME))
122 set_bit(SDF_NOATIME, &sdp->sd_flags);
124 /* Don't let the VFS update atimes. GFS2 handles this itself. */
125 sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
127 /* Set up the buffer cache and fill in some fake block size values
128 to allow us to read-in the on-disk superblock. */
129 sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
130 sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
131 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT;
132 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
135 static int init_names(struct gfs2_sbd *sdp, int silent)
137 struct gfs2_sb *sb = NULL;
141 proto = sdp->sd_args.ar_lockproto;
142 table = sdp->sd_args.ar_locktable;
144 /* Try to autodetect */
146 if (!proto[0] || !table[0]) {
147 struct buffer_head *bh;
148 bh = sb_getblk(sdp->sd_vfs,
149 GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
151 clear_buffer_uptodate(bh);
152 clear_buffer_dirty(bh);
154 ll_rw_block(READ, 1, &bh);
157 if (!buffer_uptodate(bh)) {
162 sb = kmalloc(sizeof(struct gfs2_sb), GFP_KERNEL);
167 gfs2_sb_in(sb, bh->b_data);
170 error = gfs2_check_sb(sdp, sb, silent);
175 proto = sb->sb_lockproto;
177 table = sb->sb_locktable;
181 table = sdp->sd_vfs->s_id;
183 snprintf(sdp->sd_proto_name, GFS2_FSNAME_LEN, "%s", proto);
184 snprintf(sdp->sd_table_name, GFS2_FSNAME_LEN, "%s", table);
192 static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
195 struct task_struct *p;
201 p = kthread_run(gfs2_scand, sdp, "gfs2_scand");
204 fs_err(sdp, "can't start scand thread: %d\n", error);
207 sdp->sd_scand_process = p;
209 for (sdp->sd_glockd_num = 0;
210 sdp->sd_glockd_num < sdp->sd_args.ar_num_glockd;
211 sdp->sd_glockd_num++) {
212 p = kthread_run(gfs2_glockd, sdp, "gfs2_glockd");
215 fs_err(sdp, "can't start glockd thread: %d\n", error);
218 sdp->sd_glockd_process[sdp->sd_glockd_num] = p;
221 error = gfs2_glock_nq_num(sdp,
222 GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
223 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
226 fs_err(sdp, "can't acquire mount glock: %d\n", error);
230 error = gfs2_glock_nq_num(sdp,
231 GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
233 LM_FLAG_NOEXP | GL_EXACT | GL_NEVER_RECURSE,
236 fs_err(sdp, "can't acquire live glock: %d\n", error);
240 error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
241 CREATE, &sdp->sd_rename_gl);
243 fs_err(sdp, "can't create rename glock: %d\n", error);
247 error = gfs2_glock_get(sdp, GFS2_TRANS_LOCK, &gfs2_trans_glops,
248 CREATE, &sdp->sd_trans_gl);
250 fs_err(sdp, "can't create transaction glock: %d\n", error);
253 set_bit(GLF_STICKY, &sdp->sd_trans_gl->gl_flags);
258 gfs2_glock_put(sdp->sd_trans_gl);
261 gfs2_glock_put(sdp->sd_rename_gl);
264 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
267 gfs2_glock_dq_uninit(mount_gh);
270 while (sdp->sd_glockd_num--)
271 kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
273 kthread_stop(sdp->sd_scand_process);
278 int gfs2_lookup_root(struct gfs2_sbd *sdp)
281 struct gfs2_glock *gl;
282 struct gfs2_inode *ip;
284 error = gfs2_glock_get(sdp, sdp->sd_sb.sb_root_dir.no_addr,
285 &gfs2_inode_glops, CREATE, &gl);
287 error = gfs2_inode_get(gl, &sdp->sd_sb.sb_root_dir,
291 gfs2_inode_min_init(ip, DT_DIR);
292 sdp->sd_root_dir = gfs2_ip2v(ip);
301 static int init_sb(struct gfs2_sbd *sdp, int silent, int undo)
303 struct super_block *sb = sdp->sd_vfs;
304 struct gfs2_holder sb_gh;
309 iput(sdp->sd_master_dir);
313 error = gfs2_glock_nq_num(sdp,
314 GFS2_SB_LOCK, &gfs2_meta_glops,
315 LM_ST_SHARED, 0, &sb_gh);
317 fs_err(sdp, "can't acquire superblock glock: %d\n", error);
321 error = gfs2_read_sb(sdp, sb_gh.gh_gl, silent);
323 fs_err(sdp, "can't read superblock: %d\n", error);
327 /* Set up the buffer cache and SB for real */
329 if (sdp->sd_sb.sb_bsize < bdev_hardsect_size(sb->s_bdev)) {
330 fs_err(sdp, "FS block size (%u) is too small for device "
332 sdp->sd_sb.sb_bsize, bdev_hardsect_size(sb->s_bdev));
335 if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
336 fs_err(sdp, "FS block size (%u) is too big for machine "
338 sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
342 /* Get rid of buffers from the original block size */
343 sb_gh.gh_gl->gl_ops->go_inval(sb_gh.gh_gl, DIO_METADATA | DIO_DATA);
344 sb_gh.gh_gl->gl_aspace->i_blkbits = sdp->sd_sb.sb_bsize_shift;
346 sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
348 /* Get the root inode */
349 error = gfs2_lookup_root(sdp);
351 fs_err(sdp, "can't read in root inode: %d\n", error);
355 /* Get the root inode/dentry */
356 inode = sdp->sd_root_dir;
358 fs_err(sdp, "can't get root inode\n");
364 sb->s_root = d_alloc_root(inode);
366 fs_err(sdp, "can't get root dentry\n");
372 gfs2_glock_dq_uninit(&sb_gh);
376 iput(sdp->sd_root_dir);
380 static int init_journal(struct gfs2_sbd *sdp, int undo)
382 struct gfs2_holder ji_gh;
383 struct task_struct *p;
392 error = gfs2_lookup_simple(sdp->sd_master_dir, "jindex",
395 fs_err(sdp, "can't lookup journal index: %d\n", error);
398 set_bit(GLF_STICKY, &get_v2ip(sdp->sd_jindex)->i_gl->gl_flags);
400 /* Load in the journal index special file */
402 error = gfs2_jindex_hold(sdp, &ji_gh);
404 fs_err(sdp, "can't read journal index: %d\n", error);
409 if (!gfs2_jindex_size(sdp)) {
410 fs_err(sdp, "no journals!\n");
414 if (sdp->sd_args.ar_spectator) {
415 sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
416 sdp->sd_log_blks_free = sdp->sd_jdesc->jd_blocks;
418 if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
419 fs_err(sdp, "can't mount journal #%u\n",
420 sdp->sd_lockstruct.ls_jid);
421 fs_err(sdp, "there are only %u journals (0 - %u)\n",
422 gfs2_jindex_size(sdp),
423 gfs2_jindex_size(sdp) - 1);
426 sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
428 error = gfs2_glock_nq_num(sdp,
429 sdp->sd_lockstruct.ls_jid,
431 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP,
432 &sdp->sd_journal_gh);
434 fs_err(sdp, "can't acquire journal glock: %d\n", error);
438 error = gfs2_glock_nq_init(get_v2ip(sdp->sd_jdesc->jd_inode)->i_gl,
440 LM_FLAG_NOEXP | GL_EXACT,
443 fs_err(sdp, "can't acquire journal inode glock: %d\n",
445 goto fail_journal_gh;
448 error = gfs2_jdesc_check(sdp->sd_jdesc);
450 fs_err(sdp, "my journal (%u) is bad: %d\n",
451 sdp->sd_jdesc->jd_jid, error);
454 sdp->sd_log_blks_free = sdp->sd_jdesc->jd_blocks;
457 if (sdp->sd_lockstruct.ls_first) {
459 for (x = 0; x < sdp->sd_journals; x++) {
460 error = gfs2_recover_journal(gfs2_jdesc_find(sdp, x),
463 fs_err(sdp, "error recovering journal %u: %d\n",
469 gfs2_lm_others_may_mount(sdp);
470 } else if (!sdp->sd_args.ar_spectator) {
471 error = gfs2_recover_journal(sdp->sd_jdesc, WAIT);
473 fs_err(sdp, "error recovering my journal: %d\n", error);
478 set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
479 gfs2_glock_dq_uninit(&ji_gh);
482 /* Disown my Journal glock */
484 sdp->sd_journal_gh.gh_owner = NULL;
485 sdp->sd_jinode_gh.gh_owner = NULL;
487 p = kthread_run(gfs2_recoverd, sdp, "gfs2_recoverd");
490 fs_err(sdp, "can't start recoverd thread: %d\n", error);
493 sdp->sd_recoverd_process = p;
498 kthread_stop(sdp->sd_recoverd_process);
501 if (!sdp->sd_args.ar_spectator)
502 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
505 if (!sdp->sd_args.ar_spectator)
506 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
509 gfs2_jindex_free(sdp);
511 gfs2_glock_dq_uninit(&ji_gh);
514 iput(sdp->sd_jindex);
520 static int init_inodes(struct gfs2_sbd *sdp, int undo)
527 error = gfs2_lookup_master_dir(sdp);
529 fs_err(sdp, "can't read in master directory: %d\n", error);
533 error = init_journal(sdp, undo);
537 /* Read in the master inode number inode */
538 error = gfs2_lookup_simple(sdp->sd_master_dir, "inum",
539 &sdp->sd_inum_inode);
541 fs_err(sdp, "can't read in inum inode: %d\n", error);
546 /* Read in the master statfs inode */
547 error = gfs2_lookup_simple(sdp->sd_master_dir, "statfs",
548 &sdp->sd_statfs_inode);
550 fs_err(sdp, "can't read in statfs inode: %d\n", error);
554 /* Read in the resource index inode */
555 error = gfs2_lookup_simple(sdp->sd_master_dir, "rindex",
558 fs_err(sdp, "can't get resource index inode: %d\n", error);
561 set_bit(GLF_STICKY, &get_v2ip(sdp->sd_rindex)->i_gl->gl_flags);
562 sdp->sd_rindex_vn = get_v2ip(sdp->sd_rindex)->i_gl->gl_vn - 1;
564 /* Read in the quota inode */
565 error = gfs2_lookup_simple(sdp->sd_master_dir, "quota",
566 &sdp->sd_quota_inode);
568 fs_err(sdp, "can't get quota file inode: %d\n", error);
574 iput(sdp->sd_quota_inode);
577 gfs2_clear_rgrpd(sdp);
578 iput(sdp->sd_rindex);
581 iput(sdp->sd_statfs_inode);
584 iput(sdp->sd_inum_inode);
586 init_journal(sdp, UNDO);
588 iput(sdp->sd_master_dir);
593 static int init_per_node(struct gfs2_sbd *sdp, int undo)
595 struct inode *pn = NULL;
599 if (sdp->sd_args.ar_spectator)
605 error = gfs2_lookup_simple(sdp->sd_master_dir, "per_node", &pn);
607 fs_err(sdp, "can't find per_node directory: %d\n", error);
611 sprintf(buf, "inum_range%u", sdp->sd_jdesc->jd_jid);
612 error = gfs2_lookup_simple(pn, buf, &sdp->sd_ir_inode);
614 fs_err(sdp, "can't find local \"ir\" file: %d\n", error);
618 sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
619 error = gfs2_lookup_simple(pn, buf, &sdp->sd_sc_inode);
621 fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
625 sprintf(buf, "unlinked_tag%u", sdp->sd_jdesc->jd_jid);
626 error = gfs2_lookup_simple(pn, buf, &sdp->sd_ut_inode);
628 fs_err(sdp, "can't find local \"ut\" file: %d\n", error);
632 sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
633 error = gfs2_lookup_simple(pn, buf, &sdp->sd_qc_inode);
635 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
642 error = gfs2_glock_nq_init(get_v2ip(sdp->sd_ir_inode)->i_gl,
643 LM_ST_EXCLUSIVE, GL_NEVER_RECURSE,
646 fs_err(sdp, "can't lock local \"ir\" file: %d\n", error);
650 error = gfs2_glock_nq_init(get_v2ip(sdp->sd_sc_inode)->i_gl,
651 LM_ST_EXCLUSIVE, GL_NEVER_RECURSE,
654 fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
658 error = gfs2_glock_nq_init(get_v2ip(sdp->sd_ut_inode)->i_gl,
659 LM_ST_EXCLUSIVE, GL_NEVER_RECURSE,
662 fs_err(sdp, "can't lock local \"ut\" file: %d\n", error);
666 error = gfs2_glock_nq_init(get_v2ip(sdp->sd_qc_inode)->i_gl,
667 LM_ST_EXCLUSIVE, GL_NEVER_RECURSE,
670 fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
677 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
680 gfs2_glock_dq_uninit(&sdp->sd_ut_gh);
683 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
686 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
689 iput(sdp->sd_qc_inode);
692 iput(sdp->sd_ut_inode);
695 iput(sdp->sd_sc_inode);
698 iput(sdp->sd_ir_inode);
706 static int init_threads(struct gfs2_sbd *sdp, int undo)
708 struct task_struct *p;
714 sdp->sd_log_flush_time = jiffies;
715 sdp->sd_jindex_refresh_time = jiffies;
717 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
720 fs_err(sdp, "can't start logd thread: %d\n", error);
723 sdp->sd_logd_process = p;
725 sdp->sd_statfs_sync_time = jiffies;
726 sdp->sd_quota_sync_time = jiffies;
728 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
731 fs_err(sdp, "can't start quotad thread: %d\n", error);
734 sdp->sd_quotad_process = p;
736 p = kthread_run(gfs2_inoded, sdp, "gfs2_inoded");
739 fs_err(sdp, "can't start inoded thread: %d\n", error);
742 sdp->sd_inoded_process = p;
747 kthread_stop(sdp->sd_inoded_process);
750 kthread_stop(sdp->sd_quotad_process);
753 kthread_stop(sdp->sd_logd_process);
759 * fill_super - Read in superblock
760 * @sb: The VFS superblock
761 * @data: Mount options
762 * @silent: Don't complain if it's not a GFS2 filesystem
767 static int fill_super(struct super_block *sb, void *data, int silent)
769 struct gfs2_sbd *sdp;
770 struct gfs2_holder mount_gh;
775 printk("GFS2: can't alloc struct gfs2_sbd\n");
779 error = gfs2_mount_args(sdp, (char *)data, 0);
781 printk("GFS2: can't parse mount arguments\n");
787 error = init_names(sdp, silent);
791 error = gfs2_sys_fs_add(sdp);
795 error = gfs2_lm_mount(sdp, silent);
799 error = init_locking(sdp, &mount_gh, DO);
803 error = init_sb(sdp, silent, DO);
807 error = init_inodes(sdp, DO);
811 error = init_per_node(sdp, DO);
815 error = gfs2_statfs_init(sdp);
817 fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
821 error = init_threads(sdp, DO);
825 if (!(sb->s_flags & MS_RDONLY)) {
826 error = gfs2_make_fs_rw(sdp);
828 fs_err(sdp, "can't make FS RW: %d\n", error);
833 gfs2_glock_dq_uninit(&mount_gh);
838 init_threads(sdp, UNDO);
841 init_per_node(sdp, UNDO);
844 init_inodes(sdp, UNDO);
847 init_sb(sdp, 0, UNDO);
850 init_locking(sdp, &mount_gh, UNDO);
853 gfs2_gl_hash_clear(sdp, WAIT);
854 gfs2_lm_unmount(sdp);
855 while (invalidate_inodes(sb))
859 gfs2_sys_fs_del(sdp);
868 static struct super_block *gfs2_get_sb(struct file_system_type *fs_type,
869 int flags, const char *dev_name,
872 return get_sb_bdev(fs_type, flags, dev_name, data, fill_super);
875 struct file_system_type gfs2_fs_type = {
877 .fs_flags = FS_REQUIRES_DEV,
878 .get_sb = gfs2_get_sb,
879 .kill_sb = kill_block_super,
880 .owner = THIS_MODULE,