file.c 21 KB

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