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-2008 Red Hat, Inc.  All rights reserved.
0005  */
0006 
0007 #include <linux/sched.h>
0008 #include <linux/slab.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/completion.h>
0011 #include <linux/buffer_head.h>
0012 #include <linux/mm.h>
0013 #include <linux/pagemap.h>
0014 #include <linux/writeback.h>
0015 #include <linux/swap.h>
0016 #include <linux/delay.h>
0017 #include <linux/bio.h>
0018 #include <linux/gfs2_ondisk.h>
0019 
0020 #include "gfs2.h"
0021 #include "incore.h"
0022 #include "glock.h"
0023 #include "glops.h"
0024 #include "inode.h"
0025 #include "log.h"
0026 #include "lops.h"
0027 #include "meta_io.h"
0028 #include "rgrp.h"
0029 #include "trans.h"
0030 #include "util.h"
0031 #include "trace_gfs2.h"
0032 
0033 static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
0034 {
0035     struct buffer_head *bh, *head;
0036     int nr_underway = 0;
0037     blk_opf_t write_flags = REQ_META | REQ_PRIO | wbc_to_write_flags(wbc);
0038 
0039     BUG_ON(!PageLocked(page));
0040     BUG_ON(!page_has_buffers(page));
0041 
0042     head = page_buffers(page);
0043     bh = head;
0044 
0045     do {
0046         if (!buffer_mapped(bh))
0047             continue;
0048         /*
0049          * If it's a fully non-blocking write attempt and we cannot
0050          * lock the buffer then redirty the page.  Note that this can
0051          * potentially cause a busy-wait loop from flusher thread and kswapd
0052          * activity, but those code paths have their own higher-level
0053          * throttling.
0054          */
0055         if (wbc->sync_mode != WB_SYNC_NONE) {
0056             lock_buffer(bh);
0057         } else if (!trylock_buffer(bh)) {
0058             redirty_page_for_writepage(wbc, page);
0059             continue;
0060         }
0061         if (test_clear_buffer_dirty(bh)) {
0062             mark_buffer_async_write(bh);
0063         } else {
0064             unlock_buffer(bh);
0065         }
0066     } while ((bh = bh->b_this_page) != head);
0067 
0068     /*
0069      * The page and its buffers are protected by PageWriteback(), so we can
0070      * drop the bh refcounts early.
0071      */
0072     BUG_ON(PageWriteback(page));
0073     set_page_writeback(page);
0074 
0075     do {
0076         struct buffer_head *next = bh->b_this_page;
0077         if (buffer_async_write(bh)) {
0078             submit_bh(REQ_OP_WRITE | write_flags, bh);
0079             nr_underway++;
0080         }
0081         bh = next;
0082     } while (bh != head);
0083     unlock_page(page);
0084 
0085     if (nr_underway == 0)
0086         end_page_writeback(page);
0087 
0088     return 0;
0089 }
0090 
0091 const struct address_space_operations gfs2_meta_aops = {
0092     .dirty_folio    = block_dirty_folio,
0093     .invalidate_folio = block_invalidate_folio,
0094     .writepage = gfs2_aspace_writepage,
0095     .release_folio = gfs2_release_folio,
0096 };
0097 
0098 const struct address_space_operations gfs2_rgrp_aops = {
0099     .dirty_folio    = block_dirty_folio,
0100     .invalidate_folio = block_invalidate_folio,
0101     .writepage = gfs2_aspace_writepage,
0102     .release_folio = gfs2_release_folio,
0103 };
0104 
0105 /**
0106  * gfs2_getbuf - Get a buffer with a given address space
0107  * @gl: the glock
0108  * @blkno: the block number (filesystem scope)
0109  * @create: 1 if the buffer should be created
0110  *
0111  * Returns: the buffer
0112  */
0113 
0114 struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
0115 {
0116     struct address_space *mapping = gfs2_glock2aspace(gl);
0117     struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
0118     struct page *page;
0119     struct buffer_head *bh;
0120     unsigned int shift;
0121     unsigned long index;
0122     unsigned int bufnum;
0123 
0124     if (mapping == NULL)
0125         mapping = &sdp->sd_aspace;
0126 
0127     shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
0128     index = blkno >> shift;             /* convert block to page */
0129     bufnum = blkno - (index << shift);  /* block buf index within page */
0130 
0131     if (create) {
0132         for (;;) {
0133             page = grab_cache_page(mapping, index);
0134             if (page)
0135                 break;
0136             yield();
0137         }
0138         if (!page_has_buffers(page))
0139             create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
0140     } else {
0141         page = find_get_page_flags(mapping, index,
0142                         FGP_LOCK|FGP_ACCESSED);
0143         if (!page)
0144             return NULL;
0145         if (!page_has_buffers(page)) {
0146             bh = NULL;
0147             goto out_unlock;
0148         }
0149     }
0150 
0151     /* Locate header for our buffer within our page */
0152     for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
0153         /* Do nothing */;
0154     get_bh(bh);
0155 
0156     if (!buffer_mapped(bh))
0157         map_bh(bh, sdp->sd_vfs, blkno);
0158 
0159 out_unlock:
0160     unlock_page(page);
0161     put_page(page);
0162 
0163     return bh;
0164 }
0165 
0166 static void meta_prep_new(struct buffer_head *bh)
0167 {
0168     struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
0169 
0170     lock_buffer(bh);
0171     clear_buffer_dirty(bh);
0172     set_buffer_uptodate(bh);
0173     unlock_buffer(bh);
0174 
0175     mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
0176 }
0177 
0178 /**
0179  * gfs2_meta_new - Get a block
0180  * @gl: The glock associated with this block
0181  * @blkno: The block number
0182  *
0183  * Returns: The buffer
0184  */
0185 
0186 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
0187 {
0188     struct buffer_head *bh;
0189     bh = gfs2_getbuf(gl, blkno, CREATE);
0190     meta_prep_new(bh);
0191     return bh;
0192 }
0193 
0194 static void gfs2_meta_read_endio(struct bio *bio)
0195 {
0196     struct bio_vec *bvec;
0197     struct bvec_iter_all iter_all;
0198 
0199     bio_for_each_segment_all(bvec, bio, iter_all) {
0200         struct page *page = bvec->bv_page;
0201         struct buffer_head *bh = page_buffers(page);
0202         unsigned int len = bvec->bv_len;
0203 
0204         while (bh_offset(bh) < bvec->bv_offset)
0205             bh = bh->b_this_page;
0206         do {
0207             struct buffer_head *next = bh->b_this_page;
0208             len -= bh->b_size;
0209             bh->b_end_io(bh, !bio->bi_status);
0210             bh = next;
0211         } while (bh && len);
0212     }
0213     bio_put(bio);
0214 }
0215 
0216 /*
0217  * Submit several consecutive buffer head I/O requests as a single bio I/O
0218  * request.  (See submit_bh_wbc.)
0219  */
0220 static void gfs2_submit_bhs(blk_opf_t opf, struct buffer_head *bhs[], int num)
0221 {
0222     while (num > 0) {
0223         struct buffer_head *bh = *bhs;
0224         struct bio *bio;
0225 
0226         bio = bio_alloc(bh->b_bdev, num, opf, GFP_NOIO);
0227         bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
0228         while (num > 0) {
0229             bh = *bhs;
0230             if (!bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh))) {
0231                 BUG_ON(bio->bi_iter.bi_size == 0);
0232                 break;
0233             }
0234             bhs++;
0235             num--;
0236         }
0237         bio->bi_end_io = gfs2_meta_read_endio;
0238         submit_bio(bio);
0239     }
0240 }
0241 
0242 /**
0243  * gfs2_meta_read - Read a block from disk
0244  * @gl: The glock covering the block
0245  * @blkno: The block number
0246  * @flags: flags
0247  * @rahead: Do read-ahead
0248  * @bhp: the place where the buffer is returned (NULL on failure)
0249  *
0250  * Returns: errno
0251  */
0252 
0253 int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
0254            int rahead, struct buffer_head **bhp)
0255 {
0256     struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
0257     struct buffer_head *bh, *bhs[2];
0258     int num = 0;
0259 
0260     if (unlikely(gfs2_withdrawn(sdp)) && !gfs2_withdraw_in_prog(sdp)) {
0261         *bhp = NULL;
0262         return -EIO;
0263     }
0264 
0265     *bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
0266 
0267     lock_buffer(bh);
0268     if (buffer_uptodate(bh)) {
0269         unlock_buffer(bh);
0270         flags &= ~DIO_WAIT;
0271     } else {
0272         bh->b_end_io = end_buffer_read_sync;
0273         get_bh(bh);
0274         bhs[num++] = bh;
0275     }
0276 
0277     if (rahead) {
0278         bh = gfs2_getbuf(gl, blkno + 1, CREATE);
0279 
0280         lock_buffer(bh);
0281         if (buffer_uptodate(bh)) {
0282             unlock_buffer(bh);
0283             brelse(bh);
0284         } else {
0285             bh->b_end_io = end_buffer_read_sync;
0286             bhs[num++] = bh;
0287         }
0288     }
0289 
0290     gfs2_submit_bhs(REQ_OP_READ | REQ_META | REQ_PRIO, bhs, num);
0291     if (!(flags & DIO_WAIT))
0292         return 0;
0293 
0294     bh = *bhp;
0295     wait_on_buffer(bh);
0296     if (unlikely(!buffer_uptodate(bh))) {
0297         struct gfs2_trans *tr = current->journal_info;
0298         if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
0299             gfs2_io_error_bh_wd(sdp, bh);
0300         brelse(bh);
0301         *bhp = NULL;
0302         return -EIO;
0303     }
0304 
0305     return 0;
0306 }
0307 
0308 /**
0309  * gfs2_meta_wait - Reread a block from disk
0310  * @sdp: the filesystem
0311  * @bh: The block to wait for
0312  *
0313  * Returns: errno
0314  */
0315 
0316 int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
0317 {
0318     if (unlikely(gfs2_withdrawn(sdp)) && !gfs2_withdraw_in_prog(sdp))
0319         return -EIO;
0320 
0321     wait_on_buffer(bh);
0322 
0323     if (!buffer_uptodate(bh)) {
0324         struct gfs2_trans *tr = current->journal_info;
0325         if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
0326             gfs2_io_error_bh_wd(sdp, bh);
0327         return -EIO;
0328     }
0329     if (unlikely(gfs2_withdrawn(sdp)) && !gfs2_withdraw_in_prog(sdp))
0330         return -EIO;
0331 
0332     return 0;
0333 }
0334 
0335 void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
0336 {
0337     struct address_space *mapping = bh->b_page->mapping;
0338     struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
0339     struct gfs2_bufdata *bd = bh->b_private;
0340     struct gfs2_trans *tr = current->journal_info;
0341     int was_pinned = 0;
0342 
0343     if (test_clear_buffer_pinned(bh)) {
0344         trace_gfs2_pin(bd, 0);
0345         atomic_dec(&sdp->sd_log_pinned);
0346         list_del_init(&bd->bd_list);
0347         if (meta == REMOVE_META)
0348             tr->tr_num_buf_rm++;
0349         else
0350             tr->tr_num_databuf_rm++;
0351         set_bit(TR_TOUCHED, &tr->tr_flags);
0352         was_pinned = 1;
0353         brelse(bh);
0354     }
0355     if (bd) {
0356         if (bd->bd_tr) {
0357             gfs2_trans_add_revoke(sdp, bd);
0358         } else if (was_pinned) {
0359             bh->b_private = NULL;
0360             kmem_cache_free(gfs2_bufdata_cachep, bd);
0361         } else if (!list_empty(&bd->bd_ail_st_list) &&
0362                     !list_empty(&bd->bd_ail_gl_list)) {
0363             gfs2_remove_from_ail(bd);
0364         }
0365     }
0366     clear_buffer_dirty(bh);
0367     clear_buffer_uptodate(bh);
0368 }
0369 
0370 /**
0371  * gfs2_ail1_wipe - remove deleted/freed buffers from the ail1 list
0372  * @sdp: superblock
0373  * @bstart: starting block address of buffers to remove
0374  * @blen: length of buffers to be removed
0375  *
0376  * This function is called from gfs2_journal wipe, whose job is to remove
0377  * buffers, corresponding to deleted blocks, from the journal. If we find any
0378  * bufdata elements on the system ail1 list, they haven't been written to
0379  * the journal yet. So we remove them.
0380  */
0381 static void gfs2_ail1_wipe(struct gfs2_sbd *sdp, u64 bstart, u32 blen)
0382 {
0383     struct gfs2_trans *tr, *s;
0384     struct gfs2_bufdata *bd, *bs;
0385     struct buffer_head *bh;
0386     u64 end = bstart + blen;
0387 
0388     gfs2_log_lock(sdp);
0389     spin_lock(&sdp->sd_ail_lock);
0390     list_for_each_entry_safe(tr, s, &sdp->sd_ail1_list, tr_list) {
0391         list_for_each_entry_safe(bd, bs, &tr->tr_ail1_list,
0392                      bd_ail_st_list) {
0393             bh = bd->bd_bh;
0394             if (bh->b_blocknr < bstart || bh->b_blocknr >= end)
0395                 continue;
0396 
0397             gfs2_remove_from_journal(bh, REMOVE_JDATA);
0398         }
0399     }
0400     spin_unlock(&sdp->sd_ail_lock);
0401     gfs2_log_unlock(sdp);
0402 }
0403 
0404 static struct buffer_head *gfs2_getjdatabuf(struct gfs2_inode *ip, u64 blkno)
0405 {
0406     struct address_space *mapping = ip->i_inode.i_mapping;
0407     struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
0408     struct page *page;
0409     struct buffer_head *bh;
0410     unsigned int shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
0411     unsigned long index = blkno >> shift; /* convert block to page */
0412     unsigned int bufnum = blkno - (index << shift);
0413 
0414     page = find_get_page_flags(mapping, index, FGP_LOCK|FGP_ACCESSED);
0415     if (!page)
0416         return NULL;
0417     if (!page_has_buffers(page)) {
0418         unlock_page(page);
0419         put_page(page);
0420         return NULL;
0421     }
0422     /* Locate header for our buffer within our page */
0423     for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
0424         /* Do nothing */;
0425     get_bh(bh);
0426     unlock_page(page);
0427     put_page(page);
0428     return bh;
0429 }
0430 
0431 /**
0432  * gfs2_journal_wipe - make inode's buffers so they aren't dirty/pinned anymore
0433  * @ip: the inode who owns the buffers
0434  * @bstart: the first buffer in the run
0435  * @blen: the number of buffers in the run
0436  *
0437  */
0438 
0439 void gfs2_journal_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
0440 {
0441     struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
0442     struct buffer_head *bh;
0443     int ty;
0444 
0445     gfs2_ail1_wipe(sdp, bstart, blen);
0446     while (blen) {
0447         ty = REMOVE_META;
0448         bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
0449         if (!bh && gfs2_is_jdata(ip)) {
0450             bh = gfs2_getjdatabuf(ip, bstart);
0451             ty = REMOVE_JDATA;
0452         }
0453         if (bh) {
0454             lock_buffer(bh);
0455             gfs2_log_lock(sdp);
0456             spin_lock(&sdp->sd_ail_lock);
0457             gfs2_remove_from_journal(bh, ty);
0458             spin_unlock(&sdp->sd_ail_lock);
0459             gfs2_log_unlock(sdp);
0460             unlock_buffer(bh);
0461             brelse(bh);
0462         }
0463 
0464         bstart++;
0465         blen--;
0466     }
0467 }
0468 
0469 /**
0470  * gfs2_meta_buffer - Get a metadata buffer
0471  * @ip: The GFS2 inode
0472  * @mtype: The block type (GFS2_METATYPE_*)
0473  * @num: The block number (device relative) of the buffer
0474  * @bhp: the buffer is returned here
0475  *
0476  * Returns: errno
0477  */
0478 
0479 int gfs2_meta_buffer(struct gfs2_inode *ip, u32 mtype, u64 num,
0480              struct buffer_head **bhp)
0481 {
0482     struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
0483     struct gfs2_glock *gl = ip->i_gl;
0484     struct buffer_head *bh;
0485     int ret = 0;
0486     int rahead = 0;
0487 
0488     if (num == ip->i_no_addr)
0489         rahead = ip->i_rahead;
0490 
0491     ret = gfs2_meta_read(gl, num, DIO_WAIT, rahead, &bh);
0492     if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
0493         brelse(bh);
0494         ret = -EIO;
0495     } else {
0496         *bhp = bh;
0497     }
0498     return ret;
0499 }
0500 
0501 /**
0502  * gfs2_meta_ra - start readahead on an extent of a file
0503  * @gl: the glock the blocks belong to
0504  * @dblock: the starting disk block
0505  * @extlen: the number of blocks in the extent
0506  *
0507  * returns: the first buffer in the extent
0508  */
0509 
0510 struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
0511 {
0512     struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
0513     struct buffer_head *first_bh, *bh;
0514     u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
0515               sdp->sd_sb.sb_bsize_shift;
0516 
0517     BUG_ON(!extlen);
0518 
0519     if (max_ra < 1)
0520         max_ra = 1;
0521     if (extlen > max_ra)
0522         extlen = max_ra;
0523 
0524     first_bh = gfs2_getbuf(gl, dblock, CREATE);
0525 
0526     if (buffer_uptodate(first_bh))
0527         goto out;
0528     if (!buffer_locked(first_bh))
0529         ll_rw_block(REQ_OP_READ | REQ_META | REQ_PRIO, 1, &first_bh);
0530 
0531     dblock++;
0532     extlen--;
0533 
0534     while (extlen) {
0535         bh = gfs2_getbuf(gl, dblock, CREATE);
0536 
0537         if (!buffer_uptodate(bh) && !buffer_locked(bh))
0538             ll_rw_block(REQ_OP_READ | REQ_RAHEAD | REQ_META |
0539                     REQ_PRIO, 1, &bh);
0540         brelse(bh);
0541         dblock++;
0542         extlen--;
0543         if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
0544             goto out;
0545     }
0546 
0547     wait_on_buffer(first_bh);
0548 out:
0549     return first_bh;
0550 }
0551