file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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_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_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 = squashfs_block_size(blist[i]);
  168. if (size < 0) {
  169. err = size;
  170. goto failure;
  171. }
  172. block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
  173. }
  174. n -= blocks;
  175. }
  176. kfree(blist);
  177. return block;
  178. failure:
  179. kfree(blist);
  180. return err;
  181. }
  182. /*
  183. * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
  184. * can cache one index -> datablock/blocklist-block mapping. We wish
  185. * to distribute these over the length of the file, entry[0] maps index x,
  186. * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
  187. * The larger the file, the greater the skip factor. The skip factor is
  188. * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
  189. * the number of metadata blocks that need to be read fits into the cache.
  190. * If the skip factor is limited in this way then the file will use multiple
  191. * slots.
  192. */
  193. static inline int calculate_skip(int blocks)
  194. {
  195. int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
  196. * SQUASHFS_META_INDEXES);
  197. return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
  198. }
  199. /*
  200. * Search and grow the index cache for the specified inode, returning the
  201. * on-disk locations of the datablock and block list metadata block
  202. * <index_block, index_offset> for index (scaled to nearest cache index).
  203. */
  204. static int fill_meta_index(struct inode *inode, int index,
  205. u64 *index_block, int *index_offset, u64 *data_block)
  206. {
  207. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  208. int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
  209. int offset = 0;
  210. struct meta_index *meta;
  211. struct meta_entry *meta_entry;
  212. u64 cur_index_block = squashfs_i(inode)->block_list_start;
  213. int cur_offset = squashfs_i(inode)->offset;
  214. u64 cur_data_block = squashfs_i(inode)->start;
  215. int err, i;
  216. /*
  217. * Scale index to cache index (cache slot entry)
  218. */
  219. index /= SQUASHFS_META_INDEXES * skip;
  220. while (offset < index) {
  221. meta = locate_meta_index(inode, offset + 1, index);
  222. if (meta == NULL) {
  223. meta = empty_meta_index(inode, offset + 1, skip);
  224. if (meta == NULL)
  225. goto all_done;
  226. } else {
  227. offset = index < meta->offset + meta->entries ? index :
  228. meta->offset + meta->entries - 1;
  229. meta_entry = &meta->meta_entry[offset - meta->offset];
  230. cur_index_block = meta_entry->index_block +
  231. msblk->inode_table;
  232. cur_offset = meta_entry->offset;
  233. cur_data_block = meta_entry->data_block;
  234. TRACE("get_meta_index: offset %d, meta->offset %d, "
  235. "meta->entries %d\n", offset, meta->offset,
  236. meta->entries);
  237. TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
  238. " data_block 0x%llx\n", cur_index_block,
  239. cur_offset, cur_data_block);
  240. }
  241. /*
  242. * If necessary grow cache slot by reading block list. Cache
  243. * slot is extended up to index or to the end of the slot, in
  244. * which case further slots will be used.
  245. */
  246. for (i = meta->offset + meta->entries; i <= index &&
  247. i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
  248. int blocks = skip * SQUASHFS_META_INDEXES;
  249. long long res = read_indexes(inode->i_sb, blocks,
  250. &cur_index_block, &cur_offset);
  251. if (res < 0) {
  252. if (meta->entries == 0)
  253. /*
  254. * Don't leave an empty slot on read
  255. * error allocated to this inode...
  256. */
  257. meta->inode_number = 0;
  258. err = res;
  259. goto failed;
  260. }
  261. cur_data_block += res;
  262. meta_entry = &meta->meta_entry[i - meta->offset];
  263. meta_entry->index_block = cur_index_block -
  264. msblk->inode_table;
  265. meta_entry->offset = cur_offset;
  266. meta_entry->data_block = cur_data_block;
  267. meta->entries++;
  268. offset++;
  269. }
  270. TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
  271. meta->offset, meta->entries);
  272. release_meta_index(inode, meta);
  273. }
  274. all_done:
  275. *index_block = cur_index_block;
  276. *index_offset = cur_offset;
  277. *data_block = cur_data_block;
  278. /*
  279. * Scale cache index (cache slot entry) to index
  280. */
  281. return offset * SQUASHFS_META_INDEXES * skip;
  282. failed:
  283. release_meta_index(inode, meta);
  284. return err;
  285. }
  286. /*
  287. * Get the on-disk location and compressed size of the datablock
  288. * specified by index. Fill_meta_index() does most of the work.
  289. */
  290. static int read_blocklist(struct inode *inode, int index, u64 *block)
  291. {
  292. u64 start;
  293. long long blks;
  294. int offset;
  295. __le32 size;
  296. int res = fill_meta_index(inode, index, &start, &offset, block);
  297. TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
  298. " 0x%x, block 0x%llx\n", res, index, start, offset,
  299. *block);
  300. if (res < 0)
  301. return res;
  302. /*
  303. * res contains the index of the mapping returned by fill_meta_index(),
  304. * this will likely be less than the desired index (because the
  305. * meta_index cache works at a higher granularity). Read any
  306. * extra block indexes needed.
  307. */
  308. if (res < index) {
  309. blks = read_indexes(inode->i_sb, index - res, &start, &offset);
  310. if (blks < 0)
  311. return (int) blks;
  312. *block += blks;
  313. }
  314. /*
  315. * Read length of block specified by index.
  316. */
  317. res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
  318. sizeof(size));
  319. if (res < 0)
  320. return res;
  321. return squashfs_block_size(size);
  322. }
  323. /* Copy data into page cache */
  324. void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
  325. int bytes, int offset)
  326. {
  327. struct inode *inode = page->mapping->host;
  328. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  329. void *pageaddr;
  330. int i, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
  331. int start_index = page->index & ~mask, end_index = start_index | mask;
  332. /*
  333. * Loop copying datablock into pages. As the datablock likely covers
  334. * many PAGE_SIZE pages (default block size is 128 KiB) explicitly
  335. * grab the pages from the page cache, except for the page that we've
  336. * been called to fill.
  337. */
  338. for (i = start_index; i <= end_index && bytes > 0; i++,
  339. bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
  340. struct page *push_page;
  341. int avail = buffer ? min_t(int, bytes, PAGE_SIZE) : 0;
  342. TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
  343. push_page = (i == page->index) ? page :
  344. grab_cache_page_nowait(page->mapping, i);
  345. if (!push_page)
  346. continue;
  347. if (PageUptodate(push_page))
  348. goto skip_page;
  349. pageaddr = kmap_atomic(push_page);
  350. squashfs_copy_data(pageaddr, buffer, offset, avail);
  351. memset(pageaddr + avail, 0, PAGE_SIZE - avail);
  352. kunmap_atomic(pageaddr);
  353. flush_dcache_page(push_page);
  354. SetPageUptodate(push_page);
  355. skip_page:
  356. unlock_page(push_page);
  357. if (i != page->index)
  358. put_page(push_page);
  359. }
  360. }
  361. /* Read datablock stored packed inside a fragment (tail-end packed block) */
  362. static int squashfs_readpage_fragment(struct page *page)
  363. {
  364. struct inode *inode = page->mapping->host;
  365. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  366. struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
  367. squashfs_i(inode)->fragment_block,
  368. squashfs_i(inode)->fragment_size);
  369. int res = buffer->error;
  370. if (res)
  371. ERROR("Unable to read page, block %llx, size %x\n",
  372. squashfs_i(inode)->fragment_block,
  373. squashfs_i(inode)->fragment_size);
  374. else
  375. squashfs_copy_cache(page, buffer, i_size_read(inode) &
  376. (msblk->block_size - 1),
  377. squashfs_i(inode)->fragment_offset);
  378. squashfs_cache_put(buffer);
  379. return res;
  380. }
  381. static int squashfs_readpage_sparse(struct page *page, int index, int file_end)
  382. {
  383. struct inode *inode = page->mapping->host;
  384. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  385. int bytes = index == file_end ?
  386. (i_size_read(inode) & (msblk->block_size - 1)) :
  387. msblk->block_size;
  388. squashfs_copy_cache(page, NULL, bytes, 0);
  389. return 0;
  390. }
  391. static int squashfs_readpage(struct file *file, struct page *page)
  392. {
  393. struct inode *inode = page->mapping->host;
  394. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  395. int index = page->index >> (msblk->block_log - PAGE_SHIFT);
  396. int file_end = i_size_read(inode) >> msblk->block_log;
  397. int res;
  398. void *pageaddr;
  399. TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
  400. page->index, squashfs_i(inode)->start);
  401. if (page->index >= ((i_size_read(inode) + PAGE_SIZE - 1) >>
  402. PAGE_SHIFT))
  403. goto out;
  404. if (index < file_end || squashfs_i(inode)->fragment_block ==
  405. SQUASHFS_INVALID_BLK) {
  406. u64 block = 0;
  407. int bsize = read_blocklist(inode, index, &block);
  408. if (bsize < 0)
  409. goto error_out;
  410. if (bsize == 0)
  411. res = squashfs_readpage_sparse(page, index, file_end);
  412. else
  413. res = squashfs_readpage_block(page, block, bsize);
  414. } else
  415. res = squashfs_readpage_fragment(page);
  416. if (!res)
  417. return 0;
  418. error_out:
  419. SetPageError(page);
  420. out:
  421. pageaddr = kmap_atomic(page);
  422. memset(pageaddr, 0, PAGE_SIZE);
  423. kunmap_atomic(pageaddr);
  424. flush_dcache_page(page);
  425. if (!PageError(page))
  426. SetPageUptodate(page);
  427. unlock_page(page);
  428. return 0;
  429. }
  430. const struct address_space_operations squashfs_aops = {
  431. .readpage = squashfs_readpage
  432. };