Merge tag '5.20-rc-smb3-client-fixes-part1' of git://git.samba.org/sfrench/cifs-2.6
[linux-2.6-microblaze.git] / fs / squashfs / page_actor.c
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 */
24 static void *cache_first_page(struct squashfs_page_actor *actor)
25 {
26         actor->next_page = 1;
27         return actor->buffer[0];
28 }
29
30 static 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
38 static void cache_finish_page(struct squashfs_page_actor *actor)
39 {
40         /* empty */
41 }
42
43 struct 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->squashfs_first_page = cache_first_page;
56         actor->squashfs_next_page = cache_next_page;
57         actor->squashfs_finish_page = cache_finish_page;
58         return actor;
59 }
60
61 /* Implementation of page_actor for decompressing directly into page cache. */
62 static void *handle_next_page(struct squashfs_page_actor *actor)
63 {
64         int max_pages = (actor->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
65
66         if (actor->returned_pages == max_pages)
67                 return NULL;
68
69         if ((actor->next_page == actor->pages) ||
70                         (actor->next_index != actor->page[actor->next_page]->index)) {
71                 if (actor->alloc_buffer) {
72                         void *tmp_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
73
74                         if (tmp_buffer) {
75                                 actor->tmp_buffer = tmp_buffer;
76                                 actor->next_index++;
77                                 actor->returned_pages++;
78                                 return tmp_buffer;
79                         }
80                 }
81
82                 actor->next_index++;
83                 actor->returned_pages++;
84                 return ERR_PTR(-ENOMEM);
85         }
86
87         actor->next_index++;
88         actor->returned_pages++;
89         return actor->pageaddr = kmap_local_page(actor->page[actor->next_page++]);
90 }
91
92 static void *direct_first_page(struct squashfs_page_actor *actor)
93 {
94         return handle_next_page(actor);
95 }
96
97 static void *direct_next_page(struct squashfs_page_actor *actor)
98 {
99         if (actor->pageaddr)
100                 kunmap_local(actor->pageaddr);
101
102         kfree(actor->tmp_buffer);
103         actor->pageaddr = actor->tmp_buffer = NULL;
104
105         return handle_next_page(actor);
106 }
107
108 static void direct_finish_page(struct squashfs_page_actor *actor)
109 {
110         if (actor->pageaddr)
111                 kunmap_local(actor->pageaddr);
112
113         kfree(actor->tmp_buffer);
114 }
115
116 struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_info *msblk,
117         struct page **page, int pages, int length)
118 {
119         struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
120
121         if (actor == NULL)
122                 return NULL;
123
124         actor->length = length ? : pages * PAGE_SIZE;
125         actor->page = page;
126         actor->pages = pages;
127         actor->next_page = 0;
128         actor->returned_pages = 0;
129         actor->next_index = page[0]->index & ~((1 << (msblk->block_log - PAGE_SHIFT)) - 1);
130         actor->pageaddr = NULL;
131         actor->tmp_buffer = NULL;
132         actor->alloc_buffer = msblk->decompressor->alloc_buffer;
133         actor->squashfs_first_page = direct_first_page;
134         actor->squashfs_next_page = direct_next_page;
135         actor->squashfs_finish_page = direct_finish_page;
136         return actor;
137 }