GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / erofs / unzip_pagevec.h
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * linux/drivers/staging/erofs/unzip_pagevec.h
4  *
5  * Copyright (C) 2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of the Linux
11  * distribution for more details.
12  */
13 #ifndef __EROFS_UNZIP_PAGEVEC_H
14 #define __EROFS_UNZIP_PAGEVEC_H
15
16 #include <linux/tagptr.h>
17
18 /* page type in pagevec for unzip subsystem */
19 enum z_erofs_page_type {
20         /* including Z_EROFS_VLE_PAGE_TAIL_EXCLUSIVE */
21         Z_EROFS_PAGE_TYPE_EXCLUSIVE,
22
23         Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED,
24
25         Z_EROFS_VLE_PAGE_TYPE_HEAD,
26         Z_EROFS_VLE_PAGE_TYPE_MAX
27 };
28
29 extern void __compiletime_error("Z_EROFS_PAGE_TYPE_EXCLUSIVE != 0")
30         __bad_page_type_exclusive(void);
31
32 /* pagevec tagged pointer */
33 typedef tagptr2_t       erofs_vtptr_t;
34
35 /* pagevec collector */
36 struct z_erofs_pagevec_ctor {
37         struct page *curr, *next;
38         erofs_vtptr_t *pages;
39
40         unsigned int nr, index;
41 };
42
43 static inline void z_erofs_pagevec_ctor_exit(struct z_erofs_pagevec_ctor *ctor,
44                                              bool atomic)
45 {
46         if (ctor->curr == NULL)
47                 return;
48
49         if (atomic)
50                 kunmap_atomic(ctor->pages);
51         else
52                 kunmap(ctor->curr);
53 }
54
55 static inline struct page *
56 z_erofs_pagevec_ctor_next_page(struct z_erofs_pagevec_ctor *ctor,
57                                unsigned nr)
58 {
59         unsigned index;
60
61         /* keep away from occupied pages */
62         if (ctor->next != NULL)
63                 return ctor->next;
64
65         for (index = 0; index < nr; ++index) {
66                 const erofs_vtptr_t t = ctor->pages[index];
67                 const unsigned tags = tagptr_unfold_tags(t);
68
69                 if (tags == Z_EROFS_PAGE_TYPE_EXCLUSIVE)
70                         return tagptr_unfold_ptr(t);
71         }
72
73         if (unlikely(nr >= ctor->nr))
74                 BUG();
75
76         return NULL;
77 }
78
79 static inline void
80 z_erofs_pagevec_ctor_pagedown(struct z_erofs_pagevec_ctor *ctor,
81                               bool atomic)
82 {
83         struct page *next = z_erofs_pagevec_ctor_next_page(ctor, ctor->nr);
84
85         z_erofs_pagevec_ctor_exit(ctor, atomic);
86
87         ctor->curr = next;
88         ctor->next = NULL;
89         ctor->pages = atomic ?
90                 kmap_atomic(ctor->curr) : kmap(ctor->curr);
91
92         ctor->nr = PAGE_SIZE / sizeof(struct page *);
93         ctor->index = 0;
94 }
95
96 static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor,
97                                              unsigned nr,
98                                              erofs_vtptr_t *pages, unsigned i)
99 {
100         ctor->nr = nr;
101         ctor->curr = ctor->next = NULL;
102         ctor->pages = pages;
103
104         if (i >= nr) {
105                 i -= nr;
106                 z_erofs_pagevec_ctor_pagedown(ctor, false);
107                 while (i > ctor->nr) {
108                         i -= ctor->nr;
109                         z_erofs_pagevec_ctor_pagedown(ctor, false);
110                 }
111         }
112
113         ctor->next = z_erofs_pagevec_ctor_next_page(ctor, i);
114         ctor->index = i;
115 }
116
117 static inline bool
118 z_erofs_pagevec_ctor_enqueue(struct z_erofs_pagevec_ctor *ctor,
119                              struct page *page,
120                              enum z_erofs_page_type type,
121                              bool pvec_safereuse)
122 {
123         if (!ctor->next) {
124                 /* some pages cannot be reused as pvec safely without I/O */
125                 if (type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && !pvec_safereuse)
126                         type = Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED;
127
128                 if (type != Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
129                     ctor->index + 1 == ctor->nr)
130                         return false;
131         }
132
133         if (unlikely(ctor->index >= ctor->nr))
134                 z_erofs_pagevec_ctor_pagedown(ctor, false);
135
136         /* exclusive page type must be 0 */
137         if (Z_EROFS_PAGE_TYPE_EXCLUSIVE != (uintptr_t)NULL)
138                 __bad_page_type_exclusive();
139
140         /* should remind that collector->next never equal to 1, 2 */
141         if (type == (uintptr_t)ctor->next) {
142                 ctor->next = page;
143         }
144
145         ctor->pages[ctor->index++] =
146                 tagptr_fold(erofs_vtptr_t, page, type);
147         return true;
148 }
149
150 static inline struct page *
151 z_erofs_pagevec_ctor_dequeue(struct z_erofs_pagevec_ctor *ctor,
152                              enum z_erofs_page_type *type)
153 {
154         erofs_vtptr_t t;
155
156         if (unlikely(ctor->index >= ctor->nr)) {
157                 DBG_BUGON(!ctor->next);
158                 z_erofs_pagevec_ctor_pagedown(ctor, true);
159         }
160
161         t = ctor->pages[ctor->index];
162
163         *type = tagptr_unfold_tags(t);
164
165         /* should remind that collector->next never equal to 1, 2 */
166         if (*type == (uintptr_t)ctor->next)
167                 ctor->next = tagptr_unfold_ptr(t);
168
169         ctor->pages[ctor->index++] =
170                 tagptr_fold(erofs_vtptr_t, NULL, 0);
171
172         return tagptr_unfold_ptr(t);
173 }
174
175 #endif
176