blob: 3eb49edae542dd17cf9f4597e583bf6a91e744cd [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersoncf45b752008-01-31 10:31:39 -06003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000015#include <linux/blkdev.h>
16#include <linux/kthread.h>
Abhijith Das86384602006-08-25 11:13:37 -050017#include <linux/namei.h>
18#include <linux/mount.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020020#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021
22#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include "incore.h"
Bob Petersonda6dd402007-12-11 18:49:21 -060024#include "bmap.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "glock.h"
26#include "glops.h"
27#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "recovery.h"
29#include "rgrp.h"
30#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031#include "sys.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050032#include "util.h"
Steven Whitehousebb3b0e32007-08-16 16:03:57 +010033#include "log.h"
Steven Whitehouse9ac1b4d2008-11-19 10:08:22 +000034#include "quota.h"
Steven Whitehouseb5289682008-11-26 12:49:26 +000035#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
37#define DO 0
38#define UNDO 1
39
Steven Whitehouse9b8df982008-08-08 13:45:13 +010040static const u32 gfs2_old_fs_formats[] = {
41 0
42};
43
44static const u32 gfs2_old_multihost_formats[] = {
45 0
46};
47
48/**
49 * gfs2_tune_init - Fill a gfs2_tune structure with default values
50 * @gt: tune
51 *
52 */
53
54static void gfs2_tune_init(struct gfs2_tune *gt)
55{
56 spin_lock_init(&gt->gt_spin);
57
Steven Whitehouse9b8df982008-08-08 13:45:13 +010058 gt->gt_incore_log_blocks = 1024;
59 gt->gt_log_flush_secs = 60;
60 gt->gt_recoverd_secs = 60;
61 gt->gt_logd_secs = 1;
Steven Whitehouse9b8df982008-08-08 13:45:13 +010062 gt->gt_quota_simul_sync = 64;
63 gt->gt_quota_warn_period = 10;
64 gt->gt_quota_scale_num = 1;
65 gt->gt_quota_scale_den = 1;
66 gt->gt_quota_cache_secs = 300;
67 gt->gt_quota_quantum = 60;
Steven Whitehouse9b8df982008-08-08 13:45:13 +010068 gt->gt_new_files_jdata = 0;
69 gt->gt_max_readahead = 1 << 18;
70 gt->gt_stall_secs = 600;
71 gt->gt_complain_secs = 10;
72 gt->gt_statfs_quantum = 30;
73 gt->gt_statfs_slow = 0;
74}
75
David Teiglandb3b94fa2006-01-16 16:50:04 +000076static struct gfs2_sbd *init_sbd(struct super_block *sb)
77{
78 struct gfs2_sbd *sdp;
David Teiglandb3b94fa2006-01-16 16:50:04 +000079
Steven Whitehouse85d1da62006-09-07 14:40:21 -040080 sdp = kzalloc(sizeof(struct gfs2_sbd), GFP_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 if (!sdp)
82 return NULL;
83
Steven Whitehouse5c676f62006-02-27 17:23:27 -050084 sb->s_fs_info = sdp;
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 sdp->sd_vfs = sb;
86
87 gfs2_tune_init(&sdp->sd_tune);
88
Steven Whitehousef55ab262006-02-21 12:51:39 +000089 mutex_init(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 spin_lock_init(&sdp->sd_statfs_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091
92 spin_lock_init(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +000093 mutex_init(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +000094 INIT_LIST_HEAD(&sdp->sd_rindex_list);
95 INIT_LIST_HEAD(&sdp->sd_rindex_mru_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +000096
97 INIT_LIST_HEAD(&sdp->sd_jindex_list);
98 spin_lock_init(&sdp->sd_jindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +000099 mutex_init(&sdp->sd_jindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101 INIT_LIST_HEAD(&sdp->sd_quota_list);
102 spin_lock_init(&sdp->sd_quota_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000103 mutex_init(&sdp->sd_quota_mutex);
Steven Whitehouse37b2c832008-11-17 14:25:37 +0000104 init_waitqueue_head(&sdp->sd_quota_wait);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000105 INIT_LIST_HEAD(&sdp->sd_trunc_list);
106 spin_lock_init(&sdp->sd_trunc_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107
108 spin_lock_init(&sdp->sd_log_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 INIT_LIST_HEAD(&sdp->sd_log_le_buf);
111 INIT_LIST_HEAD(&sdp->sd_log_le_revoke);
112 INIT_LIST_HEAD(&sdp->sd_log_le_rg);
113 INIT_LIST_HEAD(&sdp->sd_log_le_databuf);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100114 INIT_LIST_HEAD(&sdp->sd_log_le_ordered);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500116 mutex_init(&sdp->sd_log_reserve_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 INIT_LIST_HEAD(&sdp->sd_ail1_list);
118 INIT_LIST_HEAD(&sdp->sd_ail2_list);
119
Steven Whitehouse484adff2006-03-29 09:12:12 -0500120 init_rwsem(&sdp->sd_log_flush_lock);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100121 atomic_set(&sdp->sd_log_in_flight, 0);
122 init_waitqueue_head(&sdp->sd_log_flush_wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123
124 INIT_LIST_HEAD(&sdp->sd_revoke_list);
125
Steven Whitehousef55ab262006-02-21 12:51:39 +0000126 mutex_init(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127
128 return sdp;
129}
130
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100132/**
133 * gfs2_check_sb - Check superblock
134 * @sdp: the filesystem
135 * @sb: The superblock
136 * @silent: Don't print a message if the check fails
137 *
138 * Checks the version code of the FS is one that we understand how to
139 * read and that the sizes of the various on-disk structures have not
140 * changed.
141 */
142
143static int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb_host *sb, int silent)
144{
145 unsigned int x;
146
147 if (sb->sb_magic != GFS2_MAGIC ||
148 sb->sb_type != GFS2_METATYPE_SB) {
149 if (!silent)
150 printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
151 return -EINVAL;
152 }
153
154 /* If format numbers match exactly, we're done. */
155
156 if (sb->sb_fs_format == GFS2_FORMAT_FS &&
157 sb->sb_multihost_format == GFS2_FORMAT_MULTI)
158 return 0;
159
160 if (sb->sb_fs_format != GFS2_FORMAT_FS) {
161 for (x = 0; gfs2_old_fs_formats[x]; x++)
162 if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
163 break;
164
165 if (!gfs2_old_fs_formats[x]) {
166 printk(KERN_WARNING
167 "GFS2: code version (%u, %u) is incompatible "
168 "with ondisk format (%u, %u)\n",
169 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
170 sb->sb_fs_format, sb->sb_multihost_format);
171 printk(KERN_WARNING
172 "GFS2: I don't know how to upgrade this FS\n");
173 return -EINVAL;
174 }
175 }
176
177 if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
178 for (x = 0; gfs2_old_multihost_formats[x]; x++)
179 if (gfs2_old_multihost_formats[x] ==
180 sb->sb_multihost_format)
181 break;
182
183 if (!gfs2_old_multihost_formats[x]) {
184 printk(KERN_WARNING
185 "GFS2: code version (%u, %u) is incompatible "
186 "with ondisk format (%u, %u)\n",
187 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
188 sb->sb_fs_format, sb->sb_multihost_format);
189 printk(KERN_WARNING
190 "GFS2: I don't know how to upgrade this FS\n");
191 return -EINVAL;
192 }
193 }
194
195 if (!sdp->sd_args.ar_upgrade) {
196 printk(KERN_WARNING
197 "GFS2: code version (%u, %u) is incompatible "
198 "with ondisk format (%u, %u)\n",
199 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
200 sb->sb_fs_format, sb->sb_multihost_format);
201 printk(KERN_INFO
202 "GFS2: Use the \"upgrade\" mount option to upgrade "
203 "the FS\n");
204 printk(KERN_INFO "GFS2: See the manual for more details\n");
205 return -EINVAL;
206 }
207
208 return 0;
209}
210
211static void end_bio_io_page(struct bio *bio, int error)
212{
213 struct page *page = bio->bi_private;
214
215 if (!error)
216 SetPageUptodate(page);
217 else
218 printk(KERN_WARNING "gfs2: error %d reading superblock\n", error);
219 unlock_page(page);
220}
221
222static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf)
223{
224 const struct gfs2_sb *str = buf;
225
226 sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
227 sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
228 sb->sb_format = be32_to_cpu(str->sb_header.mh_format);
229 sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
230 sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
231 sb->sb_bsize = be32_to_cpu(str->sb_bsize);
232 sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
233 sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
234 sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
235 sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
236 sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
237
238 memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
239 memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
240}
241
242/**
243 * gfs2_read_super - Read the gfs2 super block from disk
244 * @sdp: The GFS2 super block
245 * @sector: The location of the super block
246 * @error: The error code to return
247 *
248 * This uses the bio functions to read the super block from disk
249 * because we want to be 100% sure that we never read cached data.
250 * A super block is read twice only during each GFS2 mount and is
251 * never written to by the filesystem. The first time its read no
252 * locks are held, and the only details which are looked at are those
253 * relating to the locking protocol. Once locking is up and working,
254 * the sb is read again under the lock to establish the location of
255 * the master directory (contains pointers to journals etc) and the
256 * root directory.
257 *
258 * Returns: 0 on success or error
259 */
260
261static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector)
262{
263 struct super_block *sb = sdp->sd_vfs;
264 struct gfs2_sb *p;
265 struct page *page;
266 struct bio *bio;
267
268 page = alloc_page(GFP_NOFS);
269 if (unlikely(!page))
270 return -ENOBUFS;
271
272 ClearPageUptodate(page);
273 ClearPageDirty(page);
274 lock_page(page);
275
276 bio = bio_alloc(GFP_NOFS, 1);
277 if (unlikely(!bio)) {
278 __free_page(page);
279 return -ENOBUFS;
280 }
281
282 bio->bi_sector = sector * (sb->s_blocksize >> 9);
283 bio->bi_bdev = sb->s_bdev;
284 bio_add_page(bio, page, PAGE_SIZE, 0);
285
286 bio->bi_end_io = end_bio_io_page;
287 bio->bi_private = page;
288 submit_bio(READ_SYNC | (1 << BIO_RW_META), bio);
289 wait_on_page_locked(page);
290 bio_put(bio);
291 if (!PageUptodate(page)) {
292 __free_page(page);
293 return -EIO;
294 }
295 p = kmap(page);
296 gfs2_sb_in(&sdp->sd_sb, p);
297 kunmap(page);
298 __free_page(page);
299 return 0;
300}
301/**
302 * gfs2_read_sb - Read super block
303 * @sdp: The GFS2 superblock
304 * @gl: the glock for the superblock (assumed to be held)
305 * @silent: Don't print message if mount fails
306 *
307 */
308
309static int gfs2_read_sb(struct gfs2_sbd *sdp, struct gfs2_glock *gl, int silent)
310{
311 u32 hash_blocks, ind_blocks, leaf_blocks;
312 u32 tmp_blocks;
313 unsigned int x;
314 int error;
315
316 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
317 if (error) {
318 if (!silent)
319 fs_err(sdp, "can't read superblock\n");
320 return error;
321 }
322
323 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
324 if (error)
325 return error;
326
327 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
328 GFS2_BASIC_BLOCK_SHIFT;
329 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
330 sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
331 sizeof(struct gfs2_dinode)) / sizeof(u64);
332 sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
333 sizeof(struct gfs2_meta_header)) / sizeof(u64);
334 sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
335 sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
336 sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
337 sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
338 sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
339 sizeof(struct gfs2_meta_header)) /
340 sizeof(struct gfs2_quota_change);
341
342 /* Compute maximum reservation required to add a entry to a directory */
343
344 hash_blocks = DIV_ROUND_UP(sizeof(u64) * (1 << GFS2_DIR_MAX_DEPTH),
345 sdp->sd_jbsize);
346
347 ind_blocks = 0;
348 for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
349 tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
350 ind_blocks += tmp_blocks;
351 }
352
353 leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
354
355 sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
356
357 sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
358 sizeof(struct gfs2_dinode);
359 sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
360 for (x = 2;; x++) {
361 u64 space, d;
362 u32 m;
363
364 space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
365 d = space;
366 m = do_div(d, sdp->sd_inptrs);
367
368 if (d != sdp->sd_heightsize[x - 1] || m)
369 break;
370 sdp->sd_heightsize[x] = space;
371 }
372 sdp->sd_max_height = x;
373 sdp->sd_heightsize[x] = ~0;
374 gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
375
376 sdp->sd_jheightsize[0] = sdp->sd_sb.sb_bsize -
377 sizeof(struct gfs2_dinode);
378 sdp->sd_jheightsize[1] = sdp->sd_jbsize * sdp->sd_diptrs;
379 for (x = 2;; x++) {
380 u64 space, d;
381 u32 m;
382
383 space = sdp->sd_jheightsize[x - 1] * sdp->sd_inptrs;
384 d = space;
385 m = do_div(d, sdp->sd_inptrs);
386
387 if (d != sdp->sd_jheightsize[x - 1] || m)
388 break;
389 sdp->sd_jheightsize[x] = space;
390 }
391 sdp->sd_max_jheight = x;
392 sdp->sd_jheightsize[x] = ~0;
393 gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
394
395 return 0;
396}
397
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398static int init_names(struct gfs2_sbd *sdp, int silent)
399{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 char *proto, *table;
401 int error = 0;
402
403 proto = sdp->sd_args.ar_lockproto;
404 table = sdp->sd_args.ar_locktable;
405
406 /* Try to autodetect */
407
408 if (!proto[0] || !table[0]) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100409 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
410 if (error)
411 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
Steven Whitehouse3cf1e7b2006-10-02 11:49:41 -0400413 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 if (error)
415 goto out;
416
417 if (!proto[0])
Steven Whitehouse3cf1e7b2006-10-02 11:49:41 -0400418 proto = sdp->sd_sb.sb_lockproto;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419 if (!table[0])
Steven Whitehouse3cf1e7b2006-10-02 11:49:41 -0400420 table = sdp->sd_sb.sb_locktable;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 }
422
423 if (!table[0])
424 table = sdp->sd_vfs->s_id;
425
Jean Delvare00377d82008-05-09 17:59:51 +0200426 strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
427 strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428
Denis Cheng5d35e312007-08-13 11:01:58 +0800429 table = sdp->sd_table_name;
430 while ((table = strchr(table, '/')))
Robert Petersonb35997d2007-06-07 09:10:01 -0500431 *table = '_';
432
Steven Whitehousea91ea692006-09-04 12:04:26 -0400433out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000434 return error;
435}
436
437static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
438 int undo)
439{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 int error = 0;
441
442 if (undo)
443 goto fail_trans;
444
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 error = gfs2_glock_nq_num(sdp,
446 GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
447 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
448 mount_gh);
449 if (error) {
450 fs_err(sdp, "can't acquire mount glock: %d\n", error);
451 goto fail;
452 }
453
454 error = gfs2_glock_nq_num(sdp,
455 GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
456 LM_ST_SHARED,
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400457 LM_FLAG_NOEXP | GL_EXACT,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 &sdp->sd_live_gh);
459 if (error) {
460 fs_err(sdp, "can't acquire live glock: %d\n", error);
461 goto fail_mount;
462 }
463
464 error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
465 CREATE, &sdp->sd_rename_gl);
466 if (error) {
467 fs_err(sdp, "can't create rename glock: %d\n", error);
468 goto fail_live;
469 }
470
471 error = gfs2_glock_get(sdp, GFS2_TRANS_LOCK, &gfs2_trans_glops,
472 CREATE, &sdp->sd_trans_gl);
473 if (error) {
474 fs_err(sdp, "can't create transaction glock: %d\n", error);
475 goto fail_rename;
476 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477
478 return 0;
479
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400480fail_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 gfs2_glock_put(sdp->sd_trans_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400482fail_rename:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 gfs2_glock_put(sdp->sd_rename_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400484fail_live:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400486fail_mount:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487 gfs2_glock_dq_uninit(mount_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400488fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 return error;
490}
491
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100492static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
493 u64 no_addr, const char *name)
Steven Whitehousef42faf42006-01-30 18:34:10 +0000494{
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100495 struct gfs2_sbd *sdp = sb->s_fs_info;
496 struct dentry *dentry;
497 struct inode *inode;
498
499 inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0, 0);
500 if (IS_ERR(inode)) {
501 fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode));
502 return PTR_ERR(inode);
503 }
504 dentry = d_alloc_root(inode);
505 if (!dentry) {
506 fs_err(sdp, "can't alloc %s dentry\n", name);
507 iput(inode);
508 return -ENOMEM;
509 }
510 dentry->d_op = &gfs2_dops;
511 *dptr = dentry;
512 return 0;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000513}
514
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100515static int init_sb(struct gfs2_sbd *sdp, int silent)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516{
517 struct super_block *sb = sdp->sd_vfs;
518 struct gfs2_holder sb_gh;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100519 u64 no_addr;
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100520 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100522 ret = gfs2_glock_nq_num(sdp, GFS2_SB_LOCK, &gfs2_meta_glops,
523 LM_ST_SHARED, 0, &sb_gh);
524 if (ret) {
525 fs_err(sdp, "can't acquire superblock glock: %d\n", ret);
526 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400528
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100529 ret = gfs2_read_sb(sdp, sb_gh.gh_gl, silent);
530 if (ret) {
531 fs_err(sdp, "can't read superblock: %d\n", ret);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 goto out;
533 }
534
535 /* Set up the buffer cache and SB for real */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 if (sdp->sd_sb.sb_bsize < bdev_hardsect_size(sb->s_bdev)) {
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100537 ret = -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 fs_err(sdp, "FS block size (%u) is too small for device "
539 "block size (%u)\n",
540 sdp->sd_sb.sb_bsize, bdev_hardsect_size(sb->s_bdev));
541 goto out;
542 }
543 if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100544 ret = -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 fs_err(sdp, "FS block size (%u) is too big for machine "
546 "page size (%u)\n",
547 sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
548 goto out;
549 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
551
Steven Whitehousef42faf42006-01-30 18:34:10 +0000552 /* Get the root inode */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100553 no_addr = sdp->sd_sb.sb_root_dir.no_addr;
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100554 ret = gfs2_lookup_root(sb, &sdp->sd_root_dir, no_addr, "root");
555 if (ret)
556 goto out;
557
558 /* Get the master inode */
559 no_addr = sdp->sd_sb.sb_master_dir.no_addr;
560 ret = gfs2_lookup_root(sb, &sdp->sd_master_dir, no_addr, "master");
561 if (ret) {
562 dput(sdp->sd_root_dir);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000563 goto out;
564 }
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100565 sb->s_root = dget(sdp->sd_args.ar_meta ? sdp->sd_master_dir : sdp->sd_root_dir);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000566out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567 gfs2_glock_dq_uninit(&sb_gh);
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100568 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569}
570
Bob Petersonda6dd402007-12-11 18:49:21 -0600571/**
572 * map_journal_extents - create a reusable "extent" mapping from all logical
573 * blocks to all physical blocks for the given journal. This will save
574 * us time when writing journal blocks. Most journals will have only one
575 * extent that maps all their logical blocks. That's because gfs2.mkfs
576 * arranges the journal blocks sequentially to maximize performance.
577 * So the extent would map the first block for the entire file length.
578 * However, gfs2_jadd can happen while file activity is happening, so
579 * those journals may not be sequential. Less likely is the case where
580 * the users created their own journals by mounting the metafs and
581 * laying it out. But it's still possible. These journals might have
582 * several extents.
583 *
584 * TODO: This should be done in bigger chunks rather than one block at a time,
585 * but since it's only done at mount time, I'm not worried about the
586 * time it takes.
587 */
588static int map_journal_extents(struct gfs2_sbd *sdp)
589{
590 struct gfs2_jdesc *jd = sdp->sd_jdesc;
591 unsigned int lb;
592 u64 db, prev_db; /* logical block, disk block, prev disk block */
593 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
594 struct gfs2_journal_extent *jext = NULL;
595 struct buffer_head bh;
596 int rc = 0;
597
Bob Petersonda6dd402007-12-11 18:49:21 -0600598 prev_db = 0;
599
Steven Whitehousec9e98882008-11-04 09:47:33 +0000600 for (lb = 0; lb < ip->i_disksize >> sdp->sd_sb.sb_bsize_shift; lb++) {
Bob Petersonda6dd402007-12-11 18:49:21 -0600601 bh.b_state = 0;
602 bh.b_blocknr = 0;
603 bh.b_size = 1 << ip->i_inode.i_blkbits;
604 rc = gfs2_block_map(jd->jd_inode, lb, &bh, 0);
605 db = bh.b_blocknr;
606 if (rc || !db) {
607 printk(KERN_INFO "GFS2 journal mapping error %d: lb="
608 "%u db=%llu\n", rc, lb, (unsigned long long)db);
609 break;
610 }
611 if (!prev_db || db != prev_db + 1) {
612 jext = kzalloc(sizeof(struct gfs2_journal_extent),
613 GFP_KERNEL);
614 if (!jext) {
615 printk(KERN_INFO "GFS2 error: out of memory "
616 "mapping journal extents.\n");
617 rc = -ENOMEM;
618 break;
619 }
620 jext->dblock = db;
621 jext->lblock = lb;
622 jext->blocks = 1;
623 list_add_tail(&jext->extent_list, &jd->extent_list);
624 } else {
625 jext->blocks++;
626 }
627 prev_db = db;
628 }
629 return rc;
630}
631
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000632static void gfs2_lm_others_may_mount(struct gfs2_sbd *sdp)
633{
Steven Whitehouse048bca22008-05-23 14:46:04 +0100634 if (!sdp->sd_lockstruct.ls_ops->lm_others_may_mount)
635 return;
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000636 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
637 sdp->sd_lockstruct.ls_ops->lm_others_may_mount(
638 sdp->sd_lockstruct.ls_lockspace);
639}
640
Steven Whitehouseb5289682008-11-26 12:49:26 +0000641/**
642 * gfs2_jindex_hold - Grab a lock on the jindex
643 * @sdp: The GFS2 superblock
644 * @ji_gh: the holder for the jindex glock
645 *
646 * Returns: errno
647 */
648
649static int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
650{
651 struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
652 struct qstr name;
653 char buf[20];
654 struct gfs2_jdesc *jd;
655 int error;
656
657 name.name = buf;
658
659 mutex_lock(&sdp->sd_jindex_mutex);
660
661 for (;;) {
662 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
663 if (error)
664 break;
665
666 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
667 name.hash = gfs2_disk_hash(name.name, name.len);
668
669 error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
670 if (error == -ENOENT) {
671 error = 0;
672 break;
673 }
674
675 gfs2_glock_dq_uninit(ji_gh);
676
677 if (error)
678 break;
679
680 error = -ENOMEM;
681 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
682 if (!jd)
683 break;
684
685 INIT_LIST_HEAD(&jd->extent_list);
686 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1);
687 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
688 if (!jd->jd_inode)
689 error = -ENOENT;
690 else
691 error = PTR_ERR(jd->jd_inode);
692 kfree(jd);
693 break;
694 }
695
696 spin_lock(&sdp->sd_jindex_spin);
697 jd->jd_jid = sdp->sd_journals++;
698 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
699 spin_unlock(&sdp->sd_jindex_spin);
700 }
701
702 mutex_unlock(&sdp->sd_jindex_mutex);
703
704 return error;
705}
706
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707static int init_journal(struct gfs2_sbd *sdp, int undo)
708{
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100709 struct inode *master = sdp->sd_master_dir->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 struct gfs2_holder ji_gh;
711 struct task_struct *p;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500712 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 int jindex = 1;
714 int error = 0;
715
716 if (undo) {
717 jindex = 0;
718 goto fail_recoverd;
719 }
720
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100721 sdp->sd_jindex = gfs2_lookup_simple(master, "jindex");
Steven Whitehousec7526662006-03-20 12:30:04 -0500722 if (IS_ERR(sdp->sd_jindex)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 fs_err(sdp, "can't lookup journal index: %d\n", error);
Steven Whitehousec7526662006-03-20 12:30:04 -0500724 return PTR_ERR(sdp->sd_jindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400726 ip = GFS2_I(sdp->sd_jindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727
728 /* Load in the journal index special file */
729
730 error = gfs2_jindex_hold(sdp, &ji_gh);
731 if (error) {
732 fs_err(sdp, "can't read journal index: %d\n", error);
733 goto fail;
734 }
735
736 error = -EINVAL;
737 if (!gfs2_jindex_size(sdp)) {
738 fs_err(sdp, "no journals!\n");
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400739 goto fail_jindex;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 }
741
742 if (sdp->sd_args.ar_spectator) {
743 sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
Steven Whitehousefd041f02007-11-08 14:55:03 +0000744 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 } else {
746 if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
747 fs_err(sdp, "can't mount journal #%u\n",
748 sdp->sd_lockstruct.ls_jid);
749 fs_err(sdp, "there are only %u journals (0 - %u)\n",
750 gfs2_jindex_size(sdp),
751 gfs2_jindex_size(sdp) - 1);
752 goto fail_jindex;
753 }
754 sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
755
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400756 error = gfs2_glock_nq_num(sdp, sdp->sd_lockstruct.ls_jid,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 &gfs2_journal_glops,
758 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP,
759 &sdp->sd_journal_gh);
760 if (error) {
761 fs_err(sdp, "can't acquire journal glock: %d\n", error);
762 goto fail_jindex;
763 }
764
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400765 ip = GFS2_I(sdp->sd_jdesc->jd_inode);
766 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
Bob Peterson75be73a2007-08-08 17:08:14 -0500767 LM_FLAG_NOEXP | GL_EXACT | GL_NOCACHE,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 &sdp->sd_jinode_gh);
769 if (error) {
770 fs_err(sdp, "can't acquire journal inode glock: %d\n",
771 error);
772 goto fail_journal_gh;
773 }
774
775 error = gfs2_jdesc_check(sdp->sd_jdesc);
776 if (error) {
777 fs_err(sdp, "my journal (%u) is bad: %d\n",
778 sdp->sd_jdesc->jd_jid, error);
779 goto fail_jinode_gh;
780 }
Steven Whitehousefd041f02007-11-08 14:55:03 +0000781 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
Bob Petersonda6dd402007-12-11 18:49:21 -0600782
783 /* Map the extents for this journal's blocks */
784 map_journal_extents(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 }
786
787 if (sdp->sd_lockstruct.ls_first) {
788 unsigned int x;
789 for (x = 0; x < sdp->sd_journals; x++) {
David Teiglandc63e31c2006-04-20 17:03:48 -0400790 error = gfs2_recover_journal(gfs2_jdesc_find(sdp, x));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 if (error) {
792 fs_err(sdp, "error recovering journal %u: %d\n",
793 x, error);
794 goto fail_jinode_gh;
795 }
796 }
797
798 gfs2_lm_others_may_mount(sdp);
799 } else if (!sdp->sd_args.ar_spectator) {
David Teiglandc63e31c2006-04-20 17:03:48 -0400800 error = gfs2_recover_journal(sdp->sd_jdesc);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801 if (error) {
802 fs_err(sdp, "error recovering my journal: %d\n", error);
803 goto fail_jinode_gh;
804 }
805 }
806
807 set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
808 gfs2_glock_dq_uninit(&ji_gh);
809 jindex = 0;
810
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 p = kthread_run(gfs2_recoverd, sdp, "gfs2_recoverd");
812 error = IS_ERR(p);
813 if (error) {
814 fs_err(sdp, "can't start recoverd thread: %d\n", error);
815 goto fail_jinode_gh;
816 }
817 sdp->sd_recoverd_process = p;
818
819 return 0;
820
Steven Whitehousea91ea692006-09-04 12:04:26 -0400821fail_recoverd:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 kthread_stop(sdp->sd_recoverd_process);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400823fail_jinode_gh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 if (!sdp->sd_args.ar_spectator)
825 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400826fail_journal_gh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 if (!sdp->sd_args.ar_spectator)
828 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400829fail_jindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 gfs2_jindex_free(sdp);
831 if (jindex)
832 gfs2_glock_dq_uninit(&ji_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400833fail:
Steven Whitehousef42faf42006-01-30 18:34:10 +0000834 iput(sdp->sd_jindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835 return error;
836}
837
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838
839static int init_inodes(struct gfs2_sbd *sdp, int undo)
840{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 int error = 0;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500842 struct gfs2_inode *ip;
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100843 struct inode *master = sdp->sd_master_dir->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844
845 if (undo)
Steven Whitehousef42faf42006-01-30 18:34:10 +0000846 goto fail_qinode;
847
Steven Whitehousef42faf42006-01-30 18:34:10 +0000848 error = init_journal(sdp, undo);
849 if (error)
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100850 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851
852 /* Read in the master inode number inode */
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100853 sdp->sd_inum_inode = gfs2_lookup_simple(master, "inum");
Steven Whitehousec7526662006-03-20 12:30:04 -0500854 if (IS_ERR(sdp->sd_inum_inode)) {
855 error = PTR_ERR(sdp->sd_inum_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 fs_err(sdp, "can't read in inum inode: %d\n", error);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000857 goto fail_journal;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 }
859
Steven Whitehousef42faf42006-01-30 18:34:10 +0000860
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 /* Read in the master statfs inode */
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100862 sdp->sd_statfs_inode = gfs2_lookup_simple(master, "statfs");
Steven Whitehousec7526662006-03-20 12:30:04 -0500863 if (IS_ERR(sdp->sd_statfs_inode)) {
864 error = PTR_ERR(sdp->sd_statfs_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865 fs_err(sdp, "can't read in statfs inode: %d\n", error);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000866 goto fail_inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 }
868
869 /* Read in the resource index inode */
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100870 sdp->sd_rindex = gfs2_lookup_simple(master, "rindex");
Steven Whitehousec7526662006-03-20 12:30:04 -0500871 if (IS_ERR(sdp->sd_rindex)) {
872 error = PTR_ERR(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 fs_err(sdp, "can't get resource index inode: %d\n", error);
874 goto fail_statfs;
875 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400876 ip = GFS2_I(sdp->sd_rindex);
Bob Petersoncf45b752008-01-31 10:31:39 -0600877 sdp->sd_rindex_uptodate = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878
879 /* Read in the quota inode */
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100880 sdp->sd_quota_inode = gfs2_lookup_simple(master, "quota");
Steven Whitehousec7526662006-03-20 12:30:04 -0500881 if (IS_ERR(sdp->sd_quota_inode)) {
882 error = PTR_ERR(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 fs_err(sdp, "can't get quota file inode: %d\n", error);
884 goto fail_rindex;
885 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 return 0;
887
Steven Whitehousef42faf42006-01-30 18:34:10 +0000888fail_qinode:
889 iput(sdp->sd_quota_inode);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000890fail_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 gfs2_clear_rgrpd(sdp);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000892 iput(sdp->sd_rindex);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000893fail_statfs:
894 iput(sdp->sd_statfs_inode);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000895fail_inum:
896 iput(sdp->sd_inum_inode);
897fail_journal:
898 init_journal(sdp, UNDO);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000899fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 return error;
901}
902
903static int init_per_node(struct gfs2_sbd *sdp, int undo)
904{
Steven Whitehousef42faf42006-01-30 18:34:10 +0000905 struct inode *pn = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 char buf[30];
907 int error = 0;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500908 struct gfs2_inode *ip;
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100909 struct inode *master = sdp->sd_master_dir->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910
911 if (sdp->sd_args.ar_spectator)
912 return 0;
913
914 if (undo)
915 goto fail_qc_gh;
916
Steven Whitehouse9b8df982008-08-08 13:45:13 +0100917 pn = gfs2_lookup_simple(master, "per_node");
Steven Whitehousec7526662006-03-20 12:30:04 -0500918 if (IS_ERR(pn)) {
919 error = PTR_ERR(pn);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 fs_err(sdp, "can't find per_node directory: %d\n", error);
921 return error;
922 }
923
924 sprintf(buf, "inum_range%u", sdp->sd_jdesc->jd_jid);
Steven Whitehousec7526662006-03-20 12:30:04 -0500925 sdp->sd_ir_inode = gfs2_lookup_simple(pn, buf);
926 if (IS_ERR(sdp->sd_ir_inode)) {
927 error = PTR_ERR(sdp->sd_ir_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 fs_err(sdp, "can't find local \"ir\" file: %d\n", error);
929 goto fail;
930 }
931
932 sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
Steven Whitehousec7526662006-03-20 12:30:04 -0500933 sdp->sd_sc_inode = gfs2_lookup_simple(pn, buf);
934 if (IS_ERR(sdp->sd_sc_inode)) {
935 error = PTR_ERR(sdp->sd_sc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
937 goto fail_ir_i;
938 }
939
David Teiglandb3b94fa2006-01-16 16:50:04 +0000940 sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
Steven Whitehousec7526662006-03-20 12:30:04 -0500941 sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
942 if (IS_ERR(sdp->sd_qc_inode)) {
943 error = PTR_ERR(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
945 goto fail_ut_i;
946 }
947
Steven Whitehousef42faf42006-01-30 18:34:10 +0000948 iput(pn);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 pn = NULL;
950
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400951 ip = GFS2_I(sdp->sd_ir_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500952 error = gfs2_glock_nq_init(ip->i_gl,
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400953 LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 &sdp->sd_ir_gh);
955 if (error) {
956 fs_err(sdp, "can't lock local \"ir\" file: %d\n", error);
957 goto fail_qc_i;
958 }
959
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400960 ip = GFS2_I(sdp->sd_sc_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500961 error = gfs2_glock_nq_init(ip->i_gl,
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400962 LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 &sdp->sd_sc_gh);
964 if (error) {
965 fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
966 goto fail_ir_gh;
967 }
968
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400969 ip = GFS2_I(sdp->sd_qc_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500970 error = gfs2_glock_nq_init(ip->i_gl,
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400971 LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 &sdp->sd_qc_gh);
973 if (error) {
974 fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
975 goto fail_ut_gh;
976 }
977
978 return 0;
979
Steven Whitehousea91ea692006-09-04 12:04:26 -0400980fail_qc_gh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400982fail_ut_gh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400984fail_ir_gh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400986fail_qc_i:
Steven Whitehousef42faf42006-01-30 18:34:10 +0000987 iput(sdp->sd_qc_inode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400988fail_ut_i:
Steven Whitehousef42faf42006-01-30 18:34:10 +0000989 iput(sdp->sd_sc_inode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400990fail_ir_i:
Steven Whitehousef42faf42006-01-30 18:34:10 +0000991 iput(sdp->sd_ir_inode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400992fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 if (pn)
Steven Whitehousef42faf42006-01-30 18:34:10 +0000994 iput(pn);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 return error;
996}
997
998static int init_threads(struct gfs2_sbd *sdp, int undo)
999{
1000 struct task_struct *p;
1001 int error = 0;
1002
1003 if (undo)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001004 goto fail_quotad;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005
1006 sdp->sd_log_flush_time = jiffies;
1007 sdp->sd_jindex_refresh_time = jiffies;
1008
1009 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
1010 error = IS_ERR(p);
1011 if (error) {
1012 fs_err(sdp, "can't start logd thread: %d\n", error);
1013 return error;
1014 }
1015 sdp->sd_logd_process = p;
1016
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
1018 error = IS_ERR(p);
1019 if (error) {
1020 fs_err(sdp, "can't start quotad thread: %d\n", error);
1021 goto fail;
1022 }
1023 sdp->sd_quotad_process = p;
1024
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 return 0;
1026
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001028fail_quotad:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 kthread_stop(sdp->sd_quotad_process);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001030fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 kthread_stop(sdp->sd_logd_process);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 return error;
1033}
1034
1035/**
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001036 * gfs2_lm_mount - mount a locking protocol
1037 * @sdp: the filesystem
1038 * @args: mount arguements
1039 * @silent: if 1, don't complain if the FS isn't a GFS2 fs
1040 *
1041 * Returns: errno
1042 */
1043
1044static int gfs2_lm_mount(struct gfs2_sbd *sdp, int silent)
1045{
1046 char *proto = sdp->sd_proto_name;
1047 char *table = sdp->sd_table_name;
Benjamin Marzinski58e9fee2008-03-14 13:52:52 -05001048 int flags = LM_MFLAG_CONV_NODROP;
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001049 int error;
1050
1051 if (sdp->sd_args.ar_spectator)
1052 flags |= LM_MFLAG_SPECTATOR;
1053
1054 fs_info(sdp, "Trying to join cluster \"%s\", \"%s\"\n", proto, table);
1055
1056 error = gfs2_mount_lockproto(proto, table, sdp->sd_args.ar_hostdata,
1057 gfs2_glock_cb, sdp,
1058 GFS2_MIN_LVB_SIZE, flags,
1059 &sdp->sd_lockstruct, &sdp->sd_kobj);
1060 if (error) {
1061 fs_info(sdp, "can't mount proto=%s, table=%s, hostdata=%s\n",
1062 proto, table, sdp->sd_args.ar_hostdata);
1063 goto out;
1064 }
1065
Steven Whitehouse048bca22008-05-23 14:46:04 +01001066 if (gfs2_assert_warn(sdp, sdp->sd_lockstruct.ls_ops) ||
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001067 gfs2_assert_warn(sdp, sdp->sd_lockstruct.ls_lvb_size >=
1068 GFS2_MIN_LVB_SIZE)) {
1069 gfs2_unmount_lockproto(&sdp->sd_lockstruct);
1070 goto out;
1071 }
1072
1073 if (sdp->sd_args.ar_spectator)
1074 snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s.s", table);
1075 else
1076 snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s.%u", table,
1077 sdp->sd_lockstruct.ls_jid);
1078
1079 fs_info(sdp, "Joined cluster. Now mounting FS...\n");
1080
1081 if ((sdp->sd_lockstruct.ls_flags & LM_LSFLAG_LOCAL) &&
1082 !sdp->sd_args.ar_ignore_local_fs) {
1083 sdp->sd_args.ar_localflocks = 1;
1084 sdp->sd_args.ar_localcaching = 1;
1085 }
1086
1087out:
1088 return error;
1089}
1090
1091void gfs2_lm_unmount(struct gfs2_sbd *sdp)
1092{
1093 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
1094 gfs2_unmount_lockproto(&sdp->sd_lockstruct);
1095}
1096
1097/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 * fill_super - Read in superblock
1099 * @sb: The VFS superblock
1100 * @data: Mount options
1101 * @silent: Don't complain if it's not a GFS2 filesystem
1102 *
1103 * Returns: errno
1104 */
1105
1106static int fill_super(struct super_block *sb, void *data, int silent)
1107{
1108 struct gfs2_sbd *sdp;
1109 struct gfs2_holder mount_gh;
1110 int error;
1111
1112 sdp = init_sbd(sb);
1113 if (!sdp) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001114 printk(KERN_WARNING "GFS2: can't alloc struct gfs2_sbd\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 return -ENOMEM;
1116 }
1117
Steven Whitehouse6f04c1c2009-01-06 11:52:25 +00001118 sdp->sd_args.ar_quota = GFS2_QUOTA_DEFAULT;
1119 sdp->sd_args.ar_data = GFS2_DATA_DEFAULT;
1120
1121 error = gfs2_mount_args(sdp, &sdp->sd_args, data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 if (error) {
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001123 printk(KERN_WARNING "GFS2: can't parse mount arguments\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001124 goto fail;
1125 }
1126
Steven Whitehouse6f04c1c2009-01-06 11:52:25 +00001127 if (sdp->sd_args.ar_spectator)
1128 sb->s_flags |= MS_RDONLY;
1129 if (sdp->sd_args.ar_posix_acl)
1130 sb->s_flags |= MS_POSIXACL;
1131
Steven Whitehouse719ee342008-09-18 13:53:59 +01001132 sb->s_magic = GFS2_MAGIC;
1133 sb->s_op = &gfs2_super_ops;
1134 sb->s_export_op = &gfs2_export_ops;
1135 sb->s_time_gran = 1;
1136 sb->s_maxbytes = MAX_LFS_FILESIZE;
Steven Whitehouse419c93e2006-03-02 16:33:41 -05001137
1138 /* Set up the buffer cache and fill in some fake block size values
1139 to allow us to read-in the on-disk superblock. */
1140 sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
1141 sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
1142 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
1143 GFS2_BASIC_BLOCK_SHIFT;
1144 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145
1146 error = init_names(sdp, silent);
1147 if (error)
1148 goto fail;
1149
Robert Peterson7c52b162007-03-16 10:26:37 +00001150 gfs2_create_debugfs_file(sdp);
1151
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 error = gfs2_sys_fs_add(sdp);
1153 if (error)
1154 goto fail;
1155
1156 error = gfs2_lm_mount(sdp, silent);
1157 if (error)
1158 goto fail_sys;
1159
1160 error = init_locking(sdp, &mount_gh, DO);
1161 if (error)
1162 goto fail_lm;
1163
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001164 error = init_sb(sdp, silent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 if (error)
1166 goto fail_locking;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001167
1168 error = init_inodes(sdp, DO);
1169 if (error)
Steven Whitehousef42faf42006-01-30 18:34:10 +00001170 goto fail_sb;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171
1172 error = init_per_node(sdp, DO);
1173 if (error)
1174 goto fail_inodes;
1175
1176 error = gfs2_statfs_init(sdp);
1177 if (error) {
1178 fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
1179 goto fail_per_node;
1180 }
1181
1182 error = init_threads(sdp, DO);
1183 if (error)
1184 goto fail_per_node;
1185
1186 if (!(sb->s_flags & MS_RDONLY)) {
1187 error = gfs2_make_fs_rw(sdp);
1188 if (error) {
1189 fs_err(sdp, "can't make FS RW: %d\n", error);
1190 goto fail_threads;
1191 }
1192 }
1193
1194 gfs2_glock_dq_uninit(&mount_gh);
1195
1196 return 0;
1197
Steven Whitehousea91ea692006-09-04 12:04:26 -04001198fail_threads:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 init_threads(sdp, UNDO);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001200fail_per_node:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 init_per_node(sdp, UNDO);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001202fail_inodes:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 init_inodes(sdp, UNDO);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001204fail_sb:
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001205 if (sdp->sd_root_dir)
1206 dput(sdp->sd_root_dir);
1207 if (sdp->sd_master_dir)
1208 dput(sdp->sd_master_dir);
1209 sb->s_root = NULL;
Steven Whitehousea91ea692006-09-04 12:04:26 -04001210fail_locking:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 init_locking(sdp, &mount_gh, UNDO);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001212fail_lm:
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001213 gfs2_gl_hash_clear(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214 gfs2_lm_unmount(sdp);
1215 while (invalidate_inodes(sb))
1216 yield();
Steven Whitehousea91ea692006-09-04 12:04:26 -04001217fail_sys:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001218 gfs2_sys_fs_del(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001219fail:
Robert Peterson5f882092007-04-18 11:41:11 -05001220 gfs2_delete_debugfs_file(sdp);
Steven Whitehousea2c45802006-09-08 10:13:03 -04001221 kfree(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001222 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 return error;
1224}
1225
Andrew Mortonccd6efd2006-06-30 02:16:34 -07001226static int gfs2_get_sb(struct file_system_type *fs_type, int flags,
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001227 const char *dev_name, void *data, struct vfsmount *mnt)
1228{
1229 return get_sb_bdev(fs_type, flags, dev_name, data, fill_super, mnt);
1230}
1231
1232static struct super_block *get_gfs2_sb(const char *dev_name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001233{
Abhijith Das86384602006-08-25 11:13:37 -05001234 struct super_block *sb;
Abhijith Das86384602006-08-25 11:13:37 -05001235 struct nameidata nd;
Abhijith Das86384602006-08-25 11:13:37 -05001236 int error;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001237
Abhijith Das86384602006-08-25 11:13:37 -05001238 error = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
1239 if (error) {
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001240 printk(KERN_WARNING "GFS2: path_lookup on %s returned error %d\n",
1241 dev_name, error);
1242 return NULL;
Abhijith Das86384602006-08-25 11:13:37 -05001243 }
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001244 sb = nd.path.dentry->d_inode->i_sb;
1245 if (sb && (sb->s_type == &gfs2_fs_type))
1246 atomic_inc(&sb->s_active);
1247 else
1248 sb = NULL;
Jan Blunck1d957f92008-02-14 19:34:35 -08001249 path_put(&nd.path);
Abhijith Das86384602006-08-25 11:13:37 -05001250 return sb;
1251}
1252
1253static int gfs2_get_sb_meta(struct file_system_type *fs_type, int flags,
1254 const char *dev_name, void *data, struct vfsmount *mnt)
1255{
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001256 struct super_block *sb = NULL;
Abhijith Das86384602006-08-25 11:13:37 -05001257 struct gfs2_sbd *sdp;
Abhijith Das86384602006-08-25 11:13:37 -05001258
1259 sb = get_gfs2_sb(dev_name);
1260 if (!sb) {
1261 printk(KERN_WARNING "GFS2: gfs2 mount does not exist\n");
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001262 return -ENOENT;
Abhijith Das86384602006-08-25 11:13:37 -05001263 }
Denis Cheng2d3ba1e2007-08-11 10:27:08 +08001264 sdp = sb->s_fs_info;
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001265 mnt->mnt_sb = sb;
1266 mnt->mnt_root = dget(sdp->sd_master_dir);
1267 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268}
1269
Steven Whitehouse419c93e2006-03-02 16:33:41 -05001270static void gfs2_kill_sb(struct super_block *sb)
1271{
Abhijith Das86384602006-08-25 11:13:37 -05001272 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehouse88a19ad2008-12-19 15:43:05 +00001273
1274 if (sdp == NULL) {
1275 kill_block_super(sb);
1276 return;
Abhijith Dasacd2c8a2008-09-15 08:54:06 -05001277 }
Steven Whitehouse88a19ad2008-12-19 15:43:05 +00001278
1279 gfs2_meta_syncfs(sdp);
1280 dput(sdp->sd_root_dir);
1281 dput(sdp->sd_master_dir);
1282 sdp->sd_root_dir = NULL;
1283 sdp->sd_master_dir = NULL;
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001284 shrink_dcache_sb(sb);
Steven Whitehouse9b8df982008-08-08 13:45:13 +01001285 kill_block_super(sb);
Steven Whitehouse88a19ad2008-12-19 15:43:05 +00001286 gfs2_delete_debugfs_file(sdp);
1287 kfree(sdp);
Abhijith Das86384602006-08-25 11:13:37 -05001288}
1289
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290struct file_system_type gfs2_fs_type = {
1291 .name = "gfs2",
1292 .fs_flags = FS_REQUIRES_DEV,
1293 .get_sb = gfs2_get_sb,
Steven Whitehouse419c93e2006-03-02 16:33:41 -05001294 .kill_sb = gfs2_kill_sb,
1295 .owner = THIS_MODULE,
1296};
1297
1298struct file_system_type gfs2meta_fs_type = {
1299 .name = "gfs2meta",
1300 .fs_flags = FS_REQUIRES_DEV,
Abhijith Das86384602006-08-25 11:13:37 -05001301 .get_sb = gfs2_get_sb_meta,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001302 .owner = THIS_MODULE,
1303};
1304