blob: b05464f4a80831a9a4f5bdeaed21bd6d5eae4208 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
Gao Xiang5eb20ec2018-07-26 20:21:57 +08003 * Copyright (C) 2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang5eb20ec2018-07-26 20:21:57 +08005 */
Gao Xiang57b78c92019-07-31 23:57:32 +08006#ifndef __EROFS_FS_ZPVEC_H
7#define __EROFS_FS_ZPVEC_H
Gao Xiang5eb20ec2018-07-26 20:21:57 +08008
Gao Xiang57b78c92019-07-31 23:57:32 +08009#include "tagptr.h"
Gao Xiang5eb20ec2018-07-26 20:21:57 +080010
Gao Xiang046d64e2019-07-31 23:57:45 +080011/* page type in pagevec for decompress subsystem */
Gao Xiang5eb20ec2018-07-26 20:21:57 +080012enum z_erofs_page_type {
13 /* including Z_EROFS_VLE_PAGE_TAIL_EXCLUSIVE */
14 Z_EROFS_PAGE_TYPE_EXCLUSIVE,
15
16 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED,
17
18 Z_EROFS_VLE_PAGE_TYPE_HEAD,
19 Z_EROFS_VLE_PAGE_TYPE_MAX
20};
21
22extern void __compiletime_error("Z_EROFS_PAGE_TYPE_EXCLUSIVE != 0")
23 __bad_page_type_exclusive(void);
24
25/* pagevec tagged pointer */
26typedef tagptr2_t erofs_vtptr_t;
27
28/* pagevec collector */
29struct z_erofs_pagevec_ctor {
30 struct page *curr, *next;
31 erofs_vtptr_t *pages;
32
33 unsigned int nr, index;
34};
35
36static inline void z_erofs_pagevec_ctor_exit(struct z_erofs_pagevec_ctor *ctor,
37 bool atomic)
38{
Bhanusree Pola561fb352019-03-22 10:38:16 +080039 if (!ctor->curr)
Gao Xiang5eb20ec2018-07-26 20:21:57 +080040 return;
41
42 if (atomic)
43 kunmap_atomic(ctor->pages);
44 else
45 kunmap(ctor->curr);
46}
47
48static inline struct page *
49z_erofs_pagevec_ctor_next_page(struct z_erofs_pagevec_ctor *ctor,
Pratik Shindee82a9a12019-07-15 17:51:27 +053050 unsigned int nr)
Gao Xiang5eb20ec2018-07-26 20:21:57 +080051{
Pratik Shindee82a9a12019-07-15 17:51:27 +053052 unsigned int index;
Gao Xiang5eb20ec2018-07-26 20:21:57 +080053
54 /* keep away from occupied pages */
Bhanusree Pola561fb352019-03-22 10:38:16 +080055 if (ctor->next)
Gao Xiang5eb20ec2018-07-26 20:21:57 +080056 return ctor->next;
57
58 for (index = 0; index < nr; ++index) {
59 const erofs_vtptr_t t = ctor->pages[index];
Pratik Shindee82a9a12019-07-15 17:51:27 +053060 const unsigned int tags = tagptr_unfold_tags(t);
Gao Xiang5eb20ec2018-07-26 20:21:57 +080061
62 if (tags == Z_EROFS_PAGE_TYPE_EXCLUSIVE)
63 return tagptr_unfold_ptr(t);
64 }
Hariprasad Kelam3fb58b82019-05-19 17:35:57 +080065 DBG_BUGON(nr >= ctor->nr);
Gao Xiang5eb20ec2018-07-26 20:21:57 +080066 return NULL;
67}
68
69static inline void
70z_erofs_pagevec_ctor_pagedown(struct z_erofs_pagevec_ctor *ctor,
71 bool atomic)
72{
73 struct page *next = z_erofs_pagevec_ctor_next_page(ctor, ctor->nr);
74
75 z_erofs_pagevec_ctor_exit(ctor, atomic);
76
77 ctor->curr = next;
78 ctor->next = NULL;
79 ctor->pages = atomic ?
80 kmap_atomic(ctor->curr) : kmap(ctor->curr);
81
82 ctor->nr = PAGE_SIZE / sizeof(struct page *);
83 ctor->index = 0;
84}
85
86static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor,
Pratik Shindee82a9a12019-07-15 17:51:27 +053087 unsigned int nr,
88 erofs_vtptr_t *pages,
89 unsigned int i)
Gao Xiang5eb20ec2018-07-26 20:21:57 +080090{
91 ctor->nr = nr;
92 ctor->curr = ctor->next = NULL;
93 ctor->pages = pages;
94
95 if (i >= nr) {
96 i -= nr;
97 z_erofs_pagevec_ctor_pagedown(ctor, false);
98 while (i > ctor->nr) {
99 i -= ctor->nr;
100 z_erofs_pagevec_ctor_pagedown(ctor, false);
101 }
102 }
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800103 ctor->next = z_erofs_pagevec_ctor_next_page(ctor, i);
104 ctor->index = i;
105}
106
Gao Xiang046d64e2019-07-31 23:57:45 +0800107static inline bool z_erofs_pagevec_enqueue(struct z_erofs_pagevec_ctor *ctor,
108 struct page *page,
Gao Xiang86432a62021-11-04 02:20:06 +0800109 enum z_erofs_page_type type,
110 bool pvec_safereuse)
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800111{
Gao Xiang86432a62021-11-04 02:20:06 +0800112 if (!ctor->next) {
113 /* some pages cannot be reused as pvec safely without I/O */
114 if (type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && !pvec_safereuse)
115 type = Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED;
116
117 if (type != Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
118 ctor->index + 1 == ctor->nr)
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800119 return false;
Gao Xiang86432a62021-11-04 02:20:06 +0800120 }
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800121
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800122 if (ctor->index >= ctor->nr)
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800123 z_erofs_pagevec_ctor_pagedown(ctor, false);
124
125 /* exclusive page type must be 0 */
126 if (Z_EROFS_PAGE_TYPE_EXCLUSIVE != (uintptr_t)NULL)
127 __bad_page_type_exclusive();
128
129 /* should remind that collector->next never equal to 1, 2 */
130 if (type == (uintptr_t)ctor->next) {
131 ctor->next = page;
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800132 }
Gao Xiang046d64e2019-07-31 23:57:45 +0800133 ctor->pages[ctor->index++] = tagptr_fold(erofs_vtptr_t, page, type);
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800134 return true;
135}
136
137static inline struct page *
Gao Xiang046d64e2019-07-31 23:57:45 +0800138z_erofs_pagevec_dequeue(struct z_erofs_pagevec_ctor *ctor,
139 enum z_erofs_page_type *type)
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800140{
141 erofs_vtptr_t t;
142
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800143 if (ctor->index >= ctor->nr) {
Gao Xiang70b17992018-12-11 15:17:49 +0800144 DBG_BUGON(!ctor->next);
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800145 z_erofs_pagevec_ctor_pagedown(ctor, true);
146 }
147
148 t = ctor->pages[ctor->index];
149
150 *type = tagptr_unfold_tags(t);
151
152 /* should remind that collector->next never equal to 1, 2 */
153 if (*type == (uintptr_t)ctor->next)
154 ctor->next = tagptr_unfold_ptr(t);
155
Gao Xiang046d64e2019-07-31 23:57:45 +0800156 ctor->pages[ctor->index++] = tagptr_fold(erofs_vtptr_t, NULL, 0);
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800157 return tagptr_unfold_ptr(t);
158}
Gao Xiang5eb20ec2018-07-26 20:21:57 +0800159#endif