Blame view

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