]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/erofs/unzip_pagevec.h
Merge branch 'for-linus-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/ibft
[linux.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)
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)
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         DBG_BUGON(nr >= ctor->nr);
73         return NULL;
74 }
75
76 static inline void
77 z_erofs_pagevec_ctor_pagedown(struct z_erofs_pagevec_ctor *ctor,
78                               bool atomic)
79 {
80         struct page *next = z_erofs_pagevec_ctor_next_page(ctor, ctor->nr);
81
82         z_erofs_pagevec_ctor_exit(ctor, atomic);
83
84         ctor->curr = next;
85         ctor->next = NULL;
86         ctor->pages = atomic ?
87                 kmap_atomic(ctor->curr) : kmap(ctor->curr);
88
89         ctor->nr = PAGE_SIZE / sizeof(struct page *);
90         ctor->index = 0;
91 }
92
93 static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor,
94                                              unsigned nr,
95                                              erofs_vtptr_t *pages, unsigned i)
96 {
97         ctor->nr = nr;
98         ctor->curr = ctor->next = NULL;
99         ctor->pages = pages;
100
101         if (i >= nr) {
102                 i -= nr;
103                 z_erofs_pagevec_ctor_pagedown(ctor, false);
104                 while (i > ctor->nr) {
105                         i -= ctor->nr;
106                         z_erofs_pagevec_ctor_pagedown(ctor, false);
107                 }
108         }
109
110         ctor->next = z_erofs_pagevec_ctor_next_page(ctor, i);
111         ctor->index = i;
112 }
113
114 static inline bool
115 z_erofs_pagevec_ctor_enqueue(struct z_erofs_pagevec_ctor *ctor,
116                              struct page *page,
117                              enum z_erofs_page_type type,
118                              bool *occupied)
119 {
120         *occupied = false;
121         if (unlikely(!ctor->next && type))
122                 if (ctor->index + 1 == ctor->nr)
123                         return false;
124
125         if (unlikely(ctor->index >= ctor->nr))
126                 z_erofs_pagevec_ctor_pagedown(ctor, false);
127
128         /* exclusive page type must be 0 */
129         if (Z_EROFS_PAGE_TYPE_EXCLUSIVE != (uintptr_t)NULL)
130                 __bad_page_type_exclusive();
131
132         /* should remind that collector->next never equal to 1, 2 */
133         if (type == (uintptr_t)ctor->next) {
134                 ctor->next = page;
135                 *occupied = true;
136         }
137
138         ctor->pages[ctor->index++] =
139                 tagptr_fold(erofs_vtptr_t, page, type);
140         return true;
141 }
142
143 static inline struct page *
144 z_erofs_pagevec_ctor_dequeue(struct z_erofs_pagevec_ctor *ctor,
145                              enum z_erofs_page_type *type)
146 {
147         erofs_vtptr_t t;
148
149         if (unlikely(ctor->index >= ctor->nr)) {
150                 DBG_BUGON(!ctor->next);
151                 z_erofs_pagevec_ctor_pagedown(ctor, true);
152         }
153
154         t = ctor->pages[ctor->index];
155
156         *type = tagptr_unfold_tags(t);
157
158         /* should remind that collector->next never equal to 1, 2 */
159         if (*type == (uintptr_t)ctor->next)
160                 ctor->next = tagptr_unfold_ptr(t);
161
162         ctor->pages[ctor->index++] =
163                 tagptr_fold(erofs_vtptr_t, NULL, 0);
164
165         return tagptr_unfold_ptr(t);
166 }
167
168 #endif
169