cache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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, j;
  188. if (cache == NULL)
  189. return;
  190. for (i = 0; i < cache->entries; i++) {
  191. if (cache->entry[i].data) {
  192. for (j = 0; j < cache->pages; j++)
  193. kfree(cache->entry[i].data[j]);
  194. kfree(cache->entry[i].data);
  195. }
  196. kfree(cache->entry[i].actor);
  197. }
  198. kfree(cache->entry);
  199. kfree(cache);
  200. }
  201. /*
  202. * Initialise cache allocating the specified number of entries, each of
  203. * size block_size. To avoid vmalloc fragmentation issues each entry
  204. * is allocated as a sequence of kmalloced PAGE_CACHE_SIZE buffers.
  205. */
  206. struct squashfs_cache *squashfs_cache_init(char *name, int entries,
  207. int block_size)
  208. {
  209. int i, j;
  210. struct squashfs_cache *cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  211. if (cache == NULL) {
  212. ERROR("Failed to allocate %s cache\n", name);
  213. return NULL;
  214. }
  215. cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL);
  216. if (cache->entry == NULL) {
  217. ERROR("Failed to allocate %s cache\n", name);
  218. goto cleanup;
  219. }
  220. cache->curr_blk = 0;
  221. cache->next_blk = 0;
  222. cache->unused = entries;
  223. cache->entries = entries;
  224. cache->block_size = block_size;
  225. cache->pages = block_size >> PAGE_CACHE_SHIFT;
  226. cache->pages = cache->pages ? cache->pages : 1;
  227. cache->name = name;
  228. cache->num_waiters = 0;
  229. spin_lock_init(&cache->lock);
  230. init_waitqueue_head(&cache->wait_queue);
  231. for (i = 0; i < entries; i++) {
  232. struct squashfs_cache_entry *entry = &cache->entry[i];
  233. init_waitqueue_head(&cache->entry[i].wait_queue);
  234. entry->cache = cache;
  235. entry->block = SQUASHFS_INVALID_BLK;
  236. entry->data = kcalloc(cache->pages, sizeof(void *), GFP_KERNEL);
  237. if (entry->data == NULL) {
  238. ERROR("Failed to allocate %s cache entry\n", name);
  239. goto cleanup;
  240. }
  241. for (j = 0; j < cache->pages; j++) {
  242. entry->data[j] = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  243. if (entry->data[j] == NULL) {
  244. ERROR("Failed to allocate %s buffer\n", name);
  245. goto cleanup;
  246. }
  247. }
  248. entry->actor = squashfs_page_actor_init(entry->data,
  249. cache->pages, 0);
  250. if (entry->actor == NULL) {
  251. ERROR("Failed to allocate %s cache entry\n", name);
  252. goto cleanup;
  253. }
  254. }
  255. return cache;
  256. cleanup:
  257. squashfs_cache_delete(cache);
  258. return NULL;
  259. }
  260. /*
  261. * Copy up to length bytes from cache entry to buffer starting at offset bytes
  262. * into the cache entry. If there's not length bytes then copy the number of
  263. * bytes available. In all cases return the number of bytes copied.
  264. */
  265. int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry,
  266. int offset, int length)
  267. {
  268. int remaining = length;
  269. if (length == 0)
  270. return 0;
  271. else if (buffer == NULL)
  272. return min(length, entry->length - offset);
  273. while (offset < entry->length) {
  274. void *buff = entry->data[offset / PAGE_CACHE_SIZE]
  275. + (offset % PAGE_CACHE_SIZE);
  276. int bytes = min_t(int, entry->length - offset,
  277. PAGE_CACHE_SIZE - (offset % PAGE_CACHE_SIZE));
  278. if (bytes >= remaining) {
  279. memcpy(buffer, buff, remaining);
  280. remaining = 0;
  281. break;
  282. }
  283. memcpy(buffer, buff, bytes);
  284. buffer += bytes;
  285. remaining -= bytes;
  286. offset += bytes;
  287. }
  288. return length - remaining;
  289. }
  290. /*
  291. * Read length bytes from metadata position <block, offset> (block is the
  292. * start of the compressed block on disk, and offset is the offset into
  293. * the block once decompressed). Data is packed into consecutive blocks,
  294. * and length bytes may require reading more than one block.
  295. */
  296. int squashfs_read_metadata(struct super_block *sb, void *buffer,
  297. u64 *block, int *offset, int length)
  298. {
  299. struct squashfs_sb_info *msblk = sb->s_fs_info;
  300. int bytes, res = length;
  301. struct squashfs_cache_entry *entry;
  302. TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset);
  303. if (unlikely(length < 0))
  304. return -EIO;
  305. while (length) {
  306. entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0);
  307. if (entry->error) {
  308. res = entry->error;
  309. goto error;
  310. } else if (*offset >= entry->length) {
  311. res = -EIO;
  312. goto error;
  313. }
  314. bytes = squashfs_copy_data(buffer, entry, *offset, length);
  315. if (buffer)
  316. buffer += bytes;
  317. length -= bytes;
  318. *offset += bytes;
  319. if (*offset == entry->length) {
  320. *block = entry->next_index;
  321. *offset = 0;
  322. }
  323. squashfs_cache_put(entry);
  324. }
  325. return res;
  326. error:
  327. squashfs_cache_put(entry);
  328. return res;
  329. }
  330. /*
  331. * Look-up in the fragmment cache the fragment located at <start_block> in the
  332. * filesystem. If necessary read and decompress it from disk.
  333. */
  334. struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb,
  335. u64 start_block, int length)
  336. {
  337. struct squashfs_sb_info *msblk = sb->s_fs_info;
  338. return squashfs_cache_get(sb, msblk->fragment_cache, start_block,
  339. length);
  340. }
  341. /*
  342. * Read and decompress the datablock located at <start_block> in the
  343. * filesystem. The cache is used here to avoid duplicating locking and
  344. * read/decompress code.
  345. */
  346. struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *sb,
  347. u64 start_block, int length)
  348. {
  349. struct squashfs_sb_info *msblk = sb->s_fs_info;
  350. return squashfs_cache_get(sb, msblk->read_page, start_block, length);
  351. }
  352. /*
  353. * Read a filesystem table (uncompressed sequence of bytes) from disk
  354. */
  355. void *squashfs_read_table(struct super_block *sb, u64 block, int length)
  356. {
  357. int pages = (length + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  358. int i, res;
  359. void *table, *buffer, **data;
  360. struct squashfs_page_actor *actor;
  361. table = buffer = kmalloc(length, GFP_KERNEL);
  362. if (table == NULL)
  363. return ERR_PTR(-ENOMEM);
  364. data = kcalloc(pages, sizeof(void *), GFP_KERNEL);
  365. if (data == NULL) {
  366. res = -ENOMEM;
  367. goto failed;
  368. }
  369. actor = squashfs_page_actor_init(data, pages, length);
  370. if (actor == NULL) {
  371. res = -ENOMEM;
  372. goto failed2;
  373. }
  374. for (i = 0; i < pages; i++, buffer += PAGE_CACHE_SIZE)
  375. data[i] = buffer;
  376. res = squashfs_read_data(sb, block, length |
  377. SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, actor);
  378. kfree(data);
  379. kfree(actor);
  380. if (res < 0)
  381. goto failed;
  382. return table;
  383. failed2:
  384. kfree(data);
  385. failed:
  386. kfree(table);
  387. return ERR_PTR(res);
  388. }