Skip to content

Commit 1e1de38

Browse files
adam900710kdave
authored andcommitted
btrfs: make process_one_page() to handle subpage locking
Introduce a new data inodes specific subpage member, writers, to record how many sectors are under page lock for delalloc writing. This member acts pretty much the same as readers, except it's only for delalloc writes. This is important for delalloc code to trace which page can really be freed, as we have cases like run_delalloc_nocow() where we may exit processing nocow range inside a page, but need to exit to do cow half way. In that case, we need a way to determine if we can really unlock a full page. With the new btrfs_subpage::writers, there is a new requirement: - Page locked by process_one_page() must be unlocked by process_one_page() There are still tons of call sites manually lock and unlock a page, without updating btrfs_subpage::writers. So if we lock a page through process_one_page() then it must be unlocked by process_one_page() to keep btrfs_subpage::writers consistent. This will be handled in next patch. Tested-by: Ritesh Harjani <riteshh@linux.ibm.com> # [ppc64] Tested-by: Anand Jain <anand.jain@oracle.com> # [aarch64] Reviewed-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 9047e31 commit 1e1de38

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
lines changed

fs/btrfs/extent_io.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,14 +1841,18 @@ static int process_one_page(struct btrfs_fs_info *fs_info,
18411841
if (page_ops & PAGE_END_WRITEBACK)
18421842
btrfs_page_clamp_clear_writeback(fs_info, page, start, len);
18431843
if (page_ops & PAGE_LOCK) {
1844-
lock_page(page);
1844+
int ret;
1845+
1846+
ret = btrfs_page_start_writer_lock(fs_info, page, start, len);
1847+
if (ret)
1848+
return ret;
18451849
if (!PageDirty(page) || page->mapping != mapping) {
1846-
unlock_page(page);
1850+
btrfs_page_end_writer_lock(fs_info, page, start, len);
18471851
return -EAGAIN;
18481852
}
18491853
}
18501854
if (page_ops & PAGE_UNLOCK)
1851-
unlock_page(page);
1855+
btrfs_page_end_writer_lock(fs_info, page, start, len);
18521856
return 0;
18531857
}
18541858

fs/btrfs/subpage.c

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
110110
if (!*ret)
111111
return -ENOMEM;
112112
spin_lock_init(&(*ret)->lock);
113-
if (type == BTRFS_SUBPAGE_METADATA)
113+
if (type == BTRFS_SUBPAGE_METADATA) {
114114
atomic_set(&(*ret)->eb_refs, 0);
115-
else
115+
} else {
116116
atomic_set(&(*ret)->readers, 0);
117+
atomic_set(&(*ret)->writers, 0);
118+
}
117119
return 0;
118120
}
119121

@@ -203,6 +205,79 @@ void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
203205
unlock_page(page);
204206
}
205207

208+
static void btrfs_subpage_clamp_range(struct page *page, u64 *start, u32 *len)
209+
{
210+
u64 orig_start = *start;
211+
u32 orig_len = *len;
212+
213+
*start = max_t(u64, page_offset(page), orig_start);
214+
*len = min_t(u64, page_offset(page) + PAGE_SIZE,
215+
orig_start + orig_len) - *start;
216+
}
217+
218+
void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
219+
struct page *page, u64 start, u32 len)
220+
{
221+
struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
222+
const int nbits = (len >> fs_info->sectorsize_bits);
223+
int ret;
224+
225+
btrfs_subpage_assert(fs_info, page, start, len);
226+
227+
ASSERT(atomic_read(&subpage->readers) == 0);
228+
ret = atomic_add_return(nbits, &subpage->writers);
229+
ASSERT(ret == nbits);
230+
}
231+
232+
bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
233+
struct page *page, u64 start, u32 len)
234+
{
235+
struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
236+
const int nbits = (len >> fs_info->sectorsize_bits);
237+
238+
btrfs_subpage_assert(fs_info, page, start, len);
239+
240+
ASSERT(atomic_read(&subpage->writers) >= nbits);
241+
return atomic_sub_and_test(nbits, &subpage->writers);
242+
}
243+
244+
/*
245+
* Lock a page for delalloc page writeback.
246+
*
247+
* Return -EAGAIN if the page is not properly initialized.
248+
* Return 0 with the page locked, and writer counter updated.
249+
*
250+
* Even with 0 returned, the page still need extra check to make sure
251+
* it's really the correct page, as the caller is using
252+
* find_get_pages_contig(), which can race with page invalidating.
253+
*/
254+
int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
255+
struct page *page, u64 start, u32 len)
256+
{
257+
if (unlikely(!fs_info) || fs_info->sectorsize == PAGE_SIZE) {
258+
lock_page(page);
259+
return 0;
260+
}
261+
lock_page(page);
262+
if (!PagePrivate(page) || !page->private) {
263+
unlock_page(page);
264+
return -EAGAIN;
265+
}
266+
btrfs_subpage_clamp_range(page, &start, &len);
267+
btrfs_subpage_start_writer(fs_info, page, start, len);
268+
return 0;
269+
}
270+
271+
void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
272+
struct page *page, u64 start, u32 len)
273+
{
274+
if (unlikely(!fs_info) || fs_info->sectorsize == PAGE_SIZE)
275+
return unlock_page(page);
276+
btrfs_subpage_clamp_range(page, &start, &len);
277+
if (btrfs_subpage_end_and_test_writer(fs_info, page, start, len))
278+
unlock_page(page);
279+
}
280+
206281
/*
207282
* Convert the [start, start + len) range into a u16 bitmap
208283
*
@@ -354,16 +429,6 @@ void btrfs_subpage_clear_writeback(const struct btrfs_fs_info *fs_info,
354429
spin_unlock_irqrestore(&subpage->lock, flags);
355430
}
356431

357-
static void btrfs_subpage_clamp_range(struct page *page, u64 *start, u32 *len)
358-
{
359-
u64 orig_start = *start;
360-
u32 orig_len = *len;
361-
362-
*start = max_t(u64, page_offset(page), orig_start);
363-
*len = min_t(u64, page_offset(page) + PAGE_SIZE,
364-
orig_start + orig_len) - *start;
365-
}
366-
367432
/*
368433
* Unlike set/clear which is dependent on each page status, for test all bits
369434
* are tested in the same way.

fs/btrfs/subpage.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct btrfs_subpage {
3333
/* Structures only used by data */
3434
struct {
3535
atomic_t readers;
36+
atomic_t writers;
3637
};
3738
};
3839
};
@@ -63,6 +64,15 @@ void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
6364
void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
6465
struct page *page, u64 start, u32 len);
6566

67+
void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
68+
struct page *page, u64 start, u32 len);
69+
bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
70+
struct page *page, u64 start, u32 len);
71+
int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
72+
struct page *page, u64 start, u32 len);
73+
void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
74+
struct page *page, u64 start, u32 len);
75+
6676
/*
6777
* Template for subpage related operations.
6878
*

0 commit comments

Comments
 (0)