blob: b86a4881475d138cceef0837738655fc8953a2f0 [file] [log] [blame]
Qu Wenruocac06d82021-01-26 16:33:47 +08001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_SUBPAGE_H
4#define BTRFS_SUBPAGE_H
5
6#include <linux/spinlock.h>
7
8/*
9 * Maximum page size we support is 64K, minimum sector size is 4K, u16 bitmap
10 * is sufficient. Regular bitmap_* is not used due to size reasons.
11 */
12#define BTRFS_SUBPAGE_BITMAP_SIZE 16
13
14/*
15 * Structure to trace status of each sector inside a page, attached to
16 * page::private for both data and metadata inodes.
17 */
18struct btrfs_subpage {
19 /* Common members for both data and metadata pages */
20 spinlock_t lock;
Qu Wenruoa1d767c2021-01-26 16:33:52 +080021 u16 uptodate_bitmap;
Qu Wenruo03a816b32021-01-26 16:33:53 +080022 u16 error_bitmap;
Qu Wenruo760f9912021-01-26 16:33:48 +080023 union {
Qu Wenruo8ff84662021-01-26 16:33:50 +080024 /*
25 * Structures only used by metadata
26 *
27 * @eb_refs should only be operated under private_lock, as it
28 * manages whether the subpage can be detached.
29 */
30 atomic_t eb_refs;
Qu Wenruo760f9912021-01-26 16:33:48 +080031 /* Structures only used by data */
Qu Wenruo92082d42021-02-02 10:28:36 +080032 struct {
33 atomic_t readers;
34 };
Qu Wenruo760f9912021-01-26 16:33:48 +080035 };
Qu Wenruocac06d82021-01-26 16:33:47 +080036};
37
38enum btrfs_subpage_type {
39 BTRFS_SUBPAGE_METADATA,
40 BTRFS_SUBPAGE_DATA,
41};
42
43int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
44 struct page *page, enum btrfs_subpage_type type);
45void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
46 struct page *page);
47
Qu Wenruo760f9912021-01-26 16:33:48 +080048/* Allocate additional data where page represents more than one sector */
49int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
50 struct btrfs_subpage **ret,
51 enum btrfs_subpage_type type);
52void btrfs_free_subpage(struct btrfs_subpage *subpage);
53
Qu Wenruo8ff84662021-01-26 16:33:50 +080054void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
55 struct page *page);
56void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
57 struct page *page);
58
Qu Wenruo92082d42021-02-02 10:28:36 +080059void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
60 struct page *page, u64 start, u32 len);
61void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
62 struct page *page, u64 start, u32 len);
63
Qu Wenruoa1d767c2021-01-26 16:33:52 +080064/*
65 * Template for subpage related operations.
66 *
67 * btrfs_subpage_*() are for call sites where the page has subpage attached and
68 * the range is ensured to be inside the page.
69 *
70 * btrfs_page_*() are for call sites where the page can either be subpage
71 * specific or regular page. The function will handle both cases.
72 * But the range still needs to be inside the page.
73 */
74#define DECLARE_BTRFS_SUBPAGE_OPS(name) \
75void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
76 struct page *page, u64 start, u32 len); \
77void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
78 struct page *page, u64 start, u32 len); \
79bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
80 struct page *page, u64 start, u32 len); \
81void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \
82 struct page *page, u64 start, u32 len); \
83void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \
84 struct page *page, u64 start, u32 len); \
85bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \
86 struct page *page, u64 start, u32 len);
87
88DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
Qu Wenruo03a816b32021-01-26 16:33:53 +080089DECLARE_BTRFS_SUBPAGE_OPS(error);
Qu Wenruoa1d767c2021-01-26 16:33:52 +080090
Qu Wenruocac06d82021-01-26 16:33:47 +080091#endif