file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. /*
  53. * Locate cache slot in range [offset, index] for specified inode. If
  54. * there's more than one return the slot closest to index.
  55. */
  56. static struct meta_index *locate_meta_index(struct inode *inode, int offset,
  57. int index)
  58. {
  59. struct meta_index *meta = NULL;
  60. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  61. int i;
  62. mutex_lock(&msblk->meta_index_mutex);
  63. TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
  64. if (msblk->meta_index == NULL)
  65. goto not_allocated;
  66. for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
  67. if (msblk->meta_index[i].inode_number == inode->i_ino &&
  68. msblk->meta_index[i].offset >= offset &&
  69. msblk->meta_index[i].offset <= index &&
  70. msblk->meta_index[i].locked == 0) {
  71. TRACE("locate_meta_index: entry %d, offset %d\n", i,
  72. msblk->meta_index[i].offset);
  73. meta = &msblk->meta_index[i];
  74. offset = meta->offset;
  75. }
  76. }
  77. if (meta)
  78. meta->locked = 1;
  79. not_allocated:
  80. mutex_unlock(&msblk->meta_index_mutex);
  81. return meta;
  82. }
  83. /*
  84. * Find and initialise an empty cache slot for index offset.
  85. */
  86. static struct meta_index *empty_meta_index(struct inode *inode, int offset,
  87. int skip)
  88. {
  89. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  90. struct meta_index *meta = NULL;
  91. int i;
  92. mutex_lock(&msblk->meta_index_mutex);
  93. TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
  94. if (msblk->meta_index == NULL) {
  95. /*
  96. * First time cache index has been used, allocate and
  97. * initialise. The cache index could be allocated at
  98. * mount time but doing it here means it is allocated only
  99. * if a 'large' file is read.
  100. */
  101. msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
  102. sizeof(*(msblk->meta_index)), GFP_KERNEL);
  103. if (msblk->meta_index == NULL) {
  104. ERROR("Failed to allocate meta_index\n");
  105. goto failed;
  106. }
  107. for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
  108. msblk->meta_index[i].inode_number = 0;
  109. msblk->meta_index[i].locked = 0;
  110. }
  111. msblk->next_meta_index = 0;
  112. }
  113. for (i = SQUASHFS_META_SLOTS; i &&
  114. msblk->meta_index[msblk->next_meta_index].locked; i--)
  115. msblk->next_meta_index = (msblk->next_meta_index + 1) %
  116. SQUASHFS_META_SLOTS;
  117. if (i == 0) {
  118. TRACE("empty_meta_index: failed!\n");
  119. goto failed;
  120. }
  121. TRACE("empty_meta_index: returned meta entry %d, %p\n",
  122. msblk->next_meta_index,
  123. &msblk->meta_index[msblk->next_meta_index]);
  124. meta = &msblk->meta_index[msblk->next_meta_index];
  125. msblk->next_meta_index = (msblk->next_meta_index + 1) %
  126. SQUASHFS_META_SLOTS;
  127. meta->inode_number = inode->i_ino;
  128. meta->offset = offset;
  129. meta->skip = skip;
  130. meta->entries = 0;
  131. meta->locked = 1;
  132. failed:
  133. mutex_unlock(&msblk->meta_index_mutex);
  134. return meta;
  135. }
  136. static void release_meta_index(struct inode *inode, struct meta_index *meta)
  137. {
  138. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  139. mutex_lock(&msblk->meta_index_mutex);
  140. meta->locked = 0;
  141. mutex_unlock(&msblk->meta_index_mutex);
  142. }
  143. /*
  144. * Read the next n blocks from the block list, starting from
  145. * metadata block <start_block, offset>.
  146. */
  147. static long long read_indexes(struct super_block *sb, int n,
  148. u64 *start_block, int *offset)
  149. {
  150. int err, i;
  151. long long block = 0;
  152. __le32 *blist = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  153. if (blist == NULL) {
  154. ERROR("read_indexes: Failed to allocate block_list\n");
  155. return -ENOMEM;
  156. }
  157. while (n) {
  158. int blocks = min_t(int, n, PAGE_CACHE_SIZE >> 2);
  159. err = squashfs_read_metadata(sb, blist, start_block,
  160. offset, blocks << 2);
  161. if (err < 0) {
  162. ERROR("read_indexes: reading block [%llx:%x]\n",
  163. *start_block, *offset);
  164. goto failure;
  165. }
  166. for (i = 0; i < blocks; i++) {
  167. int size = le32_to_cpu(blist[i]);
  168. block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
  169. }
  170. n -= blocks;
  171. }
  172. kfree(blist);
  173. return block;
  174. failure:
  175. kfree(blist);
  176. return err;
  177. }
  178. /*
  179. * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
  180. * can cache one index -> datablock/blocklist-block mapping. We wish
  181. * to distribute these over the length of the file, entry[0] maps index x,
  182. * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
  183. * The larger the file, the greater the skip factor. The skip factor is
  184. * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
  185. * the number of metadata blocks that need to be read fits into the cache.
  186. * If the skip factor is limited in this way then the file will use multiple
  187. * slots.
  188. */
  189. static inline int calculate_skip(int blocks)
  190. {
  191. int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
  192. * SQUASHFS_META_INDEXES);
  193. return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
  194. }
  195. /*
  196. * Search and grow the index cache for the specified inode, returning the
  197. * on-disk locations of the datablock and block list metadata block
  198. * <index_block, index_offset> for index (scaled to nearest cache index).
  199. */
  200. static int fill_meta_index(struct inode *inode, int index,
  201. u64 *index_block, int *index_offset, u64 *data_block)
  202. {
  203. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  204. int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
  205. int offset = 0;
  206. struct meta_index *meta;
  207. struct meta_entry *meta_entry;
  208. u64 cur_index_block = squashfs_i(inode)->block_list_start;
  209. int cur_offset = squashfs_i(inode)->offset;
  210. u64 cur_data_block = squashfs_i(inode)->start;
  211. int err, i;
  212. /*
  213. * Scale index to cache index (cache slot entry)
  214. */
  215. index /= SQUASHFS_META_INDEXES * skip;
  216. while (offset < index) {
  217. meta = locate_meta_index(inode, offset + 1, index);
  218. if (meta == NULL) {
  219. meta = empty_meta_index(inode, offset + 1, skip);
  220. if (meta == NULL)
  221. goto all_done;
  222. } else {
  223. offset = index < meta->offset + meta->entries ? index :
  224. meta->offset + meta->entries - 1;
  225. meta_entry = &meta->meta_entry[offset - meta->offset];
  226. cur_index_block = meta_entry->index_block +
  227. msblk->inode_table;
  228. cur_offset = meta_entry->offset;
  229. cur_data_block = meta_entry->data_block;
  230. TRACE("get_meta_index: offset %d, meta->offset %d, "
  231. "meta->entries %d\n", offset, meta->offset,
  232. meta->entries);
  233. TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
  234. " data_block 0x%llx\n", cur_index_block,
  235. cur_offset, cur_data_block);
  236. }
  237. /*
  238. * If necessary grow cache slot by reading block list. Cache
  239. * slot is extended up to index or to the end of the slot, in
  240. * which case further slots will be used.
  241. */
  242. for (i = meta->offset + meta->entries; i <= index &&
  243. i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
  244. int blocks = skip * SQUASHFS_META_INDEXES;
  245. long long res = read_indexes(inode->i_sb, blocks,
  246. &cur_index_block, &cur_offset);
  247. if (res < 0) {
  248. if (meta->entries == 0)
  249. /*
  250. * Don't leave an empty slot on read
  251. * error allocated to this inode...
  252. */
  253. meta->inode_number = 0;
  254. err = res;
  255. goto failed;
  256. }
  257. cur_data_block += res;
  258. meta_entry = &meta->meta_entry[i - meta->offset];
  259. meta_entry->index_block = cur_index_block -
  260. msblk->inode_table;
  261. meta_entry->offset = cur_offset;
  262. meta_entry->data_block = cur_data_block;
  263. meta->entries++;
  264. offset++;
  265. }
  266. TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
  267. meta->offset, meta->entries);
  268. release_meta_index(inode, meta);
  269. }
  270. all_done:
  271. *index_block = cur_index_block;
  272. *index_offset = cur_offset;
  273. *data_block = cur_data_block;
  274. /*
  275. * Scale cache index (cache slot entry) to index
  276. */
  277. return offset * SQUASHFS_META_INDEXES * skip;
  278. failed:
  279. release_meta_index(inode, meta);
  280. return err;
  281. }
  282. /*
  283. * Get the on-disk location and compressed size of the datablock
  284. * specified by index. Fill_meta_index() does most of the work.
  285. */
  286. static int read_blocklist(struct inode *inode, int index, u64 *block)
  287. {
  288. u64 start;
  289. long long blks;
  290. int offset;
  291. __le32 size;
  292. int res = fill_meta_index(inode, index, &start, &offset, block);
  293. TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
  294. " 0x%x, block 0x%llx\n", res, index, start, offset,
  295. *block);
  296. if (res < 0)
  297. return res;
  298. /*
  299. * res contains the index of the mapping returned by fill_meta_index(),
  300. * this will likely be less than the desired index (because the
  301. * meta_index cache works at a higher granularity). Read any
  302. * extra block indexes needed.
  303. */
  304. if (res < index) {
  305. blks = read_indexes(inode->i_sb, index - res, &start, &offset);
  306. if (blks < 0)
  307. return (int) blks;
  308. *block += blks;
  309. }
  310. /*
  311. * Read length of block specified by index.
  312. */
  313. res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
  314. sizeof(size));
  315. if (res < 0)
  316. return res;
  317. return le32_to_cpu(size);
  318. }
  319. static int squashfs_readpage(struct file *file, struct page *page)
  320. {
  321. struct inode *inode = page->mapping->host;
  322. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  323. int bytes, i, offset = 0, sparse = 0;
  324. struct squashfs_cache_entry *buffer = NULL;
  325. void *pageaddr;
  326. int mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
  327. int index = page->index >> (msblk->block_log - PAGE_CACHE_SHIFT);
  328. int start_index = page->index & ~mask;
  329. int end_index = start_index | mask;
  330. int file_end = i_size_read(inode) >> msblk->block_log;
  331. TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
  332. page->index, squashfs_i(inode)->start);
  333. if (page->index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  334. PAGE_CACHE_SHIFT))
  335. goto out;
  336. if (index < file_end || squashfs_i(inode)->fragment_block ==
  337. SQUASHFS_INVALID_BLK) {
  338. /*
  339. * Reading a datablock from disk. Need to read block list
  340. * to get location and block size.
  341. */
  342. u64 block = 0;
  343. int bsize = read_blocklist(inode, index, &block);
  344. if (bsize < 0)
  345. goto error_out;
  346. if (bsize == 0) { /* hole */
  347. bytes = index == file_end ?
  348. (i_size_read(inode) & (msblk->block_size - 1)) :
  349. msblk->block_size;
  350. sparse = 1;
  351. } else {
  352. /*
  353. * Read and decompress datablock.
  354. */
  355. buffer = squashfs_get_datablock(inode->i_sb,
  356. block, bsize);
  357. if (buffer->error) {
  358. ERROR("Unable to read page, block %llx, size %x"
  359. "\n", block, bsize);
  360. squashfs_cache_put(buffer);
  361. goto error_out;
  362. }
  363. bytes = buffer->length;
  364. }
  365. } else {
  366. /*
  367. * Datablock is stored inside a fragment (tail-end packed
  368. * block).
  369. */
  370. buffer = squashfs_get_fragment(inode->i_sb,
  371. squashfs_i(inode)->fragment_block,
  372. squashfs_i(inode)->fragment_size);
  373. if (buffer->error) {
  374. ERROR("Unable to read page, block %llx, size %x\n",
  375. squashfs_i(inode)->fragment_block,
  376. squashfs_i(inode)->fragment_size);
  377. squashfs_cache_put(buffer);
  378. goto error_out;
  379. }
  380. bytes = i_size_read(inode) & (msblk->block_size - 1);
  381. offset = squashfs_i(inode)->fragment_offset;
  382. }
  383. /*
  384. * Loop copying datablock into pages. As the datablock likely covers
  385. * many PAGE_CACHE_SIZE pages (default block size is 128 KiB) explicitly
  386. * grab the pages from the page cache, except for the page that we've
  387. * been called to fill.
  388. */
  389. for (i = start_index; i <= end_index && bytes > 0; i++,
  390. bytes -= PAGE_CACHE_SIZE, offset += PAGE_CACHE_SIZE) {
  391. struct page *push_page;
  392. int avail = sparse ? 0 : min_t(int, bytes, PAGE_CACHE_SIZE);
  393. TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
  394. push_page = (i == page->index) ? page :
  395. grab_cache_page_nowait(page->mapping, i);
  396. if (!push_page)
  397. continue;
  398. if (PageUptodate(push_page))
  399. goto skip_page;
  400. pageaddr = kmap_atomic(push_page, KM_USER0);
  401. squashfs_copy_data(pageaddr, buffer, offset, avail);
  402. memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
  403. kunmap_atomic(pageaddr, KM_USER0);
  404. flush_dcache_page(push_page);
  405. SetPageUptodate(push_page);
  406. skip_page:
  407. unlock_page(push_page);
  408. if (i != page->index)
  409. page_cache_release(push_page);
  410. }
  411. if (!sparse)
  412. squashfs_cache_put(buffer);
  413. return 0;
  414. error_out:
  415. SetPageError(page);
  416. out:
  417. pageaddr = kmap_atomic(page, KM_USER0);
  418. memset(pageaddr, 0, PAGE_CACHE_SIZE);
  419. kunmap_atomic(pageaddr, KM_USER0);
  420. flush_dcache_page(page);
  421. if (!PageError(page))
  422. SetPageUptodate(page);
  423. unlock_page(page);
  424. return 0;
  425. }
  426. const struct address_space_operations squashfs_aops = {
  427. .readpage = squashfs_readpage
  428. };