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