Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nilfs2: fix issue with flush kernel thread after remount in RO mode because of driver's internal error or metadata corruption

The NILFS2 driver remounts itself in RO mode in the case of discovering
metadata corruption (for example, discovering a broken bmap). But
usually, this takes place when there have been file system operations
before remounting in RO mode.

Thereby, NILFS2 driver can be in RO mode with presence of dirty pages in
modified inodes' address spaces. It results in flush kernel thread's
infinite trying to flush dirty pages in RO mode. As a result, it is
possible to see such side effects as: (1) flush kernel thread occupies
50% - 99% of CPU time; (2) system can't be shutdowned without manual
power switch off.

SYMPTOMS:
(1) System log contains error message: "Remounting filesystem read-only".
(2) The flush kernel thread occupies 50% - 99% of CPU time.
(3) The system can't be shutdowned without manual power switch off.

REPRODUCTION PATH:
(1) Create volume group with name "unencrypted" by means of vgcreate utility.
(2) Run script (prepared by Anthony Doggett <Anthony2486@interfaces.org.uk>):

----------------[BEGIN SCRIPT]--------------------
#!/bin/bash

VG=unencrypted
#apt-get install nilfs-tools darcs
lvcreate --size 2G --name ntest $VG
mkfs.nilfs2 -b 1024 -B 8192 /dev/mapper/$VG-ntest
mkdir /var/tmp/n
mkdir /var/tmp/n/ntest
mount /dev/mapper/$VG-ntest /var/tmp/n/ntest
mkdir /var/tmp/n/ntest/thedir
cd /var/tmp/n/ntest/thedir
sleep 2
date
darcs init
sleep 2
dmesg|tail -n 5
date
darcs whatsnew || true
date
sleep 2
dmesg|tail -n 5
----------------[END SCRIPT]--------------------

(3) Try to shutdown the system.

REPRODUCIBILITY: 100%

FIX:

This patch implements checking mount state of NILFS2 driver in
nilfs_writepage(), nilfs_writepages() and nilfs_mdt_write_page()
methods. If it is detected the RO mount state then all dirty pages are
simply discarded with warning messages is written in system log.

[akpm@linux-foundation.org: fix printk warning]
Signed-off-by: Vyacheslav Dubeyko <slava@dubeyko.com>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Cc: Anthony Doggett <Anthony2486@interfaces.org.uk>
Cc: ARAI Shun-ichi <hermes@ceres.dti.ne.jp>
Cc: Piotr Szymaniak <szarpaj@grubelek.pl>
Cc: Zahid Chowdhury <zahid.chowdhury@starsolutions.com>
Cc: Elmer Zhang <freeboy6716@gmail.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Vyacheslav Dubeyko and committed by
Linus Torvalds
8c26c4e2 9151b398

