file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  5. * Phillip Lougher <phillip@squashfs.org.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * file.c
  22. */
  23. /*
  24. * This file contains code for handling regular files. A regular file
  25. * consists of a sequence of contiguous compressed blocks, and/or a
  26. * compressed fragment block (tail-end packed block). The compressed size
  27. * of each datablock is stored in a block list contained within the
  28. * file inode (itself stored in one or more compressed metadata blocks).
  29. *
  30. * To speed up access to datablocks when reading 'large' files (256 Mbytes or
  31. * larger), the code implements an index cache that caches the mapping from
  32. * block index to datablock location on disk.
  33. *
  34. * The index cache allows Squashfs to handle large files (up to 1.75 TiB) while
  35. * retaining a simple and space-efficient block list on disk. The cache
  36. * is split into slots, caching up to eight 224 GiB files (128 KiB blocks).
  37. * Larger files use multiple slots, with 1.75 TiB files using all 8 slots.
  38. * The index cache is designed to be memory efficient, and by default uses
  39. * 16 KiB.
  40. */
  41. #include <linux/fs.h>
  42. #include <linux/vfs.h>
  43. #include <linux/kernel.h>
  44. #include <linux/slab.h>
  45. #include <linux/string.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/mutex.h>
  48. #include "squashfs_fs.h"
  49. #include "squashfs_fs_sb.h"
  50. #include "squashfs_fs_i.h"
  51. #include "squashfs.h"
  52. // Backported from 4.5
  53. #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
  54. /*
  55. * Locate cache slot in range [offset, index] for specified inode. If
  56. * there's more than one return the slot closest to index.
  57. */
  58. static struct meta_index *locate_meta_index(struct inode *inode, int offset,
  59. int index)
  60. {
  61. struct meta_index *meta = NULL;
  62. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  63. int i;
  64. mutex_lock(&msblk->meta_index_mutex);
  65. TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
  66. if (msblk->meta_index == NULL)
  67. goto not_allocated;
  68. for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
  69. if (msblk->meta_index[i].inode_number == inode->i_ino &&
  70. msblk->meta_index[i].offset >= offset &&
  71. msblk->meta_index[i].offset <= index &&
  72. msblk->meta_index[i].locked == 0) {
  73. TRACE("locate_meta_index: entry %d, offset %d\n", i,
  74. msblk->meta_index[i].offset);
  75. meta = &msblk->meta_index[i];
  76. offset = meta->offset;
  77. }
  78. }
  79. if (meta)
  80. meta->locked = 1;
  81. not_allocated:
  82. mutex_unlock(&msblk->meta_index_mutex);
  83. return meta;
  84. }
  85. /*
  86. * Find and initialise an empty cache slot for index offset.
  87. */
  88. static struct meta_index *empty_meta_index(struct inode *inode, int offset,
  89. int skip)
  90. {
  91. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  92. struct meta_index *meta = NULL;
  93. int i;
  94. mutex_lock(&msblk->meta_index_mutex);
  95. TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
  96. if (msblk->meta_index == NULL) {
  97. /*
  98. * First time cache index has been used, allocate and
  99. * initialise. The cache index could be allocated at
  100. * mount time but doing it here means it is allocated only
  101. * if a 'large' file is read.
  102. */
  103. msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
  104. sizeof(*(msblk->meta_index)), GFP_KERNEL);
  105. if (msblk->meta_index == NULL) {
  106. ERROR("Failed to allocate meta_index\n");
  107. goto failed;
  108. }
  109. for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
  110. msblk->meta_index[i].inode_number = 0;
  111. msblk->meta_index[i].locked = 0;
  112. }
  113. msblk->next_meta_index = 0;
  114. }
  115. for (i = SQUASHFS_META_SLOTS; i &&
  116. msblk->meta_index[msblk->next_meta_index].locked; i--)
  117. msblk->next_meta_index = (msblk->next_meta_index + 1) %
  118. SQUASHFS_META_SLOTS;
  119. if (i == 0) {
  120. TRACE("empty_meta_index: failed!\n");
  121. goto failed;
  122. }
  123. TRACE("empty_meta_index: returned meta entry %d, %p\n",
  124. msblk->next_meta_index,
  125. &msblk->meta_index[msblk->next_meta_index]);
  126. meta = &msblk->meta_index[msblk->next_meta_index];
  127. msblk->next_meta_index = (msblk->next_meta_index + 1) %
  128. SQUASHFS_META_SLOTS;
  129. meta->inode_number = inode->i_ino;
  130. meta->offset = offset;
  131. meta->skip = skip;
  132. meta->entries = 0;
  133. meta->locked = 1;
  134. failed:
  135. mutex_unlock(&msblk->meta_index_mutex);
  136. return meta;
  137. }
  138. static void release_meta_index(struct inode *inode, struct meta_index *meta)
  139. {
  140. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  141. mutex_lock(&msblk->meta_index_mutex);
  142. meta->locked = 0;
  143. mutex_unlock(&msblk->meta_index_mutex);
  144. }
  145. /*
  146. * Read the next n blocks from the block list, starting from
  147. * metadata block <start_block, offset>.
  148. */
  149. static long long read_indexes(struct super_block *sb, int n,
  150. u64 *start_block, int *offset)
  151. {
  152. int err, i;
  153. long long block = 0;
  154. __le32 *blist = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  155. if (blist == NULL) {
  156. ERROR("read_indexes: Failed to allocate block_list\n");
  157. return -ENOMEM;
  158. }
  159. while (n) {
  160. int blocks = min_t(int, n, PAGE_CACHE_SIZE >> 2);
  161. err = squashfs_read_metadata(sb, blist, start_block,
  162. offset, blocks << 2);
  163. if (err < 0) {
  164. ERROR("read_indexes: reading block [%llx:%x]\n",
  165. *start_block, *offset);
  166. goto failure;
  167. }
  168. for (i = 0; i < blocks; i++) {
  169. int size = le32_to_cpu(blist[i]);
  170. block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
  171. }
  172. n -= blocks;
  173. }
  174. kfree(blist);
  175. return block;
  176. failure:
  177. kfree(blist);
  178. return err;
  179. }
  180. /*
  181. * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
  182. * can cache one index -> datablock/blocklist-block mapping. We wish
  183. * to distribute these over the length of the file, entry[0] maps index x,
  184. * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
  185. * The larger the file, the greater the skip factor. The skip factor is
  186. * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
  187. * the number of metadata blocks that need to be read fits into the cache.
  188. * If the skip factor is limited in this way then the file will use multiple
  189. * slots.
  190. */
  191. static inline int calculate_skip(int blocks)
  192. {
  193. int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
  194. * SQUASHFS_META_INDEXES);
  195. return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
  196. }
  197. /*
  198. * Search and grow the index cache for the specified inode, returning the
  199. * on-disk locations of the datablock and block list metadata block
  200. * <index_block, index_offset> for index (scaled to nearest cache index).
  201. */
  202. static int fill_meta_index(struct inode *inode, int index,
  203. u64 *index_block, int *index_offset, u64 *data_block)
  204. {
  205. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  206. int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
  207. int offset = 0;
  208. struct meta_index *meta;
  209. struct meta_entry *meta_entry;
  210. u64 cur_index_block = squashfs_i(inode)->block_list_start;
  211. int cur_offset = squashfs_i(inode)->offset;
  212. u64 cur_data_block = squashfs_i(inode)->start;
  213. int err, i;
  214. /*
  215. * Scale index to cache index (cache slot entry)
  216. */
  217. index /= SQUASHFS_META_INDEXES * skip;
  218. while (offset < index) {
  219. meta = locate_meta_index(inode, offset + 1, index);
  220. if (meta == NULL) {
  221. meta = empty_meta_index(inode, offset + 1, skip);
  222. if (meta == NULL)
  223. goto all_done;
  224. } else {
  225. offset = index < meta->offset + meta->entries ? index :
  226. meta->offset + meta->entries - 1;
  227. meta_entry = &meta->meta_entry[offset - meta->offset];
  228. cur_index_block = meta_entry->index_block +
  229. msblk->inode_table;
  230. cur_offset = meta_entry->offset;
  231. cur_data_block = meta_entry->data_block;
  232. TRACE("get_meta_index: offset %d, meta->offset %d, "
  233. "meta->entries %d\n", offset, meta->offset,
  234. meta->entries);
  235. TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
  236. " data_block 0x%llx\n", cur_index_block,
  237. cur_offset, cur_data_block);
  238. }
  239. /*
  240. * If necessary grow cache slot by reading block list. Cache
  241. * slot is extended up to index or to the end of the slot, in
  242. * which case further slots will be used.
  243. */
  244. for (i = meta->offset + meta->entries; i <= index &&
  245. i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
  246. int blocks = skip * SQUASHFS_META_INDEXES;
  247. long long res = read_indexes(inode->i_sb, blocks,
  248. &cur_index_block, &cur_offset);
  249. if (res < 0) {
  250. if (meta->entries == 0)
  251. /*
  252. * Don't leave an empty slot on read
  253. * error allocated to this inode...
  254. */
  255. meta->inode_number = 0;
  256. err = res;
  257. goto failed;
  258. }
  259. cur_data_block += res;
  260. meta_entry = &meta->meta_entry[i - meta->offset];
  261. meta_entry->index_block = cur_index_block -
  262. msblk->inode_table;
  263. meta_entry->offset = cur_offset;
  264. meta_entry->data_block = cur_data_block;
  265. meta->entries++;
  266. offset++;
  267. }
  268. TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
  269. meta->offset, meta->entries);
  270. release_meta_index(inode, meta);
  271. }
  272. all_done:
  273. *index_block = cur_index_block;
  274. *index_offset = cur_offset;
  275. *data_block = cur_data_block;
  276. /*
  277. * Scale cache index (cache slot entry) to index
  278. */
  279. return offset * SQUASHFS_META_INDEXES * skip;
  280. failed:
  281. release_meta_index(inode, meta);
  282. return err;
  283. }
  284. /*
  285. * Get the on-disk location and compressed size of the datablock
  286. * specified by index. Fill_meta_index() does most of the work.
  287. */
  288. static int read_blocklist(struct inode *inode, int index, u64 *block)
  289. {
  290. u64 start;
  291. long long blks;
  292. int offset;
  293. __le32 size;
  294. int res = fill_meta_index(inode, index, &start, &offset, block);
  295. TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
  296. " 0x%x, block 0x%llx\n", res, index, start, offset,
  297. *block);
  298. if (res < 0)
  299. return res;
  300. /*
  301. * res contains the index of the mapping returned by fill_meta_index(),
  302. * this will likely be less than the desired index (because the
  303. * meta_index cache works at a higher granularity). Read any
  304. * extra block indexes needed.
  305. */
  306. if (res < index) {
  307. blks = read_indexes(inode->i_sb, index - res, &start, &offset);
  308. if (blks < 0)
  309. return (int) blks;
  310. *block += blks;
  311. }
  312. /*
  313. * Read length of block specified by index.
  314. */
  315. res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
  316. sizeof(size));
  317. if (res < 0)
  318. return res;
  319. return le32_to_cpu(size);
  320. }
  321. /* Copy data into page cache */
  322. void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
  323. int bytes, int offset)
  324. {
  325. struct inode *inode = page->mapping->host;
  326. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  327. void *pageaddr;
  328. int i, mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
  329. int start_index = page->index & ~mask, end_index = start_index | mask;
  330. /*
  331. * Loop copying datablock into pages. As the datablock likely covers
  332. * many PAGE_CACHE_SIZE pages (default block size is 128 KiB) explicitly
  333. * grab the pages from the page cache, except for the page that we've
  334. * been called to fill.
  335. */
  336. for (i = start_index; i <= end_index && bytes > 0; i++,
  337. bytes -= PAGE_CACHE_SIZE, offset += PAGE_CACHE_SIZE) {
  338. struct page *push_page;
  339. int avail = buffer ? min_t(int, bytes, PAGE_CACHE_SIZE) : 0;
  340. TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
  341. push_page = (i == page->index) ? page :
  342. grab_cache_page_nowait(page->mapping, i);
  343. if (!push_page)
  344. continue;
  345. if (PageUptodate(push_page))
  346. goto skip_page;
  347. pageaddr = kmap_atomic(push_page);
  348. squashfs_copy_data(pageaddr, buffer, offset, avail);
  349. memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
  350. kunmap_atomic(pageaddr);
  351. flush_dcache_page(push_page);
  352. SetPageUptodate(push_page);
  353. skip_page:
  354. unlock_page(push_page);
  355. if (i != page->index)
  356. page_cache_release(push_page);
  357. }
  358. }
  359. /* Read datablock stored packed inside a fragment (tail-end packed block) */
  360. static int squashfs_readpage_fragment(struct page *page)
  361. {
  362. struct inode *inode = page->mapping->host;
  363. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  364. struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
  365. squashfs_i(inode)->fragment_block,
  366. squashfs_i(inode)->fragment_size);
  367. int res = buffer->error;
  368. if (res)
  369. ERROR("Unable to read page, block %llx, size %x\n",
  370. squashfs_i(inode)->fragment_block,
  371. squashfs_i(inode)->fragment_size);
  372. else
  373. squashfs_copy_cache(page, buffer, i_size_read(inode) &
  374. (msblk->block_size - 1),
  375. squashfs_i(inode)->fragment_offset);
  376. squashfs_cache_put(buffer);
  377. return res;
  378. }
  379. static int squashfs_readpages_fragment(struct page *page,
  380. struct list_head *readahead_pages, struct address_space *mapping)
  381. {
  382. if (!page) {
  383. page = lru_to_page(readahead_pages);
  384. list_del(&page->lru);
  385. if (add_to_page_cache_lru(page, mapping, page->index,
  386. GFP_KERNEL)) {
  387. put_page(page);
  388. return 0;
  389. }
  390. }
  391. return squashfs_readpage_fragment(page);
  392. }
  393. static int squashfs_readpage_sparse(struct page *page, int index, int file_end)
  394. {
  395. struct inode *inode = page->mapping->host;
  396. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  397. int bytes = index == file_end ?
  398. (i_size_read(inode) & (msblk->block_size - 1)) :
  399. msblk->block_size;
  400. squashfs_copy_cache(page, NULL, bytes, 0);
  401. return 0;
  402. }
  403. static int squashfs_readpages_sparse(struct page *page,
  404. struct list_head *readahead_pages, int index, int file_end,
  405. struct address_space *mapping)
  406. {
  407. if (!page) {
  408. page = lru_to_page(readahead_pages);
  409. list_del(&page->lru);
  410. if (add_to_page_cache_lru(page, mapping, page->index,
  411. GFP_KERNEL)) {
  412. put_page(page);
  413. return 0;
  414. }
  415. }
  416. return squashfs_readpage_sparse(page, index, file_end);
  417. }
  418. static int __squashfs_readpages(struct file *file, struct page *page,
  419. struct list_head *readahead_pages, unsigned int nr_pages,
  420. struct address_space *mapping)
  421. {
  422. struct inode *inode = mapping->host;
  423. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  424. int file_end = i_size_read(inode) >> msblk->block_log;
  425. int res;
  426. do {
  427. struct page *cur_page = page ? page
  428. : lru_to_page(readahead_pages);
  429. int page_index = cur_page->index;
  430. int index = page_index >> (msblk->block_log - PAGE_CACHE_SHIFT);
  431. if (page_index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  432. PAGE_CACHE_SHIFT))
  433. return 1;
  434. if (index < file_end || squashfs_i(inode)->fragment_block ==
  435. SQUASHFS_INVALID_BLK) {
  436. u64 block = 0;
  437. int bsize = read_blocklist(inode, index, &block);
  438. if (bsize < 0)
  439. return -1;
  440. if (bsize == 0) {
  441. res = squashfs_readpages_sparse(page,
  442. readahead_pages, index, file_end,
  443. mapping);
  444. } else {
  445. res = squashfs_readpages_block(page,
  446. readahead_pages, &nr_pages, mapping,
  447. page_index, block, bsize);
  448. }
  449. } else {
  450. res = squashfs_readpages_fragment(page,
  451. readahead_pages, mapping);
  452. }
  453. if (res)
  454. return 0;
  455. page = NULL;
  456. } while (readahead_pages && !list_empty(readahead_pages));
  457. return 0;
  458. }
  459. static int squashfs_readpage(struct file *file, struct page *page)
  460. {
  461. int ret;
  462. TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
  463. page->index, squashfs_i(page->mapping->host)->start);
  464. get_page(page);
  465. ret = __squashfs_readpages(file, page, NULL, 1, page->mapping);
  466. if (ret) {
  467. flush_dcache_page(page);
  468. if (ret < 0)
  469. SetPageError(page);
  470. else
  471. SetPageUptodate(page);
  472. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  473. unlock_page(page);
  474. put_page(page);
  475. }
  476. return 0;
  477. }
  478. static int squashfs_readpages(struct file *file, struct address_space *mapping,
  479. struct list_head *pages, unsigned int nr_pages)
  480. {
  481. TRACE("Entered squashfs_readpages, %u pages, first page index %lx\n",
  482. nr_pages, lru_to_page(pages)->index);
  483. __squashfs_readpages(file, NULL, pages, nr_pages, mapping);
  484. return 0;
  485. }
  486. const struct address_space_operations squashfs_aops = {
  487. .readpage = squashfs_readpage,
  488. .readpages = squashfs_readpages,
  489. };