^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) 2013
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Phillip Lougher <phillip@squashfs.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "page_actor.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * This file contains implementations of page_actor for decompressing into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * an intermediate buffer, and for decompressing directly into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Calling code should avoid sleeping between calls to squashfs_first_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * and squashfs_finish_page().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Implementation of page_actor for decompressing into intermediate buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void *cache_first_page(struct squashfs_page_actor *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) actor->next_page = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return actor->buffer[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static void *cache_next_page(struct squashfs_page_actor *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (actor->next_page == actor->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return actor->buffer[actor->next_page++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void cache_finish_page(struct squashfs_page_actor *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int pages, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (actor == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) actor->length = length ? : pages * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) actor->buffer = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) actor->pages = pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) actor->next_page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) actor->squashfs_first_page = cache_first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) actor->squashfs_next_page = cache_next_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) actor->squashfs_finish_page = cache_finish_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return actor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Implementation of page_actor for decompressing directly into page cache. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void *direct_first_page(struct squashfs_page_actor *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) actor->next_page = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return actor->pageaddr = kmap_atomic(actor->page[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void *direct_next_page(struct squashfs_page_actor *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (actor->pageaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) kunmap_atomic(actor->pageaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return actor->pageaddr = actor->next_page == actor->pages ? NULL :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kmap_atomic(actor->page[actor->next_page++]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void direct_finish_page(struct squashfs_page_actor *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (actor->pageaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) kunmap_atomic(actor->pageaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct squashfs_page_actor *squashfs_page_actor_init_special(struct page **page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int pages, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (actor == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) actor->length = length ? : pages * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) actor->page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) actor->pages = pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) actor->next_page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) actor->pageaddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) actor->squashfs_first_page = direct_first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) actor->squashfs_next_page = direct_next_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) actor->squashfs_finish_page = direct_finish_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return actor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }