]> Git Repo - linux.git/blame - fs/squashfs/page_actor.c
squashfs: don't call kmalloc in decompressors
[linux.git] / fs / squashfs / page_actor.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
0d455c12
PL
2/*
3 * Copyright (c) 2013
4 * Phillip Lougher <[email protected]>
0d455c12
PL
5 */
6
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/pagemap.h>
f268eedd
PL
10#include "squashfs_fs_sb.h"
11#include "decompressor.h"
0d455c12
PL
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
09cbfeaf 51 actor->length = length ? : pages * PAGE_SIZE;
0d455c12
PL
52 actor->buffer = buffer;
53 actor->pages = pages;
54 actor->next_page = 0;
1f13dff0 55 actor->tmp_buffer = NULL;
0d455c12
PL
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. */
f268eedd
PL
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)) {
f268eedd
PL
72 actor->next_index++;
73 actor->returned_pages++;
1f13dff0 74 return actor->alloc_buffer ? actor->tmp_buffer : ERR_PTR(-ENOMEM);
f268eedd
PL
75 }
76
77 actor->next_index++;
78 actor->returned_pages++;
79 return actor->pageaddr = kmap_local_page(actor->page[actor->next_page++]);
80}
81
0d455c12
PL
82static void *direct_first_page(struct squashfs_page_actor *actor)
83{
f268eedd 84 return handle_next_page(actor);
0d455c12
PL
85}
86
87static void *direct_next_page(struct squashfs_page_actor *actor)
88{
1f13dff0 89 if (actor->pageaddr) {
f268eedd 90 kunmap_local(actor->pageaddr);
1f13dff0
PL
91 actor->pageaddr = NULL;
92 }
0d455c12 93
f268eedd 94 return handle_next_page(actor);
0d455c12
PL
95}
96
97static void direct_finish_page(struct squashfs_page_actor *actor)
98{
99 if (actor->pageaddr)
f268eedd 100 kunmap_local(actor->pageaddr);
0d455c12
PL
101}
102
f268eedd
PL
103struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_info *msblk,
104 struct page **page, int pages, int length)
0d455c12
PL
105{
106 struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
107
108 if (actor == NULL)
109 return NULL;
110
1f13dff0
PL
111 if (msblk->decompressor->alloc_buffer) {
112 actor->tmp_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
113
114 if (actor->tmp_buffer == NULL) {
115 kfree(actor);
116 return NULL;
117 }
118 } else
119 actor->tmp_buffer = NULL;
120
09cbfeaf 121 actor->length = length ? : pages * PAGE_SIZE;
0d455c12
PL
122 actor->page = page;
123 actor->pages = pages;
124 actor->next_page = 0;
f268eedd
PL
125 actor->returned_pages = 0;
126 actor->next_index = page[0]->index & ~((1 << (msblk->block_log - PAGE_SHIFT)) - 1);
0d455c12 127 actor->pageaddr = NULL;
f268eedd 128 actor->alloc_buffer = msblk->decompressor->alloc_buffer;
0d455c12
PL
129 actor->squashfs_first_page = direct_first_page;
130 actor->squashfs_next_page = direct_next_page;
131 actor->squashfs_finish_page = direct_finish_page;
132 return actor;
133}
This page took 0.805532 seconds and 4 git commands to generate.