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;
25 * Structure fields follow one of the following exclusion rules.
27 * I: Modifiable by initialization/destruction paths and read-only
30 * L: Field should be protected by the pcluster lock;
32 * A: Field should be accessed / updated in atomic for parallelized code.
34 struct z_erofs_pcluster {
35 struct erofs_workgroup obj;
38 /* A: point to next chained pcluster or TAILs */
39 z_erofs_next_pcluster_t next;
41 /* A: lower limit of decompressed length and if full length or not */
44 /* I: page offset of start position of decompression */
45 unsigned short pageofs_out;
47 /* I: page offset of inline compressed data */
48 unsigned short pageofs_in;
50 /* L: maximum relative page index in pagevec[] */
51 unsigned short nr_pages;
53 /* L: total number of pages in pagevec[] */
57 /* L: inline a certain number of pagevecs for bootstrap */
58 erofs_vtptr_t pagevec[Z_EROFS_NR_INLINE_PAGEVECS];
60 /* I: can be used to free the pcluster by RCU. */
65 /* I: physical cluster size in pages */
66 unsigned short pclusterpages;
68 /* I: tailpacking inline compressed size */
69 unsigned short tailpacking_size;
72 /* I: compression algorithm format */
73 unsigned char algorithmformat;
75 /* A: compressed pages (can be cached or inplaced pages) */
76 struct page *compressed_pages[];
79 /* let's avoid the valid 32-bit kernel addresses */
81 /* the chained workgroup has't submitted io (still open) */
82 #define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE)
83 /* the chained workgroup has already submitted io */
84 #define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD)
86 #define Z_EROFS_PCLUSTER_NIL (NULL)
88 struct z_erofs_decompressqueue {
89 struct super_block *sb;
90 atomic_t pending_bios;
91 z_erofs_next_pcluster_t head;
94 struct completion done;
95 struct work_struct work;
99 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
101 return !pcl->obj.index;
104 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
106 if (z_erofs_is_inline_pcluster(pcl))
108 return pcl->pclusterpages;
111 #define Z_EROFS_ONLINEPAGE_COUNT_BITS 2
112 #define Z_EROFS_ONLINEPAGE_COUNT_MASK ((1 << Z_EROFS_ONLINEPAGE_COUNT_BITS) - 1)
113 #define Z_EROFS_ONLINEPAGE_INDEX_SHIFT (Z_EROFS_ONLINEPAGE_COUNT_BITS)
116 * waiters (aka. ongoing_packs): # to unlock the page
117 * sub-index: 0 - for partial page, >= 1 full page sub-index
119 typedef atomic_t z_erofs_onlinepage_t;
122 union z_erofs_onlinepage_converter {
123 z_erofs_onlinepage_t *o;
127 static inline unsigned int z_erofs_onlinepage_index(struct page *page)
129 union z_erofs_onlinepage_converter u;
131 DBG_BUGON(!PagePrivate(page));
132 u.v = &page_private(page);
134 return atomic_read(u.o) >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
137 static inline void z_erofs_onlinepage_init(struct page *page)
140 z_erofs_onlinepage_t o;
142 /* keep from being unlocked in advance */
143 } u = { .o = ATOMIC_INIT(1) };
145 set_page_private(page, u.v);
147 SetPagePrivate(page);
150 static inline void z_erofs_onlinepage_fixup(struct page *page,
151 uintptr_t index, bool down)
153 union z_erofs_onlinepage_converter u = { .v = &page_private(page) };
154 int orig, orig_index, val;
157 orig = atomic_read(u.o);
158 orig_index = orig >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
163 DBG_BUGON(orig_index != index);
166 val = (index << Z_EROFS_ONLINEPAGE_INDEX_SHIFT) |
167 ((orig & Z_EROFS_ONLINEPAGE_COUNT_MASK) + (unsigned int)down);
168 if (atomic_cmpxchg(u.o, orig, val) != orig)
172 static inline void z_erofs_onlinepage_endio(struct page *page)
174 union z_erofs_onlinepage_converter u;
177 DBG_BUGON(!PagePrivate(page));
178 u.v = &page_private(page);
180 v = atomic_dec_return(u.o);
181 if (!(v & Z_EROFS_ONLINEPAGE_COUNT_MASK)) {
182 set_page_private(page, 0);
183 ClearPagePrivate(page);
184 if (!PageError(page))
185 SetPageUptodate(page);
188 erofs_dbg("%s, page %p value %x", __func__, page, atomic_read(u.o));
191 #define Z_EROFS_VMAP_ONSTACK_PAGES \
192 min_t(unsigned int, THREAD_SIZE / 8 / sizeof(struct page *), 96U)
193 #define Z_EROFS_VMAP_GLOBAL_PAGES 2048