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/sched.h>
0010 #include <linux/slab.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/completion.h>
0013 #include <linux/buffer_head.h>
0014 #include <linux/kallsyms.h>
0015 #include <linux/gfs2_ondisk.h>
0016 
0017 #include "gfs2.h"
0018 #include "incore.h"
0019 #include "glock.h"
0020 #include "inode.h"
0021 #include "log.h"
0022 #include "lops.h"
0023 #include "meta_io.h"
0024 #include "trans.h"
0025 #include "util.h"
0026 #include "trace_gfs2.h"
0027 
0028 static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
0029 {
0030     fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
0031     fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
0032         tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
0033         test_bit(TR_TOUCHED, &tr->tr_flags));
0034     fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u\n",
0035         tr->tr_num_buf_new, tr->tr_num_buf_rm,
0036         tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
0037         tr->tr_num_revoke);
0038 }
0039 
0040 int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp,
0041                unsigned int blocks, unsigned int revokes,
0042                unsigned long ip)
0043 {
0044     unsigned int extra_revokes;
0045 
0046     if (current->journal_info) {
0047         gfs2_print_trans(sdp, current->journal_info);
0048         BUG();
0049     }
0050     BUG_ON(blocks == 0 && revokes == 0);
0051 
0052     if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
0053         return -EROFS;
0054 
0055     tr->tr_ip = ip;
0056     tr->tr_blocks = blocks;
0057     tr->tr_revokes = revokes;
0058     tr->tr_reserved = GFS2_LOG_FLUSH_MIN_BLOCKS;
0059     if (blocks) {
0060         /*
0061          * The reserved blocks are either used for data or metadata.
0062          * We can have mixed data and metadata, each with its own log
0063          * descriptor block; see calc_reserved().
0064          */
0065         tr->tr_reserved += blocks + 1 + DIV_ROUND_UP(blocks - 1, databuf_limit(sdp));
0066     }
0067     INIT_LIST_HEAD(&tr->tr_databuf);
0068     INIT_LIST_HEAD(&tr->tr_buf);
0069     INIT_LIST_HEAD(&tr->tr_list);
0070     INIT_LIST_HEAD(&tr->tr_ail1_list);
0071     INIT_LIST_HEAD(&tr->tr_ail2_list);
0072 
0073     if (gfs2_assert_warn(sdp, tr->tr_reserved <= sdp->sd_jdesc->jd_blocks))
0074         return -EINVAL;
0075 
0076     sb_start_intwrite(sdp->sd_vfs);
0077 
0078     /*
0079      * Try the reservations under sd_log_flush_lock to prevent log flushes
0080      * from creating inconsistencies between the number of allocated and
0081      * reserved revokes.  If that fails, do a full-block allocation outside
0082      * of the lock to avoid stalling log flushes.  Then, allot the
0083      * appropriate number of blocks to revokes, use as many revokes locally
0084      * as needed, and "release" the surplus into the revokes pool.
0085      */
0086 
0087     down_read(&sdp->sd_log_flush_lock);
0088     if (gfs2_log_try_reserve(sdp, tr, &extra_revokes))
0089         goto reserved;
0090     up_read(&sdp->sd_log_flush_lock);
0091     gfs2_log_reserve(sdp, tr, &extra_revokes);
0092     down_read(&sdp->sd_log_flush_lock);
0093 
0094 reserved:
0095     gfs2_log_release_revokes(sdp, extra_revokes);
0096     if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
0097         gfs2_log_release_revokes(sdp, tr->tr_revokes);
0098         up_read(&sdp->sd_log_flush_lock);
0099         gfs2_log_release(sdp, tr->tr_reserved);
0100         sb_end_intwrite(sdp->sd_vfs);
0101         return -EROFS;
0102     }
0103 
0104     current->journal_info = tr;
0105 
0106     return 0;
0107 }
0108 
0109 int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
0110              unsigned int revokes)
0111 {
0112     struct gfs2_trans *tr;
0113     int error;
0114 
0115     tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS);
0116     if (!tr)
0117         return -ENOMEM;
0118     error = __gfs2_trans_begin(tr, sdp, blocks, revokes, _RET_IP_);
0119     if (error)
0120         kmem_cache_free(gfs2_trans_cachep, tr);
0121     return error;
0122 }
0123 
0124 void gfs2_trans_end(struct gfs2_sbd *sdp)
0125 {
0126     struct gfs2_trans *tr = current->journal_info;
0127     s64 nbuf;
0128 
0129     current->journal_info = NULL;
0130 
0131     if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
0132         gfs2_log_release_revokes(sdp, tr->tr_revokes);
0133         up_read(&sdp->sd_log_flush_lock);
0134         gfs2_log_release(sdp, tr->tr_reserved);
0135         if (!test_bit(TR_ONSTACK, &tr->tr_flags))
0136             gfs2_trans_free(sdp, tr);
0137         sb_end_intwrite(sdp->sd_vfs);
0138         return;
0139     }
0140 
0141     gfs2_log_release_revokes(sdp, tr->tr_revokes - tr->tr_num_revoke);
0142 
0143     nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
0144     nbuf -= tr->tr_num_buf_rm;
0145     nbuf -= tr->tr_num_databuf_rm;
0146 
0147     if (gfs2_assert_withdraw(sdp, nbuf <= tr->tr_blocks) ||
0148         gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
0149         gfs2_print_trans(sdp, tr);
0150 
0151     gfs2_log_commit(sdp, tr);
0152     if (!test_bit(TR_ONSTACK, &tr->tr_flags) &&
0153         !test_bit(TR_ATTACHED, &tr->tr_flags))
0154         gfs2_trans_free(sdp, tr);
0155     up_read(&sdp->sd_log_flush_lock);
0156 
0157     if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
0158         gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
0159                    GFS2_LFC_TRANS_END);
0160     sb_end_intwrite(sdp->sd_vfs);
0161 }
0162 
0163 static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
0164                            struct buffer_head *bh)
0165 {
0166     struct gfs2_bufdata *bd;
0167 
0168     bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
0169     bd->bd_bh = bh;
0170     bd->bd_gl = gl;
0171     INIT_LIST_HEAD(&bd->bd_list);
0172     INIT_LIST_HEAD(&bd->bd_ail_st_list);
0173     INIT_LIST_HEAD(&bd->bd_ail_gl_list);
0174     bh->b_private = bd;
0175     return bd;
0176 }
0177 
0178 /**
0179  * gfs2_trans_add_data - Add a databuf to the transaction.
0180  * @gl: The inode glock associated with the buffer
0181  * @bh: The buffer to add
0182  *
0183  * This is used in journaled data mode.
0184  * We need to journal the data block in the same way as metadata in
0185  * the functions above. The difference is that here we have a tag
0186  * which is two __be64's being the block number (as per meta data)
0187  * and a flag which says whether the data block needs escaping or
0188  * not. This means we need a new log entry for each 251 or so data
0189  * blocks, which isn't an enormous overhead but twice as much as
0190  * for normal metadata blocks.
0191  */
0192 void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
0193 {
0194     struct gfs2_trans *tr = current->journal_info;
0195     struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
0196     struct gfs2_bufdata *bd;
0197 
0198     lock_buffer(bh);
0199     if (buffer_pinned(bh)) {
0200         set_bit(TR_TOUCHED, &tr->tr_flags);
0201         goto out;
0202     }
0203     gfs2_log_lock(sdp);
0204     bd = bh->b_private;
0205     if (bd == NULL) {
0206         gfs2_log_unlock(sdp);
0207         unlock_buffer(bh);
0208         if (bh->b_private == NULL)
0209             bd = gfs2_alloc_bufdata(gl, bh);
0210         else
0211             bd = bh->b_private;
0212         lock_buffer(bh);
0213         gfs2_log_lock(sdp);
0214     }
0215     gfs2_assert(sdp, bd->bd_gl == gl);
0216     set_bit(TR_TOUCHED, &tr->tr_flags);
0217     if (list_empty(&bd->bd_list)) {
0218         set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
0219         set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
0220         gfs2_pin(sdp, bd->bd_bh);
0221         tr->tr_num_databuf_new++;
0222         list_add_tail(&bd->bd_list, &tr->tr_databuf);
0223     }
0224     gfs2_log_unlock(sdp);
0225 out:
0226     unlock_buffer(bh);
0227 }
0228 
0229 void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
0230 {
0231 
0232     struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
0233     struct gfs2_bufdata *bd;
0234     struct gfs2_meta_header *mh;
0235     struct gfs2_trans *tr = current->journal_info;
0236     enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
0237 
0238     lock_buffer(bh);
0239     if (buffer_pinned(bh)) {
0240         set_bit(TR_TOUCHED, &tr->tr_flags);
0241         goto out;
0242     }
0243     gfs2_log_lock(sdp);
0244     bd = bh->b_private;
0245     if (bd == NULL) {
0246         gfs2_log_unlock(sdp);
0247         unlock_buffer(bh);
0248         lock_page(bh->b_page);
0249         if (bh->b_private == NULL)
0250             bd = gfs2_alloc_bufdata(gl, bh);
0251         else
0252             bd = bh->b_private;
0253         unlock_page(bh->b_page);
0254         lock_buffer(bh);
0255         gfs2_log_lock(sdp);
0256     }
0257     gfs2_assert(sdp, bd->bd_gl == gl);
0258     set_bit(TR_TOUCHED, &tr->tr_flags);
0259     if (!list_empty(&bd->bd_list))
0260         goto out_unlock;
0261     set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
0262     set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
0263     mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
0264     if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
0265         fs_err(sdp, "Attempting to add uninitialised block to "
0266                "journal (inplace block=%lld)\n",
0267                (unsigned long long)bd->bd_bh->b_blocknr);
0268         BUG();
0269     }
0270     if (unlikely(state == SFS_FROZEN)) {
0271         fs_info(sdp, "GFS2:adding buf while frozen\n");
0272         gfs2_assert_withdraw(sdp, 0);
0273     }
0274     if (unlikely(gfs2_withdrawn(sdp))) {
0275         fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
0276             (unsigned long long)bd->bd_bh->b_blocknr);
0277     }
0278     gfs2_pin(sdp, bd->bd_bh);
0279     mh->__pad0 = cpu_to_be64(0);
0280     mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
0281     list_add(&bd->bd_list, &tr->tr_buf);
0282     tr->tr_num_buf_new++;
0283 out_unlock:
0284     gfs2_log_unlock(sdp);
0285 out:
0286     unlock_buffer(bh);
0287 }
0288 
0289 void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
0290 {
0291     struct gfs2_trans *tr = current->journal_info;
0292 
0293     BUG_ON(!list_empty(&bd->bd_list));
0294     gfs2_add_revoke(sdp, bd);
0295     set_bit(TR_TOUCHED, &tr->tr_flags);
0296     tr->tr_num_revoke++;
0297 }
0298 
0299 void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
0300 {
0301     struct gfs2_bufdata *bd, *tmp;
0302     unsigned int n = len;
0303 
0304     gfs2_log_lock(sdp);
0305     list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
0306         if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
0307             list_del_init(&bd->bd_list);
0308             gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
0309             sdp->sd_log_num_revoke--;
0310             if (bd->bd_gl)
0311                 gfs2_glock_remove_revoke(bd->bd_gl);
0312             kmem_cache_free(gfs2_bufdata_cachep, bd);
0313             gfs2_log_release_revokes(sdp, 1);
0314             if (--n == 0)
0315                 break;
0316         }
0317     }
0318     gfs2_log_unlock(sdp);
0319 }
0320 
0321 void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
0322 {
0323     if (tr == NULL)
0324         return;
0325 
0326     gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
0327     gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
0328     gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
0329     gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
0330     kmem_cache_free(gfs2_trans_cachep, tr);
0331 }