cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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@lougher.demon.co.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 isssues 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/zlib.h>
  53. #include <linux/pagemap.h>
  54. #include "squashfs_fs.h"
  55. #include "squashfs_fs_sb.h"
  56. #include "squashfs_fs_i.h"
  57. #include "squashfs.h"
  58. /*
  59. * Look-up block in cache, and increment usage count. If not in cache, read
  60. * and decompress it from disk.
  61. */
  62. struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
  63. struct squashfs_cache *cache, u64 block, int length)
  64. {
  65. int i, n;
  66. struct squashfs_cache_entry *entry;
  67. spin_lock(&cache->lock);
  68. while (1) {
  69. for (i = 0; i < cache->entries; i++)
  70. if (cache->entry[i].block == block)
  71. break;
  72. if (i == cache->entries) {
  73. /*
  74. * Block not in cache, if all cache entries are used
  75. * go to sleep waiting for one to become available.
  76. */
  77. if (cache->unused == 0) {
  78. cache->num_waiters++;
  79. spin_unlock(&cache->lock);
  80. wait_event(cache->wait_queue, cache->unused);
  81. spin_lock(&cache->lock);
  82. cache->num_waiters--;
  83. continue;
  84. }
  85. /*
  86. * At least one unused cache entry. A simple
  87. * round-robin strategy is used to choose the entry to
  88. * be evicted from the cache.
  89. */
  90. i = cache->next_blk;
  91. for (n = 0; n < cache->entries; n++) {
  92. if (cache->entry[i].refcount == 0)
  93. break;
  94. i = (i + 1) % cache->entries;
  95. }
  96. cache->next_blk = (i + 1) % cache->entries;
  97. entry = &cache->entry[i];
  98. /*
  99. * Initialise choosen cache entry, and fill it in from
  100. * disk.
  101. */
  102. cache->unused--;
  103. entry->block = block;
  104. entry->refcount = 1;
  105. entry->pending = 1;
  106. entry->num_waiters = 0;
  107. entry->error = 0;
  108. spin_unlock(&cache->lock);
  109. entry->length = squashfs_read_data(sb, entry->data,
  110. block, length, &entry->next_index,
  111. cache->block_size, cache->pages);
  112. spin_lock(&cache->lock);
  113. if (entry->length < 0)
  114. entry->error = entry->length;
  115. entry->pending = 0;
  116. /*
  117. * While filling this entry one or more other processes
  118. * have looked it up in the cache, and have slept
  119. * waiting for it to become available.
  120. */
  121. if (entry->num_waiters) {
  122. spin_unlock(&cache->lock);
  123. wake_up_all(&entry->wait_queue);
  124. } else
  125. spin_unlock(&cache->lock);
  126. goto out;
  127. }
  128. /*
  129. * Block already in cache. Increment refcount so it doesn't
  130. * get reused until we're finished with it, if it was
  131. * previously unused there's one less cache entry available
  132. * for reuse.
  133. */
  134. entry = &cache->entry[i];
  135. if (entry->refcount == 0)
  136. cache->unused--;
  137. entry->refcount++;
  138. /*
  139. * If the entry is currently being filled in by another process
  140. * go to sleep waiting for it to become available.
  141. */
  142. if (entry->pending) {
  143. entry->num_waiters++;
  144. spin_unlock(&cache->lock);
  145. wait_event(entry->wait_queue, !entry->pending);
  146. } else
  147. spin_unlock(&cache->lock);
  148. goto out;
  149. }
  150. out:
  151. TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
  152. cache->name, i, entry->block, entry->refcount, entry->error);
  153. if (entry->error)
  154. ERROR("Unable to read %s cache entry [%llx]\n", cache->name,
  155. block);
  156. return entry;
  157. }
  158. /*
  159. * Release cache entry, once usage count is zero it can be reused.
  160. */
  161. void squashfs_cache_put(struct squashfs_cache_entry *entry)
  162. {
  163. struct squashfs_cache *cache = entry->cache;
  164. spin_lock(&cache->lock);
  165. entry->refcount--;
  166. if (entry->refcount == 0) {
  167. cache->unused++;
  168. /*
  169. * If there's any processes waiting for a block to become
  170. * available, wake one up.
  171. */
  172. if (cache->num_waiters) {
  173. spin_unlock(&cache->lock);
  174. wake_up(&cache->wait_queue);
  175. return;
  176. }
  177. }
  178. spin_unlock(&cache->lock);
  179. }
  180. /*
  181. * Delete cache reclaiming all kmalloced buffers.
  182. */
  183. void squashfs_cache_delete(struct squashfs_cache *cache)
  184. {
  185. int i, j;
  186. if (cache == NULL)
  187. return;
  188. for (i = 0; i < cache->entries; i++) {
  189. if (cache->entry[i].data) {
  190. for (j = 0; j < cache->pages; j++)
  191. kfree(cache->entry[i].data[j]);
  192. kfree(cache->entry[i].data);
  193. }
  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, j;
  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->next_blk = 0;
  218. cache->unused = entries;
  219. cache->entries = entries;
  220. cache->block_size = block_size;
  221. cache->pages = block_size >> PAGE_CACHE_SHIFT;
  222. cache->pages = cache->pages ? cache->pages : 1;
  223. cache->name = name;
  224. cache->num_waiters = 0;
  225. spin_lock_init(&cache->lock);
  226. init_waitqueue_head(&cache->wait_queue);
  227. for (i = 0; i < entries; i++) {
  228. struct squashfs_cache_entry *entry = &cache->entry[i];
  229. init_waitqueue_head(&cache->entry[i].wait_queue);
  230. entry->cache = cache;
  231. entry->block = SQUASHFS_INVALID_BLK;
  232. entry->data = kcalloc(cache->pages, sizeof(void *), GFP_KERNEL);
  233. if (entry->data == NULL) {
  234. ERROR("Failed to allocate %s cache entry\n", name);
  235. goto cleanup;
  236. }
  237. for (j = 0; j < cache->pages; j++) {
  238. entry->data[j] = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  239. if (entry->data[j] == NULL) {
  240. ERROR("Failed to allocate %s buffer\n", name);
  241. goto cleanup;
  242. }
  243. }
  244. }
  245. return cache;
  246. cleanup:
  247. squashfs_cache_delete(cache);
  248. return NULL;
  249. }
  250. /*
  251. * Copy upto 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 = entry->data[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. remaining = 0;
  271. break;
  272. }
  273. memcpy(buffer, buff, bytes);
  274. buffer += bytes;
  275. remaining -= bytes;
  276. offset += bytes;
  277. }
  278. return length - remaining;
  279. }
  280. /*
  281. * Read length bytes from metadata position <block, offset> (block is the
  282. * start of the compressed block on disk, and offset is the offset into
  283. * the block once decompressed). Data is packed into consecutive blocks,
  284. * and length bytes may require reading more than one block.
  285. */
  286. int squashfs_read_metadata(struct super_block *sb, void *buffer,
  287. u64 *block, int *offset, int length)
  288. {
  289. struct squashfs_sb_info *msblk = sb->s_fs_info;
  290. int bytes, copied = length;
  291. struct squashfs_cache_entry *entry;
  292. TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset);
  293. while (length) {
  294. entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0);
  295. if (entry->error)
  296. return entry->error;
  297. else if (*offset >= entry->length)
  298. return -EIO;
  299. bytes = squashfs_copy_data(buffer, entry, *offset, length);
  300. if (buffer)
  301. buffer += bytes;
  302. length -= bytes;
  303. *offset += bytes;
  304. if (*offset == entry->length) {
  305. *block = entry->next_index;
  306. *offset = 0;
  307. }
  308. squashfs_cache_put(entry);
  309. }
  310. return copied;
  311. }
  312. /*
  313. * Look-up in the fragmment cache the fragment located at <start_block> in the
  314. * filesystem. If necessary read and decompress it from disk.
  315. */
  316. struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb,
  317. u64 start_block, int length)
  318. {
  319. struct squashfs_sb_info *msblk = sb->s_fs_info;
  320. return squashfs_cache_get(sb, msblk->fragment_cache, start_block,
  321. length);
  322. }
  323. /*
  324. * Read and decompress the datablock located at <start_block> in the
  325. * filesystem. The cache is used here to avoid duplicating locking and
  326. * read/decompress code.
  327. */
  328. struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *sb,
  329. u64 start_block, int length)
  330. {
  331. struct squashfs_sb_info *msblk = sb->s_fs_info;
  332. return squashfs_cache_get(sb, msblk->read_page, start_block, length);
  333. }
  334. /*
  335. * Read a filesystem table (uncompressed sequence of bytes) from disk
  336. */
  337. int squashfs_read_table(struct super_block *sb, void *buffer, u64 block,
  338. int length)
  339. {
  340. int pages = (length + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  341. int i, res;
  342. void **data = kcalloc(pages, sizeof(void *), GFP_KERNEL);
  343. if (data == NULL)
  344. return -ENOMEM;
  345. for (i = 0; i < pages; i++, buffer += PAGE_CACHE_SIZE)
  346. data[i] = buffer;
  347. res = squashfs_read_data(sb, data, block, length |
  348. SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, length, pages);
  349. kfree(data);
  350. return res;
  351. }