+86 -23
+17
fs/nilfs2/inode.c
··· 175 175 struct inode *inode = mapping->host; 176 176 int err = 0; 177 177 178 + if (inode->i_sb->s_flags & MS_RDONLY) { 179 + nilfs_clear_dirty_pages(mapping, false); 180 + return -EROFS; 181 + } 182 + 178 183 if (wbc->sync_mode == WB_SYNC_ALL) 179 184 err = nilfs_construct_dsync_segment(inode->i_sb, inode, 180 185 wbc->range_start, ··· 191 186 { 192 187 struct inode *inode = page->mapping->host; 193 188 int err; 189 + 190 + if (inode && (inode->i_sb->s_flags & MS_RDONLY)) { 191 + /* 192 + * It means that filesystem was remounted in read-only 193 + * mode because of error or metadata corruption. But we 194 + * have dirty pages that try to be flushed in background. 195 + * So, here we simply discard this dirty page. 196 + */ 197 + nilfs_clear_dirty_page(page, false); 198 + unlock_page(page); 199 + return -EROFS; 200 + } 194 201 195 202 redirty_page_for_writepage(wbc, page); 196 203 unlock_page(page);
+15 -4
fs/nilfs2/mdt.c
··· 375 375 static int 376 376 nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc) 377 377 { 378 - struct inode *inode; 378 + struct inode *inode = page->mapping->host; 379 379 struct super_block *sb; 380 380 int err = 0; 381 + 382 + if (inode && (inode->i_sb->s_flags & MS_RDONLY)) { 383 + /* 384 + * It means that filesystem was remounted in read-only 385 + * mode because of error or metadata corruption. But we 386 + * have dirty pages that try to be flushed in background. 387 + * So, here we simply discard this dirty page. 388 + */ 389 + nilfs_clear_dirty_page(page, false); 390 + unlock_page(page); 391 + return -EROFS; 392 + } 381 393 382 394 redirty_page_for_writepage(wbc, page); 383 395 unlock_page(page); 384 396 385 - inode = page->mapping->host; 386 397 if (!inode) 387 398 return 0; 388 399 ··· 572 561 if (mi->mi_palloc_cache) 573 562 nilfs_palloc_clear_cache(inode); 574 563 575 - nilfs_clear_dirty_pages(inode->i_mapping); 564 + nilfs_clear_dirty_pages(inode->i_mapping, true); 576 565 nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data); 577 566 578 - nilfs_clear_dirty_pages(&ii->i_btnode_cache); 567 + nilfs_clear_dirty_pages(&ii->i_btnode_cache, true); 579 568 nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes); 580 569 581 570 nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
+52 -18
fs/nilfs2/page.c
··· 370 370 goto repeat; 371 371 } 372 372 373 - void nilfs_clear_dirty_pages(struct address_space *mapping) 373 + /** 374 + * nilfs_clear_dirty_pages - discard dirty pages in address space 375 + * @mapping: address space with dirty pages for discarding 376 + * @silent: suppress [true] or print [false] warning messages 377 + */ 378 + void nilfs_clear_dirty_pages(struct address_space *mapping, bool silent) 374 379 { 375 380 struct pagevec pvec; 376 381 unsigned int i; ··· 387 382 PAGEVEC_SIZE)) { 388 383 for (i = 0; i < pagevec_count(&pvec); i++) { 389 384 struct page *page = pvec.pages[i]; 390 - struct buffer_head *bh, *head; 391 385 392 386 lock_page(page); 393 - ClearPageUptodate(page); 394 - ClearPageMappedToDisk(page); 395 - bh = head = page_buffers(page); 396 - do { 397 - lock_buffer(bh); 398 - clear_buffer_dirty(bh); 399 - clear_buffer_nilfs_volatile(bh); 400 - clear_buffer_nilfs_checked(bh); 401 - clear_buffer_nilfs_redirected(bh); 402 - clear_buffer_uptodate(bh); 403 - clear_buffer_mapped(bh); 404 - unlock_buffer(bh); 405 - bh = bh->b_this_page; 406 - } while (bh != head); 407 - 408 - __nilfs_clear_page_dirty(page); 387 + nilfs_clear_dirty_page(page, silent); 409 388 unlock_page(page); 410 389 } 411 390 pagevec_release(&pvec); 412 391 cond_resched(); 413 392 } 393 + } 394 + 395 + /** 396 + * nilfs_clear_dirty_page - discard dirty page 397 + * @page: dirty page that will be discarded 398 + * @silent: suppress [true] or print [false] warning messages 399 + */ 400 + void nilfs_clear_dirty_page(struct page *page, bool silent) 401 + { 402 + struct inode *inode = page->mapping->host; 403 + struct super_block *sb = inode->i_sb; 404 + 405 + BUG_ON(!test_bit(PG_locked, &page->flags)); 406 + 407 + if (!silent) { 408 + nilfs_warning(sb, __func__, 409 + "discard page: offset %lld, ino %lu", 410 + page_offset(page), inode->i_ino); 411 + } 412 + 413 + ClearPageUptodate(page); 414 + ClearPageMappedToDisk(page); 415 + 416 + if (page_has_buffers(page)) { 417 + struct buffer_head *bh, *head; 418 + 419 + bh = head = page_buffers(page); 420 + do { 421 + lock_buffer(bh); 422 + if (!silent) { 423 + nilfs_warning(sb, __func__, 424 + "discard block %llu, size %zu", 425 + (u64)bh->b_blocknr, bh->b_size); 426 + } 427 + clear_buffer_dirty(bh); 428 + clear_buffer_nilfs_volatile(bh); 429 + clear_buffer_nilfs_checked(bh); 430 + clear_buffer_nilfs_redirected(bh); 431 + clear_buffer_uptodate(bh); 432 + clear_buffer_mapped(bh); 433 + unlock_buffer(bh); 434 + } while (bh = bh->b_this_page, bh != head); 435 + } 436 + 437 + __nilfs_clear_page_dirty(page); 414 438 } 415 439 416 440 unsigned nilfs_page_count_clean_buffers(struct page *page,
+2 -1
fs/nilfs2/page.h
··· 55 55 56 56 int nilfs_copy_dirty_pages(struct address_space *, struct address_space *); 57 57 void nilfs_copy_back_pages(struct address_space *, struct address_space *); 58 - void nilfs_clear_dirty_pages(struct address_space *); 58 + void nilfs_clear_dirty_page(struct page *, bool); 59 + void nilfs_clear_dirty_pages(struct address_space *, bool); 59 60 void nilfs_mapping_init(struct address_space *mapping, struct inode *inode, 60 61 struct backing_dev_info *bdi); 61 62 unsigned nilfs_page_count_clean_buffers(struct page *, unsigned, unsigned);