Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
0004  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
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  * check_journal_clean - Make sure a journal is clean for a spectator mount
0045  * @sdp: The GFS2 superblock
0046  * @jd: The journal descriptor
0047  * @verbose: Show more prints in the log
0048  *
0049  * Returns: 0 if the journal is clean or locked, else an error
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  * gfs2_freeze_lock - hold the freeze glock
0097  * @sdp: the superblock
0098  * @freeze_gh: pointer to the requested holder
0099  * @caller_flags: any additional flags needed by the caller
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); /* frees all transactions */
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     /* Prevent any glock dq until withdraw recovery is complete */
0141     set_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
0142     /*
0143      * Don't tell dlm we're bailing until we have no more buffers in the
0144      * wind. If journal had an IO error, the log code should just purge
0145      * the outstanding buffers rather than submitting new IO. Making the
0146      * file system read-only will flush the journal, etc.
0147      *
0148      * During a normal unmount, gfs2_make_fs_ro calls gfs2_log_shutdown
0149      * which clears SDF_JOURNAL_LIVE. In a withdraw, we must not write
0150      * any UNMOUNT log header, so we can't call gfs2_log_shutdown, and
0151      * therefore we need to clear SDF_JOURNAL_LIVE manually.
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) { /* lock_nolock */
0171         if (!ret)
0172             ret = -EIO;
0173         clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
0174         goto skip_recovery;
0175     }
0176     /*
0177      * Drop the glock for our journal so another node can recover it.
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         /* Make sure gfs2_unfreeze works if partially-frozen */
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      * holder_uninit to force glock_put, to force dlm to let go
0197      */
0198     gfs2_holder_uninit(&sdp->sd_jinode_gh);
0199 
0200     /*
0201      * Note: We need to be careful here:
0202      * Our iput of jd_inode will evict it. The evict will dequeue its
0203      * glock, but the glock dq will wait for the withdraw unless we have
0204      * exception code in glock_dq.
0205      */
0206     iput(inode);
0207     /*
0208      * Wait until the journal inode's glock is freed. This allows try locks
0209      * on other nodes to be successful, otherwise we remain the owner of
0210      * the glock as far as dlm is concerned.
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      * Dequeue the "live" glock, but keep a reference so it's never freed.
0219      */
0220     gfs2_glock_hold(live_gl);
0221     gfs2_glock_dq_wait(&sdp->sd_live_gh);
0222     /*
0223      * We enqueue the "live" glock in EX so that all other nodes
0224      * get a demote request and act on it. We don't really want the
0225      * lock in EX, so we send a "try" lock with 1CB to produce a callback.
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      * This will likely fail in a cluster, but succeed standalone:
0234      */
0235     ret = gfs2_glock_nq(&sdp->sd_live_gh);
0236 
0237     /*
0238      * If we actually got the "live" lock in EX mode, there are no other
0239      * nodes available to replay our journal. So we try to replay it
0240      * ourselves. We hold the "live" glock to prevent other mounters
0241      * during recovery, then just dequeue it and reacquire it in our
0242      * normal SH mode. Just in case the problem that caused us to
0243      * withdraw prevents us from recovering our journal (e.g. io errors
0244      * and such) we still check if the journal is clean before proceeding
0245      * but we may wait forever until another mounter does the recovery.
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); /* drop extra reference we acquired */
0260     clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
0261 
0262     /*
0263      * At this point our journal is evicted, so we need to get a new inode
0264      * for it. Once done, we need to call gfs2_find_jhead which
0265      * calls gfs2_map_journal_extents to map it for us again.
0266      *
0267      * Note that we don't really want it to look up a FREE block. The
0268      * GFS2_BLKST_FREE simply overrides a block check in gfs2_inode_lookup
0269      * which would otherwise fail because it requires grabbing an rgrp
0270      * glock, which would fail with -EIO because we're withdrawing.
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      * Now wait until recovery is complete.
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  * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
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      * If errors=panic was specified on mount, it won't help to delay the
0386      * withdraw.
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  * gfs2_assert_warn_i - Print a message to the console if @assertion is false
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  * gfs2_consist_i - Flag a filesystem consistency error and withdraw
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  * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
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  * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
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  * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
0485  * Returns: -1 if this call withdrew the machine,
0486  *          -2 if it was already withdrawn
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  * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
0507  * Returns: -1 if this call withdrew the machine,
0508  *          -2 if it was already withdrawn
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  * gfs2_io_error_i - Flag an I/O error and withdraw
0529  * Returns: -1 if this call withdrew the machine,
0530  *          0 if it was already withdrawn
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  * gfs2_io_error_bh_i - Flag a buffer I/O error
0545  * @withdraw: withdraw the filesystem
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