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/pagemap.h>
0013 #include <linux/pagevec.h>
0014 #include <linux/mpage.h>
0015 #include <linux/fs.h>
0016 #include <linux/writeback.h>
0017 #include <linux/swap.h>
0018 #include <linux/gfs2_ondisk.h>
0019 #include <linux/backing-dev.h>
0020 #include <linux/uio.h>
0021 #include <trace/events/writeback.h>
0022 #include <linux/sched/signal.h>
0023 
0024 #include "gfs2.h"
0025 #include "incore.h"
0026 #include "bmap.h"
0027 #include "glock.h"
0028 #include "inode.h"
0029 #include "log.h"
0030 #include "meta_io.h"
0031 #include "quota.h"
0032 #include "trans.h"
0033 #include "rgrp.h"
0034 #include "super.h"
0035 #include "util.h"
0036 #include "glops.h"
0037 #include "aops.h"
0038 
0039 
0040 void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
0041                 unsigned int from, unsigned int len)
0042 {
0043     struct buffer_head *head = page_buffers(page);
0044     unsigned int bsize = head->b_size;
0045     struct buffer_head *bh;
0046     unsigned int to = from + len;
0047     unsigned int start, end;
0048 
0049     for (bh = head, start = 0; bh != head || !start;
0050          bh = bh->b_this_page, start = end) {
0051         end = start + bsize;
0052         if (end <= from)
0053             continue;
0054         if (start >= to)
0055             break;
0056         set_buffer_uptodate(bh);
0057         gfs2_trans_add_data(ip->i_gl, bh);
0058     }
0059 }
0060 
0061 /**
0062  * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
0063  * @inode: The inode
0064  * @lblock: The block number to look up
0065  * @bh_result: The buffer head to return the result in
0066  * @create: Non-zero if we may add block to the file
0067  *
0068  * Returns: errno
0069  */
0070 
0071 static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
0072                   struct buffer_head *bh_result, int create)
0073 {
0074     int error;
0075 
0076     error = gfs2_block_map(inode, lblock, bh_result, 0);
0077     if (error)
0078         return error;
0079     if (!buffer_mapped(bh_result))
0080         return -ENODATA;
0081     return 0;
0082 }
0083 
0084 /**
0085  * gfs2_write_jdata_page - gfs2 jdata-specific version of block_write_full_page
0086  * @page: The page to write
0087  * @wbc: The writeback control
0088  *
0089  * This is the same as calling block_write_full_page, but it also
0090  * writes pages outside of i_size
0091  */
0092 static int gfs2_write_jdata_page(struct page *page,
0093                  struct writeback_control *wbc)
0094 {
0095     struct inode * const inode = page->mapping->host;
0096     loff_t i_size = i_size_read(inode);
0097     const pgoff_t end_index = i_size >> PAGE_SHIFT;
0098     unsigned offset;
0099 
0100     /*
0101      * The page straddles i_size.  It must be zeroed out on each and every
0102      * writepage invocation because it may be mmapped.  "A file is mapped
0103      * in multiples of the page size.  For a file that is not a multiple of
0104      * the  page size, the remaining memory is zeroed when mapped, and
0105      * writes to that region are not written out to the file."
0106      */
0107     offset = i_size & (PAGE_SIZE - 1);
0108     if (page->index == end_index && offset)
0109         zero_user_segment(page, offset, PAGE_SIZE);
0110 
0111     return __block_write_full_page(inode, page, gfs2_get_block_noalloc, wbc,
0112                        end_buffer_async_write);
0113 }
0114 
0115 /**
0116  * __gfs2_jdata_writepage - The core of jdata writepage
0117  * @page: The page to write
0118  * @wbc: The writeback control
0119  *
0120  * This is shared between writepage and writepages and implements the
0121  * core of the writepage operation. If a transaction is required then
0122  * PageChecked will have been set and the transaction will have
0123  * already been started before this is called.
0124  */
0125 
0126 static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
0127 {
0128     struct inode *inode = page->mapping->host;
0129     struct gfs2_inode *ip = GFS2_I(inode);
0130     struct gfs2_sbd *sdp = GFS2_SB(inode);
0131 
0132     if (PageChecked(page)) {
0133         ClearPageChecked(page);
0134         if (!page_has_buffers(page)) {
0135             create_empty_buffers(page, inode->i_sb->s_blocksize,
0136                          BIT(BH_Dirty)|BIT(BH_Uptodate));
0137         }
0138         gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize);
0139     }
0140     return gfs2_write_jdata_page(page, wbc);
0141 }
0142 
0143 /**
0144  * gfs2_jdata_writepage - Write complete page
0145  * @page: Page to write
0146  * @wbc: The writeback control
0147  *
0148  * Returns: errno
0149  *
0150  */
0151 
0152 static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
0153 {
0154     struct inode *inode = page->mapping->host;
0155     struct gfs2_inode *ip = GFS2_I(inode);
0156     struct gfs2_sbd *sdp = GFS2_SB(inode);
0157 
0158     if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
0159         goto out;
0160     if (PageChecked(page) || current->journal_info)
0161         goto out_ignore;
0162     return __gfs2_jdata_writepage(page, wbc);
0163 
0164 out_ignore:
0165     redirty_page_for_writepage(wbc, page);
0166 out:
0167     unlock_page(page);
0168     return 0;
0169 }
0170 
0171 /**
0172  * gfs2_writepages - Write a bunch of dirty pages back to disk
0173  * @mapping: The mapping to write
0174  * @wbc: Write-back control
0175  *
0176  * Used for both ordered and writeback modes.
0177  */
0178 static int gfs2_writepages(struct address_space *mapping,
0179                struct writeback_control *wbc)
0180 {
0181     struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
0182     struct iomap_writepage_ctx wpc = { };
0183     int ret;
0184 
0185     /*
0186      * Even if we didn't write any pages here, we might still be holding
0187      * dirty pages in the ail. We forcibly flush the ail because we don't
0188      * want balance_dirty_pages() to loop indefinitely trying to write out
0189      * pages held in the ail that it can't find.
0190      */
0191     ret = iomap_writepages(mapping, wbc, &wpc, &gfs2_writeback_ops);
0192     if (ret == 0)
0193         set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
0194     return ret;
0195 }
0196 
0197 /**
0198  * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
0199  * @mapping: The mapping
0200  * @wbc: The writeback control
0201  * @pvec: The vector of pages
0202  * @nr_pages: The number of pages to write
0203  * @done_index: Page index
0204  *
0205  * Returns: non-zero if loop should terminate, zero otherwise
0206  */
0207 
0208 static int gfs2_write_jdata_pagevec(struct address_space *mapping,
0209                     struct writeback_control *wbc,
0210                     struct pagevec *pvec,
0211                     int nr_pages,
0212                     pgoff_t *done_index)
0213 {
0214     struct inode *inode = mapping->host;
0215     struct gfs2_sbd *sdp = GFS2_SB(inode);
0216     unsigned nrblocks = nr_pages * (PAGE_SIZE >> inode->i_blkbits);
0217     int i;
0218     int ret;
0219 
0220     ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
0221     if (ret < 0)
0222         return ret;
0223 
0224     for(i = 0; i < nr_pages; i++) {
0225         struct page *page = pvec->pages[i];
0226 
0227         *done_index = page->index;
0228 
0229         lock_page(page);
0230 
0231         if (unlikely(page->mapping != mapping)) {
0232 continue_unlock:
0233             unlock_page(page);
0234             continue;
0235         }
0236 
0237         if (!PageDirty(page)) {
0238             /* someone wrote it for us */
0239             goto continue_unlock;
0240         }
0241 
0242         if (PageWriteback(page)) {
0243             if (wbc->sync_mode != WB_SYNC_NONE)
0244                 wait_on_page_writeback(page);
0245             else
0246                 goto continue_unlock;
0247         }
0248 
0249         BUG_ON(PageWriteback(page));
0250         if (!clear_page_dirty_for_io(page))
0251             goto continue_unlock;
0252 
0253         trace_wbc_writepage(wbc, inode_to_bdi(inode));
0254 
0255         ret = __gfs2_jdata_writepage(page, wbc);
0256         if (unlikely(ret)) {
0257             if (ret == AOP_WRITEPAGE_ACTIVATE) {
0258                 unlock_page(page);
0259                 ret = 0;
0260             } else {
0261 
0262                 /*
0263                  * done_index is set past this page,
0264                  * so media errors will not choke
0265                  * background writeout for the entire
0266                  * file. This has consequences for
0267                  * range_cyclic semantics (ie. it may
0268                  * not be suitable for data integrity
0269                  * writeout).
0270                  */
0271                 *done_index = page->index + 1;
0272                 ret = 1;
0273                 break;
0274             }
0275         }
0276 
0277         /*
0278          * We stop writing back only if we are not doing
0279          * integrity sync. In case of integrity sync we have to
0280          * keep going until we have written all the pages
0281          * we tagged for writeback prior to entering this loop.
0282          */
0283         if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
0284             ret = 1;
0285             break;
0286         }
0287 
0288     }
0289     gfs2_trans_end(sdp);
0290     return ret;
0291 }
0292 
0293 /**
0294  * gfs2_write_cache_jdata - Like write_cache_pages but different
0295  * @mapping: The mapping to write
0296  * @wbc: The writeback control
0297  *
0298  * The reason that we use our own function here is that we need to
0299  * start transactions before we grab page locks. This allows us
0300  * to get the ordering right.
0301  */
0302 
0303 static int gfs2_write_cache_jdata(struct address_space *mapping,
0304                   struct writeback_control *wbc)
0305 {
0306     int ret = 0;
0307     int done = 0;
0308     struct pagevec pvec;
0309     int nr_pages;
0310     pgoff_t writeback_index;
0311     pgoff_t index;
0312     pgoff_t end;
0313     pgoff_t done_index;
0314     int cycled;
0315     int range_whole = 0;
0316     xa_mark_t tag;
0317 
0318     pagevec_init(&pvec);
0319     if (wbc->range_cyclic) {
0320         writeback_index = mapping->writeback_index; /* prev offset */
0321         index = writeback_index;
0322         if (index == 0)
0323             cycled = 1;
0324         else
0325             cycled = 0;
0326         end = -1;
0327     } else {
0328         index = wbc->range_start >> PAGE_SHIFT;
0329         end = wbc->range_end >> PAGE_SHIFT;
0330         if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
0331             range_whole = 1;
0332         cycled = 1; /* ignore range_cyclic tests */
0333     }
0334     if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
0335         tag = PAGECACHE_TAG_TOWRITE;
0336     else
0337         tag = PAGECACHE_TAG_DIRTY;
0338 
0339 retry:
0340     if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
0341         tag_pages_for_writeback(mapping, index, end);
0342     done_index = index;
0343     while (!done && (index <= end)) {
0344         nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
0345                 tag);
0346         if (nr_pages == 0)
0347             break;
0348 
0349         ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, &done_index);
0350         if (ret)
0351             done = 1;
0352         if (ret > 0)
0353             ret = 0;
0354         pagevec_release(&pvec);
0355         cond_resched();
0356     }
0357 
0358     if (!cycled && !done) {
0359         /*
0360          * range_cyclic:
0361          * We hit the last page and there is more work to be done: wrap
0362          * back to the start of the file
0363          */
0364         cycled = 1;
0365         index = 0;
0366         end = writeback_index - 1;
0367         goto retry;
0368     }
0369 
0370     if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
0371         mapping->writeback_index = done_index;
0372 
0373     return ret;
0374 }
0375 
0376 
0377 /**
0378  * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
0379  * @mapping: The mapping to write
0380  * @wbc: The writeback control
0381  * 
0382  */
0383 
0384 static int gfs2_jdata_writepages(struct address_space *mapping,
0385                  struct writeback_control *wbc)
0386 {
0387     struct gfs2_inode *ip = GFS2_I(mapping->host);
0388     struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
0389     int ret;
0390 
0391     ret = gfs2_write_cache_jdata(mapping, wbc);
0392     if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
0393         gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
0394                    GFS2_LFC_JDATA_WPAGES);
0395         ret = gfs2_write_cache_jdata(mapping, wbc);
0396     }
0397     return ret;
0398 }
0399 
0400 /**
0401  * stuffed_readpage - Fill in a Linux page with stuffed file data
0402  * @ip: the inode
0403  * @page: the page
0404  *
0405  * Returns: errno
0406  */
0407 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
0408 {
0409     struct buffer_head *dibh;
0410     u64 dsize = i_size_read(&ip->i_inode);
0411     void *kaddr;
0412     int error;
0413 
0414     /*
0415      * Due to the order of unstuffing files and ->fault(), we can be
0416      * asked for a zero page in the case of a stuffed file being extended,
0417      * so we need to supply one here. It doesn't happen often.
0418      */
0419     if (unlikely(page->index)) {
0420         zero_user(page, 0, PAGE_SIZE);
0421         SetPageUptodate(page);
0422         return 0;
0423     }
0424 
0425     error = gfs2_meta_inode_buffer(ip, &dibh);
0426     if (error)
0427         return error;
0428 
0429     kaddr = kmap_atomic(page);
0430     if (dsize > gfs2_max_stuffed_size(ip))
0431         dsize = gfs2_max_stuffed_size(ip);
0432     memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
0433     memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
0434     kunmap_atomic(kaddr);
0435     flush_dcache_page(page);
0436     brelse(dibh);
0437     SetPageUptodate(page);
0438 
0439     return 0;
0440 }
0441 
0442 /**
0443  * gfs2_read_folio - read a folio from a file
0444  * @file: The file to read
0445  * @folio: The folio in the file
0446  */
0447 static int gfs2_read_folio(struct file *file, struct folio *folio)
0448 {
0449     struct inode *inode = folio->mapping->host;
0450     struct gfs2_inode *ip = GFS2_I(inode);
0451     struct gfs2_sbd *sdp = GFS2_SB(inode);
0452     int error;
0453 
0454     if (!gfs2_is_jdata(ip) ||
0455         (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
0456         error = iomap_read_folio(folio, &gfs2_iomap_ops);
0457     } else if (gfs2_is_stuffed(ip)) {
0458         error = stuffed_readpage(ip, &folio->page);
0459         folio_unlock(folio);
0460     } else {
0461         error = mpage_read_folio(folio, gfs2_block_map);
0462     }
0463 
0464     if (unlikely(gfs2_withdrawn(sdp)))
0465         return -EIO;
0466 
0467     return error;
0468 }
0469 
0470 /**
0471  * gfs2_internal_read - read an internal file
0472  * @ip: The gfs2 inode
0473  * @buf: The buffer to fill
0474  * @pos: The file position
0475  * @size: The amount to read
0476  *
0477  */
0478 
0479 int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
0480                        unsigned size)
0481 {
0482     struct address_space *mapping = ip->i_inode.i_mapping;
0483     unsigned long index = *pos >> PAGE_SHIFT;
0484     unsigned offset = *pos & (PAGE_SIZE - 1);
0485     unsigned copied = 0;
0486     unsigned amt;
0487     struct page *page;
0488     void *p;
0489 
0490     do {
0491         amt = size - copied;
0492         if (offset + size > PAGE_SIZE)
0493             amt = PAGE_SIZE - offset;
0494         page = read_cache_page(mapping, index, gfs2_read_folio, NULL);
0495         if (IS_ERR(page))
0496             return PTR_ERR(page);
0497         p = kmap_atomic(page);
0498         memcpy(buf + copied, p + offset, amt);
0499         kunmap_atomic(p);
0500         put_page(page);
0501         copied += amt;
0502         index++;
0503         offset = 0;
0504     } while(copied < size);
0505     (*pos) += size;
0506     return size;
0507 }
0508 
0509 /**
0510  * gfs2_readahead - Read a bunch of pages at once
0511  * @rac: Read-ahead control structure
0512  *
0513  * Some notes:
0514  * 1. This is only for readahead, so we can simply ignore any things
0515  *    which are slightly inconvenient (such as locking conflicts between
0516  *    the page lock and the glock) and return having done no I/O. Its
0517  *    obviously not something we'd want to do on too regular a basis.
0518  *    Any I/O we ignore at this time will be done via readpage later.
0519  * 2. We don't handle stuffed files here we let readpage do the honours.
0520  * 3. mpage_readahead() does most of the heavy lifting in the common case.
0521  * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
0522  */
0523 
0524 static void gfs2_readahead(struct readahead_control *rac)
0525 {
0526     struct inode *inode = rac->mapping->host;
0527     struct gfs2_inode *ip = GFS2_I(inode);
0528 
0529     if (gfs2_is_stuffed(ip))
0530         ;
0531     else if (gfs2_is_jdata(ip))
0532         mpage_readahead(rac, gfs2_block_map);
0533     else
0534         iomap_readahead(rac, &gfs2_iomap_ops);
0535 }
0536 
0537 /**
0538  * adjust_fs_space - Adjusts the free space available due to gfs2_grow
0539  * @inode: the rindex inode
0540  */
0541 void adjust_fs_space(struct inode *inode)
0542 {
0543     struct gfs2_sbd *sdp = GFS2_SB(inode);
0544     struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
0545     struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
0546     struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
0547     struct buffer_head *m_bh;
0548     u64 fs_total, new_free;
0549 
0550     if (gfs2_trans_begin(sdp, 2 * RES_STATFS, 0) != 0)
0551         return;
0552 
0553     /* Total up the file system space, according to the latest rindex. */
0554     fs_total = gfs2_ri_total(sdp);
0555     if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
0556         goto out;
0557 
0558     spin_lock(&sdp->sd_statfs_spin);
0559     gfs2_statfs_change_in(m_sc, m_bh->b_data +
0560                   sizeof(struct gfs2_dinode));
0561     if (fs_total > (m_sc->sc_total + l_sc->sc_total))
0562         new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
0563     else
0564         new_free = 0;
0565     spin_unlock(&sdp->sd_statfs_spin);
0566     fs_warn(sdp, "File system extended by %llu blocks.\n",
0567         (unsigned long long)new_free);
0568     gfs2_statfs_change(sdp, new_free, new_free, 0);
0569 
0570     update_statfs(sdp, m_bh);
0571     brelse(m_bh);
0572 out:
0573     sdp->sd_rindex_uptodate = 0;
0574     gfs2_trans_end(sdp);
0575 }
0576 
0577 static bool jdata_dirty_folio(struct address_space *mapping,
0578         struct folio *folio)
0579 {
0580     if (current->journal_info)
0581         folio_set_checked(folio);
0582     return block_dirty_folio(mapping, folio);
0583 }
0584 
0585 /**
0586  * gfs2_bmap - Block map function
0587  * @mapping: Address space info
0588  * @lblock: The block to map
0589  *
0590  * Returns: The disk address for the block or 0 on hole or error
0591  */
0592 
0593 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
0594 {
0595     struct gfs2_inode *ip = GFS2_I(mapping->host);
0596     struct gfs2_holder i_gh;
0597     sector_t dblock = 0;
0598     int error;
0599 
0600     error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
0601     if (error)
0602         return 0;
0603 
0604     if (!gfs2_is_stuffed(ip))
0605         dblock = iomap_bmap(mapping, lblock, &gfs2_iomap_ops);
0606 
0607     gfs2_glock_dq_uninit(&i_gh);
0608 
0609     return dblock;
0610 }
0611 
0612 static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
0613 {
0614     struct gfs2_bufdata *bd;
0615 
0616     lock_buffer(bh);
0617     gfs2_log_lock(sdp);
0618     clear_buffer_dirty(bh);
0619     bd = bh->b_private;
0620     if (bd) {
0621         if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
0622             list_del_init(&bd->bd_list);
0623         else {
0624             spin_lock(&sdp->sd_ail_lock);
0625             gfs2_remove_from_journal(bh, REMOVE_JDATA);
0626             spin_unlock(&sdp->sd_ail_lock);
0627         }
0628     }
0629     bh->b_bdev = NULL;
0630     clear_buffer_mapped(bh);
0631     clear_buffer_req(bh);
0632     clear_buffer_new(bh);
0633     gfs2_log_unlock(sdp);
0634     unlock_buffer(bh);
0635 }
0636 
0637 static void gfs2_invalidate_folio(struct folio *folio, size_t offset,
0638                 size_t length)
0639 {
0640     struct gfs2_sbd *sdp = GFS2_SB(folio->mapping->host);
0641     size_t stop = offset + length;
0642     int partial_page = (offset || length < folio_size(folio));
0643     struct buffer_head *bh, *head;
0644     unsigned long pos = 0;
0645 
0646     BUG_ON(!folio_test_locked(folio));
0647     if (!partial_page)
0648         folio_clear_checked(folio);
0649     head = folio_buffers(folio);
0650     if (!head)
0651         goto out;
0652 
0653     bh = head;
0654     do {
0655         if (pos + bh->b_size > stop)
0656             return;
0657 
0658         if (offset <= pos)
0659             gfs2_discard(sdp, bh);
0660         pos += bh->b_size;
0661         bh = bh->b_this_page;
0662     } while (bh != head);
0663 out:
0664     if (!partial_page)
0665         filemap_release_folio(folio, 0);
0666 }
0667 
0668 /**
0669  * gfs2_release_folio - free the metadata associated with a folio
0670  * @folio: the folio that's being released
0671  * @gfp_mask: passed from Linux VFS, ignored by us
0672  *
0673  * Calls try_to_free_buffers() to free the buffers and put the folio if the
0674  * buffers can be released.
0675  *
0676  * Returns: true if the folio was put or else false
0677  */
0678 
0679 bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
0680 {
0681     struct address_space *mapping = folio->mapping;
0682     struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
0683     struct buffer_head *bh, *head;
0684     struct gfs2_bufdata *bd;
0685 
0686     head = folio_buffers(folio);
0687     if (!head)
0688         return false;
0689 
0690     /*
0691      * mm accommodates an old ext3 case where clean folios might
0692      * not have had the dirty bit cleared.  Thus, it can send actual
0693      * dirty folios to ->release_folio() via shrink_active_list().
0694      *
0695      * As a workaround, we skip folios that contain dirty buffers
0696      * below.  Once ->release_folio isn't called on dirty folios
0697      * anymore, we can warn on dirty buffers like we used to here
0698      * again.
0699      */
0700 
0701     gfs2_log_lock(sdp);
0702     bh = head;
0703     do {
0704         if (atomic_read(&bh->b_count))
0705             goto cannot_release;
0706         bd = bh->b_private;
0707         if (bd && bd->bd_tr)
0708             goto cannot_release;
0709         if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
0710             goto cannot_release;
0711         bh = bh->b_this_page;
0712     } while (bh != head);
0713 
0714     bh = head;
0715     do {
0716         bd = bh->b_private;
0717         if (bd) {
0718             gfs2_assert_warn(sdp, bd->bd_bh == bh);
0719             bd->bd_bh = NULL;
0720             bh->b_private = NULL;
0721             /*
0722              * The bd may still be queued as a revoke, in which
0723              * case we must not dequeue nor free it.
0724              */
0725             if (!bd->bd_blkno && !list_empty(&bd->bd_list))
0726                 list_del_init(&bd->bd_list);
0727             if (list_empty(&bd->bd_list))
0728                 kmem_cache_free(gfs2_bufdata_cachep, bd);
0729         }
0730 
0731         bh = bh->b_this_page;
0732     } while (bh != head);
0733     gfs2_log_unlock(sdp);
0734 
0735     return try_to_free_buffers(folio);
0736 
0737 cannot_release:
0738     gfs2_log_unlock(sdp);
0739     return false;
0740 }
0741 
0742 static const struct address_space_operations gfs2_aops = {
0743     .writepages = gfs2_writepages,
0744     .read_folio = gfs2_read_folio,
0745     .readahead = gfs2_readahead,
0746     .dirty_folio = filemap_dirty_folio,
0747     .release_folio = iomap_release_folio,
0748     .invalidate_folio = iomap_invalidate_folio,
0749     .bmap = gfs2_bmap,
0750     .direct_IO = noop_direct_IO,
0751     .migrate_folio = filemap_migrate_folio,
0752     .is_partially_uptodate = iomap_is_partially_uptodate,
0753     .error_remove_page = generic_error_remove_page,
0754 };
0755 
0756 static const struct address_space_operations gfs2_jdata_aops = {
0757     .writepage = gfs2_jdata_writepage,
0758     .writepages = gfs2_jdata_writepages,
0759     .read_folio = gfs2_read_folio,
0760     .readahead = gfs2_readahead,
0761     .dirty_folio = jdata_dirty_folio,
0762     .bmap = gfs2_bmap,
0763     .invalidate_folio = gfs2_invalidate_folio,
0764     .release_folio = gfs2_release_folio,
0765     .is_partially_uptodate = block_is_partially_uptodate,
0766     .error_remove_page = generic_error_remove_page,
0767 };
0768 
0769 void gfs2_set_aops(struct inode *inode)
0770 {
0771     if (gfs2_is_jdata(GFS2_I(inode)))
0772         inode->i_mapping->a_ops = &gfs2_jdata_aops;
0773     else
0774         inode->i_mapping->a_ops = &gfs2_aops;
0775 }