1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
7 #ifndef __EROFS_FS_ZDATA_H
8 #define __EROFS_FS_ZDATA_H
13 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
14 #define Z_EROFS_NR_INLINE_PAGEVECS 3
17 * Structure fields follow one of the following exclusion rules.
19 * I: Modifiable by initialization/destruction paths and read-only
22 * L: Field should be protected by pageset lock;
24 * A: Field should be accessed / updated in atomic for parallelized code.
26 struct z_erofs_collection {
29 /* I: page offset of start position of decompression */
30 unsigned short pageofs;
32 /* L: maximum relative page index in pagevec[] */
33 unsigned short nr_pages;
35 /* L: total number of pages in pagevec[] */
39 /* L: inline a certain number of pagevecs for bootstrap */
40 erofs_vtptr_t pagevec[Z_EROFS_NR_INLINE_PAGEVECS];
42 /* I: can be used to free the pcluster by RCU. */
47 #define Z_EROFS_PCLUSTER_FULL_LENGTH 0x00000001
48 #define Z_EROFS_PCLUSTER_LENGTH_BIT 1
51 * let's leave a type here in case of introducing
52 * another tagged pointer later.
54 typedef void *z_erofs_next_pcluster_t;
56 struct z_erofs_pcluster {
57 struct erofs_workgroup obj;
58 struct z_erofs_collection primary_collection;
60 /* A: point to next chained pcluster or TAILs */
61 z_erofs_next_pcluster_t next;
63 /* A: lower limit of decompressed length and if full length or not */
66 /* I: physical cluster size in pages */
67 unsigned short pclusterpages;
69 /* I: compression algorithm format */
70 unsigned char algorithmformat;
72 /* A: compressed pages (can be cached or inplaced pages) */
73 struct page *compressed_pages[];
76 #define z_erofs_primarycollection(pcluster) (&(pcluster)->primary_collection)
78 /* let's avoid the valid 32-bit kernel addresses */
80 /* the chained workgroup has't submitted io (still open) */
81 #define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE)
82 /* the chained workgroup has already submitted io */
83 #define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD)
85 #define Z_EROFS_PCLUSTER_NIL (NULL)
87 struct z_erofs_decompressqueue {
88 struct super_block *sb;
89 atomic_t pending_bios;
90 z_erofs_next_pcluster_t head;
93 wait_queue_head_t wait;
94 struct work_struct work;
98 #define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping)
99 static inline bool erofs_page_is_managed(const struct erofs_sb_info *sbi,
102 return page->mapping == MNGD_MAPPING(sbi);
105 #define Z_EROFS_ONLINEPAGE_COUNT_BITS 2
106 #define Z_EROFS_ONLINEPAGE_COUNT_MASK ((1 << Z_EROFS_ONLINEPAGE_COUNT_BITS) - 1)
107 #define Z_EROFS_ONLINEPAGE_INDEX_SHIFT (Z_EROFS_ONLINEPAGE_COUNT_BITS)
110 * waiters (aka. ongoing_packs): # to unlock the page
111 * sub-index: 0 - for partial page, >= 1 full page sub-index
113 typedef atomic_t z_erofs_onlinepage_t;
116 union z_erofs_onlinepage_converter {
117 z_erofs_onlinepage_t *o;
121 static inline unsigned int z_erofs_onlinepage_index(struct page *page)
123 union z_erofs_onlinepage_converter u;
125 DBG_BUGON(!PagePrivate(page));
126 u.v = &page_private(page);
128 return atomic_read(u.o) >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
131 static inline void z_erofs_onlinepage_init(struct page *page)
134 z_erofs_onlinepage_t o;
136 /* keep from being unlocked in advance */
137 } u = { .o = ATOMIC_INIT(1) };
139 set_page_private(page, u.v);
141 SetPagePrivate(page);
144 static inline void z_erofs_onlinepage_fixup(struct page *page,
145 uintptr_t index, bool down)
147 union z_erofs_onlinepage_converter u = { .v = &page_private(page) };
148 int orig, orig_index, val;
151 orig = atomic_read(u.o);
152 orig_index = orig >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
157 DBG_BUGON(orig_index != index);
160 val = (index << Z_EROFS_ONLINEPAGE_INDEX_SHIFT) |
161 ((orig & Z_EROFS_ONLINEPAGE_COUNT_MASK) + (unsigned int)down);
162 if (atomic_cmpxchg(u.o, orig, val) != orig)
166 static inline void z_erofs_onlinepage_endio(struct page *page)
168 union z_erofs_onlinepage_converter u;
171 DBG_BUGON(!PagePrivate(page));
172 u.v = &page_private(page);
174 v = atomic_dec_return(u.o);
175 if (!(v & Z_EROFS_ONLINEPAGE_COUNT_MASK)) {
176 set_page_private(page, 0);
177 ClearPagePrivate(page);
178 if (!PageError(page))
179 SetPageUptodate(page);
182 erofs_dbg("%s, page %p value %x", __func__, page, atomic_read(u.o));
185 #define Z_EROFS_VMAP_ONSTACK_PAGES \
186 min_t(unsigned int, THREAD_SIZE / 8 / sizeof(struct page *), 96U)
187 #define Z_EROFS_VMAP_GLOBAL_PAGES 2048