1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
6 #ifndef __EROFS_FS_ZDATA_H
7 #define __EROFS_FS_ZDATA_H
12 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_NR_INLINE_PAGEVECS 3
15 #define Z_EROFS_PCLUSTER_FULL_LENGTH 0x00000001
16 #define Z_EROFS_PCLUSTER_LENGTH_BIT 1
19 * let's leave a type here in case of introducing
20 * another tagged pointer later.
22 typedef void *z_erofs_next_pcluster_t;
30 #define __Z_EROFS_BVSET(name, total) \
32 /* point to the next page which contains the following bvecs */ \
33 struct page *nextpage; \
34 struct z_erofs_bvec bvec[total]; \
36 __Z_EROFS_BVSET(z_erofs_bvset,);
37 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_NR_INLINE_PAGEVECS);
40 * Structure fields follow one of the following exclusion rules.
42 * I: Modifiable by initialization/destruction paths and read-only
45 * L: Field should be protected by the pcluster lock;
47 * A: Field should be accessed / updated in atomic for parallelized code.
49 struct z_erofs_pcluster {
50 struct erofs_workgroup obj;
53 /* A: point to next chained pcluster or TAILs */
54 z_erofs_next_pcluster_t next;
56 /* A: lower limit of decompressed length and if full length or not */
59 /* L: total number of bvecs */
62 /* I: page offset of start position of decompression */
63 unsigned short pageofs_out;
65 /* I: page offset of inline compressed data */
66 unsigned short pageofs_in;
68 /* L: maximum relative page index in bvecs */
69 unsigned short nr_pages;
72 /* L: inline a certain number of pagevecs for bootstrap */
73 erofs_vtptr_t pagevec[Z_EROFS_NR_INLINE_PAGEVECS];
75 /* L: inline a certain number of bvec for bootstrap */
76 struct z_erofs_bvset_inline bvset;
78 /* I: can be used to free the pcluster by RCU. */
83 /* I: physical cluster size in pages */
84 unsigned short pclusterpages;
86 /* I: tailpacking inline compressed size */
87 unsigned short tailpacking_size;
90 /* I: compression algorithm format */
91 unsigned char algorithmformat;
93 /* A: compressed pages (can be cached or inplaced pages) */
94 struct page *compressed_pages[];
97 /* let's avoid the valid 32-bit kernel addresses */
99 /* the chained workgroup has't submitted io (still open) */
100 #define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE)
101 /* the chained workgroup has already submitted io */
102 #define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD)
104 #define Z_EROFS_PCLUSTER_NIL (NULL)
106 struct z_erofs_decompressqueue {
107 struct super_block *sb;
108 atomic_t pending_bios;
109 z_erofs_next_pcluster_t head;
112 struct completion done;
113 struct work_struct work;
117 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
119 return !pcl->obj.index;
122 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
124 if (z_erofs_is_inline_pcluster(pcl))
126 return pcl->pclusterpages;
129 #define Z_EROFS_ONLINEPAGE_COUNT_BITS 2
130 #define Z_EROFS_ONLINEPAGE_COUNT_MASK ((1 << Z_EROFS_ONLINEPAGE_COUNT_BITS) - 1)
131 #define Z_EROFS_ONLINEPAGE_INDEX_SHIFT (Z_EROFS_ONLINEPAGE_COUNT_BITS)
134 * waiters (aka. ongoing_packs): # to unlock the page
135 * sub-index: 0 - for partial page, >= 1 full page sub-index
137 typedef atomic_t z_erofs_onlinepage_t;
140 union z_erofs_onlinepage_converter {
141 z_erofs_onlinepage_t *o;
145 static inline unsigned int z_erofs_onlinepage_index(struct page *page)
147 union z_erofs_onlinepage_converter u;
149 DBG_BUGON(!PagePrivate(page));
150 u.v = &page_private(page);
152 return atomic_read(u.o) >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
155 static inline void z_erofs_onlinepage_init(struct page *page)
158 z_erofs_onlinepage_t o;
160 /* keep from being unlocked in advance */
161 } u = { .o = ATOMIC_INIT(1) };
163 set_page_private(page, u.v);
165 SetPagePrivate(page);
168 static inline void z_erofs_onlinepage_fixup(struct page *page,
169 uintptr_t index, bool down)
171 union z_erofs_onlinepage_converter u = { .v = &page_private(page) };
172 int orig, orig_index, val;
175 orig = atomic_read(u.o);
176 orig_index = orig >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
181 DBG_BUGON(orig_index != index);
184 val = (index << Z_EROFS_ONLINEPAGE_INDEX_SHIFT) |
185 ((orig & Z_EROFS_ONLINEPAGE_COUNT_MASK) + (unsigned int)down);
186 if (atomic_cmpxchg(u.o, orig, val) != orig)
190 static inline void z_erofs_onlinepage_endio(struct page *page)
192 union z_erofs_onlinepage_converter u;
195 DBG_BUGON(!PagePrivate(page));
196 u.v = &page_private(page);
198 v = atomic_dec_return(u.o);
199 if (!(v & Z_EROFS_ONLINEPAGE_COUNT_MASK)) {
200 set_page_private(page, 0);
201 ClearPagePrivate(page);
202 if (!PageError(page))
203 SetPageUptodate(page);
206 erofs_dbg("%s, page %p value %x", __func__, page, atomic_read(u.o));
209 #define Z_EROFS_VMAP_ONSTACK_PAGES \
210 min_t(unsigned int, THREAD_SIZE / 8 / sizeof(struct page *), 96U)
211 #define Z_EROFS_VMAP_GLOBAL_PAGES 2048