]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/gfs2/ops_fstype.c
GFS2: Streamline alloc calculations for writes
[net-next-2.6.git] / fs / gfs2 / ops_fstype.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
cf45b752 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
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>
b3b94faa
DT
15#include <linux/blkdev.h>
16#include <linux/kthread.h>
86384605
AD
17#include <linux/namei.h>
18#include <linux/mount.h>
5c676f6d 19#include <linux/gfs2_ondisk.h>
7d308590 20#include <linux/lm_interface.h>
b3b94faa
DT
21
22#include "gfs2.h"
5c676f6d 23#include "incore.h"
da6dd40d 24#include "bmap.h"
b3b94faa
DT
25#include "glock.h"
26#include "glops.h"
27#include "inode.h"
b3b94faa 28#include "mount.h"
b3b94faa
DT
29#include "recovery.h"
30#include "rgrp.h"
31#include "super.h"
b3b94faa 32#include "sys.h"
5c676f6d 33#include "util.h"
bb3b0e3d 34#include "log.h"
9ac1b4d9 35#include "quota.h"
b5289681 36#include "dir.h"
b3b94faa
DT
37
38#define DO 0
39#define UNDO 1
40
9b8df98f
SW
41static const u32 gfs2_old_fs_formats[] = {
42 0
43};
44
45static const u32 gfs2_old_multihost_formats[] = {
46 0
47};
48
49/**
50 * gfs2_tune_init - Fill a gfs2_tune structure with default values
51 * @gt: tune
52 *
53 */
54
55static void gfs2_tune_init(struct gfs2_tune *gt)
56{
57 spin_lock_init(&gt->gt_spin);
58
9b8df98f
SW
59 gt->gt_incore_log_blocks = 1024;
60 gt->gt_log_flush_secs = 60;
61 gt->gt_recoverd_secs = 60;
62 gt->gt_logd_secs = 1;
9b8df98f
SW
63 gt->gt_quota_simul_sync = 64;
64 gt->gt_quota_warn_period = 10;
65 gt->gt_quota_scale_num = 1;
66 gt->gt_quota_scale_den = 1;
67 gt->gt_quota_cache_secs = 300;
68 gt->gt_quota_quantum = 60;
9b8df98f
SW
69 gt->gt_new_files_jdata = 0;
70 gt->gt_max_readahead = 1 << 18;
71 gt->gt_stall_secs = 600;
72 gt->gt_complain_secs = 10;
73 gt->gt_statfs_quantum = 30;
74 gt->gt_statfs_slow = 0;
75}
76
b3b94faa
DT
77static struct gfs2_sbd *init_sbd(struct super_block *sb)
78{
79 struct gfs2_sbd *sdp;
b3b94faa 80
85d1da67 81 sdp = kzalloc(sizeof(struct gfs2_sbd), GFP_KERNEL);
b3b94faa
DT
82 if (!sdp)
83 return NULL;
84
5c676f6d 85 sb->s_fs_info = sdp;
b3b94faa
DT
86 sdp->sd_vfs = sb;
87
88 gfs2_tune_init(&sdp->sd_tune);
89
f55ab26a 90 mutex_init(&sdp->sd_inum_mutex);
b3b94faa 91 spin_lock_init(&sdp->sd_statfs_spin);
b3b94faa
DT
92
93 spin_lock_init(&sdp->sd_rindex_spin);
f55ab26a 94 mutex_init(&sdp->sd_rindex_mutex);
b3b94faa
DT
95 INIT_LIST_HEAD(&sdp->sd_rindex_list);
96 INIT_LIST_HEAD(&sdp->sd_rindex_mru_list);
b3b94faa
DT
97
98 INIT_LIST_HEAD(&sdp->sd_jindex_list);
99 spin_lock_init(&sdp->sd_jindex_spin);
f55ab26a 100 mutex_init(&sdp->sd_jindex_mutex);
b3b94faa 101
b3b94faa
DT
102 INIT_LIST_HEAD(&sdp->sd_quota_list);
103 spin_lock_init(&sdp->sd_quota_spin);
f55ab26a 104 mutex_init(&sdp->sd_quota_mutex);
37b2c837 105 init_waitqueue_head(&sdp->sd_quota_wait);
813e0c46
SW
106 INIT_LIST_HEAD(&sdp->sd_trunc_list);
107 spin_lock_init(&sdp->sd_trunc_lock);
b3b94faa
DT
108
109 spin_lock_init(&sdp->sd_log_lock);
b3b94faa 110
b3b94faa
DT
111 INIT_LIST_HEAD(&sdp->sd_log_le_buf);
112 INIT_LIST_HEAD(&sdp->sd_log_le_revoke);
113 INIT_LIST_HEAD(&sdp->sd_log_le_rg);
114 INIT_LIST_HEAD(&sdp->sd_log_le_databuf);
d7b616e2 115 INIT_LIST_HEAD(&sdp->sd_log_le_ordered);
b3b94faa 116
71b86f56 117 mutex_init(&sdp->sd_log_reserve_mutex);
b3b94faa
DT
118 INIT_LIST_HEAD(&sdp->sd_ail1_list);
119 INIT_LIST_HEAD(&sdp->sd_ail2_list);
120
484adff8 121 init_rwsem(&sdp->sd_log_flush_lock);
16615be1
SW
122 atomic_set(&sdp->sd_log_in_flight, 0);
123 init_waitqueue_head(&sdp->sd_log_flush_wait);
b3b94faa
DT
124
125 INIT_LIST_HEAD(&sdp->sd_revoke_list);
126
f55ab26a 127 mutex_init(&sdp->sd_freeze_lock);
b3b94faa
DT
128
129 return sdp;
130}
131
b3b94faa 132
9b8df98f
SW
133/**
134 * gfs2_check_sb - Check superblock
135 * @sdp: the filesystem
136 * @sb: The superblock
137 * @silent: Don't print a message if the check fails
138 *
139 * Checks the version code of the FS is one that we understand how to
140 * read and that the sizes of the various on-disk structures have not
141 * changed.
142 */
143
144static int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb_host *sb, int silent)
145{
146 unsigned int x;
147
148 if (sb->sb_magic != GFS2_MAGIC ||
149 sb->sb_type != GFS2_METATYPE_SB) {
150 if (!silent)
151 printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
152 return -EINVAL;
153 }
154
155 /* If format numbers match exactly, we're done. */
156
157 if (sb->sb_fs_format == GFS2_FORMAT_FS &&
158 sb->sb_multihost_format == GFS2_FORMAT_MULTI)
159 return 0;
160
161 if (sb->sb_fs_format != GFS2_FORMAT_FS) {
162 for (x = 0; gfs2_old_fs_formats[x]; x++)
163 if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
164 break;
165
166 if (!gfs2_old_fs_formats[x]) {
167 printk(KERN_WARNING
168 "GFS2: code version (%u, %u) is incompatible "
169 "with ondisk format (%u, %u)\n",
170 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
171 sb->sb_fs_format, sb->sb_multihost_format);
172 printk(KERN_WARNING
173 "GFS2: I don't know how to upgrade this FS\n");
174 return -EINVAL;
175 }
176 }
177
178 if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
179 for (x = 0; gfs2_old_multihost_formats[x]; x++)
180 if (gfs2_old_multihost_formats[x] ==
181 sb->sb_multihost_format)
182 break;
183
184 if (!gfs2_old_multihost_formats[x]) {
185 printk(KERN_WARNING
186 "GFS2: code version (%u, %u) is incompatible "
187 "with ondisk format (%u, %u)\n",
188 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
189 sb->sb_fs_format, sb->sb_multihost_format);
190 printk(KERN_WARNING
191 "GFS2: I don't know how to upgrade this FS\n");
192 return -EINVAL;
193 }
194 }
195
196 if (!sdp->sd_args.ar_upgrade) {
197 printk(KERN_WARNING
198 "GFS2: code version (%u, %u) is incompatible "
199 "with ondisk format (%u, %u)\n",
200 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
201 sb->sb_fs_format, sb->sb_multihost_format);
202 printk(KERN_INFO
203 "GFS2: Use the \"upgrade\" mount option to upgrade "
204 "the FS\n");
205 printk(KERN_INFO "GFS2: See the manual for more details\n");
206 return -EINVAL;
207 }
208
209 return 0;
210}
211
212static void end_bio_io_page(struct bio *bio, int error)
213{
214 struct page *page = bio->bi_private;
215
216 if (!error)
217 SetPageUptodate(page);
218 else
219 printk(KERN_WARNING "gfs2: error %d reading superblock\n", error);
220 unlock_page(page);
221}
222
223static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf)
224{
225 const struct gfs2_sb *str = buf;
226
227 sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
228 sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
229 sb->sb_format = be32_to_cpu(str->sb_header.mh_format);
230 sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
231 sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
232 sb->sb_bsize = be32_to_cpu(str->sb_bsize);
233 sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
234 sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
235 sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
236 sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
237 sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
238
239 memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
240 memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
241}
242
243/**
244 * gfs2_read_super - Read the gfs2 super block from disk
245 * @sdp: The GFS2 super block
246 * @sector: The location of the super block
247 * @error: The error code to return
248 *
249 * This uses the bio functions to read the super block from disk
250 * because we want to be 100% sure that we never read cached data.
251 * A super block is read twice only during each GFS2 mount and is
252 * never written to by the filesystem. The first time its read no
253 * locks are held, and the only details which are looked at are those
254 * relating to the locking protocol. Once locking is up and working,
255 * the sb is read again under the lock to establish the location of
256 * the master directory (contains pointers to journals etc) and the
257 * root directory.
258 *
259 * Returns: 0 on success or error
260 */
261
262static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector)
263{
264 struct super_block *sb = sdp->sd_vfs;
265 struct gfs2_sb *p;
266 struct page *page;
267 struct bio *bio;
268
269 page = alloc_page(GFP_NOFS);
270 if (unlikely(!page))
271 return -ENOBUFS;
272
273 ClearPageUptodate(page);
274 ClearPageDirty(page);
275 lock_page(page);
276
277 bio = bio_alloc(GFP_NOFS, 1);
278 if (unlikely(!bio)) {
279 __free_page(page);
280 return -ENOBUFS;
281 }
282
283 bio->bi_sector = sector * (sb->s_blocksize >> 9);
284 bio->bi_bdev = sb->s_bdev;
285 bio_add_page(bio, page, PAGE_SIZE, 0);
286
287 bio->bi_end_io = end_bio_io_page;
288 bio->bi_private = page;
289 submit_bio(READ_SYNC | (1 << BIO_RW_META), bio);
290 wait_on_page_locked(page);
291 bio_put(bio);
292 if (!PageUptodate(page)) {
293 __free_page(page);
294 return -EIO;
295 }
296 p = kmap(page);
297 gfs2_sb_in(&sdp->sd_sb, p);
298 kunmap(page);
299 __free_page(page);
300 return 0;
301}
302/**
303 * gfs2_read_sb - Read super block
304 * @sdp: The GFS2 superblock
305 * @gl: the glock for the superblock (assumed to be held)
306 * @silent: Don't print message if mount fails
307 *
308 */
309
310static int gfs2_read_sb(struct gfs2_sbd *sdp, struct gfs2_glock *gl, int silent)
311{
312 u32 hash_blocks, ind_blocks, leaf_blocks;
313 u32 tmp_blocks;
314 unsigned int x;
315 int error;
316
317 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
318 if (error) {
319 if (!silent)
320 fs_err(sdp, "can't read superblock\n");
321 return error;
322 }
323
324 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
325 if (error)
326 return error;
327
328 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
329 GFS2_BASIC_BLOCK_SHIFT;
330 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
331 sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
332 sizeof(struct gfs2_dinode)) / sizeof(u64);
333 sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
334 sizeof(struct gfs2_meta_header)) / sizeof(u64);
335 sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
336 sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
337 sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
338 sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
339 sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
340 sizeof(struct gfs2_meta_header)) /
341 sizeof(struct gfs2_quota_change);
342
343 /* Compute maximum reservation required to add a entry to a directory */
344
345 hash_blocks = DIV_ROUND_UP(sizeof(u64) * (1 << GFS2_DIR_MAX_DEPTH),
346 sdp->sd_jbsize);
347
348 ind_blocks = 0;
349 for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
350 tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
351 ind_blocks += tmp_blocks;
352 }
353
354 leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
355
356 sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
357
358 sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
359 sizeof(struct gfs2_dinode);
360 sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
361 for (x = 2;; x++) {
362 u64 space, d;
363 u32 m;
364
365 space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
366 d = space;
367 m = do_div(d, sdp->sd_inptrs);
368
369 if (d != sdp->sd_heightsize[x - 1] || m)
370 break;
371 sdp->sd_heightsize[x] = space;
372 }
373 sdp->sd_max_height = x;
374 sdp->sd_heightsize[x] = ~0;
375 gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
376
377 sdp->sd_jheightsize[0] = sdp->sd_sb.sb_bsize -
378 sizeof(struct gfs2_dinode);
379 sdp->sd_jheightsize[1] = sdp->sd_jbsize * sdp->sd_diptrs;
380 for (x = 2;; x++) {
381 u64 space, d;
382 u32 m;
383
384 space = sdp->sd_jheightsize[x - 1] * sdp->sd_inptrs;
385 d = space;
386 m = do_div(d, sdp->sd_inptrs);
387
388 if (d != sdp->sd_jheightsize[x - 1] || m)
389 break;
390 sdp->sd_jheightsize[x] = space;
391 }
392 sdp->sd_max_jheight = x;
393 sdp->sd_jheightsize[x] = ~0;
394 gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
395
396 return 0;
397}
398
b3b94faa
DT
399static int init_names(struct gfs2_sbd *sdp, int silent)
400{
b3b94faa
DT
401 char *proto, *table;
402 int error = 0;
403
404 proto = sdp->sd_args.ar_lockproto;
405 table = sdp->sd_args.ar_locktable;
406
407 /* Try to autodetect */
408
409 if (!proto[0] || !table[0]) {
bb8d8a6f
SW
410 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
411 if (error)
412 return error;
3cf1e7be
SW
413
414 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
b3b94faa
DT
415 if (error)
416 goto out;
417
418 if (!proto[0])
3cf1e7be 419 proto = sdp->sd_sb.sb_lockproto;
b3b94faa 420 if (!table[0])
3cf1e7be 421 table = sdp->sd_sb.sb_locktable;
b3b94faa
DT
422 }
423
424 if (!table[0])
425 table = sdp->sd_vfs->s_id;
426
00377d8e
JD
427 strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
428 strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
b3b94faa 429
5d35e31f
DC
430 table = sdp->sd_table_name;
431 while ((table = strchr(table, '/')))
b35997d4
RP
432 *table = '_';
433
a91ea69f 434out:
b3b94faa
DT
435 return error;
436}
437
438static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
439 int undo)
440{
b3b94faa
DT
441 int error = 0;
442
443 if (undo)
444 goto fail_trans;
445
b3b94faa
DT
446 error = gfs2_glock_nq_num(sdp,
447 GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
448 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
449 mount_gh);
450 if (error) {
451 fs_err(sdp, "can't acquire mount glock: %d\n", error);
452 goto fail;
453 }
454
455 error = gfs2_glock_nq_num(sdp,
456 GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
457 LM_ST_SHARED,
579b78a4 458 LM_FLAG_NOEXP | GL_EXACT,
b3b94faa
DT
459 &sdp->sd_live_gh);
460 if (error) {
461 fs_err(sdp, "can't acquire live glock: %d\n", error);
462 goto fail_mount;
463 }
464
465 error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
466 CREATE, &sdp->sd_rename_gl);
467 if (error) {
468 fs_err(sdp, "can't create rename glock: %d\n", error);
469 goto fail_live;
470 }
471
472 error = gfs2_glock_get(sdp, GFS2_TRANS_LOCK, &gfs2_trans_glops,
473 CREATE, &sdp->sd_trans_gl);
474 if (error) {
475 fs_err(sdp, "can't create transaction glock: %d\n", error);
476 goto fail_rename;
477 }
b3b94faa
DT
478
479 return 0;
480
feaa7bba 481fail_trans:
b3b94faa 482 gfs2_glock_put(sdp->sd_trans_gl);
feaa7bba 483fail_rename:
b3b94faa 484 gfs2_glock_put(sdp->sd_rename_gl);
feaa7bba 485fail_live:
b3b94faa 486 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
feaa7bba 487fail_mount:
b3b94faa 488 gfs2_glock_dq_uninit(mount_gh);
feaa7bba 489fail:
b3b94faa
DT
490 return error;
491}
492
9b8df98f
SW
493static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
494 u64 no_addr, const char *name)
f42faf4f 495{
9b8df98f
SW
496 struct gfs2_sbd *sdp = sb->s_fs_info;
497 struct dentry *dentry;
498 struct inode *inode;
499
500 inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0, 0);
501 if (IS_ERR(inode)) {
502 fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode));
503 return PTR_ERR(inode);
504 }
505 dentry = d_alloc_root(inode);
506 if (!dentry) {
507 fs_err(sdp, "can't alloc %s dentry\n", name);
508 iput(inode);
509 return -ENOMEM;
510 }
511 dentry->d_op = &gfs2_dops;
512 *dptr = dentry;
513 return 0;
f42faf4f
SW
514}
515
9b8df98f 516static int init_sb(struct gfs2_sbd *sdp, int silent)
b3b94faa
DT
517{
518 struct super_block *sb = sdp->sd_vfs;
519 struct gfs2_holder sb_gh;
dbb7cae2 520 u64 no_addr;
9b8df98f 521 int ret;
b3b94faa 522
9b8df98f
SW
523 ret = gfs2_glock_nq_num(sdp, GFS2_SB_LOCK, &gfs2_meta_glops,
524 LM_ST_SHARED, 0, &sb_gh);
525 if (ret) {
526 fs_err(sdp, "can't acquire superblock glock: %d\n", ret);
527 return ret;
b3b94faa 528 }
907b9bce 529
9b8df98f
SW
530 ret = gfs2_read_sb(sdp, sb_gh.gh_gl, silent);
531 if (ret) {
532 fs_err(sdp, "can't read superblock: %d\n", ret);
b3b94faa
DT
533 goto out;
534 }
535
536 /* Set up the buffer cache and SB for real */
b3b94faa 537 if (sdp->sd_sb.sb_bsize < bdev_hardsect_size(sb->s_bdev)) {
9b8df98f 538 ret = -EINVAL;
b3b94faa
DT
539 fs_err(sdp, "FS block size (%u) is too small for device "
540 "block size (%u)\n",
541 sdp->sd_sb.sb_bsize, bdev_hardsect_size(sb->s_bdev));
542 goto out;
543 }
544 if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
9b8df98f 545 ret = -EINVAL;
b3b94faa
DT
546 fs_err(sdp, "FS block size (%u) is too big for machine "
547 "page size (%u)\n",
548 sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
549 goto out;
550 }
b3b94faa
DT
551 sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
552
f42faf4f 553 /* Get the root inode */
dbb7cae2 554 no_addr = sdp->sd_sb.sb_root_dir.no_addr;
9b8df98f
SW
555 ret = gfs2_lookup_root(sb, &sdp->sd_root_dir, no_addr, "root");
556 if (ret)
f42faf4f 557 goto out;
b3b94faa 558
9b8df98f
SW
559 /* Get the master inode */
560 no_addr = sdp->sd_sb.sb_master_dir.no_addr;
561 ret = gfs2_lookup_root(sb, &sdp->sd_master_dir, no_addr, "master");
562 if (ret) {
563 dput(sdp->sd_root_dir);
564 goto out;
565 }
566 sb->s_root = dget(sdp->sd_args.ar_meta ? sdp->sd_master_dir : sdp->sd_root_dir);
f42faf4f 567out:
b3b94faa 568 gfs2_glock_dq_uninit(&sb_gh);
9b8df98f 569 return ret;
b3b94faa
DT
570}
571
da6dd40d
BP
572/**
573 * map_journal_extents - create a reusable "extent" mapping from all logical
574 * blocks to all physical blocks for the given journal. This will save
575 * us time when writing journal blocks. Most journals will have only one
576 * extent that maps all their logical blocks. That's because gfs2.mkfs
577 * arranges the journal blocks sequentially to maximize performance.
578 * So the extent would map the first block for the entire file length.
579 * However, gfs2_jadd can happen while file activity is happening, so
580 * those journals may not be sequential. Less likely is the case where
581 * the users created their own journals by mounting the metafs and
582 * laying it out. But it's still possible. These journals might have
583 * several extents.
584 *
585 * TODO: This should be done in bigger chunks rather than one block at a time,
586 * but since it's only done at mount time, I'm not worried about the
587 * time it takes.
588 */
589static int map_journal_extents(struct gfs2_sbd *sdp)
590{
591 struct gfs2_jdesc *jd = sdp->sd_jdesc;
592 unsigned int lb;
593 u64 db, prev_db; /* logical block, disk block, prev disk block */
594 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
595 struct gfs2_journal_extent *jext = NULL;
596 struct buffer_head bh;
597 int rc = 0;
598
da6dd40d
BP
599 prev_db = 0;
600
c9e98886 601 for (lb = 0; lb < ip->i_disksize >> sdp->sd_sb.sb_bsize_shift; lb++) {
da6dd40d
BP
602 bh.b_state = 0;
603 bh.b_blocknr = 0;
604 bh.b_size = 1 << ip->i_inode.i_blkbits;
605 rc = gfs2_block_map(jd->jd_inode, lb, &bh, 0);
606 db = bh.b_blocknr;
607 if (rc || !db) {
608 printk(KERN_INFO "GFS2 journal mapping error %d: lb="
609 "%u db=%llu\n", rc, lb, (unsigned long long)db);
610 break;
611 }
612 if (!prev_db || db != prev_db + 1) {
613 jext = kzalloc(sizeof(struct gfs2_journal_extent),
614 GFP_KERNEL);
615 if (!jext) {
616 printk(KERN_INFO "GFS2 error: out of memory "
617 "mapping journal extents.\n");
618 rc = -ENOMEM;
619 break;
620 }
621 jext->dblock = db;
622 jext->lblock = lb;
623 jext->blocks = 1;
624 list_add_tail(&jext->extent_list, &jd->extent_list);
625 } else {
626 jext->blocks++;
627 }
628 prev_db = db;
629 }
630 return rc;
631}
632
da755fdb
SW
633static void gfs2_lm_others_may_mount(struct gfs2_sbd *sdp)
634{
048bca22
SW
635 if (!sdp->sd_lockstruct.ls_ops->lm_others_may_mount)
636 return;
da755fdb
SW
637 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
638 sdp->sd_lockstruct.ls_ops->lm_others_may_mount(
639 sdp->sd_lockstruct.ls_lockspace);
640}
641
b5289681
SW
642/**
643 * gfs2_jindex_hold - Grab a lock on the jindex
644 * @sdp: The GFS2 superblock
645 * @ji_gh: the holder for the jindex glock
646 *
647 * Returns: errno
648 */
649
650static int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
651{
652 struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
653 struct qstr name;
654 char buf[20];
655 struct gfs2_jdesc *jd;
656 int error;
657
658 name.name = buf;
659
660 mutex_lock(&sdp->sd_jindex_mutex);
661
662 for (;;) {
663 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
664 if (error)
665 break;
666
667 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
668 name.hash = gfs2_disk_hash(name.name, name.len);
669
670 error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
671 if (error == -ENOENT) {
672 error = 0;
673 break;
674 }
675
676 gfs2_glock_dq_uninit(ji_gh);
677
678 if (error)
679 break;
680
681 error = -ENOMEM;
682 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
683 if (!jd)
684 break;
685
686 INIT_LIST_HEAD(&jd->extent_list);
687 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1);
688 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
689 if (!jd->jd_inode)
690 error = -ENOENT;
691 else
692 error = PTR_ERR(jd->jd_inode);
693 kfree(jd);
694 break;
695 }
696
697 spin_lock(&sdp->sd_jindex_spin);
698 jd->jd_jid = sdp->sd_journals++;
699 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
700 spin_unlock(&sdp->sd_jindex_spin);
701 }
702
703 mutex_unlock(&sdp->sd_jindex_mutex);
704
705 return error;
706}
707
3af165ac
SW
708/**
709 * gfs2_jindex_free - Clear all the journal index information
710 * @sdp: The GFS2 superblock
711 *
712 */
713
714static void gfs2_jindex_free(struct gfs2_sbd *sdp)
715{
716 struct list_head list, *head;
717 struct gfs2_jdesc *jd;
718 struct gfs2_journal_extent *jext;
719
720 spin_lock(&sdp->sd_jindex_spin);
721 list_add(&list, &sdp->sd_jindex_list);
722 list_del_init(&sdp->sd_jindex_list);
723 sdp->sd_journals = 0;
724 spin_unlock(&sdp->sd_jindex_spin);
725
726 while (!list_empty(&list)) {
727 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
728 head = &jd->extent_list;
729 while (!list_empty(head)) {
730 jext = list_entry(head->next,
731 struct gfs2_journal_extent,
732 extent_list);
733 list_del(&jext->extent_list);
734 kfree(jext);
735 }
736 list_del(&jd->jd_list);
737 iput(jd->jd_inode);
738 kfree(jd);
739 }
740}
741
b3b94faa
DT
742static int init_journal(struct gfs2_sbd *sdp, int undo)
743{
9b8df98f 744 struct inode *master = sdp->sd_master_dir->d_inode;
b3b94faa
DT
745 struct gfs2_holder ji_gh;
746 struct task_struct *p;
5c676f6d 747 struct gfs2_inode *ip;
b3b94faa
DT
748 int jindex = 1;
749 int error = 0;
750
751 if (undo) {
752 jindex = 0;
753 goto fail_recoverd;
754 }
755
9b8df98f 756 sdp->sd_jindex = gfs2_lookup_simple(master, "jindex");
c752666c 757 if (IS_ERR(sdp->sd_jindex)) {
b3b94faa 758 fs_err(sdp, "can't lookup journal index: %d\n", error);
c752666c 759 return PTR_ERR(sdp->sd_jindex);
b3b94faa 760 }
feaa7bba 761 ip = GFS2_I(sdp->sd_jindex);
b3b94faa
DT
762
763 /* Load in the journal index special file */
764
765 error = gfs2_jindex_hold(sdp, &ji_gh);
766 if (error) {
767 fs_err(sdp, "can't read journal index: %d\n", error);
768 goto fail;
769 }
770
771 error = -EINVAL;
772 if (!gfs2_jindex_size(sdp)) {
773 fs_err(sdp, "no journals!\n");
907b9bce 774 goto fail_jindex;
b3b94faa
DT
775 }
776
777 if (sdp->sd_args.ar_spectator) {
778 sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
fd041f0b 779 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
780 } else {
781 if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
782 fs_err(sdp, "can't mount journal #%u\n",
783 sdp->sd_lockstruct.ls_jid);
784 fs_err(sdp, "there are only %u journals (0 - %u)\n",
785 gfs2_jindex_size(sdp),
786 gfs2_jindex_size(sdp) - 1);
787 goto fail_jindex;
788 }
789 sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
790
feaa7bba 791 error = gfs2_glock_nq_num(sdp, sdp->sd_lockstruct.ls_jid,
b3b94faa
DT
792 &gfs2_journal_glops,
793 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP,
794 &sdp->sd_journal_gh);
795 if (error) {
796 fs_err(sdp, "can't acquire journal glock: %d\n", error);
797 goto fail_jindex;
798 }
799
feaa7bba
SW
800 ip = GFS2_I(sdp->sd_jdesc->jd_inode);
801 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
75be73a8 802 LM_FLAG_NOEXP | GL_EXACT | GL_NOCACHE,
b3b94faa
DT
803 &sdp->sd_jinode_gh);
804 if (error) {
805 fs_err(sdp, "can't acquire journal inode glock: %d\n",
806 error);
807 goto fail_journal_gh;
808 }
809
810 error = gfs2_jdesc_check(sdp->sd_jdesc);
811 if (error) {
812 fs_err(sdp, "my journal (%u) is bad: %d\n",
813 sdp->sd_jdesc->jd_jid, error);
814 goto fail_jinode_gh;
815 }
fd041f0b 816 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
da6dd40d
BP
817
818 /* Map the extents for this journal's blocks */
819 map_journal_extents(sdp);
b3b94faa
DT
820 }
821
822 if (sdp->sd_lockstruct.ls_first) {
823 unsigned int x;
824 for (x = 0; x < sdp->sd_journals; x++) {
c63e31c2 825 error = gfs2_recover_journal(gfs2_jdesc_find(sdp, x));
b3b94faa
DT
826 if (error) {
827 fs_err(sdp, "error recovering journal %u: %d\n",
828 x, error);
829 goto fail_jinode_gh;
830 }
831 }
832
833 gfs2_lm_others_may_mount(sdp);
834 } else if (!sdp->sd_args.ar_spectator) {
c63e31c2 835 error = gfs2_recover_journal(sdp->sd_jdesc);
b3b94faa
DT
836 if (error) {
837 fs_err(sdp, "error recovering my journal: %d\n", error);
838 goto fail_jinode_gh;
839 }
840 }
841
842 set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
843 gfs2_glock_dq_uninit(&ji_gh);
844 jindex = 0;
845
b3b94faa
DT
846 p = kthread_run(gfs2_recoverd, sdp, "gfs2_recoverd");
847 error = IS_ERR(p);
848 if (error) {
849 fs_err(sdp, "can't start recoverd thread: %d\n", error);
850 goto fail_jinode_gh;
851 }
852 sdp->sd_recoverd_process = p;
853
854 return 0;
855
a91ea69f 856fail_recoverd:
b3b94faa 857 kthread_stop(sdp->sd_recoverd_process);
a91ea69f 858fail_jinode_gh:
b3b94faa
DT
859 if (!sdp->sd_args.ar_spectator)
860 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
a91ea69f 861fail_journal_gh:
b3b94faa
DT
862 if (!sdp->sd_args.ar_spectator)
863 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
a91ea69f 864fail_jindex:
b3b94faa
DT
865 gfs2_jindex_free(sdp);
866 if (jindex)
867 gfs2_glock_dq_uninit(&ji_gh);
a91ea69f 868fail:
f42faf4f 869 iput(sdp->sd_jindex);
b3b94faa
DT
870 return error;
871}
872
b3b94faa
DT
873
874static int init_inodes(struct gfs2_sbd *sdp, int undo)
875{
b3b94faa 876 int error = 0;
5c676f6d 877 struct gfs2_inode *ip;
9b8df98f 878 struct inode *master = sdp->sd_master_dir->d_inode;
b3b94faa
DT
879
880 if (undo)
f42faf4f
SW
881 goto fail_qinode;
882
f42faf4f
SW
883 error = init_journal(sdp, undo);
884 if (error)
9b8df98f 885 goto fail;
b3b94faa
DT
886
887 /* Read in the master inode number inode */
9b8df98f 888 sdp->sd_inum_inode = gfs2_lookup_simple(master, "inum");
c752666c
SW
889 if (IS_ERR(sdp->sd_inum_inode)) {
890 error = PTR_ERR(sdp->sd_inum_inode);
b3b94faa 891 fs_err(sdp, "can't read in inum inode: %d\n", error);
f42faf4f 892 goto fail_journal;
b3b94faa
DT
893 }
894
f42faf4f 895
b3b94faa 896 /* Read in the master statfs inode */
9b8df98f 897 sdp->sd_statfs_inode = gfs2_lookup_simple(master, "statfs");
c752666c
SW
898 if (IS_ERR(sdp->sd_statfs_inode)) {
899 error = PTR_ERR(sdp->sd_statfs_inode);
b3b94faa 900 fs_err(sdp, "can't read in statfs inode: %d\n", error);
f42faf4f 901 goto fail_inum;
b3b94faa
DT
902 }
903
904 /* Read in the resource index inode */
9b8df98f 905 sdp->sd_rindex = gfs2_lookup_simple(master, "rindex");
c752666c
SW
906 if (IS_ERR(sdp->sd_rindex)) {
907 error = PTR_ERR(sdp->sd_rindex);
b3b94faa
DT
908 fs_err(sdp, "can't get resource index inode: %d\n", error);
909 goto fail_statfs;
910 }
feaa7bba 911 ip = GFS2_I(sdp->sd_rindex);
cf45b752 912 sdp->sd_rindex_uptodate = 0;
b3b94faa
DT
913
914 /* Read in the quota inode */
9b8df98f 915 sdp->sd_quota_inode = gfs2_lookup_simple(master, "quota");
c752666c
SW
916 if (IS_ERR(sdp->sd_quota_inode)) {
917 error = PTR_ERR(sdp->sd_quota_inode);
b3b94faa
DT
918 fs_err(sdp, "can't get quota file inode: %d\n", error);
919 goto fail_rindex;
920 }
b3b94faa
DT
921 return 0;
922
f42faf4f
SW
923fail_qinode:
924 iput(sdp->sd_quota_inode);
f42faf4f 925fail_rindex:
b3b94faa 926 gfs2_clear_rgrpd(sdp);
f42faf4f 927 iput(sdp->sd_rindex);
f42faf4f
SW
928fail_statfs:
929 iput(sdp->sd_statfs_inode);
f42faf4f
SW
930fail_inum:
931 iput(sdp->sd_inum_inode);
932fail_journal:
933 init_journal(sdp, UNDO);
f42faf4f 934fail:
b3b94faa
DT
935 return error;
936}
937
938static int init_per_node(struct gfs2_sbd *sdp, int undo)
939{
f42faf4f 940 struct inode *pn = NULL;
b3b94faa
DT
941 char buf[30];
942 int error = 0;
5c676f6d 943 struct gfs2_inode *ip;
9b8df98f 944 struct inode *master = sdp->sd_master_dir->d_inode;
b3b94faa
DT
945
946 if (sdp->sd_args.ar_spectator)
947 return 0;
948
949 if (undo)
950 goto fail_qc_gh;
951
9b8df98f 952 pn = gfs2_lookup_simple(master, "per_node");
c752666c
SW
953 if (IS_ERR(pn)) {
954 error = PTR_ERR(pn);
b3b94faa
DT
955 fs_err(sdp, "can't find per_node directory: %d\n", error);
956 return error;
957 }
958
959 sprintf(buf, "inum_range%u", sdp->sd_jdesc->jd_jid);
c752666c
SW
960 sdp->sd_ir_inode = gfs2_lookup_simple(pn, buf);
961 if (IS_ERR(sdp->sd_ir_inode)) {
962 error = PTR_ERR(sdp->sd_ir_inode);
b3b94faa
DT
963 fs_err(sdp, "can't find local \"ir\" file: %d\n", error);
964 goto fail;
965 }
966
967 sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
c752666c
SW
968 sdp->sd_sc_inode = gfs2_lookup_simple(pn, buf);
969 if (IS_ERR(sdp->sd_sc_inode)) {
970 error = PTR_ERR(sdp->sd_sc_inode);
b3b94faa
DT
971 fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
972 goto fail_ir_i;
973 }
974
b3b94faa 975 sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
c752666c
SW
976 sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
977 if (IS_ERR(sdp->sd_qc_inode)) {
978 error = PTR_ERR(sdp->sd_qc_inode);
b3b94faa
DT
979 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
980 goto fail_ut_i;
981 }
982
f42faf4f 983 iput(pn);
b3b94faa
DT
984 pn = NULL;
985
feaa7bba 986 ip = GFS2_I(sdp->sd_ir_inode);
5c676f6d 987 error = gfs2_glock_nq_init(ip->i_gl,
579b78a4 988 LM_ST_EXCLUSIVE, 0,
b3b94faa
DT
989 &sdp->sd_ir_gh);
990 if (error) {
991 fs_err(sdp, "can't lock local \"ir\" file: %d\n", error);
992 goto fail_qc_i;
993 }
994
feaa7bba 995 ip = GFS2_I(sdp->sd_sc_inode);
5c676f6d 996 error = gfs2_glock_nq_init(ip->i_gl,
579b78a4 997 LM_ST_EXCLUSIVE, 0,
b3b94faa
DT
998 &sdp->sd_sc_gh);
999 if (error) {
1000 fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
1001 goto fail_ir_gh;
1002 }
1003
feaa7bba 1004 ip = GFS2_I(sdp->sd_qc_inode);
5c676f6d 1005 error = gfs2_glock_nq_init(ip->i_gl,
579b78a4 1006 LM_ST_EXCLUSIVE, 0,
b3b94faa
DT
1007 &sdp->sd_qc_gh);
1008 if (error) {
1009 fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
1010 goto fail_ut_gh;
1011 }
1012
1013 return 0;
1014
a91ea69f 1015fail_qc_gh:
b3b94faa 1016 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
a91ea69f 1017fail_ut_gh:
b3b94faa 1018 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
a91ea69f 1019fail_ir_gh:
b3b94faa 1020 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
a91ea69f 1021fail_qc_i:
f42faf4f 1022 iput(sdp->sd_qc_inode);
a91ea69f 1023fail_ut_i:
f42faf4f 1024 iput(sdp->sd_sc_inode);
a91ea69f 1025fail_ir_i:
f42faf4f 1026 iput(sdp->sd_ir_inode);
a91ea69f 1027fail:
b3b94faa 1028 if (pn)
f42faf4f 1029 iput(pn);
b3b94faa
DT
1030 return error;
1031}
1032
1033static int init_threads(struct gfs2_sbd *sdp, int undo)
1034{
1035 struct task_struct *p;
1036 int error = 0;
1037
1038 if (undo)
feaa7bba 1039 goto fail_quotad;
b3b94faa
DT
1040
1041 sdp->sd_log_flush_time = jiffies;
1042 sdp->sd_jindex_refresh_time = jiffies;
1043
1044 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
1045 error = IS_ERR(p);
1046 if (error) {
1047 fs_err(sdp, "can't start logd thread: %d\n", error);
1048 return error;
1049 }
1050 sdp->sd_logd_process = p;
1051
b3b94faa
DT
1052 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
1053 error = IS_ERR(p);
1054 if (error) {
1055 fs_err(sdp, "can't start quotad thread: %d\n", error);
1056 goto fail;
1057 }
1058 sdp->sd_quotad_process = p;
1059
b3b94faa
DT
1060 return 0;
1061
b3b94faa 1062
feaa7bba 1063fail_quotad:
b3b94faa 1064 kthread_stop(sdp->sd_quotad_process);
feaa7bba 1065fail:
b3b94faa 1066 kthread_stop(sdp->sd_logd_process);
b3b94faa
DT
1067 return error;
1068}
da755fdb
SW
1069
1070/**
1071 * gfs2_lm_mount - mount a locking protocol
1072 * @sdp: the filesystem
1073 * @args: mount arguements
1074 * @silent: if 1, don't complain if the FS isn't a GFS2 fs
1075 *
1076 * Returns: errno
1077 */
1078
1079static int gfs2_lm_mount(struct gfs2_sbd *sdp, int silent)
1080{
1081 char *proto = sdp->sd_proto_name;
1082 char *table = sdp->sd_table_name;
58e9fee1 1083 int flags = LM_MFLAG_CONV_NODROP;
da755fdb
SW
1084 int error;
1085
1086 if (sdp->sd_args.ar_spectator)
1087 flags |= LM_MFLAG_SPECTATOR;
1088
1089 fs_info(sdp, "Trying to join cluster \"%s\", \"%s\"\n", proto, table);
1090
1091 error = gfs2_mount_lockproto(proto, table, sdp->sd_args.ar_hostdata,
1092 gfs2_glock_cb, sdp,
1093 GFS2_MIN_LVB_SIZE, flags,
1094 &sdp->sd_lockstruct, &sdp->sd_kobj);
1095 if (error) {
1096 fs_info(sdp, "can't mount proto=%s, table=%s, hostdata=%s\n",
1097 proto, table, sdp->sd_args.ar_hostdata);
1098 goto out;
1099 }
1100
048bca22 1101 if (gfs2_assert_warn(sdp, sdp->sd_lockstruct.ls_ops) ||
da755fdb
SW
1102 gfs2_assert_warn(sdp, sdp->sd_lockstruct.ls_lvb_size >=
1103 GFS2_MIN_LVB_SIZE)) {
1104 gfs2_unmount_lockproto(&sdp->sd_lockstruct);
1105 goto out;
1106 }
1107
1108 if (sdp->sd_args.ar_spectator)
1109 snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s.s", table);
1110 else
1111 snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s.%u", table,
1112 sdp->sd_lockstruct.ls_jid);
1113
1114 fs_info(sdp, "Joined cluster. Now mounting FS...\n");
1115
1116 if ((sdp->sd_lockstruct.ls_flags & LM_LSFLAG_LOCAL) &&
1117 !sdp->sd_args.ar_ignore_local_fs) {
1118 sdp->sd_args.ar_localflocks = 1;
1119 sdp->sd_args.ar_localcaching = 1;
1120 }
1121
1122out:
1123 return error;
1124}
1125
1126void gfs2_lm_unmount(struct gfs2_sbd *sdp)
1127{
1128 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
1129 gfs2_unmount_lockproto(&sdp->sd_lockstruct);
1130}
b3b94faa
DT
1131
1132/**
1133 * fill_super - Read in superblock
1134 * @sb: The VFS superblock
1135 * @data: Mount options
1136 * @silent: Don't complain if it's not a GFS2 filesystem
1137 *
1138 * Returns: errno
1139 */
1140
1141static int fill_super(struct super_block *sb, void *data, int silent)
1142{
1143 struct gfs2_sbd *sdp;
1144 struct gfs2_holder mount_gh;
1145 int error;
1146
1147 sdp = init_sbd(sb);
1148 if (!sdp) {
d92a8d48 1149 printk(KERN_WARNING "GFS2: can't alloc struct gfs2_sbd\n");
b3b94faa
DT
1150 return -ENOMEM;
1151 }
1152
1153 error = gfs2_mount_args(sdp, (char *)data, 0);
1154 if (error) {
d92a8d48 1155 printk(KERN_WARNING "GFS2: can't parse mount arguments\n");
b3b94faa
DT
1156 goto fail;
1157 }
1158
719ee344
SW
1159 sb->s_magic = GFS2_MAGIC;
1160 sb->s_op = &gfs2_super_ops;
1161 sb->s_export_op = &gfs2_export_ops;
1162 sb->s_time_gran = 1;
1163 sb->s_maxbytes = MAX_LFS_FILESIZE;
419c93e0
SW
1164
1165 /* Set up the buffer cache and fill in some fake block size values
1166 to allow us to read-in the on-disk superblock. */
1167 sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
1168 sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
1169 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
1170 GFS2_BASIC_BLOCK_SHIFT;
1171 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
b3b94faa
DT
1172
1173 error = init_names(sdp, silent);
1174 if (error)
1175 goto fail;
1176
7c52b166
RP
1177 gfs2_create_debugfs_file(sdp);
1178
b3b94faa
DT
1179 error = gfs2_sys_fs_add(sdp);
1180 if (error)
1181 goto fail;
1182
1183 error = gfs2_lm_mount(sdp, silent);
1184 if (error)
1185 goto fail_sys;
1186
1187 error = init_locking(sdp, &mount_gh, DO);
1188 if (error)
1189 goto fail_lm;
1190
9b8df98f 1191 error = init_sb(sdp, silent);
b3b94faa
DT
1192 if (error)
1193 goto fail_locking;
b3b94faa
DT
1194
1195 error = init_inodes(sdp, DO);
1196 if (error)
f42faf4f 1197 goto fail_sb;
b3b94faa
DT
1198
1199 error = init_per_node(sdp, DO);
1200 if (error)
1201 goto fail_inodes;
1202
1203 error = gfs2_statfs_init(sdp);
1204 if (error) {
1205 fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
1206 goto fail_per_node;
1207 }
1208
1209 error = init_threads(sdp, DO);
1210 if (error)
1211 goto fail_per_node;
1212
1213 if (!(sb->s_flags & MS_RDONLY)) {
1214 error = gfs2_make_fs_rw(sdp);
1215 if (error) {
1216 fs_err(sdp, "can't make FS RW: %d\n", error);
1217 goto fail_threads;
1218 }
1219 }
1220
1221 gfs2_glock_dq_uninit(&mount_gh);
1222
1223 return 0;
1224
a91ea69f 1225fail_threads:
b3b94faa 1226 init_threads(sdp, UNDO);
a91ea69f 1227fail_per_node:
b3b94faa 1228 init_per_node(sdp, UNDO);
a91ea69f 1229fail_inodes:
b3b94faa 1230 init_inodes(sdp, UNDO);
a91ea69f 1231fail_sb:
9b8df98f
SW
1232 if (sdp->sd_root_dir)
1233 dput(sdp->sd_root_dir);
1234 if (sdp->sd_master_dir)
1235 dput(sdp->sd_master_dir);
1236 sb->s_root = NULL;
a91ea69f 1237fail_locking:
b3b94faa 1238 init_locking(sdp, &mount_gh, UNDO);
a91ea69f 1239fail_lm:
3af165ac 1240 gfs2_gl_hash_clear(sb);
b3b94faa
DT
1241 gfs2_lm_unmount(sdp);
1242 while (invalidate_inodes(sb))
1243 yield();
a91ea69f 1244fail_sys:
b3b94faa 1245 gfs2_sys_fs_del(sdp);
a91ea69f 1246fail:
5f882096 1247 gfs2_delete_debugfs_file(sdp);
a2c45807 1248 kfree(sdp);
5c676f6d 1249 sb->s_fs_info = NULL;
b3b94faa
DT
1250 return error;
1251}
1252
ccd6efd0 1253static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
9b8df98f 1254 const char *dev_name, void *data, struct vfsmount *mnt)
b3b94faa 1255{
9b8df98f 1256 return get_sb_bdev(fs_type, flags, dev_name, data, fill_super, mnt);
86384605
AD
1257}
1258
9b8df98f 1259static struct super_block *get_gfs2_sb(const char *dev_name)
86384605 1260{
9b8df98f 1261 struct super_block *sb;
86384605 1262 struct nameidata nd;
86384605 1263 int error;
907b9bce 1264
86384605
AD
1265 error = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
1266 if (error) {
9b8df98f
SW
1267 printk(KERN_WARNING "GFS2: path_lookup on %s returned error %d\n",
1268 dev_name, error);
1269 return NULL;
86384605 1270 }
9b8df98f
SW
1271 sb = nd.path.dentry->d_inode->i_sb;
1272 if (sb && (sb->s_type == &gfs2_fs_type))
1273 atomic_inc(&sb->s_active);
1274 else
1275 sb = NULL;
1d957f9b 1276 path_put(&nd.path);
86384605
AD
1277 return sb;
1278}
1279
1280static int gfs2_get_sb_meta(struct file_system_type *fs_type, int flags,
1281 const char *dev_name, void *data, struct vfsmount *mnt)
1282{
9b8df98f 1283 struct super_block *sb = NULL;
86384605 1284 struct gfs2_sbd *sdp;
86384605
AD
1285
1286 sb = get_gfs2_sb(dev_name);
1287 if (!sb) {
1288 printk(KERN_WARNING "GFS2: gfs2 mount does not exist\n");
9b8df98f 1289 return -ENOENT;
86384605 1290 }
2d3ba1ea 1291 sdp = sb->s_fs_info;
9b8df98f
SW
1292 mnt->mnt_sb = sb;
1293 mnt->mnt_root = dget(sdp->sd_master_dir);
1294 return 0;
b3b94faa
DT
1295}
1296
419c93e0 1297static void gfs2_kill_sb(struct super_block *sb)
86384605
AD
1298{
1299 struct gfs2_sbd *sdp = sb->s_fs_info;
3af165ac
SW
1300
1301 if (sdp == NULL) {
1302 kill_block_super(sb);
1303 return;
acd2c8aa 1304 }
3af165ac
SW
1305 gfs2_meta_syncfs(sdp);
1306 dput(sdp->sd_root_dir);
1307 dput(sdp->sd_master_dir);
1308 sdp->sd_root_dir = NULL;
1309 sdp->sd_master_dir = NULL;
1310
1311 /* Unfreeze the filesystem, if we need to */
1312 mutex_lock(&sdp->sd_freeze_lock);
1313 if (sdp->sd_freeze_count)
1314 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
1315 mutex_unlock(&sdp->sd_freeze_lock);
1316
1317 kthread_stop(sdp->sd_quotad_process);
1318 kthread_stop(sdp->sd_logd_process);
1319 kthread_stop(sdp->sd_recoverd_process);
1320
1321 if (!(sb->s_flags & MS_RDONLY)) {
1322 int error = gfs2_make_fs_ro(sdp);
1323 if (error)
1324 gfs2_io_error(sdp);
1325 }
1326
1327 /* At this point, we're through modifying the disk */
1328 gfs2_jindex_free(sdp);
1329 gfs2_clear_rgrpd(sdp);
1330 iput(sdp->sd_jindex);
1331 iput(sdp->sd_inum_inode);
1332 iput(sdp->sd_statfs_inode);
1333 iput(sdp->sd_rindex);
1334 iput(sdp->sd_quota_inode);
1335
1336 gfs2_glock_put(sdp->sd_rename_gl);
1337 gfs2_glock_put(sdp->sd_trans_gl);
1338
1339 if (!sdp->sd_args.ar_spectator) {
1340 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
1341 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
1342 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
1343 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
1344 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
1345 iput(sdp->sd_ir_inode);
1346 iput(sdp->sd_sc_inode);
1347 iput(sdp->sd_qc_inode);
1348 }
1349 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
9b8df98f 1350 kill_block_super(sb);
3af165ac
SW
1351 gfs2_lm_unmount(sdp);
1352 gfs2_sys_fs_del(sdp);
1353 gfs2_delete_debugfs_file(sdp);
1354 kfree(sdp);
86384605
AD
1355}
1356
b3b94faa
DT
1357struct file_system_type gfs2_fs_type = {
1358 .name = "gfs2",
1359 .fs_flags = FS_REQUIRES_DEV,
1360 .get_sb = gfs2_get_sb,
419c93e0
SW
1361 .kill_sb = gfs2_kill_sb,
1362 .owner = THIS_MODULE,
1363};
1364
1365struct file_system_type gfs2meta_fs_type = {
1366 .name = "gfs2meta",
1367 .fs_flags = FS_REQUIRES_DEV,
86384605 1368 .get_sb = gfs2_get_sb_meta,
b3b94faa
DT
1369 .owner = THIS_MODULE,
1370};
1371