blob: 3a008f1b9f7879ea2430f1b6bb63e5ce314e7124 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
Gao Xiang0d40d6e32018-07-26 20:22:02 +08003 * Copyright (C) 2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang0d40d6e32018-07-26 20:22:02 +08005 */
Gao Xiang57b78c92019-07-31 23:57:32 +08006#ifndef __EROFS_FS_ZDATA_H
7#define __EROFS_FS_ZDATA_H
Gao Xiang0d40d6e32018-07-26 20:22:02 +08008
9#include "internal.h"
Gao Xiang57b78c92019-07-31 23:57:32 +080010#include "zpvec.h"
Gao Xiang3883a792018-07-26 20:22:06 +080011
Gao Xiang9f6cc762021-04-07 12:39:20 +080012#define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
Gao Xiang88aaf5a2019-06-24 15:22:57 +080013#define Z_EROFS_NR_INLINE_PAGEVECS 3
14
Gao Xiang3883a792018-07-26 20:22:06 +080015/*
Gao Xiang3883a792018-07-26 20:22:06 +080016 * Structure fields follow one of the following exclusion rules.
17 *
18 * I: Modifiable by initialization/destruction paths and read-only
Gao Xiang97e86a82019-07-31 23:57:47 +080019 * for everyone else;
Gao Xiang3883a792018-07-26 20:22:06 +080020 *
Gao Xiang97e86a82019-07-31 23:57:47 +080021 * L: Field should be protected by pageset lock;
22 *
23 * A: Field should be accessed / updated in atomic for parallelized code.
Gao Xiang3883a792018-07-26 20:22:06 +080024 */
Gao Xiang97e86a82019-07-31 23:57:47 +080025struct z_erofs_collection {
Gao Xiang3883a792018-07-26 20:22:06 +080026 struct mutex lock;
27
Gao Xiang97e86a82019-07-31 23:57:47 +080028 /* I: page offset of start position of decompression */
Gao Xiang3883a792018-07-26 20:22:06 +080029 unsigned short pageofs;
Gao Xiang97e86a82019-07-31 23:57:47 +080030
31 /* L: maximum relative page index in pagevec[] */
Gao Xiang3883a792018-07-26 20:22:06 +080032 unsigned short nr_pages;
33
Gao Xiang97e86a82019-07-31 23:57:47 +080034 /* L: total number of pages in pagevec[] */
Pratik Shindee82a9a12019-07-15 17:51:27 +053035 unsigned int vcnt;
Gao Xiang3883a792018-07-26 20:22:06 +080036
37 union {
Gao Xiang97e86a82019-07-31 23:57:47 +080038 /* L: inline a certain number of pagevecs for bootstrap */
Gao Xiangfa61a332019-06-24 15:22:53 +080039 erofs_vtptr_t pagevec[Z_EROFS_NR_INLINE_PAGEVECS];
Gao Xiang97e86a82019-07-31 23:57:47 +080040
41 /* I: can be used to free the pcluster by RCU. */
Gao Xiang3883a792018-07-26 20:22:06 +080042 struct rcu_head rcu;
43 };
44};
45
Gao Xiang97e86a82019-07-31 23:57:47 +080046#define Z_EROFS_PCLUSTER_FULL_LENGTH 0x00000001
47#define Z_EROFS_PCLUSTER_LENGTH_BIT 1
Gao Xiang3883a792018-07-26 20:22:06 +080048
Gao Xiang97e86a82019-07-31 23:57:47 +080049/*
50 * let's leave a type here in case of introducing
51 * another tagged pointer later.
52 */
53typedef void *z_erofs_next_pcluster_t;
Gao Xiang3883a792018-07-26 20:22:06 +080054
Gao Xiang97e86a82019-07-31 23:57:47 +080055struct z_erofs_pcluster {
Gao Xiang3883a792018-07-26 20:22:06 +080056 struct erofs_workgroup obj;
Gao Xiang97e86a82019-07-31 23:57:47 +080057 struct z_erofs_collection primary_collection;
Gao Xiang3883a792018-07-26 20:22:06 +080058
Gao Xiang97e86a82019-07-31 23:57:47 +080059 /* A: point to next chained pcluster or TAILs */
60 z_erofs_next_pcluster_t next;
Gao Xiang3883a792018-07-26 20:22:06 +080061
Gao Xiang97e86a82019-07-31 23:57:47 +080062 /* A: lower limit of decompressed length and if full length or not */
63 unsigned int length;
64
Gao Xiang9f6cc762021-04-07 12:39:20 +080065 /* I: physical cluster size in pages */
66 unsigned short pclusterpages;
67
Gao Xiang97e86a82019-07-31 23:57:47 +080068 /* I: compression algorithm format */
69 unsigned char algorithmformat;
Gao Xiang9f6cc762021-04-07 12:39:20 +080070
71 /* A: compressed pages (can be cached or inplaced pages) */
72 struct page *compressed_pages[];
Gao Xiang3883a792018-07-26 20:22:06 +080073};
74
Gao Xiang97e86a82019-07-31 23:57:47 +080075#define z_erofs_primarycollection(pcluster) (&(pcluster)->primary_collection)
76
Gao Xiang3883a792018-07-26 20:22:06 +080077/* let's avoid the valid 32-bit kernel addresses */
78
79/* the chained workgroup has't submitted io (still open) */
Gao Xiang97e86a82019-07-31 23:57:47 +080080#define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE)
Gao Xiang3883a792018-07-26 20:22:06 +080081/* the chained workgroup has already submitted io */
Gao Xiang97e86a82019-07-31 23:57:47 +080082#define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD)
Gao Xiang3883a792018-07-26 20:22:06 +080083
Gao Xiang97e86a82019-07-31 23:57:47 +080084#define Z_EROFS_PCLUSTER_NIL (NULL)
Gao Xiang3883a792018-07-26 20:22:06 +080085
Gao Xianga4b1fab2019-10-08 20:56:15 +080086struct z_erofs_decompressqueue {
87 struct super_block *sb;
Gao Xiang3883a792018-07-26 20:22:06 +080088 atomic_t pending_bios;
Gao Xiang97e86a82019-07-31 23:57:47 +080089 z_erofs_next_pcluster_t head;
Gao Xiang3883a792018-07-26 20:22:06 +080090
91 union {
92 wait_queue_head_t wait;
93 struct work_struct work;
94 } u;
95};
96
Gao Xiang14f362b2019-07-31 23:57:36 +080097#define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping)
98static inline bool erofs_page_is_managed(const struct erofs_sb_info *sbi,
99 struct page *page)
100{
101 return page->mapping == MNGD_MAPPING(sbi);
102}
Gao Xiang14f362b2019-07-31 23:57:36 +0800103
Gao Xiang97e86a82019-07-31 23:57:47 +0800104#define Z_EROFS_ONLINEPAGE_COUNT_BITS 2
105#define Z_EROFS_ONLINEPAGE_COUNT_MASK ((1 << Z_EROFS_ONLINEPAGE_COUNT_BITS) - 1)
Gao Xiang3883a792018-07-26 20:22:06 +0800106#define Z_EROFS_ONLINEPAGE_INDEX_SHIFT (Z_EROFS_ONLINEPAGE_COUNT_BITS)
107
108/*
109 * waiters (aka. ongoing_packs): # to unlock the page
110 * sub-index: 0 - for partial page, >= 1 full page sub-index
111 */
112typedef atomic_t z_erofs_onlinepage_t;
113
114/* type punning */
115union z_erofs_onlinepage_converter {
116 z_erofs_onlinepage_t *o;
117 unsigned long *v;
118};
119
Pratik Shindee82a9a12019-07-15 17:51:27 +0530120static inline unsigned int z_erofs_onlinepage_index(struct page *page)
Gao Xiang3883a792018-07-26 20:22:06 +0800121{
122 union z_erofs_onlinepage_converter u;
123
Hariprasad Kelam800c16c2019-06-08 15:19:18 +0530124 DBG_BUGON(!PagePrivate(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800125 u.v = &page_private(page);
126
127 return atomic_read(u.o) >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
128}
129
130static inline void z_erofs_onlinepage_init(struct page *page)
131{
132 union {
133 z_erofs_onlinepage_t o;
134 unsigned long v;
135 /* keep from being unlocked in advance */
136 } u = { .o = ATOMIC_INIT(1) };
137
138 set_page_private(page, u.v);
139 smp_wmb();
140 SetPagePrivate(page);
141}
142
143static inline void z_erofs_onlinepage_fixup(struct page *page,
144 uintptr_t index, bool down)
145{
Gao Xiang3c5972822020-06-19 07:43:49 +0800146 union z_erofs_onlinepage_converter u = { .v = &page_private(page) };
147 int orig, orig_index, val;
Gao Xiang3883a792018-07-26 20:22:06 +0800148
Gao Xiang3c5972822020-06-19 07:43:49 +0800149repeat:
150 orig = atomic_read(u.o);
151 orig_index = orig >> Z_EROFS_ONLINEPAGE_INDEX_SHIFT;
152 if (orig_index) {
Gao Xiang3883a792018-07-26 20:22:06 +0800153 if (!index)
154 return;
155
Gao Xiang3c5972822020-06-19 07:43:49 +0800156 DBG_BUGON(orig_index != index);
Gao Xiang3883a792018-07-26 20:22:06 +0800157 }
158
Gao Xiang3c5972822020-06-19 07:43:49 +0800159 val = (index << Z_EROFS_ONLINEPAGE_INDEX_SHIFT) |
160 ((orig & Z_EROFS_ONLINEPAGE_COUNT_MASK) + (unsigned int)down);
161 if (atomic_cmpxchg(u.o, orig, val) != orig)
Gao Xiang3883a792018-07-26 20:22:06 +0800162 goto repeat;
163}
164
165static inline void z_erofs_onlinepage_endio(struct page *page)
166{
167 union z_erofs_onlinepage_converter u;
Pratik Shindee82a9a12019-07-15 17:51:27 +0530168 unsigned int v;
Gao Xiang3883a792018-07-26 20:22:06 +0800169
Hariprasad Kelam800c16c2019-06-08 15:19:18 +0530170 DBG_BUGON(!PagePrivate(page));
Gao Xiang3883a792018-07-26 20:22:06 +0800171 u.v = &page_private(page);
172
173 v = atomic_dec_return(u.o);
174 if (!(v & Z_EROFS_ONLINEPAGE_COUNT_MASK)) {
Gao Xiang6aaa7b02020-12-08 17:58:32 +0800175 set_page_private(page, 0);
Gao Xiang3883a792018-07-26 20:22:06 +0800176 ClearPagePrivate(page);
177 if (!PageError(page))
178 SetPageUptodate(page);
179 unlock_page(page);
180 }
Gao Xiang4f761fa2019-09-04 10:09:09 +0800181 erofs_dbg("%s, page %p value %x", __func__, page, atomic_read(u.o));
Gao Xiang3883a792018-07-26 20:22:06 +0800182}
183
Gao Xiang97e86a82019-07-31 23:57:47 +0800184#define Z_EROFS_VMAP_ONSTACK_PAGES \
Gao Xiang81edee72018-07-28 15:10:32 +0800185 min_t(unsigned int, THREAD_SIZE / 8 / sizeof(struct page *), 96U)
Gao Xiang97e86a82019-07-31 23:57:47 +0800186#define Z_EROFS_VMAP_GLOBAL_PAGES 2048
Gao Xiang3883a792018-07-26 20:22:06 +0800187
Gao Xiang0d40d6e32018-07-26 20:22:02 +0800188#endif
189