Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * page.c - buffer/page management specific to NILFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Written by Ryusuke Konishi and Seiji Kihara.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/page-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "nilfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "page.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "mdt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define NILFS_BUFFER_INHERENT_BITS					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	(BIT(BH_Uptodate) | BIT(BH_Mapped) | BIT(BH_NILFS_Node) |	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	 BIT(BH_NILFS_Volatile) | BIT(BH_NILFS_Checked))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) __nilfs_get_page_block(struct page *page, unsigned long block, pgoff_t index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		       int blkbits, unsigned long b_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned long first_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (!page_has_buffers(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		create_empty_buffers(page, 1 << blkbits, b_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	first_block = (unsigned long)index << (PAGE_SHIFT - blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	bh = nilfs_page_get_nth_block(page, block - first_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	touch_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct buffer_head *nilfs_grab_buffer(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				      struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 				      unsigned long blkoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				      unsigned long b_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int blkbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	pgoff_t index = blkoff >> (PAGE_SHIFT - blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	page = grab_cache_page(mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (unlikely(!page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	bh = __nilfs_get_page_block(page, blkoff, index, blkbits, b_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (unlikely(!bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * nilfs_forget_buffer - discard dirty state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * @bh: buffer head of the buffer to be discarded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) void nilfs_forget_buffer(struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct page *page = bh->b_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	const unsigned long clear_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		(BIT(BH_Uptodate) | BIT(BH_Dirty) | BIT(BH_Mapped) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		 BIT(BH_Async_Write) | BIT(BH_NILFS_Volatile) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		 BIT(BH_NILFS_Checked) | BIT(BH_NILFS_Redirected));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	set_mask_bits(&bh->b_state, clear_bits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (nilfs_page_buffers_clean(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		__nilfs_clear_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	bh->b_blocknr = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ClearPageMappedToDisk(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * nilfs_copy_buffer -- copy buffer data and flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * @dbh: destination buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * @sbh: source buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) void nilfs_copy_buffer(struct buffer_head *dbh, struct buffer_head *sbh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	void *kaddr0, *kaddr1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	unsigned long bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct page *spage = sbh->b_page, *dpage = dbh->b_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	kaddr0 = kmap_atomic(spage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	kaddr1 = kmap_atomic(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	memcpy(kaddr1 + bh_offset(dbh), kaddr0 + bh_offset(sbh), sbh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	kunmap_atomic(kaddr1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	kunmap_atomic(kaddr0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	dbh->b_state = sbh->b_state & NILFS_BUFFER_INHERENT_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	dbh->b_blocknr = sbh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	dbh->b_bdev = sbh->b_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	bh = dbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	bits = sbh->b_state & (BIT(BH_Uptodate) | BIT(BH_Mapped));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	while ((bh = bh->b_this_page) != dbh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		bits &= bh->b_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (bits & BIT(BH_Uptodate))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		SetPageUptodate(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		ClearPageUptodate(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (bits & BIT(BH_Mapped))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		SetPageMappedToDisk(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		ClearPageMappedToDisk(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * nilfs_page_buffers_clean - check if a page has dirty buffers or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * @page: page to be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * nilfs_page_buffers_clean() returns zero if the page has dirty buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * Otherwise, it returns non-zero value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int nilfs_page_buffers_clean(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct buffer_head *bh, *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	bh = head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		if (buffer_dirty(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	} while (bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void nilfs_page_bug(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct address_space *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	unsigned long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (unlikely(!page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		printk(KERN_CRIT "NILFS_PAGE_BUG(NULL)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	m = page->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ino = m ? m->host->i_ino : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	printk(KERN_CRIT "NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	       "mapping=%p ino=%lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	       page, page_ref_count(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	       (unsigned long long)page->index, page->flags, m, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		struct buffer_head *bh, *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		bh = head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			printk(KERN_CRIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			       " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			       i++, bh, atomic_read(&bh->b_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			       (unsigned long long)bh->b_blocknr, bh->b_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		} while (bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * nilfs_copy_page -- copy the page with buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * @dst: destination page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * @src: source page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * @copy_dirty: flag whether to copy dirty states on the page's buffer heads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * This function is for both data pages and btnode pages.  The dirty flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * should be treated by caller.  The page must not be under i/o.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * Both src and dst page must be locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static void nilfs_copy_page(struct page *dst, struct page *src, int copy_dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct buffer_head *dbh, *dbufs, *sbh, *sbufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	unsigned long mask = NILFS_BUFFER_INHERENT_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	BUG_ON(PageWriteback(dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	sbh = sbufs = page_buffers(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (!page_has_buffers(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		create_empty_buffers(dst, sbh->b_size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (copy_dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		mask |= BIT(BH_Dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	dbh = dbufs = page_buffers(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		lock_buffer(sbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		lock_buffer(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		dbh->b_state = sbh->b_state & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		dbh->b_blocknr = sbh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		dbh->b_bdev = sbh->b_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		sbh = sbh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		dbh = dbh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	} while (dbh != dbufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	copy_highpage(dst, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (PageUptodate(src) && !PageUptodate(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		SetPageUptodate(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	else if (!PageUptodate(src) && PageUptodate(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		ClearPageUptodate(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (PageMappedToDisk(src) && !PageMappedToDisk(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		SetPageMappedToDisk(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	else if (!PageMappedToDisk(src) && PageMappedToDisk(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ClearPageMappedToDisk(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		unlock_buffer(sbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		unlock_buffer(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		sbh = sbh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		dbh = dbh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	} while (dbh != dbufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) int nilfs_copy_dirty_pages(struct address_space *dmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			   struct address_space *smap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	pgoff_t index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!pagevec_lookup_tag(&pvec, smap, &index, PAGECACHE_TAG_DIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	for (i = 0; i < pagevec_count(&pvec); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		struct page *page = pvec.pages[i], *dpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		if (unlikely(!PageDirty(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			NILFS_PAGE_BUG(page, "inconsistent dirty state");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		dpage = grab_cache_page(dmap, page->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (unlikely(!dpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			/* No empty page is added to the page cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (unlikely(!page_has_buffers(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			NILFS_PAGE_BUG(page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				       "found empty page in dat page cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		nilfs_copy_page(dpage, page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		__set_page_dirty_nobuffers(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		unlock_page(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		put_page(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (likely(!err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * @dmap: destination page cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * @smap: source page cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * No pages must be added to the cache during this process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * This must be ensured by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) void nilfs_copy_back_pages(struct address_space *dmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			   struct address_space *smap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	unsigned int i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	pgoff_t index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	n = pagevec_lookup(&pvec, smap, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	for (i = 0; i < pagevec_count(&pvec); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		struct page *page = pvec.pages[i], *dpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		pgoff_t offset = page->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		dpage = find_lock_page(dmap, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (dpage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			/* overwrite existing page in the destination cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			WARN_ON(PageDirty(dpage));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			nilfs_copy_page(dpage, page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			unlock_page(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			put_page(dpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			/* Do we not need to remove page from smap here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			struct page *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			/* move the page to the destination cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			xa_lock_irq(&smap->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			p = __xa_erase(&smap->i_pages, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			WARN_ON(page != p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			smap->nrpages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			xa_unlock_irq(&smap->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			xa_lock_irq(&dmap->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			p = __xa_store(&dmap->i_pages, offset, page, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			if (unlikely(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				/* Probably -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 				page->mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 				page->mapping = dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 				dmap->nrpages++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				if (PageDirty(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 					__xa_set_mark(&dmap->i_pages, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 							PAGECACHE_TAG_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			xa_unlock_irq(&dmap->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  * nilfs_clear_dirty_pages - discard dirty pages in address space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  * @mapping: address space with dirty pages for discarding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  * @silent: suppress [true] or print [false] warning messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) void nilfs_clear_dirty_pages(struct address_space *mapping, bool silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	pgoff_t index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	while (pagevec_lookup_tag(&pvec, mapping, &index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					PAGECACHE_TAG_DIRTY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		for (i = 0; i < pagevec_count(&pvec); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			nilfs_clear_dirty_page(page, silent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * nilfs_clear_dirty_page - discard dirty page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * @page: dirty page that will be discarded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  * @silent: suppress [true] or print [false] warning messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) void nilfs_clear_dirty_page(struct page *page, bool silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	BUG_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		nilfs_warn(sb, "discard dirty page: offset=%lld, ino=%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			   page_offset(page), inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	ClearPageMappedToDisk(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		struct buffer_head *bh, *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		const unsigned long clear_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			(BIT(BH_Uptodate) | BIT(BH_Dirty) | BIT(BH_Mapped) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			 BIT(BH_Async_Write) | BIT(BH_NILFS_Volatile) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			 BIT(BH_NILFS_Checked) | BIT(BH_NILFS_Redirected));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		bh = head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 				nilfs_warn(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 					   "discard dirty block: blocknr=%llu, size=%zu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 					   (u64)bh->b_blocknr, bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			set_mask_bits(&bh->b_state, clear_bits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		} while (bh = bh->b_this_page, bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	__nilfs_clear_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) unsigned int nilfs_page_count_clean_buffers(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 					    unsigned int from, unsigned int to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	unsigned int block_start, block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct buffer_head *bh, *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	unsigned int nc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	for (bh = head = page_buffers(page), block_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	     bh != head || !block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	     block_start = block_end, bh = bh->b_this_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		block_end = block_start + bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		if (block_end > from && block_start < to && !buffer_dirty(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			nc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	return nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) void nilfs_mapping_init(struct address_space *mapping, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	mapping->host = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	mapping->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	mapping_set_gfp_mask(mapping, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	mapping->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	mapping->a_ops = &empty_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * NILFS2 needs clear_page_dirty() in the following two cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  *    page dirty flags when it copies back pages from the shadow cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  *    (gcdat->{i_mapping,i_btnode_cache}) to its original cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  *    (dat->{i_mapping,i_btnode_cache}).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  * 2) Some B-tree operations like insertion or deletion may dispose buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)  *    in dirty state, and this needs to cancel the dirty state of their pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int __nilfs_clear_page_dirty(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	struct address_space *mapping = page->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		xa_lock_irq(&mapping->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		if (test_bit(PG_dirty, &page->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			__xa_clear_mark(&mapping->i_pages, page_index(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 					     PAGECACHE_TAG_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			xa_unlock_irq(&mapping->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			return clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		xa_unlock_irq(&mapping->i_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return TestClearPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)  * nilfs_find_uncommitted_extent - find extent of uncommitted data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)  * @inode: inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)  * @start_blk: start block offset (in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)  * @blkoff: start offset of the found extent (out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)  * This function searches an extent of buffers marked "delayed" which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  * starts from a block offset equal to or larger than @start_blk.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * such an extent was found, this will store the start offset in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  * @blkoff and return its length in blocks.  Otherwise, zero is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 					    sector_t start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 					    sector_t *blkoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	unsigned int nblocks_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	unsigned long length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	sector_t b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (inode->i_mapping->nrpages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	index = start_blk >> (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	nblocks_in_page = 1U << (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	pvec.nr = find_get_pages_contig(inode->i_mapping, index, PAGEVEC_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 					pvec.pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (pvec.nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	if (length > 0 && pvec.pages[0]->index > index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	b = pvec.pages[0]->index << (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		if (page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			struct buffer_head *bh, *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			bh = head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 				if (b < start_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 				if (buffer_delay(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 					if (length == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 						*blkoff = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 					length++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 				} else if (length > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 					goto out_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			} while (++b, bh = bh->b_this_page, bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			if (length > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 				goto out_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			b += nblocks_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	} while (++i < pagevec_count(&pvec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	index = page->index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) out_locked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }