Lines Matching +full:block +full:- +full:offset
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
23 * are decompressed and cached in the page-cache in the normal way. The
29 * have been packed with it, these because of locality-of-reference may be read
49 * Look-up block in cache, and increment usage count. If not in cache, read
53 struct squashfs_cache *cache, u64 block, int length) in squashfs_cache_get() argument
58 spin_lock(&cache->lock); in squashfs_cache_get()
61 for (i = cache->curr_blk, n = 0; n < cache->entries; n++) { in squashfs_cache_get()
62 if (cache->entry[i].block == block) { in squashfs_cache_get()
63 cache->curr_blk = i; in squashfs_cache_get()
66 i = (i + 1) % cache->entries; in squashfs_cache_get()
69 if (n == cache->entries) { in squashfs_cache_get()
71 * Block not in cache, if all cache entries are used in squashfs_cache_get()
74 if (cache->unused == 0) { in squashfs_cache_get()
75 cache->num_waiters++; in squashfs_cache_get()
76 spin_unlock(&cache->lock); in squashfs_cache_get()
77 wait_event(cache->wait_queue, cache->unused); in squashfs_cache_get()
78 spin_lock(&cache->lock); in squashfs_cache_get()
79 cache->num_waiters--; in squashfs_cache_get()
85 * round-robin strategy is used to choose the entry to in squashfs_cache_get()
88 i = cache->next_blk; in squashfs_cache_get()
89 for (n = 0; n < cache->entries; n++) { in squashfs_cache_get()
90 if (cache->entry[i].refcount == 0) in squashfs_cache_get()
92 i = (i + 1) % cache->entries; in squashfs_cache_get()
95 cache->next_blk = (i + 1) % cache->entries; in squashfs_cache_get()
96 entry = &cache->entry[i]; in squashfs_cache_get()
102 cache->unused--; in squashfs_cache_get()
103 entry->block = block; in squashfs_cache_get()
104 entry->refcount = 1; in squashfs_cache_get()
105 entry->pending = 1; in squashfs_cache_get()
106 entry->num_waiters = 0; in squashfs_cache_get()
107 entry->error = 0; in squashfs_cache_get()
108 spin_unlock(&cache->lock); in squashfs_cache_get()
110 entry->length = squashfs_read_data(sb, block, length, in squashfs_cache_get()
111 &entry->next_index, entry->actor); in squashfs_cache_get()
113 spin_lock(&cache->lock); in squashfs_cache_get()
115 if (entry->length < 0) in squashfs_cache_get()
116 entry->error = entry->length; in squashfs_cache_get()
118 entry->pending = 0; in squashfs_cache_get()
125 if (entry->num_waiters) { in squashfs_cache_get()
126 spin_unlock(&cache->lock); in squashfs_cache_get()
127 wake_up_all(&entry->wait_queue); in squashfs_cache_get()
129 spin_unlock(&cache->lock); in squashfs_cache_get()
135 * Block already in cache. Increment refcount so it doesn't in squashfs_cache_get()
140 entry = &cache->entry[i]; in squashfs_cache_get()
141 if (entry->refcount == 0) in squashfs_cache_get()
142 cache->unused--; in squashfs_cache_get()
143 entry->refcount++; in squashfs_cache_get()
149 if (entry->pending) { in squashfs_cache_get()
150 entry->num_waiters++; in squashfs_cache_get()
151 spin_unlock(&cache->lock); in squashfs_cache_get()
152 wait_event(entry->wait_queue, !entry->pending); in squashfs_cache_get()
154 spin_unlock(&cache->lock); in squashfs_cache_get()
160 TRACE("Got %s %d, start block %lld, refcount %d, error %d\n", in squashfs_cache_get()
161 cache->name, i, entry->block, entry->refcount, entry->error); in squashfs_cache_get()
163 if (entry->error) in squashfs_cache_get()
164 ERROR("Unable to read %s cache entry [%llx]\n", cache->name, in squashfs_cache_get()
165 block); in squashfs_cache_get()
175 struct squashfs_cache *cache = entry->cache; in squashfs_cache_put()
177 spin_lock(&cache->lock); in squashfs_cache_put()
178 entry->refcount--; in squashfs_cache_put()
179 if (entry->refcount == 0) { in squashfs_cache_put()
180 cache->unused++; in squashfs_cache_put()
182 * If there's any processes waiting for a block to become in squashfs_cache_put()
185 if (cache->num_waiters) { in squashfs_cache_put()
186 spin_unlock(&cache->lock); in squashfs_cache_put()
187 wake_up(&cache->wait_queue); in squashfs_cache_put()
191 spin_unlock(&cache->lock); in squashfs_cache_put()
204 for (i = 0; i < cache->entries; i++) { in squashfs_cache_delete()
205 if (cache->entry[i].data) { in squashfs_cache_delete()
206 for (j = 0; j < cache->pages; j++) in squashfs_cache_delete()
207 kfree(cache->entry[i].data[j]); in squashfs_cache_delete()
208 kfree(cache->entry[i].data); in squashfs_cache_delete()
210 kfree(cache->entry[i].actor); in squashfs_cache_delete()
213 kfree(cache->entry); in squashfs_cache_delete()
235 return ERR_PTR(-ENOMEM); in squashfs_cache_init()
238 cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL); in squashfs_cache_init()
239 if (cache->entry == NULL) { in squashfs_cache_init()
244 cache->curr_blk = 0; in squashfs_cache_init()
245 cache->next_blk = 0; in squashfs_cache_init()
246 cache->unused = entries; in squashfs_cache_init()
247 cache->entries = entries; in squashfs_cache_init()
248 cache->block_size = block_size; in squashfs_cache_init()
249 cache->pages = block_size >> PAGE_SHIFT; in squashfs_cache_init()
250 cache->pages = cache->pages ? cache->pages : 1; in squashfs_cache_init()
251 cache->name = name; in squashfs_cache_init()
252 cache->num_waiters = 0; in squashfs_cache_init()
253 spin_lock_init(&cache->lock); in squashfs_cache_init()
254 init_waitqueue_head(&cache->wait_queue); in squashfs_cache_init()
257 struct squashfs_cache_entry *entry = &cache->entry[i]; in squashfs_cache_init()
259 init_waitqueue_head(&cache->entry[i].wait_queue); in squashfs_cache_init()
260 entry->cache = cache; in squashfs_cache_init()
261 entry->block = SQUASHFS_INVALID_BLK; in squashfs_cache_init()
262 entry->data = kcalloc(cache->pages, sizeof(void *), GFP_KERNEL); in squashfs_cache_init()
263 if (entry->data == NULL) { in squashfs_cache_init()
268 for (j = 0; j < cache->pages; j++) { in squashfs_cache_init()
269 entry->data[j] = kmalloc(PAGE_SIZE, GFP_KERNEL); in squashfs_cache_init()
270 if (entry->data[j] == NULL) { in squashfs_cache_init()
276 entry->actor = squashfs_page_actor_init(entry->data, in squashfs_cache_init()
277 cache->pages, 0); in squashfs_cache_init()
278 if (entry->actor == NULL) { in squashfs_cache_init()
288 return ERR_PTR(-ENOMEM); in squashfs_cache_init()
293 * Copy up to length bytes from cache entry to buffer starting at offset bytes
298 int offset, int length) in squashfs_copy_data() argument
305 return min(length, entry->length - offset); in squashfs_copy_data()
307 while (offset < entry->length) { in squashfs_copy_data()
308 void *buff = entry->data[offset / PAGE_SIZE] in squashfs_copy_data()
309 + (offset % PAGE_SIZE); in squashfs_copy_data()
310 int bytes = min_t(int, entry->length - offset, in squashfs_copy_data()
311 PAGE_SIZE - (offset % PAGE_SIZE)); in squashfs_copy_data()
321 remaining -= bytes; in squashfs_copy_data()
322 offset += bytes; in squashfs_copy_data()
325 return length - remaining; in squashfs_copy_data()
330 * Read length bytes from metadata position <block, offset> (block is the
331 * start of the compressed block on disk, and offset is the offset into
332 * the block once decompressed). Data is packed into consecutive blocks,
333 * and length bytes may require reading more than one block.
336 u64 *block, int *offset, int length) in squashfs_read_metadata() argument
338 struct squashfs_sb_info *msblk = sb->s_fs_info; in squashfs_read_metadata()
342 TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset); in squashfs_read_metadata()
345 return -EIO; in squashfs_read_metadata()
348 entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0); in squashfs_read_metadata()
349 if (entry->error) { in squashfs_read_metadata()
350 res = entry->error; in squashfs_read_metadata()
352 } else if (*offset >= entry->length) { in squashfs_read_metadata()
353 res = -EIO; in squashfs_read_metadata()
357 bytes = squashfs_copy_data(buffer, entry, *offset, length); in squashfs_read_metadata()
360 length -= bytes; in squashfs_read_metadata()
361 *offset += bytes; in squashfs_read_metadata()
363 if (*offset == entry->length) { in squashfs_read_metadata()
364 *block = entry->next_index; in squashfs_read_metadata()
365 *offset = 0; in squashfs_read_metadata()
380 * Look-up in the fragmment cache the fragment located at <start_block> in the
386 struct squashfs_sb_info *msblk = sb->s_fs_info; in squashfs_get_fragment()
388 return squashfs_cache_get(sb, msblk->fragment_cache, start_block, in squashfs_get_fragment()
401 struct squashfs_sb_info *msblk = sb->s_fs_info; in squashfs_get_datablock()
403 return squashfs_cache_get(sb, msblk->read_page, start_block, length); in squashfs_get_datablock()
410 void *squashfs_read_table(struct super_block *sb, u64 block, int length) in squashfs_read_table() argument
412 int pages = (length + PAGE_SIZE - 1) >> PAGE_SHIFT; in squashfs_read_table()
419 return ERR_PTR(-ENOMEM); in squashfs_read_table()
423 res = -ENOMEM; in squashfs_read_table()
429 res = -ENOMEM; in squashfs_read_table()
436 res = squashfs_read_data(sb, block, length | in squashfs_read_table()