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 "page_actor.h"
13 * This file contains implementations of page_actor for decompressing into
14 * an intermediate buffer, and for decompressing directly into the
17 * Calling code should avoid sleeping between calls to squashfs_first_page()
18 * and squashfs_finish_page().
21 /* Implementation of page_actor for decompressing into intermediate buffer */
22 static void *cache_first_page(struct squashfs_page_actor *actor)
25 return actor->buffer[0];
28 static void *cache_next_page(struct squashfs_page_actor *actor)
30 if (actor->next_page == actor->pages)
33 return actor->buffer[actor->next_page++];
36 static void cache_finish_page(struct squashfs_page_actor *actor)
41 struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
42 int pages, int length)
44 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
49 actor->length = length ? : pages * PAGE_SIZE;
50 actor->buffer = buffer;
53 actor->squashfs_first_page = cache_first_page;
54 actor->squashfs_next_page = cache_next_page;
55 actor->squashfs_finish_page = cache_finish_page;
59 /* Implementation of page_actor for decompressing directly into page cache. */
60 static void *direct_first_page(struct squashfs_page_actor *actor)
63 return actor->pageaddr = kmap_atomic(actor->page[0]);
66 static void *direct_next_page(struct squashfs_page_actor *actor)
69 kunmap_atomic(actor->pageaddr);
71 return actor->pageaddr = actor->next_page == actor->pages ? NULL :
72 kmap_atomic(actor->page[actor->next_page++]);
75 static void direct_finish_page(struct squashfs_page_actor *actor)
78 kunmap_atomic(actor->pageaddr);
81 struct squashfs_page_actor *squashfs_page_actor_init_special(struct page **page,
82 int pages, int length)
84 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
89 actor->length = length ? : pages * PAGE_SIZE;
93 actor->pageaddr = NULL;
94 actor->squashfs_first_page = direct_first_page;
95 actor->squashfs_next_page = direct_next_page;
96 actor->squashfs_finish_page = direct_finish_page;