0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #include <linux/spinlock.h>
0010 #include <linux/completion.h>
0011 #include <linux/buffer_head.h>
0012 #include <linux/crc32.h>
0013 #include <linux/gfs2_ondisk.h>
0014 #include <linux/delay.h>
0015 #include <linux/uaccess.h>
0016
0017 #include "gfs2.h"
0018 #include "incore.h"
0019 #include "glock.h"
0020 #include "glops.h"
0021 #include "log.h"
0022 #include "lops.h"
0023 #include "recovery.h"
0024 #include "rgrp.h"
0025 #include "super.h"
0026 #include "util.h"
0027
0028 struct kmem_cache *gfs2_glock_cachep __read_mostly;
0029 struct kmem_cache *gfs2_glock_aspace_cachep __read_mostly;
0030 struct kmem_cache *gfs2_inode_cachep __read_mostly;
0031 struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
0032 struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
0033 struct kmem_cache *gfs2_quotad_cachep __read_mostly;
0034 struct kmem_cache *gfs2_qadata_cachep __read_mostly;
0035 struct kmem_cache *gfs2_trans_cachep __read_mostly;
0036 mempool_t *gfs2_page_pool __read_mostly;
0037
0038 void gfs2_assert_i(struct gfs2_sbd *sdp)
0039 {
0040 fs_emerg(sdp, "fatal assertion failed\n");
0041 }
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 int check_journal_clean(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
0052 bool verbose)
0053 {
0054 int error;
0055 struct gfs2_holder j_gh;
0056 struct gfs2_log_header_host head;
0057 struct gfs2_inode *ip;
0058
0059 ip = GFS2_I(jd->jd_inode);
0060 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_NOEXP |
0061 GL_EXACT | GL_NOCACHE, &j_gh);
0062 if (error) {
0063 if (verbose)
0064 fs_err(sdp, "Error %d locking journal for spectator "
0065 "mount.\n", error);
0066 return -EPERM;
0067 }
0068 error = gfs2_jdesc_check(jd);
0069 if (error) {
0070 if (verbose)
0071 fs_err(sdp, "Error checking journal for spectator "
0072 "mount.\n");
0073 goto out_unlock;
0074 }
0075 error = gfs2_find_jhead(jd, &head, false);
0076 if (error) {
0077 if (verbose)
0078 fs_err(sdp, "Error parsing journal for spectator "
0079 "mount.\n");
0080 goto out_unlock;
0081 }
0082 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
0083 error = -EPERM;
0084 if (verbose)
0085 fs_err(sdp, "jid=%u: Journal is dirty, so the first "
0086 "mounter must not be a spectator.\n",
0087 jd->jd_jid);
0088 }
0089
0090 out_unlock:
0091 gfs2_glock_dq_uninit(&j_gh);
0092 return error;
0093 }
0094
0095
0096
0097
0098
0099
0100
0101 int gfs2_freeze_lock(struct gfs2_sbd *sdp, struct gfs2_holder *freeze_gh,
0102 int caller_flags)
0103 {
0104 int flags = LM_FLAG_NOEXP | GL_EXACT | caller_flags;
0105 int error;
0106
0107 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, flags,
0108 freeze_gh);
0109 if (error && error != GLR_TRYFAILED)
0110 fs_err(sdp, "can't lock the freeze lock: %d\n", error);
0111 return error;
0112 }
0113
0114 void gfs2_freeze_unlock(struct gfs2_holder *freeze_gh)
0115 {
0116 if (gfs2_holder_initialized(freeze_gh))
0117 gfs2_glock_dq_uninit(freeze_gh);
0118 }
0119
0120 static void signal_our_withdraw(struct gfs2_sbd *sdp)
0121 {
0122 struct gfs2_glock *live_gl = sdp->sd_live_gh.gh_gl;
0123 struct inode *inode;
0124 struct gfs2_inode *ip;
0125 struct gfs2_glock *i_gl;
0126 u64 no_formal_ino;
0127 int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
0128 int ret = 0;
0129 int tries;
0130
0131 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) || !sdp->sd_jdesc)
0132 return;
0133
0134 gfs2_ail_drain(sdp);
0135 inode = sdp->sd_jdesc->jd_inode;
0136 ip = GFS2_I(inode);
0137 i_gl = ip->i_gl;
0138 no_formal_ino = ip->i_no_formal_ino;
0139
0140
0141 set_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
0154 if (!sb_rdonly(sdp->sd_vfs)) {
0155 struct gfs2_holder freeze_gh;
0156
0157 gfs2_holder_mark_uninitialized(&freeze_gh);
0158 if (sdp->sd_freeze_gl &&
0159 !gfs2_glock_is_locked_by_me(sdp->sd_freeze_gl)) {
0160 ret = gfs2_freeze_lock(sdp, &freeze_gh,
0161 log_write_allowed ? 0 : LM_FLAG_TRY);
0162 if (ret == GLR_TRYFAILED)
0163 ret = 0;
0164 }
0165 if (!ret)
0166 gfs2_make_fs_ro(sdp);
0167 gfs2_freeze_unlock(&freeze_gh);
0168 }
0169
0170 if (sdp->sd_lockstruct.ls_ops->lm_lock == NULL) {
0171 if (!ret)
0172 ret = -EIO;
0173 clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
0174 goto skip_recovery;
0175 }
0176
0177
0178
0179 if (gfs2_holder_initialized(&sdp->sd_journal_gh)) {
0180 gfs2_glock_dq_wait(&sdp->sd_journal_gh);
0181 gfs2_holder_uninit(&sdp->sd_journal_gh);
0182 }
0183 sdp->sd_jinode_gh.gh_flags |= GL_NOCACHE;
0184 gfs2_glock_dq(&sdp->sd_jinode_gh);
0185 if (test_bit(SDF_FS_FROZEN, &sdp->sd_flags)) {
0186
0187 flush_work(&sdp->sd_freeze_work);
0188 atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
0189 thaw_super(sdp->sd_vfs);
0190 } else {
0191 wait_on_bit(&i_gl->gl_flags, GLF_DEMOTE,
0192 TASK_UNINTERRUPTIBLE);
0193 }
0194
0195
0196
0197
0198 gfs2_holder_uninit(&sdp->sd_jinode_gh);
0199
0200
0201
0202
0203
0204
0205
0206 iput(inode);
0207
0208
0209
0210
0211
0212 if (i_gl->gl_ops->go_free) {
0213 set_bit(GLF_FREEING, &i_gl->gl_flags);
0214 wait_on_bit(&i_gl->gl_flags, GLF_FREEING, TASK_UNINTERRUPTIBLE);
0215 }
0216
0217
0218
0219
0220 gfs2_glock_hold(live_gl);
0221 gfs2_glock_dq_wait(&sdp->sd_live_gh);
0222
0223
0224
0225
0226
0227 fs_warn(sdp, "Requesting recovery of jid %d.\n",
0228 sdp->sd_lockstruct.ls_jid);
0229 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | LM_FLAG_NOEXP,
0230 &sdp->sd_live_gh);
0231 msleep(GL_GLOCK_MAX_HOLD);
0232
0233
0234
0235 ret = gfs2_glock_nq(&sdp->sd_live_gh);
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247 if (ret == 0) {
0248 fs_warn(sdp, "No other mounters found. Trying to recover our "
0249 "own journal jid %d.\n", sdp->sd_lockstruct.ls_jid);
0250 if (gfs2_recover_journal(sdp->sd_jdesc, 1))
0251 fs_warn(sdp, "Unable to recover our journal jid %d.\n",
0252 sdp->sd_lockstruct.ls_jid);
0253 gfs2_glock_dq_wait(&sdp->sd_live_gh);
0254 gfs2_holder_reinit(LM_ST_SHARED, LM_FLAG_NOEXP | GL_EXACT,
0255 &sdp->sd_live_gh);
0256 gfs2_glock_nq(&sdp->sd_live_gh);
0257 }
0258
0259 gfs2_glock_queue_put(live_gl);
0260 clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 inode = gfs2_inode_lookup(sdp->sd_vfs, DT_UNKNOWN,
0273 sdp->sd_jdesc->jd_no_addr, no_formal_ino,
0274 GFS2_BLKST_FREE);
0275 if (IS_ERR(inode)) {
0276 fs_warn(sdp, "Reprocessing of jid %d failed with %ld.\n",
0277 sdp->sd_lockstruct.ls_jid, PTR_ERR(inode));
0278 goto skip_recovery;
0279 }
0280 sdp->sd_jdesc->jd_inode = inode;
0281 d_mark_dontcache(inode);
0282
0283
0284
0285
0286 for (tries = 0; tries < 10; tries++) {
0287 ret = check_journal_clean(sdp, sdp->sd_jdesc, false);
0288 if (!ret)
0289 break;
0290 msleep(HZ);
0291 fs_warn(sdp, "Waiting for journal recovery jid %d.\n",
0292 sdp->sd_lockstruct.ls_jid);
0293 }
0294 skip_recovery:
0295 if (!ret)
0296 fs_warn(sdp, "Journal recovery complete for jid %d.\n",
0297 sdp->sd_lockstruct.ls_jid);
0298 else
0299 fs_warn(sdp, "Journal recovery skipped for jid %d until next "
0300 "mount.\n", sdp->sd_lockstruct.ls_jid);
0301 fs_warn(sdp, "Glock dequeues delayed: %lu\n", sdp->sd_glock_dqs_held);
0302 sdp->sd_glock_dqs_held = 0;
0303 wake_up_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY);
0304 }
0305
0306 void gfs2_lm(struct gfs2_sbd *sdp, const char *fmt, ...)
0307 {
0308 struct va_format vaf;
0309 va_list args;
0310
0311 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
0312 test_bit(SDF_WITHDRAWN, &sdp->sd_flags))
0313 return;
0314
0315 va_start(args, fmt);
0316 vaf.fmt = fmt;
0317 vaf.va = &args;
0318 fs_err(sdp, "%pV", &vaf);
0319 va_end(args);
0320 }
0321
0322 int gfs2_withdraw(struct gfs2_sbd *sdp)
0323 {
0324 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
0325 const struct lm_lockops *lm = ls->ls_ops;
0326
0327 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
0328 test_and_set_bit(SDF_WITHDRAWN, &sdp->sd_flags)) {
0329 if (!test_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags))
0330 return -1;
0331
0332 wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_IN_PROG,
0333 TASK_UNINTERRUPTIBLE);
0334 return -1;
0335 }
0336
0337 set_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags);
0338
0339 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) {
0340 fs_err(sdp, "about to withdraw this file system\n");
0341 BUG_ON(sdp->sd_args.ar_debug);
0342
0343 signal_our_withdraw(sdp);
0344
0345 kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE);
0346
0347 if (!strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
0348 wait_for_completion(&sdp->sd_wdack);
0349
0350 if (lm->lm_unmount) {
0351 fs_err(sdp, "telling LM to unmount\n");
0352 lm->lm_unmount(sdp);
0353 }
0354 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
0355 fs_err(sdp, "File system withdrawn\n");
0356 dump_stack();
0357 clear_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags);
0358 smp_mb__after_atomic();
0359 wake_up_bit(&sdp->sd_flags, SDF_WITHDRAW_IN_PROG);
0360 }
0361
0362 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
0363 panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname);
0364
0365 return -1;
0366 }
0367
0368
0369
0370
0371
0372 void gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
0373 const char *function, char *file, unsigned int line,
0374 bool delayed)
0375 {
0376 if (gfs2_withdrawn(sdp))
0377 return;
0378
0379 fs_err(sdp,
0380 "fatal: assertion \"%s\" failed\n"
0381 " function = %s, file = %s, line = %u\n",
0382 assertion, function, file, line);
0383
0384
0385
0386
0387
0388 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
0389 delayed = false;
0390
0391 if (delayed)
0392 gfs2_withdraw_delayed(sdp);
0393 else
0394 gfs2_withdraw(sdp);
0395 dump_stack();
0396 }
0397
0398
0399
0400
0401
0402 void gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
0403 const char *function, char *file, unsigned int line)
0404 {
0405 if (time_before(jiffies,
0406 sdp->sd_last_warning +
0407 gfs2_tune_get(sdp, gt_complain_secs) * HZ))
0408 return;
0409
0410 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW)
0411 fs_warn(sdp, "warning: assertion \"%s\" failed at function = %s, file = %s, line = %u\n",
0412 assertion, function, file, line);
0413
0414 if (sdp->sd_args.ar_debug)
0415 BUG();
0416 else
0417 dump_stack();
0418
0419 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
0420 panic("GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
0421 "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
0422 sdp->sd_fsname, assertion,
0423 sdp->sd_fsname, function, file, line);
0424
0425 sdp->sd_last_warning = jiffies;
0426 }
0427
0428
0429
0430
0431
0432 void gfs2_consist_i(struct gfs2_sbd *sdp, const char *function,
0433 char *file, unsigned int line)
0434 {
0435 gfs2_lm(sdp,
0436 "fatal: filesystem consistency error - function = %s, file = %s, line = %u\n",
0437 function, file, line);
0438 gfs2_withdraw(sdp);
0439 }
0440
0441
0442
0443
0444
0445 void gfs2_consist_inode_i(struct gfs2_inode *ip,
0446 const char *function, char *file, unsigned int line)
0447 {
0448 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
0449
0450 gfs2_lm(sdp,
0451 "fatal: filesystem consistency error\n"
0452 " inode = %llu %llu\n"
0453 " function = %s, file = %s, line = %u\n",
0454 (unsigned long long)ip->i_no_formal_ino,
0455 (unsigned long long)ip->i_no_addr,
0456 function, file, line);
0457 gfs2_dump_glock(NULL, ip->i_gl, 1);
0458 gfs2_withdraw(sdp);
0459 }
0460
0461
0462
0463
0464
0465 void gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd,
0466 const char *function, char *file, unsigned int line)
0467 {
0468 struct gfs2_sbd *sdp = rgd->rd_sbd;
0469 char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
0470
0471 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
0472 gfs2_rgrp_dump(NULL, rgd, fs_id_buf);
0473 gfs2_lm(sdp,
0474 "fatal: filesystem consistency error\n"
0475 " RG = %llu\n"
0476 " function = %s, file = %s, line = %u\n",
0477 (unsigned long long)rgd->rd_addr,
0478 function, file, line);
0479 gfs2_dump_glock(NULL, rgd->rd_gl, 1);
0480 gfs2_withdraw(sdp);
0481 }
0482
0483
0484
0485
0486
0487
0488
0489 int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
0490 const char *type, const char *function, char *file,
0491 unsigned int line)
0492 {
0493 int me;
0494
0495 gfs2_lm(sdp,
0496 "fatal: invalid metadata block\n"
0497 " bh = %llu (%s)\n"
0498 " function = %s, file = %s, line = %u\n",
0499 (unsigned long long)bh->b_blocknr, type,
0500 function, file, line);
0501 me = gfs2_withdraw(sdp);
0502 return (me) ? -1 : -2;
0503 }
0504
0505
0506
0507
0508
0509
0510
0511 int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
0512 u16 type, u16 t, const char *function,
0513 char *file, unsigned int line)
0514 {
0515 int me;
0516
0517 gfs2_lm(sdp,
0518 "fatal: invalid metadata block\n"
0519 " bh = %llu (type: exp=%u, found=%u)\n"
0520 " function = %s, file = %s, line = %u\n",
0521 (unsigned long long)bh->b_blocknr, type, t,
0522 function, file, line);
0523 me = gfs2_withdraw(sdp);
0524 return (me) ? -1 : -2;
0525 }
0526
0527
0528
0529
0530
0531
0532
0533 int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
0534 unsigned int line)
0535 {
0536 gfs2_lm(sdp,
0537 "fatal: I/O error\n"
0538 " function = %s, file = %s, line = %u\n",
0539 function, file, line);
0540 return gfs2_withdraw(sdp);
0541 }
0542
0543
0544
0545
0546
0547
0548 void gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
0549 const char *function, char *file, unsigned int line,
0550 bool withdraw)
0551 {
0552 if (gfs2_withdrawn(sdp))
0553 return;
0554
0555 fs_err(sdp, "fatal: I/O error\n"
0556 " block = %llu\n"
0557 " function = %s, file = %s, line = %u\n",
0558 (unsigned long long)bh->b_blocknr, function, file, line);
0559 if (withdraw)
0560 gfs2_withdraw(sdp);
0561 }
0562