cache.c 11 KB

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