Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / fs / squashfs / file_direct.c
1 /*
2  * Copyright (c) 2013
3  * Phillip Lougher <phillip@squashfs.org.uk>
4  *
5  * This work is licensed under the terms of the GNU GPL, version 2. See
6  * the COPYING file in the top-level directory.
7  */
8
9 #include <linux/fs.h>
10 #include <linux/vfs.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/pagemap.h>
15 #include <linux/mutex.h>
16
17 #include "squashfs_fs.h"
18 #include "squashfs_fs_sb.h"
19 #include "squashfs_fs_i.h"
20 #include "squashfs.h"
21 #include "page_actor.h"
22
23 static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
24         int pages, struct page **page);
25
26 /* Read separately compressed datablock directly into page cache */
27 int squashfs_readpage_block(struct page *target_page, u64 block, int bsize)
28
29 {
30         struct inode *inode = target_page->mapping->host;
31         struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
32
33         int file_end = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
34         int mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
35         int start_index = target_page->index & ~mask;
36         int end_index = start_index | mask;
37         int i, n, pages, missing_pages, bytes, res = -ENOMEM;
38         struct page **page;
39         struct squashfs_page_actor *actor;
40         void *pageaddr;
41
42         if (end_index > file_end)
43                 end_index = file_end;
44
45         pages = end_index - start_index + 1;
46
47         page = kmalloc(sizeof(void *) * pages, GFP_KERNEL);
48         if (page == NULL)
49                 return res;
50
51         /*
52          * Create a "page actor" which will kmap and kunmap the
53          * page cache pages appropriately within the decompressor
54          */
55         actor = squashfs_page_actor_init_special(page, pages, 0);
56         if (actor == NULL)
57                 goto out;
58
59         /* Try to grab all the pages covered by the Squashfs block */
60         for (missing_pages = 0, i = 0, n = start_index; i < pages; i++, n++) {
61                 page[i] = (n == target_page->index) ? target_page :
62                         grab_cache_page_nowait(target_page->mapping, n);
63
64                 if (page[i] == NULL) {
65                         missing_pages++;
66                         continue;
67                 }
68
69                 if (PageUptodate(page[i])) {
70                         unlock_page(page[i]);
71                         page_cache_release(page[i]);
72                         page[i] = NULL;
73                         missing_pages++;
74                 }
75         }
76
77         if (missing_pages) {
78                 /*
79                  * Couldn't get one or more pages, this page has either
80                  * been VM reclaimed, but others are still in the page cache
81                  * and uptodate, or we're racing with another thread in
82                  * squashfs_readpage also trying to grab them.  Fall back to
83                  * using an intermediate buffer.
84                  */
85                 res = squashfs_read_cache(target_page, block, bsize, pages,
86                                                                 page);
87                 goto out;
88         }
89
90         /* Decompress directly into the page cache buffers */
91         res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
92         if (res < 0)
93                 goto mark_errored;
94
95         /* Last page may have trailing bytes not filled */
96         bytes = res % PAGE_CACHE_SIZE;
97         if (bytes) {
98                 pageaddr = kmap_atomic(page[pages - 1]);
99                 memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
100                 kunmap_atomic(pageaddr);
101         }
102
103         /* Mark pages as uptodate, unlock and release */
104         for (i = 0; i < pages; i++) {
105                 flush_dcache_page(page[i]);
106                 SetPageUptodate(page[i]);
107                 unlock_page(page[i]);
108                 if (page[i] != target_page)
109                         page_cache_release(page[i]);
110         }
111
112         kfree(actor);
113         kfree(page);
114
115         return 0;
116
117 mark_errored:
118         /* Decompression failed, mark pages as errored.  Target_page is
119          * dealt with by the caller
120          */
121         for (i = 0; i < pages; i++) {
122                 if (page[i] == target_page)
123                         continue;
124                 flush_dcache_page(page[i]);
125                 SetPageError(page[i]);
126                 unlock_page(page[i]);
127                 page_cache_release(page[i]);
128         }
129
130 out:
131         kfree(actor);
132         kfree(page);
133         return res;
134 }
135
136
137 static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
138         int pages, struct page **page)
139 {
140         struct inode *i = target_page->mapping->host;
141         struct squashfs_cache_entry *buffer = squashfs_get_datablock(i->i_sb,
142                                                  block, bsize);
143         int bytes = buffer->length, res = buffer->error, n, offset = 0;
144         void *pageaddr;
145
146         if (res) {
147                 ERROR("Unable to read page, block %llx, size %x\n", block,
148                         bsize);
149                 goto out;
150         }
151
152         for (n = 0; n < pages && bytes > 0; n++,
153                         bytes -= PAGE_CACHE_SIZE, offset += PAGE_CACHE_SIZE) {
154                 int avail = min_t(int, bytes, PAGE_CACHE_SIZE);
155
156                 if (page[n] == NULL)
157                         continue;
158
159                 pageaddr = kmap_atomic(page[n]);
160                 squashfs_copy_data(pageaddr, buffer, offset, avail);
161                 memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
162                 kunmap_atomic(pageaddr);
163                 flush_dcache_page(page[n]);
164                 SetPageUptodate(page[n]);
165                 unlock_page(page[n]);
166                 if (page[n] != target_page)
167                         page_cache_release(page[n]);
168         }
169
170 out:
171         squashfs_cache_put(buffer);
172         return res;
173 }