Linux Audio

Check our new training course

Loading...
v4.6
 
  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
 23static 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 */
 27int 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_SHIFT;
 34	int mask = (1 << (msblk->block_log - PAGE_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_array(pages, sizeof(void *), 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			put_page(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		if (res < 0)
 88			goto mark_errored;
 89
 
 
 
 
 
 
 
 90		goto out;
 91	}
 92
 93	/* Decompress directly into the page cache buffers */
 94	res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
 
 
 
 95	if (res < 0)
 96		goto mark_errored;
 97
 98	/* Last page may have trailing bytes not filled */
 
 
 
 
 
 99	bytes = res % PAGE_SIZE;
100	if (bytes) {
101		pageaddr = kmap_atomic(page[pages - 1]);
102		memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
103		kunmap_atomic(pageaddr);
104	}
105
106	/* Mark pages as uptodate, unlock and release */
107	for (i = 0; i < pages; i++) {
108		flush_dcache_page(page[i]);
109		SetPageUptodate(page[i]);
110		unlock_page(page[i]);
111		if (page[i] != target_page)
112			put_page(page[i]);
113	}
114
115	kfree(actor);
116	kfree(page);
117
118	return 0;
119
120mark_errored:
121	/* Decompression failed, mark pages as errored.  Target_page is
122	 * dealt with by the caller
123	 */
124	for (i = 0; i < pages; i++) {
125		if (page[i] == NULL || page[i] == target_page)
126			continue;
127		flush_dcache_page(page[i]);
128		SetPageError(page[i]);
129		unlock_page(page[i]);
130		put_page(page[i]);
131	}
132
133out:
134	kfree(actor);
135	kfree(page);
136	return res;
137}
138
139
140static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
141	int pages, struct page **page)
142{
143	struct inode *i = target_page->mapping->host;
144	struct squashfs_cache_entry *buffer = squashfs_get_datablock(i->i_sb,
145						 block, bsize);
146	int bytes = buffer->length, res = buffer->error, n, offset = 0;
147	void *pageaddr;
148
149	if (res) {
150		ERROR("Unable to read page, block %llx, size %x\n", block,
151			bsize);
152		goto out;
153	}
154
155	for (n = 0; n < pages && bytes > 0; n++,
156			bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
157		int avail = min_t(int, bytes, PAGE_SIZE);
158
159		if (page[n] == NULL)
160			continue;
161
162		pageaddr = kmap_atomic(page[n]);
163		squashfs_copy_data(pageaddr, buffer, offset, avail);
164		memset(pageaddr + avail, 0, PAGE_SIZE - avail);
165		kunmap_atomic(pageaddr);
166		flush_dcache_page(page[n]);
167		SetPageUptodate(page[n]);
168		unlock_page(page[n]);
169		if (page[n] != target_page)
170			put_page(page[n]);
171	}
172
173out:
174	squashfs_cache_put(buffer);
175	return res;
176}
v6.13.7
  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/fs.h>
  8#include <linux/vfs.h>
  9#include <linux/kernel.h>
 10#include <linux/slab.h>
 11#include <linux/string.h>
 12#include <linux/pagemap.h>
 13#include <linux/mutex.h>
 14
 15#include "squashfs_fs.h"
 16#include "squashfs_fs_sb.h"
 17#include "squashfs_fs_i.h"
 18#include "squashfs.h"
 19#include "page_actor.h"
 20
 
 
 
 21/* Read separately compressed datablock directly into page cache */
 22int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
 23	int expected)
 24
 25{
 26	struct folio *folio = page_folio(target_page);
 27	struct inode *inode = target_page->mapping->host;
 28	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
 29	loff_t file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
 
 30	int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
 31	loff_t start_index = folio->index & ~mask;
 32	loff_t end_index = start_index | mask;
 33	loff_t index;
 34	int i, pages, bytes, res = -ENOMEM;
 35	struct page **page, *last_page;
 36	struct squashfs_page_actor *actor;
 37	void *pageaddr;
 38
 39	if (end_index > file_end)
 40		end_index = file_end;
 41
 42	pages = end_index - start_index + 1;
 43
 44	page = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
 45	if (page == NULL)
 46		return res;
 47
 
 
 
 
 
 
 
 
 48	/* Try to grab all the pages covered by the Squashfs block */
 49	for (i = 0, index = start_index; index <= end_index; index++) {
 50		page[i] = (index == folio->index) ? target_page :
 51			grab_cache_page_nowait(target_page->mapping, index);
 52
 53		if (page[i] == NULL)
 
 54			continue;
 
 55
 56		if (PageUptodate(page[i])) {
 57			unlock_page(page[i]);
 58			put_page(page[i]);
 59			continue;
 
 60		}
 61
 62		i++;
 63	}
 64
 65	pages = i;
 
 
 
 
 
 
 
 
 
 
 
 66
 67	/*
 68	 * Create a "page actor" which will kmap and kunmap the
 69	 * page cache pages appropriately within the decompressor
 70	 */
 71	actor = squashfs_page_actor_init_special(msblk, page, pages, expected,
 72						start_index << PAGE_SHIFT);
 73	if (actor == NULL)
 74		goto out;
 
 75
 76	/* Decompress directly into the page cache buffers */
 77	res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
 78
 79	last_page = squashfs_page_actor_free(actor);
 80
 81	if (res < 0)
 82		goto mark_errored;
 83
 84	if (res != expected || IS_ERR(last_page)) {
 85		res = -EIO;
 86		goto mark_errored;
 87	}
 88
 89	/* Last page (if present) may have trailing bytes not filled */
 90	bytes = res % PAGE_SIZE;
 91	if (end_index == file_end && last_page && bytes) {
 92		pageaddr = kmap_local_page(last_page);
 93		memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
 94		kunmap_local(pageaddr);
 95	}
 96
 97	/* Mark pages as uptodate, unlock and release */
 98	for (i = 0; i < pages; i++) {
 99		flush_dcache_page(page[i]);
100		SetPageUptodate(page[i]);
101		unlock_page(page[i]);
102		if (page[i] != target_page)
103			put_page(page[i]);
104	}
105
 
106	kfree(page);
107
108	return 0;
109
110mark_errored:
111	/* Decompression failed.  Target_page is
112	 * dealt with by the caller
113	 */
114	for (i = 0; i < pages; i++) {
115		if (page[i] == NULL || page[i] == target_page)
116			continue;
117		flush_dcache_page(page[i]);
 
118		unlock_page(page[i]);
119		put_page(page[i]);
120	}
121
122out:
 
123	kfree(page);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124	return res;
125}