Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 3 | * Copyright (C) 2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 5 | */ |
Gao Xiang | 57b78c9 | 2019-07-31 23:57:32 +0800 | [diff] [blame] | 6 | #ifndef __EROFS_FS_ZPVEC_H |
| 7 | #define __EROFS_FS_ZPVEC_H |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 8 | |
Gao Xiang | 57b78c9 | 2019-07-31 23:57:32 +0800 | [diff] [blame] | 9 | #include "tagptr.h" |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 10 | |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 11 | /* page type in pagevec for decompress subsystem */ |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 12 | enum 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 | |
| 22 | extern void __compiletime_error("Z_EROFS_PAGE_TYPE_EXCLUSIVE != 0") |
| 23 | __bad_page_type_exclusive(void); |
| 24 | |
| 25 | /* pagevec tagged pointer */ |
| 26 | typedef tagptr2_t erofs_vtptr_t; |
| 27 | |
| 28 | /* pagevec collector */ |
| 29 | struct z_erofs_pagevec_ctor { |
| 30 | struct page *curr, *next; |
| 31 | erofs_vtptr_t *pages; |
| 32 | |
| 33 | unsigned int nr, index; |
| 34 | }; |
| 35 | |
| 36 | static inline void z_erofs_pagevec_ctor_exit(struct z_erofs_pagevec_ctor *ctor, |
| 37 | bool atomic) |
| 38 | { |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 39 | if (!ctor->curr) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 40 | return; |
| 41 | |
| 42 | if (atomic) |
| 43 | kunmap_atomic(ctor->pages); |
| 44 | else |
| 45 | kunmap(ctor->curr); |
| 46 | } |
| 47 | |
| 48 | static inline struct page * |
| 49 | z_erofs_pagevec_ctor_next_page(struct z_erofs_pagevec_ctor *ctor, |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 50 | unsigned int nr) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 51 | { |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 52 | unsigned int index; |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 53 | |
| 54 | /* keep away from occupied pages */ |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 55 | if (ctor->next) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 56 | return ctor->next; |
| 57 | |
| 58 | for (index = 0; index < nr; ++index) { |
| 59 | const erofs_vtptr_t t = ctor->pages[index]; |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 60 | const unsigned int tags = tagptr_unfold_tags(t); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 61 | |
| 62 | if (tags == Z_EROFS_PAGE_TYPE_EXCLUSIVE) |
| 63 | return tagptr_unfold_ptr(t); |
| 64 | } |
Hariprasad Kelam | 3fb58b8 | 2019-05-19 17:35:57 +0800 | [diff] [blame] | 65 | DBG_BUGON(nr >= ctor->nr); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | static inline void |
| 70 | z_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 | |
| 86 | static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor, |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 87 | unsigned int nr, |
| 88 | erofs_vtptr_t *pages, |
| 89 | unsigned int i) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 90 | { |
| 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 Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 103 | ctor->next = z_erofs_pagevec_ctor_next_page(ctor, i); |
| 104 | ctor->index = i; |
| 105 | } |
| 106 | |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 107 | static inline bool z_erofs_pagevec_enqueue(struct z_erofs_pagevec_ctor *ctor, |
| 108 | struct page *page, |
Gao Xiang | 86432a6 | 2021-11-04 02:20:06 +0800 | [diff] [blame] | 109 | enum z_erofs_page_type type, |
| 110 | bool pvec_safereuse) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 111 | { |
Gao Xiang | 86432a6 | 2021-11-04 02:20:06 +0800 | [diff] [blame] | 112 | 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 Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 119 | return false; |
Gao Xiang | 86432a6 | 2021-11-04 02:20:06 +0800 | [diff] [blame] | 120 | } |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 121 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 122 | if (ctor->index >= ctor->nr) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 123 | 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 Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 132 | } |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 133 | ctor->pages[ctor->index++] = tagptr_fold(erofs_vtptr_t, page, type); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | static inline struct page * |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 138 | z_erofs_pagevec_dequeue(struct z_erofs_pagevec_ctor *ctor, |
| 139 | enum z_erofs_page_type *type) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 140 | { |
| 141 | erofs_vtptr_t t; |
| 142 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 143 | if (ctor->index >= ctor->nr) { |
Gao Xiang | 70b1799 | 2018-12-11 15:17:49 +0800 | [diff] [blame] | 144 | DBG_BUGON(!ctor->next); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 145 | 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 Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 156 | ctor->pages[ctor->index++] = tagptr_fold(erofs_vtptr_t, NULL, 0); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 157 | return tagptr_unfold_ptr(t); |
| 158 | } |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 159 | #endif |