Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2013
4 * Phillip Lougher <phillip@squashfs.org.uk>
5 */
6
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"
13
14/*
15 * This file contains implementations of page_actor for decompressing into
16 * an intermediate buffer, and for decompressing directly into the
17 * page cache.
18 *
19 * Calling code should avoid sleeping between calls to squashfs_first_page()
20 * and squashfs_finish_page().
21 */
22
23/* Implementation of page_actor for decompressing into intermediate buffer */
24static void *cache_first_page(struct squashfs_page_actor *actor)
25{
26 actor->next_page = 1;
27 return actor->buffer[0];
28}
29
30static void *cache_next_page(struct squashfs_page_actor *actor)
31{
32 if (actor->next_page == actor->pages)
33 return NULL;
34
35 return actor->buffer[actor->next_page++];
36}
37
38static void cache_finish_page(struct squashfs_page_actor *actor)
39{
40 /* empty */
41}
42
43struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
44 int pages, int length)
45{
46 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
47
48 if (actor == NULL)
49 return NULL;
50
51 actor->length = length ? : pages * PAGE_SIZE;
52 actor->buffer = buffer;
53 actor->pages = pages;
54 actor->next_page = 0;
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;
59 return actor;
60}
61
62/* Implementation of page_actor for decompressing directly into page cache. */
63static void *handle_next_page(struct squashfs_page_actor *actor)
64{
65 int max_pages = (actor->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
66
67 if (actor->returned_pages == max_pages)
68 return NULL;
69
70 if ((actor->next_page == actor->pages) ||
71 (actor->next_index != actor->page[actor->next_page]->index)) {
72 actor->next_index++;
73 actor->returned_pages++;
74 actor->last_page = NULL;
75 return actor->alloc_buffer ? actor->tmp_buffer : ERR_PTR(-ENOMEM);
76 }
77
78 actor->next_index++;
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++]);
82}
83
84static void *direct_first_page(struct squashfs_page_actor *actor)
85{
86 return handle_next_page(actor);
87}
88
89static void *direct_next_page(struct squashfs_page_actor *actor)
90{
91 if (actor->pageaddr) {
92 kunmap_local(actor->pageaddr);
93 actor->pageaddr = NULL;
94 }
95
96 return handle_next_page(actor);
97}
98
99static void direct_finish_page(struct squashfs_page_actor *actor)
100{
101 if (actor->pageaddr)
102 kunmap_local(actor->pageaddr);
103}
104
105struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_info *msblk,
106 struct page **page, int pages, int length)
107{
108 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
109
110 if (actor == NULL)
111 return NULL;
112
113 if (msblk->decompressor->alloc_buffer) {
114 actor->tmp_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
115
116 if (actor->tmp_buffer == NULL) {
117 kfree(actor);
118 return NULL;
119 }
120 } else
121 actor->tmp_buffer = NULL;
122
123 actor->length = length ? : pages * PAGE_SIZE;
124 actor->page = page;
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;
135 return actor;
136}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2013
4 * Phillip Lougher <phillip@squashfs.org.uk>
5 */
6
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/pagemap.h>
10#include "page_actor.h"
11
12/*
13 * This file contains implementations of page_actor for decompressing into
14 * an intermediate buffer, and for decompressing directly into the
15 * page cache.
16 *
17 * Calling code should avoid sleeping between calls to squashfs_first_page()
18 * and squashfs_finish_page().
19 */
20
21/* Implementation of page_actor for decompressing into intermediate buffer */
22static void *cache_first_page(struct squashfs_page_actor *actor)
23{
24 actor->next_page = 1;
25 return actor->buffer[0];
26}
27
28static void *cache_next_page(struct squashfs_page_actor *actor)
29{
30 if (actor->next_page == actor->pages)
31 return NULL;
32
33 return actor->buffer[actor->next_page++];
34}
35
36static void cache_finish_page(struct squashfs_page_actor *actor)
37{
38 /* empty */
39}
40
41struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
42 int pages, int length)
43{
44 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
45
46 if (actor == NULL)
47 return NULL;
48
49 actor->length = length ? : pages * PAGE_SIZE;
50 actor->buffer = buffer;
51 actor->pages = pages;
52 actor->next_page = 0;
53 actor->squashfs_first_page = cache_first_page;
54 actor->squashfs_next_page = cache_next_page;
55 actor->squashfs_finish_page = cache_finish_page;
56 return actor;
57}
58
59/* Implementation of page_actor for decompressing directly into page cache. */
60static void *direct_first_page(struct squashfs_page_actor *actor)
61{
62 actor->next_page = 1;
63 return actor->pageaddr = kmap_atomic(actor->page[0]);
64}
65
66static void *direct_next_page(struct squashfs_page_actor *actor)
67{
68 if (actor->pageaddr)
69 kunmap_atomic(actor->pageaddr);
70
71 return actor->pageaddr = actor->next_page == actor->pages ? NULL :
72 kmap_atomic(actor->page[actor->next_page++]);
73}
74
75static void direct_finish_page(struct squashfs_page_actor *actor)
76{
77 if (actor->pageaddr)
78 kunmap_atomic(actor->pageaddr);
79}
80
81struct squashfs_page_actor *squashfs_page_actor_init_special(struct page **page,
82 int pages, int length)
83{
84 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
85
86 if (actor == NULL)
87 return NULL;
88
89 actor->length = length ? : pages * PAGE_SIZE;
90 actor->page = page;
91 actor->pages = pages;
92 actor->next_page = 0;
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;
97 return actor;
98}