1 // SPDX-License-Identifier: GPL-2.0-only
4 * Phillip Lougher <phillip@squashfs.org.uk>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/pagemap.h>
10 #include "squashfs_fs_sb.h"
11 #include "decompressor.h"
12 #include "page_actor.h"
15 * This file contains implementations of page_actor for decompressing into
16 * an intermediate buffer, and for decompressing directly into the
19 * Calling code should avoid sleeping between calls to squashfs_first_page()
20 * and squashfs_finish_page().
23 /* Implementation of page_actor for decompressing into intermediate buffer */
24 static void *cache_first_page(struct squashfs_page_actor *actor)
27 return actor->buffer[0];
30 static void *cache_next_page(struct squashfs_page_actor *actor)
32 if (actor->next_page == actor->pages)
35 return actor->buffer[actor->next_page++];
38 static void cache_finish_page(struct squashfs_page_actor *actor)
43 struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
44 int pages, int length)
46 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
51 actor->length = length ? : pages * PAGE_SIZE;
52 actor->buffer = buffer;
55 actor->tmp_buffer = NULL;
56 actor->squashfs_first_page = cache_first_page;
57 actor->squashfs_next_page = cache_next_page;
58 actor->squashfs_finish_page = cache_finish_page;
62 /* Implementation of page_actor for decompressing directly into page cache. */
63 static void *handle_next_page(struct squashfs_page_actor *actor)
65 int max_pages = (actor->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
67 if (actor->returned_pages == max_pages)
70 if ((actor->next_page == actor->pages) ||
71 (actor->next_index != actor->page[actor->next_page]->index)) {
73 actor->returned_pages++;
74 actor->last_page = NULL;
75 return actor->alloc_buffer ? actor->tmp_buffer : ERR_PTR(-ENOMEM);
79 actor->returned_pages++;
80 actor->last_page = actor->page[actor->next_page];
81 return actor->pageaddr = kmap_local_page(actor->page[actor->next_page++]);
84 static void *direct_first_page(struct squashfs_page_actor *actor)
86 return handle_next_page(actor);
89 static void *direct_next_page(struct squashfs_page_actor *actor)
91 if (actor->pageaddr) {
92 kunmap_local(actor->pageaddr);
93 actor->pageaddr = NULL;
96 return handle_next_page(actor);
99 static void direct_finish_page(struct squashfs_page_actor *actor)
102 kunmap_local(actor->pageaddr);
105 struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_info *msblk,
106 struct page **page, int pages, int length)
108 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
113 if (msblk->decompressor->alloc_buffer) {
114 actor->tmp_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
116 if (actor->tmp_buffer == NULL) {
121 actor->tmp_buffer = NULL;
123 actor->length = length ? : pages * PAGE_SIZE;
125 actor->pages = pages;
126 actor->next_page = 0;
127 actor->returned_pages = 0;
128 actor->next_index = page[0]->index & ~((1 << (msblk->block_log - PAGE_SHIFT)) - 1);
129 actor->pageaddr = NULL;
130 actor->last_page = NULL;
131 actor->alloc_buffer = msblk->decompressor->alloc_buffer;
132 actor->squashfs_first_page = direct_first_page;
133 actor->squashfs_next_page = direct_next_page;
134 actor->squashfs_finish_page = direct_finish_page;