cache.c 11 KB

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