1cac06d84SQu Wenruo/* SPDX-License-Identifier: GPL-2.0 */
2cac06d84SQu Wenruo
3cac06d84SQu Wenruo#ifndef BTRFS_SUBPAGE_H
4cac06d84SQu Wenruo#define BTRFS_SUBPAGE_H
5cac06d84SQu Wenruo
6cac06d84SQu Wenruo#include <linux/spinlock.h>
7cac06d84SQu Wenruo
88481dd80SQu Wenruo/*
98481dd80SQu Wenruo * Extra info for subpapge bitmap.
108481dd80SQu Wenruo *
118481dd80SQu Wenruo * For subpage we pack all uptodate/error/dirty/writeback/ordered bitmaps into
128481dd80SQu Wenruo * one larger bitmap.
138481dd80SQu Wenruo *
148481dd80SQu Wenruo * This structure records how they are organized in the bitmap:
158481dd80SQu Wenruo *
168481dd80SQu Wenruo * /- uptodate_offset	/- error_offset	/- dirty_offset
178481dd80SQu Wenruo * |			|		|
188481dd80SQu Wenruo * v			v		v
198481dd80SQu Wenruo * |u|u|u|u|........|u|u|e|e|.......|e|e| ...	|o|o|
208481dd80SQu Wenruo * |<- bitmap_nr_bits ->|
218481dd80SQu Wenruo * |<--------------- total_nr_bits ---------------->|
228481dd80SQu Wenruo */
238481dd80SQu Wenruostruct btrfs_subpage_info {
248481dd80SQu Wenruo	/* Number of bits for each bitmap */
258481dd80SQu Wenruo	unsigned int bitmap_nr_bits;
268481dd80SQu Wenruo
278481dd80SQu Wenruo	/* Total number of bits for the whole bitmap */
288481dd80SQu Wenruo	unsigned int total_nr_bits;
298481dd80SQu Wenruo
308481dd80SQu Wenruo	/*
318481dd80SQu Wenruo	 * *_start indicates where the bitmap starts, the length is always
328481dd80SQu Wenruo	 * @bitmap_size, which is calculated from PAGE_SIZE / sectorsize.
338481dd80SQu Wenruo	 */
348481dd80SQu Wenruo	unsigned int uptodate_offset;
358481dd80SQu Wenruo	unsigned int error_offset;
368481dd80SQu Wenruo	unsigned int dirty_offset;
378481dd80SQu Wenruo	unsigned int writeback_offset;
388481dd80SQu Wenruo	unsigned int ordered_offset;
39e4f94347SQu Wenruo	unsigned int checked_offset;
408481dd80SQu Wenruo};
418481dd80SQu Wenruo
42cac06d84SQu Wenruo/*
43cac06d84SQu Wenruo * Structure to trace status of each sector inside a page, attached to
44cac06d84SQu Wenruo * page::private for both data and metadata inodes.
45cac06d84SQu Wenruo */
46cac06d84SQu Wenruostruct btrfs_subpage {
47cac06d84SQu Wenruo	/* Common members for both data and metadata pages */
48cac06d84SQu Wenruo	spinlock_t lock;
493d078efaSQu Wenruo	/*
503d078efaSQu Wenruo	 * Both data and metadata needs to track how many readers are for the
513d078efaSQu Wenruo	 * page.
523d078efaSQu Wenruo	 * Data relies on @readers to unlock the page when last reader finished.
533d078efaSQu Wenruo	 * While metadata doesn't need page unlock, it needs to prevent
543d078efaSQu Wenruo	 * page::private get cleared before the last end_page_read().
553d078efaSQu Wenruo	 */
563d078efaSQu Wenruo	atomic_t readers;
57760f991fSQu Wenruo	union {
588ff8466dSQu Wenruo		/*
598ff8466dSQu Wenruo		 * Structures only used by metadata
608ff8466dSQu Wenruo		 *
618ff8466dSQu Wenruo		 * @eb_refs should only be operated under private_lock, as it
628ff8466dSQu Wenruo		 * manages whether the subpage can be detached.
638ff8466dSQu Wenruo		 */
648ff8466dSQu Wenruo		atomic_t eb_refs;
656f17400bSQu Wenruo
6672a69cd0SQu Wenruo		/* Structures only used by data */
6772a69cd0SQu Wenruo		atomic_t writers;
68760f991fSQu Wenruo	};
6972a69cd0SQu Wenruo	unsigned long bitmaps[];
70cac06d84SQu Wenruo};
71cac06d84SQu Wenruo
72cac06d84SQu Wenruoenum btrfs_subpage_type {
74cac06d84SQu Wenruo	BTRFS_SUBPAGE_DATA,
75cac06d84SQu Wenruo};
76cac06d84SQu Wenruo
778481dd80SQu Wenruovoid btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sectorsize);
78cac06d84SQu Wenruoint btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
79cac06d84SQu Wenruo			 struct page *page, enum btrfs_subpage_type type);
80cac06d84SQu Wenruovoid btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
81cac06d84SQu Wenruo			  struct page *page);
82cac06d84SQu Wenruo
83760f991fSQu Wenruo/* Allocate additional data where page represents more than one sector */
84651fb419SQu Wenruostruct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
85651fb419SQu Wenruo					  enum btrfs_subpage_type type);
86760f991fSQu Wenruovoid btrfs_free_subpage(struct btrfs_subpage *subpage);
87760f991fSQu Wenruo
888ff8466dSQu Wenruovoid btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
898ff8466dSQu Wenruo			    struct page *page);
908ff8466dSQu Wenruovoid btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
918ff8466dSQu Wenruo			    struct page *page);
928ff8466dSQu Wenruo
9392082d40SQu Wenruovoid btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
9492082d40SQu Wenruo		struct page *page, u64 start, u32 len);
9592082d40SQu Wenruovoid btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
9692082d40SQu Wenruo		struct page *page, u64 start, u32 len);
9792082d40SQu Wenruo
981e1de387SQu Wenruovoid btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
991e1de387SQu Wenruo		struct page *page, u64 start, u32 len);
1001e1de387SQu Wenruobool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
1011e1de387SQu Wenruo		struct page *page, u64 start, u32 len);
1021e1de387SQu Wenruoint btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
1031e1de387SQu Wenruo		struct page *page, u64 start, u32 len);
1041e1de387SQu Wenruovoid btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
1051e1de387SQu Wenruo		struct page *page, u64 start, u32 len);
1061e1de387SQu Wenruo
107a1d767c1SQu Wenruo/*
108a1d767c1SQu Wenruo * Template for subpage related operations.
109a1d767c1SQu Wenruo *
110a1d767c1SQu Wenruo * btrfs_subpage_*() are for call sites where the page has subpage attached and
111a1d767c1SQu Wenruo * the range is ensured to be inside the page.
112a1d767c1SQu Wenruo *
113a1d767c1SQu Wenruo * btrfs_page_*() are for call sites where the page can either be subpage
114a1d767c1SQu Wenruo * specific or regular page. The function will handle both cases.
115a1d767c1SQu Wenruo * But the range still needs to be inside the page.
11660e2d255SQu Wenruo *
11760e2d255SQu Wenruo * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't
11860e2d255SQu Wenruo * need to be inside the page. Those functions will truncate the range
11960e2d255SQu Wenruo * automatically.
120a1d767c1SQu Wenruo */
121a1d767c1SQu Wenruo#define DECLARE_BTRFS_SUBPAGE_OPS(name)					\
122a1d767c1SQu Wenruovoid btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info,	\
123a1d767c1SQu Wenruo		struct page *page, u64 start, u32 len);			\
124a1d767c1SQu Wenruovoid btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info,	\
125a1d767c1SQu Wenruo		struct page *page, u64 start, u32 len);			\
126a1d767c1SQu Wenruobool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info,	\
127a1d767c1SQu Wenruo		struct page *page, u64 start, u32 len);			\
128a1d767c1SQu Wenruovoid btrfs_page_set_##name(const struct btrfs_fs_info *fs_info,		\
129a1d767c1SQu Wenruo		struct page *page, u64 start, u32 len);			\
130a1d767c1SQu Wenruovoid btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info,	\
131a1d767c1SQu Wenruo		struct page *page, u64 start, u32 len);			\
132a1d767c1SQu Wenruobool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info,	\
13360e2d255SQu Wenruo		struct page *page, u64 start, u32 len);			\
13460e2d255SQu Wenruovoid btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info,	\
13560e2d255SQu Wenruo		struct page *page, u64 start, u32 len);			\
13660e2d255SQu Wenruovoid btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info,	\
13760e2d255SQu Wenruo		struct page *page, u64 start, u32 len);			\
13860e2d255SQu Wenruobool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info,	\
139a1d767c1SQu Wenruo		struct page *page, u64 start, u32 len);
140a1d767c1SQu Wenruo
141a1d767c1SQu WenruoDECLARE_BTRFS_SUBPAGE_OPS(uptodate);
14203a816b3SQu WenruoDECLARE_BTRFS_SUBPAGE_OPS(error);
143d8a5713eSQu WenruoDECLARE_BTRFS_SUBPAGE_OPS(dirty);
1443470da3bSQu WenruoDECLARE_BTRFS_SUBPAGE_OPS(writeback);
1456f17400bSQu WenruoDECLARE_BTRFS_SUBPAGE_OPS(ordered);
146e4f94347SQu WenruoDECLARE_BTRFS_SUBPAGE_OPS(checked);
147d8a5713eSQu Wenruo
148d8a5713eSQu Wenruobool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
149d8a5713eSQu Wenruo		struct page *page, u64 start, u32 len);
150a1d767c1SQu Wenruo
151cc1d0d93SQu Wenruovoid btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,
152cc1d0d93SQu Wenruo				 struct page *page);
153e55a0de1SQu Wenruovoid btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page,
154e55a0de1SQu Wenruo			      u64 start, u32 len);
155cc1d0d93SQu Wenruo
156cac06d84SQu Wenruo#endif