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. |
| 4 | * http://www.huawei.com/ |
| 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 6 | */ |
Gao Xiang | 57b78c9 | 2019-07-31 23:57:32 +0800 | [diff] [blame] | 7 | #ifndef __EROFS_FS_ZPVEC_H |
| 8 | #define __EROFS_FS_ZPVEC_H |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 9 | |
Gao Xiang | 57b78c9 | 2019-07-31 23:57:32 +0800 | [diff] [blame] | 10 | #include "tagptr.h" |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 11 | |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 12 | /* page type in pagevec for decompress subsystem */ |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 13 | enum z_erofs_page_type { |
| 14 | /* including Z_EROFS_VLE_PAGE_TAIL_EXCLUSIVE */ |
| 15 | Z_EROFS_PAGE_TYPE_EXCLUSIVE, |
| 16 | |
| 17 | Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED, |
| 18 | |
| 19 | Z_EROFS_VLE_PAGE_TYPE_HEAD, |
| 20 | Z_EROFS_VLE_PAGE_TYPE_MAX |
| 21 | }; |
| 22 | |
| 23 | extern void __compiletime_error("Z_EROFS_PAGE_TYPE_EXCLUSIVE != 0") |
| 24 | __bad_page_type_exclusive(void); |
| 25 | |
| 26 | /* pagevec tagged pointer */ |
| 27 | typedef tagptr2_t erofs_vtptr_t; |
| 28 | |
| 29 | /* pagevec collector */ |
| 30 | struct z_erofs_pagevec_ctor { |
| 31 | struct page *curr, *next; |
| 32 | erofs_vtptr_t *pages; |
| 33 | |
| 34 | unsigned int nr, index; |
| 35 | }; |
| 36 | |
| 37 | static inline void z_erofs_pagevec_ctor_exit(struct z_erofs_pagevec_ctor *ctor, |
| 38 | bool atomic) |
| 39 | { |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 40 | if (!ctor->curr) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 41 | return; |
| 42 | |
| 43 | if (atomic) |
| 44 | kunmap_atomic(ctor->pages); |
| 45 | else |
| 46 | kunmap(ctor->curr); |
| 47 | } |
| 48 | |
| 49 | static inline struct page * |
| 50 | z_erofs_pagevec_ctor_next_page(struct z_erofs_pagevec_ctor *ctor, |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 51 | unsigned int nr) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 52 | { |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 53 | unsigned int index; |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 54 | |
| 55 | /* keep away from occupied pages */ |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 56 | if (ctor->next) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 57 | return ctor->next; |
| 58 | |
| 59 | for (index = 0; index < nr; ++index) { |
| 60 | const erofs_vtptr_t t = ctor->pages[index]; |
Pratik Shinde | e82a9a1 | 2019-07-15 17:51:27 +0530 | [diff] [blame] | 61 | const unsigned int tags = tagptr_unfold_tags(t); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 62 | |
| 63 | if (tags == Z_EROFS_PAGE_TYPE_EXCLUSIVE) |
| 64 | return tagptr_unfold_ptr(t); |
| 65 | } |
Hariprasad Kelam | 3fb58b8 | 2019-05-19 17:35:57 +0800 | [diff] [blame] | 66 | DBG_BUGON(nr >= ctor->nr); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | static inline void |
| 71 | z_erofs_pagevec_ctor_pagedown(struct z_erofs_pagevec_ctor *ctor, |
| 72 | bool atomic) |
| 73 | { |
| 74 | struct page *next = z_erofs_pagevec_ctor_next_page(ctor, ctor->nr); |
| 75 | |
| 76 | z_erofs_pagevec_ctor_exit(ctor, atomic); |
| 77 | |
| 78 | ctor->curr = next; |
| 79 | ctor->next = NULL; |
| 80 | ctor->pages = atomic ? |
| 81 | kmap_atomic(ctor->curr) : kmap(ctor->curr); |
| 82 | |
| 83 | ctor->nr = PAGE_SIZE / sizeof(struct page *); |
| 84 | ctor->index = 0; |
| 85 | } |
| 86 | |
| 87 | 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] | 88 | unsigned int nr, |
| 89 | erofs_vtptr_t *pages, |
| 90 | unsigned int i) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 91 | { |
| 92 | ctor->nr = nr; |
| 93 | ctor->curr = ctor->next = NULL; |
| 94 | ctor->pages = pages; |
| 95 | |
| 96 | if (i >= nr) { |
| 97 | i -= nr; |
| 98 | z_erofs_pagevec_ctor_pagedown(ctor, false); |
| 99 | while (i > ctor->nr) { |
| 100 | i -= ctor->nr; |
| 101 | z_erofs_pagevec_ctor_pagedown(ctor, false); |
| 102 | } |
| 103 | } |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 104 | ctor->next = z_erofs_pagevec_ctor_next_page(ctor, i); |
| 105 | ctor->index = i; |
| 106 | } |
| 107 | |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 108 | static inline bool z_erofs_pagevec_enqueue(struct z_erofs_pagevec_ctor *ctor, |
| 109 | struct page *page, |
| 110 | enum z_erofs_page_type type, |
| 111 | bool *occupied) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 112 | { |
| 113 | *occupied = false; |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 114 | if (!ctor->next && type) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 115 | if (ctor->index + 1 == ctor->nr) |
| 116 | return false; |
| 117 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 118 | if (ctor->index >= ctor->nr) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 119 | z_erofs_pagevec_ctor_pagedown(ctor, false); |
| 120 | |
| 121 | /* exclusive page type must be 0 */ |
| 122 | if (Z_EROFS_PAGE_TYPE_EXCLUSIVE != (uintptr_t)NULL) |
| 123 | __bad_page_type_exclusive(); |
| 124 | |
| 125 | /* should remind that collector->next never equal to 1, 2 */ |
| 126 | if (type == (uintptr_t)ctor->next) { |
| 127 | ctor->next = page; |
| 128 | *occupied = true; |
| 129 | } |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 130 | ctor->pages[ctor->index++] = tagptr_fold(erofs_vtptr_t, page, type); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static inline struct page * |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 135 | z_erofs_pagevec_dequeue(struct z_erofs_pagevec_ctor *ctor, |
| 136 | enum z_erofs_page_type *type) |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 137 | { |
| 138 | erofs_vtptr_t t; |
| 139 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 140 | if (ctor->index >= ctor->nr) { |
Gao Xiang | 70b1799 | 2018-12-11 15:17:49 +0800 | [diff] [blame] | 141 | DBG_BUGON(!ctor->next); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 142 | z_erofs_pagevec_ctor_pagedown(ctor, true); |
| 143 | } |
| 144 | |
| 145 | t = ctor->pages[ctor->index]; |
| 146 | |
| 147 | *type = tagptr_unfold_tags(t); |
| 148 | |
| 149 | /* should remind that collector->next never equal to 1, 2 */ |
| 150 | if (*type == (uintptr_t)ctor->next) |
| 151 | ctor->next = tagptr_unfold_ptr(t); |
| 152 | |
Gao Xiang | 046d64e | 2019-07-31 23:57:45 +0800 | [diff] [blame] | 153 | ctor->pages[ctor->index++] = tagptr_fold(erofs_vtptr_t, NULL, 0); |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 154 | return tagptr_unfold_ptr(t); |
| 155 | } |
Gao Xiang | 5eb20ec | 2018-07-26 20:21:57 +0800 | [diff] [blame] | 156 | #endif |
| 157 | |