0001
0002
0003
0004
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
0063
0064
0065
0066
0067
0068
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
0086
0087
0088
0089
0090
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
0102
0103
0104
0105
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
0117
0118
0119
0120
0121
0122
0123
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
0145
0146
0147
0148
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
0173
0174
0175
0176
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
0187
0188
0189
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
0199
0200
0201
0202
0203
0204
0205
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
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
0264
0265
0266
0267
0268
0269
0270
0271 *done_index = page->index + 1;
0272 ret = 1;
0273 break;
0274 }
0275 }
0276
0277
0278
0279
0280
0281
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
0295
0296
0297
0298
0299
0300
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;
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;
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
0361
0362
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
0379
0380
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
0402
0403
0404
0405
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
0416
0417
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
0444
0445
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
0472
0473
0474
0475
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
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
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
0539
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
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
0587
0588
0589
0590
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
0670
0671
0672
0673
0674
0675
0676
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
0692
0693
0694
0695
0696
0697
0698
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
0723
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 }