cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. * cache.c
  22. */
  23. /*
  24. * Blocks in Squashfs are compressed. To avoid repeatedly decompressing
  25. * recently accessed data Squashfs uses two small metadata and fragment caches.
  26. *
  27. * This file implements a generic cache implementation used for both caches,
  28. * plus functions layered ontop of the generic cache implementation to
  29. * access the metadata and fragment caches.
  30. *
  31. * To avoid out of memory and fragmentation issues with vmalloc the cache
  32. * uses sequences of kmalloced PAGE_CACHE_SIZE buffers.
  33. *
  34. * It should be noted that the cache is not used for file datablocks, these
  35. * are decompressed and cached in the page-cache in the normal way. The
  36. * cache is only used to temporarily cache fragment and metadata blocks
  37. * which have been read as as a result of a metadata (i.e. inode or
  38. * directory) or fragment access. Because metadata and fragments are packed
  39. * together into blocks (to gain greater compression) the read of a particular
  40. * piece of metadata or fragment will retrieve other metadata/fragments which
  41. * have been packed with it, these because of locality-of-reference may be read
  42. * in the near future. Temporarily caching them ensures they are available for
  43. * near future access without requiring an additional read and decompress.
  44. */
  45. #include <linux/fs.h>
  46. #include <linux/vfs.h>
  47. #include <linux/slab.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/sched.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/wait.h>
  52. #include <linux/pagemap.h>
  53. #include "squashfs_fs.h"
  54. #include "squashfs_fs_sb.h"
  55. #include "squashfs.h"
  56. #include "page_actor.h"
  57. /*
  58. * Look-up block in cache, and increment usage count. If not in cache, read
  59. * and decompress it from disk.
  60. */
  61. struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
  62. struct squashfs_cache *cache, u64 block, int length)
  63. {
  64. int i, n;
  65. struct squashfs_cache_entry *entry;
  66. spin_lock(&cache->lock);
  67. while (1) {
  68. for (i = cache->curr_blk, n = 0; n < cache->entries; n++) {
  69. if (cache->entry[i].block == block) {
  70. cache->curr_blk = i;
  71. break;
  72. }
  73. i = (i + 1) % cache->entries;
  74. }
  75. if (n == cache->entries) {
  76. /*
  77. * Block not in cache, if all cache entries are used
  78. * go to sleep waiting for one to become available.
  79. */
  80. if (cache->unused == 0) {
  81. cache->num_waiters++;
  82. spin_unlock(&cache->lock);
  83. wait_event(cache->wait_queue, cache->unused);
  84. spin_lock(&cache->lock);
  85. cache->num_waiters--;
  86. continue;
  87. }
  88. /*
  89. * At least one unused cache entry. A simple
  90. * round-robin strategy is used to choose the entry to
  91. * be evicted from the cache.
  92. */
  93. i = cache->next_blk;
  94. for (n = 0; n < cache->entries; n++) {
  95. if (cache->entry[i].refcount == 0)
  96. break;
  97. i = (i + 1) % cache->entries;
  98. }
  99. cache->next_blk = (i + 1) % cache->entries;
  100. entry = &cache->entry[i];
  101. /*
  102. * Initialise chosen cache entry, and fill it in from
  103. * disk.
  104. */
  105. cache->unused--;
  106. entry->block = block;
  107. entry->refcount = 1;
  108. entry->pending = 1;
  109. entry->num_waiters = 0;
  110. entry->error = 0;
  111. spin_unlock(&cache->lock);
  112. entry->length = squashfs_read_data(sb, block, length,
  113. &entry->next_index, entry->actor);
  114. spin_lock(&cache->lock);
  115. if (entry->length < 0)
  116. entry->error = entry->length;
  117. entry->pending = 0;
  118. /*
  119. * While filling this entry one or more other processes
  120. * have looked it up in the cache, and have slept
  121. * waiting for it to become available.
  122. */
  123. if (entry->num_waiters) {
  124. spin_unlock(&cache->lock);
  125. wake_up_all(&entry->wait_queue);
  126. } else
  127. spin_unlock(&cache->lock);
  128. goto out;
  129. }
  130. /*
  131. * Block already in cache. Increment refcount so it doesn't
  132. * get reused until we're finished with it, if it was
  133. * previously unused there's one less cache entry available
  134. * for reuse.
  135. */
  136. entry = &cache->entry[i];
  137. if (entry->refcount == 0)
  138. cache->unused--;
  139. entry->refcount++;
  140. /*
  141. * If the entry is currently being filled in by another process
  142. * go to sleep waiting for it to become available.
  143. */
  144. if (entry->pending) {
  145. entry->num_waiters++;
  146. spin_unlock(&cache->lock);
  147. wait_event(entry->wait_queue, !entry->pending);
  148. } else
  149. spin_unlock(&cache->lock);
  150. goto out;
  151. }
  152. out:
  153. TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
  154. cache->name, i, entry->block, entry->refcount, entry->error);
  155. if (entry->error)
  156. ERROR("Unable to read %s cache entry [%llx]\n", cache->name,
  157. block);
  158. return entry;
  159. }
  160. /*
  161. * Release cache entry, once usage count is zero it can be reused.
  162. */
  163. void squashfs_cache_put(struct squashfs_cache_entry *entry)
  164. {
  165. struct squashfs_cache *cache = entry->cache;
  166. spin_lock(&cache->lock);
  167. entry->refcount--;
  168. if (entry->refcount == 0) {
  169. cache->unused++;
  170. /*
  171. * If there's any processes waiting for a block to become
  172. * available, wake one up.
  173. */
  174. if (cache->num_waiters) {
  175. spin_unlock(&cache->lock);
  176. wake_up(&cache->wait_queue);
  177. return;
  178. }
  179. }
  180. spin_unlock(&cache->lock);
  181. }
  182. /*
  183. * Delete cache reclaiming all kmalloced buffers.
  184. */
  185. void squashfs_cache_delete(struct squashfs_cache *cache)
  186. {
  187. int i;
  188. if (cache == NULL)
  189. return;
  190. for (i = 0; i < cache->entries; i++) {
  191. if (cache->entry[i].page)
  192. free_page_array(cache->entry[i].page, cache->pages);
  193. kfree(cache->entry[i].actor);
  194. }
  195. kfree(cache->entry);
  196. kfree(cache);
  197. }
  198. /*
  199. * Initialise cache allocating the specified number of entries, each of
  200. * size block_size. To avoid vmalloc fragmentation issues each entry
  201. * is allocated as a sequence of kmalloced PAGE_CACHE_SIZE buffers.
  202. */
  203. struct squashfs_cache *squashfs_cache_init(char *name, int entries,
  204. int block_size)
  205. {
  206. int i;
  207. struct squashfs_cache *cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  208. if (cache == NULL) {
  209. ERROR("Failed to allocate %s cache\n", name);
  210. return NULL;
  211. }
  212. cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL);
  213. if (cache->entry == NULL) {
  214. ERROR("Failed to allocate %s cache\n", name);
  215. goto cleanup;
  216. }
  217. cache->curr_blk = 0;
  218. cache->next_blk = 0;
  219. cache->unused = entries;
  220. cache->entries = entries;
  221. cache->block_size = block_size;
  222. cache->pages = block_size >> PAGE_CACHE_SHIFT;
  223. cache->pages = cache->pages ? cache->pages : 1;
  224. cache->name = name;
  225. cache->num_waiters = 0;
  226. spin_lock_init(&cache->lock);
  227. init_waitqueue_head(&cache->wait_queue);
  228. for (i = 0; i < entries; i++) {
  229. struct squashfs_cache_entry *entry = &cache->entry[i];
  230. init_waitqueue_head(&cache->entry[i].wait_queue);
  231. entry->cache = cache;
  232. entry->block = SQUASHFS_INVALID_BLK;
  233. entry->page = alloc_page_array(cache->pages, GFP_KERNEL);
  234. if (!entry->page) {
  235. ERROR("Failed to allocate %s cache entry\n", name);
  236. goto cleanup;
  237. }
  238. entry->actor = squashfs_page_actor_init(entry->page,
  239. cache->pages, 0, NULL);
  240. if (entry->actor == NULL) {
  241. ERROR("Failed to allocate %s cache entry\n", name);
  242. goto cleanup;
  243. }
  244. }
  245. return cache;
  246. cleanup:
  247. squashfs_cache_delete(cache);
  248. return NULL;
  249. }
  250. /*
  251. * Copy up to length bytes from cache entry to buffer starting at offset bytes
  252. * into the cache entry. If there's not length bytes then copy the number of
  253. * bytes available. In all cases return the number of bytes copied.
  254. */
  255. int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry,
  256. int offset, int length)
  257. {
  258. int remaining = length;
  259. if (length == 0)
  260. return 0;
  261. else if (buffer == NULL)
  262. return min(length, entry->length - offset);
  263. while (offset < entry->length) {
  264. void *buff = kmap_atomic(entry->page[offset / PAGE_CACHE_SIZE])
  265. + (offset % PAGE_CACHE_SIZE);
  266. int bytes = min_t(int, entry->length - offset,
  267. PAGE_CACHE_SIZE - (offset % PAGE_CACHE_SIZE));
  268. if (bytes >= remaining) {
  269. memcpy(buffer, buff, remaining);
  270. kunmap_atomic(buff);
  271. remaining = 0;
  272. break;
  273. }
  274. memcpy(buffer, buff, bytes);
  275. kunmap_atomic(buff);
  276. buffer += bytes;
  277. remaining -= bytes;
  278. offset += bytes;
  279. }
  280. return length - remaining;
  281. }
  282. /*
  283. * Read length bytes from metadata position <block, offset> (block is the
  284. * start of the compressed block on disk, and offset is the offset into
  285. * the block once decompressed). Data is packed into consecutive blocks,
  286. * and length bytes may require reading more than one block.
  287. */
  288. int squashfs_read_metadata(struct super_block *sb, void *buffer,
  289. u64 *block, int *offset, int length)
  290. {
  291. struct squashfs_sb_info *msblk = sb->s_fs_info;
  292. int bytes, res = length;
  293. struct squashfs_cache_entry *entry;
  294. TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset);
  295. while (length) {
  296. entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0);
  297. if (entry->error) {
  298. res = entry->error;
  299. goto error;
  300. } else if (*offset >= entry->length) {
  301. res = -EIO;
  302. goto error;
  303. }
  304. bytes = squashfs_copy_data(buffer, entry, *offset, length);
  305. if (buffer)
  306. buffer += bytes;
  307. length -= bytes;
  308. *offset += bytes;
  309. if (*offset == entry->length) {
  310. *block = entry->next_index;
  311. *offset = 0;
  312. }
  313. squashfs_cache_put(entry);
  314. }
  315. return res;
  316. error:
  317. squashfs_cache_put(entry);
  318. return res;
  319. }
  320. /*
  321. * Look-up in the fragmment cache the fragment located at <start_block> in the
  322. * filesystem. If necessary read and decompress it from disk.
  323. */
  324. struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb,
  325. u64 start_block, int length)
  326. {
  327. struct squashfs_sb_info *msblk = sb->s_fs_info;
  328. return squashfs_cache_get(sb, msblk->fragment_cache, start_block,
  329. length);
  330. }
  331. /*
  332. * Read and decompress the datablock located at <start_block> in the
  333. * filesystem. The cache is used here to avoid duplicating locking and
  334. * read/decompress code.
  335. */
  336. struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *sb,
  337. u64 start_block, int length)
  338. {
  339. struct squashfs_sb_info *msblk = sb->s_fs_info;
  340. return squashfs_cache_get(sb, msblk->read_page, start_block, length);
  341. }
  342. /*
  343. * Read a filesystem table (uncompressed sequence of bytes) from disk
  344. */
  345. void *squashfs_read_table(struct super_block *sb, u64 block, int length)
  346. {
  347. int pages = (length + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  348. struct page **page;
  349. void *buff;
  350. int res;
  351. struct squashfs_page_actor *actor;
  352. page = alloc_page_array(pages, GFP_KERNEL);
  353. if (!page)
  354. return ERR_PTR(-ENOMEM);
  355. actor = squashfs_page_actor_init(page, pages, length, NULL);
  356. if (actor == NULL) {
  357. res = -ENOMEM;
  358. goto failed;
  359. }
  360. res = squashfs_read_data(sb, block, length |
  361. SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, actor);
  362. if (res < 0)
  363. goto failed2;
  364. buff = kmalloc(length, GFP_KERNEL);
  365. if (!buff)
  366. goto failed2;
  367. squashfs_actor_to_buf(actor, buff, length);
  368. squashfs_page_actor_free(actor, 0);
  369. free_page_array(page, pages);
  370. return buff;
  371. failed2:
  372. squashfs_page_actor_free(actor, 0);
  373. failed:
  374. free_page_array(page, pages);
  375. return ERR_PTR(res);
  376. }