File Index
Files and directories
Sign in
FILE

file.c

File preview with syntax highlighting.

linux-6.19.14/fs/squashfs/file.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Squashfs - a compressed read only filesystem for Linux
  4 *
  5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  6 * Phillip Lougher <phillip@squashfs.org.uk>
  7 *
  8 * file.c
  9 */
 10
 11/*
 12 * This file contains code for handling regular files.  A regular file
 13 * consists of a sequence of contiguous compressed blocks, and/or a
 14 * compressed fragment block (tail-end packed block).   The compressed size
 15 * of each datablock is stored in a block list contained within the
 16 * file inode (itself stored in one or more compressed metadata blocks).
 17 *
 18 * To speed up access to datablocks when reading 'large' files (256 Mbytes or
 19 * larger), the code implements an index cache that caches the mapping from
 20 * block index to datablock location on disk.
 21 *
 22 * The index cache allows Squashfs to handle large files (up to 1.75 TiB) while
 23 * retaining a simple and space-efficient block list on disk.  The cache
 24 * is split into slots, caching up to eight 224 GiB files (128 KiB blocks).
 25 * Larger files use multiple slots, with 1.75 TiB files using all 8 slots.
 26 * The index cache is designed to be memory efficient, and by default uses
 27 * 16 KiB.
 28 */
 29
 30#include <linux/fs.h>
 31#include <linux/vfs.h>
 32#include <linux/kernel.h>
 33#include <linux/slab.h>
 34#include <linux/string.h>
 35#include <linux/pagemap.h>
 36#include <linux/mutex.h>
 37
 38#include "squashfs_fs.h"
 39#include "squashfs_fs_sb.h"
 40#include "squashfs_fs_i.h"
 41#include "squashfs.h"
 42#include "page_actor.h"
 43
 44/*
 45 * Locate cache slot in range [offset, index] for specified inode.  If
 46 * there's more than one return the slot closest to index.
 47 */
 48static struct meta_index *locate_meta_index(struct inode *inode, int offset,
 49				int index)
 50{
 51	struct meta_index *meta = NULL;
 52	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
 53	int i;
 54
 55	mutex_lock(&msblk->meta_index_mutex);
 56
 57	TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
 58
 59	if (msblk->meta_index == NULL)
 60		goto not_allocated;
 61
 62	for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
 63		if (msblk->meta_index[i].inode_number == inode->i_ino &&
 64				msblk->meta_index[i].offset >= offset &&
 65				msblk->meta_index[i].offset <= index &&
 66				msblk->meta_index[i].locked == 0) {
 67			TRACE("locate_meta_index: entry %d, offset %d\n", i,
 68					msblk->meta_index[i].offset);
 69			meta = &msblk->meta_index[i];
 70			offset = meta->offset;
 71		}
 72	}
 73
 74	if (meta)
 75		meta->locked = 1;
 76
 77not_allocated:
 78	mutex_unlock(&msblk->meta_index_mutex);
 79
 80	return meta;
 81}
 82
 83
 84/*
 85 * Find and initialise an empty cache slot for index offset.
 86 */
 87static struct meta_index *empty_meta_index(struct inode *inode, int offset,
 88				int skip)
 89{
 90	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
 91	struct meta_index *meta = NULL;
 92	int i;
 93
 94	mutex_lock(&msblk->meta_index_mutex);
 95
 96	TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
 97
 98	if (msblk->meta_index == NULL) {
 99		/*
100		 * First time cache index has been used, allocate and
101		 * initialise.  The cache index could be allocated at
102		 * mount time but doing it here means it is allocated only
103		 * if a 'large' file is read.
104		 */
105		msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
106			sizeof(*(msblk->meta_index)), GFP_KERNEL);
107		if (msblk->meta_index == NULL) {
108			ERROR("Failed to allocate meta_index\n");
109			goto failed;
110		}
111		for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
112			msblk->meta_index[i].inode_number = 0;
113			msblk->meta_index[i].locked = 0;
114		}
115		msblk->next_meta_index = 0;
116	}
117
118	for (i = SQUASHFS_META_SLOTS; i &&
119			msblk->meta_index[msblk->next_meta_index].locked; i--)
120		msblk->next_meta_index = (msblk->next_meta_index + 1) %
121			SQUASHFS_META_SLOTS;
122
123	if (i == 0) {
124		TRACE("empty_meta_index: failed!\n");
125		goto failed;
126	}
127
128	TRACE("empty_meta_index: returned meta entry %d, %p\n",
129			msblk->next_meta_index,
130			&msblk->meta_index[msblk->next_meta_index]);
131
132	meta = &msblk->meta_index[msblk->next_meta_index];
133	msblk->next_meta_index = (msblk->next_meta_index + 1) %
134			SQUASHFS_META_SLOTS;
135
136	meta->inode_number = inode->i_ino;
137	meta->offset = offset;
138	meta->skip = skip;
139	meta->entries = 0;
140	meta->locked = 1;
141
142failed:
143	mutex_unlock(&msblk->meta_index_mutex);
144	return meta;
145}
146
147
148static void release_meta_index(struct inode *inode, struct meta_index *meta)
149{
150	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
151	mutex_lock(&msblk->meta_index_mutex);
152	meta->locked = 0;
153	mutex_unlock(&msblk->meta_index_mutex);
154}
155
156
157/*
158 * Read the next n blocks from the block list, starting from
159 * metadata block <start_block, offset>.
160 */
161static long long read_indexes(struct super_block *sb, int n,
162				u64 *start_block, int *offset)
163{
164	int err, i;
165	long long block = 0;
166	__le32 *blist = kmalloc(PAGE_SIZE, GFP_KERNEL);
167
168	if (blist == NULL) {
169		ERROR("read_indexes: Failed to allocate block_list\n");
170		return -ENOMEM;
171	}
172
173	while (n) {
174		int blocks = min_t(int, n, PAGE_SIZE >> 2);
175
176		err = squashfs_read_metadata(sb, blist, start_block,
177				offset, blocks << 2);
178		if (err < 0) {
179			ERROR("read_indexes: reading block [%llx:%x]\n",
180				*start_block, *offset);
181			goto failure;
182		}
183
184		for (i = 0; i < blocks; i++) {
185			int size = squashfs_block_size(blist[i]);
186			if (size < 0) {
187				err = size;
188				goto failure;
189			}
190			block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
191		}
192		n -= blocks;
193	}
194
195	kfree(blist);
196	return block;
197
198failure:
199	kfree(blist);
200	return err;
201}
202
203
204/*
205 * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
206 * can cache one index -> datablock/blocklist-block mapping.  We wish
207 * to distribute these over the length of the file, entry[0] maps index x,
208 * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
209 * The larger the file, the greater the skip factor.  The skip factor is
210 * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
211 * the number of metadata blocks that need to be read fits into the cache.
212 * If the skip factor is limited in this way then the file will use multiple
213 * slots.
214 */
215static inline int calculate_skip(u64 blocks)
216{
217	u64 skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
218		 * SQUASHFS_META_INDEXES);
219	return min((u64) SQUASHFS_CACHED_BLKS - 1, skip + 1);
220}
221
222
223/*
224 * Search and grow the index cache for the specified inode, returning the
225 * on-disk locations of the datablock and block list metadata block
226 * <index_block, index_offset> for index (scaled to nearest cache index).
227 */
228static int fill_meta_index(struct inode *inode, int index,
229		u64 *index_block, int *index_offset, u64 *data_block)
230{
231	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
232	int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
233	int offset = 0;
234	struct meta_index *meta;
235	struct meta_entry *meta_entry;
236	u64 cur_index_block = squashfs_i(inode)->block_list_start;
237	int cur_offset = squashfs_i(inode)->offset;
238	u64 cur_data_block = squashfs_i(inode)->start;
239	int err, i;
240
241	/*
242	 * Scale index to cache index (cache slot entry)
243	 */
244	index /= SQUASHFS_META_INDEXES * skip;
245
246	while (offset < index) {
247		meta = locate_meta_index(inode, offset + 1, index);
248
249		if (meta == NULL) {
250			meta = empty_meta_index(inode, offset + 1, skip);
251			if (meta == NULL)
252				goto all_done;
253		} else {
254			offset = index < meta->offset + meta->entries ? index :
255				meta->offset + meta->entries - 1;
256			meta_entry = &meta->meta_entry[offset - meta->offset];
257			cur_index_block = meta_entry->index_block +
258				msblk->inode_table;
259			cur_offset = meta_entry->offset;
260			cur_data_block = meta_entry->data_block;
261			TRACE("get_meta_index: offset %d, meta->offset %d, "
262				"meta->entries %d\n", offset, meta->offset,
263				meta->entries);
264			TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
265				" data_block 0x%llx\n", cur_index_block,
266				cur_offset, cur_data_block);
267		}
268
269		/*
270		 * If necessary grow cache slot by reading block list.  Cache
271		 * slot is extended up to index or to the end of the slot, in
272		 * which case further slots will be used.
273		 */
274		for (i = meta->offset + meta->entries; i <= index &&
275				i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
276			int blocks = skip * SQUASHFS_META_INDEXES;
277			long long res = read_indexes(inode->i_sb, blocks,
278					&cur_index_block, &cur_offset);
279
280			if (res < 0) {
281				if (meta->entries == 0)
282					/*
283					 * Don't leave an empty slot on read
284					 * error allocated to this inode...
285					 */
286					meta->inode_number = 0;
287				err = res;
288				goto failed;
289			}
290
291			cur_data_block += res;
292			meta_entry = &meta->meta_entry[i - meta->offset];
293			meta_entry->index_block = cur_index_block -
294				msblk->inode_table;
295			meta_entry->offset = cur_offset;
296			meta_entry->data_block = cur_data_block;
297			meta->entries++;
298			offset++;
299		}
300
301		TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
302				meta->offset, meta->entries);
303
304		release_meta_index(inode, meta);
305	}
306
307all_done:
308	*index_block = cur_index_block;
309	*index_offset = cur_offset;
310	if (data_block)
311		*data_block = cur_data_block;
312
313	/*
314	 * Scale cache index (cache slot entry) to index
315	 */
316	return offset * SQUASHFS_META_INDEXES * skip;
317
318failed:
319	release_meta_index(inode, meta);
320	return err;
321}
322
323
324/*
325 * Get the on-disk location and compressed size of the datablock
326 * specified by index.  Fill_meta_index() does most of the work.
327 */
328static int read_blocklist_ptrs(struct inode *inode, int index, u64 *start,
329	int *offset, u64 *block)
330{
331	long long blks;
332	__le32 size;
333	int res = fill_meta_index(inode, index, start, offset, block);
334
335	TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset 0x%x, block 0x%llx\n",
336				res, index, *start, *offset, block ? *block : 0);
337
338	if (res < 0)
339		return res;
340
341	/*
342	 * res contains the index of the mapping returned by fill_meta_index(),
343	 * this will likely be less than the desired index (because the
344	 * meta_index cache works at a higher granularity).  Read any
345	 * extra block indexes needed.
346	 */
347	if (res < index) {
348		blks = read_indexes(inode->i_sb, index - res, start, offset);
349		if (blks < 0)
350			return (int) blks;
351		if (block)
352			*block += blks;
353	}
354
355	/*
356	 * Read length of block specified by index.
357	 */
358	res = squashfs_read_metadata(inode->i_sb, &size, start, offset,
359			sizeof(size));
360	if (res < 0)
361		return res;
362	return squashfs_block_size(size);
363}
364
365static inline int read_blocklist(struct inode *inode, int index, u64 *block)
366{
367	u64 start;
368	int offset;
369
370	return read_blocklist_ptrs(inode, index, &start, &offset, block);
371}
372
373static bool squashfs_fill_page(struct folio *folio,
374		struct squashfs_cache_entry *buffer, size_t offset,
375		size_t avail)
376{
377	size_t copied;
378	void *pageaddr;
379
380	pageaddr = kmap_local_folio(folio, 0);
381	copied = squashfs_copy_data(pageaddr, buffer, offset, avail);
382	memset(pageaddr + copied, 0, PAGE_SIZE - copied);
383	kunmap_local(pageaddr);
384
385	flush_dcache_folio(folio);
386
387	return copied == avail;
388}
389
390/* Copy data into page cache  */
391void squashfs_copy_cache(struct folio *folio,
392		struct squashfs_cache_entry *buffer, size_t bytes,
393		size_t offset)
394{
395	struct address_space *mapping = folio->mapping;
396	struct inode *inode = mapping->host;
397	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
398	int i, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
399	int start_index = folio->index & ~mask, end_index = start_index | mask;
400
401	/*
402	 * Loop copying datablock into pages.  As the datablock likely covers
403	 * many PAGE_SIZE pages (default block size is 128 KiB) explicitly
404	 * grab the pages from the page cache, except for the page that we've
405	 * been called to fill.
406	 */
407	for (i = start_index; i <= end_index && bytes > 0; i++,
408			bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
409		struct folio *push_folio;
410		size_t avail = buffer ? min(bytes, PAGE_SIZE) : 0;
411		bool updated = false;
412
413		TRACE("bytes %zu, i %d, available_bytes %zu\n", bytes, i, avail);
414
415		push_folio = (i == folio->index) ? folio :
416			__filemap_get_folio(mapping, i,
417					FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
418					mapping_gfp_mask(mapping));
419
420		if (IS_ERR(push_folio))
421			continue;
422
423		if (folio_test_uptodate(push_folio))
424			goto skip_folio;
425
426		updated = squashfs_fill_page(push_folio, buffer, offset, avail);
427skip_folio:
428		folio_end_read(push_folio, updated);
429		if (i != folio->index)
430			folio_put(push_folio);
431	}
432}
433
434/* Read datablock stored packed inside a fragment (tail-end packed block) */
435static int squashfs_readpage_fragment(struct folio *folio, int expected)
436{
437	struct inode *inode = folio->mapping->host;
438	struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
439		squashfs_i(inode)->fragment_block,
440		squashfs_i(inode)->fragment_size);
441	int res = buffer->error;
442
443	if (res)
444		ERROR("Unable to read page, block %llx, size %x\n",
445			squashfs_i(inode)->fragment_block,
446			squashfs_i(inode)->fragment_size);
447	else
448		squashfs_copy_cache(folio, buffer, expected,
449			squashfs_i(inode)->fragment_offset);
450
451	squashfs_cache_put(buffer);
452	return res;
453}
454
455static int squashfs_readpage_sparse(struct folio *folio, int expected)
456{
457	squashfs_copy_cache(folio, NULL, expected, 0);
458	return 0;
459}
460
461static int squashfs_read_folio(struct file *file, struct folio *folio)
462{
463	struct inode *inode = folio->mapping->host;
464	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
465	int index = folio->index >> (msblk->block_log - PAGE_SHIFT);
466	int file_end = i_size_read(inode) >> msblk->block_log;
467	int expected = index == file_end ?
468			(i_size_read(inode) & (msblk->block_size - 1)) :
469			 msblk->block_size;
470	int res = 0;
471
472	TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
473				folio->index, squashfs_i(inode)->start);
474
475	if (folio->index >= ((i_size_read(inode) + PAGE_SIZE - 1) >>
476					PAGE_SHIFT))
477		goto out;
478
479	if (index < file_end || squashfs_i(inode)->fragment_block ==
480					SQUASHFS_INVALID_BLK) {
481		u64 block = 0;
482
483		res = read_blocklist(inode, index, &block);
484		if (res < 0)
485			goto out;
486
487		if (res == 0)
488			res = squashfs_readpage_sparse(folio, expected);
489		else
490			res = squashfs_readpage_block(folio, block, res, expected);
491	} else
492		res = squashfs_readpage_fragment(folio, expected);
493
494	if (!res)
495		return 0;
496
497out:
498	folio_zero_segment(folio, 0, folio_size(folio));
499	folio_end_read(folio, res == 0);
500
501	return res;
502}
503
504static int squashfs_readahead_fragment(struct inode *inode, struct page **page,
505	unsigned int pages, unsigned int expected, loff_t start)
506{
507	struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
508		squashfs_i(inode)->fragment_block,
509		squashfs_i(inode)->fragment_size);
510	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
511	int i, bytes, copied;
512	struct squashfs_page_actor *actor;
513	unsigned int offset;
514	void *addr;
515	struct page *last_page;
516
517	if (buffer->error)
518		goto out;
519
520	actor = squashfs_page_actor_init_special(msblk, page, pages,
521							expected, start);
522	if (!actor)
523		goto out;
524
525	squashfs_actor_nobuff(actor);
526	addr = squashfs_first_page(actor);
527
528	for (copied = offset = 0; offset < expected; offset += PAGE_SIZE) {
529		int avail = min_t(int, expected - offset, PAGE_SIZE);
530
531		if (!IS_ERR(addr)) {
532			bytes = squashfs_copy_data(addr, buffer, offset +
533					squashfs_i(inode)->fragment_offset, avail);
534
535			if (bytes != avail)
536				goto failed;
537		}
538
539		copied += avail;
540		addr = squashfs_next_page(actor);
541	}
542
543	last_page = squashfs_page_actor_free(actor);
544
545	if (copied == expected && !IS_ERR(last_page)) {
546		/* Last page (if present) may have trailing bytes not filled */
547		bytes = copied % PAGE_SIZE;
548		if (bytes && last_page)
549			memzero_page(last_page, bytes, PAGE_SIZE - bytes);
550
551		for (i = 0; i < pages; i++) {
552			flush_dcache_page(page[i]);
553			SetPageUptodate(page[i]);
554		}
555	}
556
557	for (i = 0; i < pages; i++) {
558		unlock_page(page[i]);
559		put_page(page[i]);
560	}
561
562	squashfs_cache_put(buffer);
563	return 0;
564
565failed:
566	squashfs_page_actor_free(actor);
567
568out:
569	squashfs_cache_put(buffer);
570	return 1;
571}
572
573static void squashfs_readahead(struct readahead_control *ractl)
574{
575	struct inode *inode = ractl->mapping->host;
576	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
577	size_t mask = (1UL << msblk->block_log) - 1;
578	unsigned short shift = msblk->block_log - PAGE_SHIFT;
579	loff_t start = readahead_pos(ractl) & ~mask;
580	size_t len = readahead_length(ractl) + readahead_pos(ractl) - start;
581	struct squashfs_page_actor *actor;
582	unsigned int nr_pages = 0;
583	struct page **pages;
584	int i;
585	loff_t file_end = i_size_read(inode) >> msblk->block_log;
586	unsigned int max_pages = 1UL << shift;
587
588	readahead_expand(ractl, start, (len | mask) + 1);
589
590	pages = kmalloc_array(max_pages, sizeof(void *), GFP_KERNEL);
591	if (!pages)
592		return;
593
594	for (;;) {
595		int res, bsize;
596		u64 block = 0;
597		unsigned int expected;
598		struct page *last_page;
599
600		expected = start >> msblk->block_log == file_end ?
601			   (i_size_read(inode) & (msblk->block_size - 1)) :
602			    msblk->block_size;
603
604		max_pages = (expected + PAGE_SIZE - 1) >> PAGE_SHIFT;
605
606		nr_pages = __readahead_batch(ractl, pages, max_pages);
607		if (!nr_pages)
608			break;
609
610		if (readahead_pos(ractl) >= i_size_read(inode))
611			goto skip_pages;
612
613		if (start >> msblk->block_log == file_end &&
614				squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK) {
615			res = squashfs_readahead_fragment(inode, pages,
616					nr_pages, expected, start);
617			if (res)
618				goto skip_pages;
619			continue;
620		}
621
622		bsize = read_blocklist(inode, start >> msblk->block_log, &block);
623		if (bsize == 0)
624			goto skip_pages;
625
626		actor = squashfs_page_actor_init_special(msblk, pages, nr_pages,
627							expected, start);
628		if (!actor)
629			goto skip_pages;
630
631		res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
632
633		last_page = squashfs_page_actor_free(actor);
634
635		if (res == expected && !IS_ERR(last_page)) {
636			int bytes;
637
638			/* Last page (if present) may have trailing bytes not filled */
639			bytes = res % PAGE_SIZE;
640			if (start >> msblk->block_log == file_end && bytes && last_page)
641				memzero_page(last_page, bytes,
642					     PAGE_SIZE - bytes);
643
644			for (i = 0; i < nr_pages; i++) {
645				flush_dcache_page(pages[i]);
646				SetPageUptodate(pages[i]);
647			}
648		}
649
650		for (i = 0; i < nr_pages; i++) {
651			unlock_page(pages[i]);
652			put_page(pages[i]);
653		}
654
655		start += readahead_batch_length(ractl);
656	}
657
658	kfree(pages);
659	return;
660
661skip_pages:
662	for (i = 0; i < nr_pages; i++) {
663		unlock_page(pages[i]);
664		put_page(pages[i]);
665	}
666	kfree(pages);
667}
668
669static loff_t seek_hole_data(struct file *file, loff_t offset, int whence)
670{
671	struct inode *inode = file->f_mapping->host;
672	struct super_block *sb = inode->i_sb;
673	struct squashfs_sb_info *msblk = sb->s_fs_info;
674	u64 start, index = offset >> msblk->block_log;
675	u64 file_end = (i_size_read(inode) + msblk->block_size - 1) >> msblk->block_log;
676	int s_offset, length;
677	__le32 *blist = NULL;
678
679	/* reject offset if negative or beyond file end */
680	if ((unsigned long long)offset >= i_size_read(inode))
681		return -ENXIO;
682
683	/* is offset within tailend and is tailend packed into a fragment? */
684	if (index + 1 == file_end &&
685			squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK) {
686		if (whence == SEEK_DATA)
687			return offset;
688
689		/* there is an implicit hole at the end of any file */
690		return i_size_read(inode);
691	}
692
693	length = read_blocklist_ptrs(inode, index, &start, &s_offset, NULL);
694	if (length < 0)
695		return length;
696
697	/* nothing more to do if offset matches desired whence value */
698	if ((length == 0 && whence == SEEK_HOLE) ||
699					(length && whence == SEEK_DATA))
700		return offset;
701
702	/* skip scanning forwards if we're at file end */
703	if (++ index == file_end)
704		goto not_found;
705
706	blist = kmalloc(SQUASHFS_SCAN_INDEXES << 2, GFP_KERNEL);
707	if (blist == NULL) {
708		ERROR("%s: Failed to allocate block_list\n", __func__);
709		return -ENOMEM;
710	}
711
712	while (index < file_end) {
713		int i, indexes = min(file_end - index, SQUASHFS_SCAN_INDEXES);
714
715		offset = squashfs_read_metadata(sb, blist, &start, &s_offset, indexes << 2);
716		if (offset < 0)
717			goto finished;
718
719		for (i = 0; i < indexes; i++) {
720			length = squashfs_block_size(blist[i]);
721			if (length < 0) {
722				offset = length;
723				goto finished;
724			}
725
726			/* does this block match desired whence value? */
727			if ((length == 0 && whence == SEEK_HOLE) ||
728					(length && whence == SEEK_DATA)) {
729				offset = (index + i) << msblk->block_log;
730				goto finished;
731			}
732		}
733
734		index += indexes;
735	}
736
737not_found:
738	/* whence value determines what happens */
739	if (whence == SEEK_DATA)
740		offset = -ENXIO;
741	else
742		/* there is an implicit hole at the end of any file */
743		offset = i_size_read(inode);
744
745finished:
746	kfree(blist);
747	return offset;
748}
749
750static loff_t squashfs_llseek(struct file *file, loff_t offset, int whence)
751{
752	struct inode *inode = file->f_mapping->host;
753
754	switch (whence) {
755	default:
756		return generic_file_llseek(file, offset, whence);
757	case SEEK_DATA:
758	case SEEK_HOLE:
759		offset = seek_hole_data(file, offset, whence);
760		break;
761	}
762
763	if (offset < 0)
764		return offset;
765
766	return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
767}
768
769const struct address_space_operations squashfs_aops = {
770	.read_folio = squashfs_read_folio,
771	.readahead = squashfs_readahead
772};
773
774const struct file_operations squashfs_file_operations = {
775	.llseek		= squashfs_llseek,
776	.read_iter	= generic_file_read_iter,
777	.mmap_prepare	= generic_file_readonly_mmap_prepare,
778	.splice_read	= filemap_splice_read
779};
~/ Read-only file preview.