~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/squashfs/cache.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/squashfs/cache.c (Version linux-6.12-rc7) and /fs/squashfs/cache.c (Version linux-5.10.228)


** Warning: Cannot open xref database.

  1 // SPDX-License-Identifier: GPL-2.0-or-later        1 
  2 /*                                                
  3  * Squashfs - a compressed read only filesyste    
  4  *                                                
  5  * Copyright (c) 2002, 2003, 2004, 2005, 2006,    
  6  * Phillip Lougher <phillip@squashfs.org.uk>      
  7  *                                                
  8  * cache.c                                        
  9  */                                               
 10                                                   
 11 /*                                                
 12  * Blocks in Squashfs are compressed.  To avoi    
 13  * recently accessed data Squashfs uses two sm    
 14  *                                                
 15  * This file implements a generic cache implem    
 16  * plus functions layered ontop of the generic    
 17  * access the metadata and fragment caches.       
 18  *                                                
 19  * To avoid out of memory and fragmentation is    
 20  * uses sequences of kmalloced PAGE_SIZE buffe    
 21  *                                                
 22  * It should be noted that the cache is not us    
 23  * are decompressed and cached in the page-cac    
 24  * cache is only used to temporarily cache fra    
 25  * which have been read as as a result of a me    
 26  * directory) or fragment access.  Because met    
 27  * together into blocks (to gain greater compr    
 28  * piece of metadata or fragment will retrieve    
 29  * have been packed with it, these because of     
 30  * in the near future. Temporarily caching the    
 31  * near future access without requiring an add    
 32  */                                               
 33                                                   
 34 #include <linux/fs.h>                             
 35 #include <linux/vfs.h>                            
 36 #include <linux/slab.h>                           
 37 #include <linux/vmalloc.h>                        
 38 #include <linux/sched.h>                          
 39 #include <linux/spinlock.h>                       
 40 #include <linux/wait.h>                           
 41 #include <linux/pagemap.h>                        
 42                                                   
 43 #include "squashfs_fs.h"                          
 44 #include "squashfs_fs_sb.h"                       
 45 #include "squashfs.h"                             
 46 #include "page_actor.h"                           
 47                                                   
 48 /*                                                
 49  * Look-up block in cache, and increment usage    
 50  * and decompress it from disk.                   
 51  */                                               
 52 struct squashfs_cache_entry *squashfs_cache_ge    
 53         struct squashfs_cache *cache, u64 bloc    
 54 {                                                 
 55         int i, n;                                 
 56         struct squashfs_cache_entry *entry;       
 57                                                   
 58         spin_lock(&cache->lock);                  
 59                                                   
 60         while (1) {                               
 61                 for (i = cache->curr_blk, n =     
 62                         if (cache->entry[i].bl    
 63                                 cache->curr_bl    
 64                                 break;            
 65                         }                         
 66                         i = (i + 1) % cache->e    
 67                 }                                 
 68                                                   
 69                 if (n == cache->entries) {        
 70                         /*                        
 71                          * Block not in cache,    
 72                          * go to sleep waiting    
 73                          */                       
 74                         if (cache->unused == 0    
 75                                 cache->num_wai    
 76                                 spin_unlock(&c    
 77                                 wait_event(cac    
 78                                 spin_lock(&cac    
 79                                 cache->num_wai    
 80                                 continue;         
 81                         }                         
 82                                                   
 83                         /*                        
 84                          * At least one unused    
 85                          * round-robin strateg    
 86                          * be evicted from the    
 87                          */                       
 88                         i = cache->next_blk;      
 89                         for (n = 0; n < cache-    
 90                                 if (cache->ent    
 91                                         break;    
 92                                 i = (i + 1) %     
 93                         }                         
 94                                                   
 95                         cache->next_blk = (i +    
 96                         entry = &cache->entry[    
 97                                                   
 98                         /*                        
 99                          * Initialise chosen c    
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->lo    
109                                                   
110                         entry->length = squash    
111                                 &entry->next_i    
112                                                   
113                         spin_lock(&cache->lock    
114                                                   
115                         if (entry->length < 0)    
116                                 entry->error =    
117                                                   
118                         entry->pending = 0;       
119                                                   
120                         /*                        
121                          * While filling this     
122                          * have looked it up i    
123                          * waiting for it to b    
124                          */                       
125                         if (entry->num_waiters    
126                                 spin_unlock(&c    
127                                 wake_up_all(&e    
128                         } else                    
129                                 spin_unlock(&c    
130                                                   
131                         goto out;                 
132                 }                                 
133                                                   
134                 /*                                
135                  * Block already in cache.  In    
136                  * get reused until we're fini    
137                  * previously unused there's o    
138                  * for reuse.                     
139                  */                               
140                 entry = &cache->entry[i];         
141                 if (entry->refcount == 0)         
142                         cache->unused--;          
143                 entry->refcount++;                
144                                                   
145                 /*                                
146                  * If the entry is currently b    
147                  * go to sleep waiting for it     
148                  */                               
149                 if (entry->pending) {             
150                         entry->num_waiters++;     
151                         spin_unlock(&cache->lo    
152                         wait_event(entry->wait    
153                 } else                            
154                         spin_unlock(&cache->lo    
155                                                   
156                 goto out;                         
157         }                                         
158                                                   
159 out:                                              
160         TRACE("Got %s %d, start block %lld, re    
161                 cache->name, i, entry->block,     
162                                                   
163         if (entry->error)                         
164                 ERROR("Unable to read %s cache    
165                                                   
166         return entry;                             
167 }                                                 
168                                                   
169                                                   
170 /*                                                
171  * Release cache entry, once usage count is ze    
172  */                                               
173 void squashfs_cache_put(struct squashfs_cache_    
174 {                                                 
175         struct squashfs_cache *cache = entry->    
176                                                   
177         spin_lock(&cache->lock);                  
178         entry->refcount--;                        
179         if (entry->refcount == 0) {               
180                 cache->unused++;                  
181                 /*                                
182                  * If there's any processes wa    
183                  * available, wake one up.        
184                  */                               
185                 if (cache->num_waiters) {         
186                         spin_unlock(&cache->lo    
187                         wake_up(&cache->wait_q    
188                         return;                   
189                 }                                 
190         }                                         
191         spin_unlock(&cache->lock);                
192 }                                                 
193                                                   
194 /*                                                
195  * Delete cache reclaiming all kmalloced buffe    
196  */                                               
197 void squashfs_cache_delete(struct squashfs_cac    
198 {                                                 
199         int i, j;                                 
200                                                   
201         if (cache == NULL)                        
202                 return;                           
203                                                   
204         for (i = 0; i < cache->entries; i++) {    
205                 if (cache->entry[i].data) {       
206                         for (j = 0; j < cache-    
207                                 kfree(cache->e    
208                         kfree(cache->entry[i].    
209                 }                                 
210                 kfree(cache->entry[i].actor);     
211         }                                         
212                                                   
213         kfree(cache->entry);                      
214         kfree(cache);                             
215 }                                                 
216                                                   
217                                                   
218 /*                                                
219  * Initialise cache allocating the specified n    
220  * size block_size.  To avoid vmalloc fragment    
221  * is allocated as a sequence of kmalloced PAG    
222  */                                               
223 struct squashfs_cache *squashfs_cache_init(cha    
224         int block_size)                           
225 {                                                 
226         int i, j;                                 
227         struct squashfs_cache *cache = kzalloc    
228                                                   
229         if (cache == NULL) {                      
230                 ERROR("Failed to allocate %s c    
231                 return NULL;                      
232         }                                         
233                                                   
234         cache->entry = kcalloc(entries, sizeof    
235         if (cache->entry == NULL) {               
236                 ERROR("Failed to allocate %s c    
237                 goto cleanup;                     
238         }                                         
239                                                   
240         cache->curr_blk = 0;                      
241         cache->next_blk = 0;                      
242         cache->unused = entries;                  
243         cache->entries = entries;                 
244         cache->block_size = block_size;           
245         cache->pages = block_size >> PAGE_SHIF    
246         cache->pages = cache->pages ? cache->p    
247         cache->name = name;                       
248         cache->num_waiters = 0;                   
249         spin_lock_init(&cache->lock);             
250         init_waitqueue_head(&cache->wait_queue    
251                                                   
252         for (i = 0; i < entries; i++) {           
253                 struct squashfs_cache_entry *e    
254                                                   
255                 init_waitqueue_head(&cache->en    
256                 entry->cache = cache;             
257                 entry->block = SQUASHFS_INVALI    
258                 entry->data = kcalloc(cache->p    
259                 if (entry->data == NULL) {        
260                         ERROR("Failed to alloc    
261                         goto cleanup;             
262                 }                                 
263                                                   
264                 for (j = 0; j < cache->pages;     
265                         entry->data[j] = kmall    
266                         if (entry->data[j] ==     
267                                 ERROR("Failed     
268                                 goto cleanup;     
269                         }                         
270                 }                                 
271                                                   
272                 entry->actor = squashfs_page_a    
273                                                   
274                 if (entry->actor == NULL) {       
275                         ERROR("Failed to alloc    
276                         goto cleanup;             
277                 }                                 
278         }                                         
279                                                   
280         return cache;                             
281                                                   
282 cleanup:                                          
283         squashfs_cache_delete(cache);             
284         return NULL;                              
285 }                                                 
286                                                   
287                                                   
288 /*                                                
289  * Copy up to length bytes from cache entry to    
290  * into the cache entry.  If there's not lengt    
291  * bytes available.  In all cases return the n    
292  */                                               
293 int squashfs_copy_data(void *buffer, struct sq    
294                 int offset, int length)           
295 {                                                 
296         int remaining = length;                   
297                                                   
298         if (length == 0)                          
299                 return 0;                         
300         else if (buffer == NULL)                  
301                 return min(length, entry->leng    
302                                                   
303         while (offset < entry->length) {          
304                 void *buff = entry->data[offse    
305                                 + (offset % PA    
306                 int bytes = min_t(int, entry->    
307                                 PAGE_SIZE - (o    
308                                                   
309                 if (bytes >= remaining) {         
310                         memcpy(buffer, buff, r    
311                         remaining = 0;            
312                         break;                    
313                 }                                 
314                                                   
315                 memcpy(buffer, buff, bytes);      
316                 buffer += bytes;                  
317                 remaining -= bytes;               
318                 offset += bytes;                  
319         }                                         
320                                                   
321         return length - remaining;                
322 }                                                 
323                                                   
324                                                   
325 /*                                                
326  * Read length bytes from metadata position <b    
327  * start of the compressed block on disk, and     
328  * the block once decompressed).  Data is pack    
329  * and length bytes may require reading more t    
330  */                                               
331 int squashfs_read_metadata(struct super_block     
332                 u64 *block, int *offset, int l    
333 {                                                 
334         struct squashfs_sb_info *msblk = sb->s    
335         int bytes, res = length;                  
336         struct squashfs_cache_entry *entry;       
337                                                   
338         TRACE("Entered squashfs_read_metadata     
339                                                   
340         if (unlikely(length < 0))                 
341                 return -EIO;                      
342                                                   
343         while (length) {                          
344                 entry = squashfs_cache_get(sb,    
345                 if (entry->error) {               
346                         res = entry->error;       
347                         goto error;               
348                 } else if (*offset >= entry->l    
349                         res = -EIO;               
350                         goto error;               
351                 }                                 
352                                                   
353                 bytes = squashfs_copy_data(buf    
354                 if (buffer)                       
355                         buffer += bytes;          
356                 length -= bytes;                  
357                 *offset += bytes;                 
358                                                   
359                 if (*offset == entry->length)     
360                         *block = entry->next_i    
361                         *offset = 0;              
362                 }                                 
363                                                   
364                 squashfs_cache_put(entry);        
365         }                                         
366                                                   
367         return res;                               
368                                                   
369 error:                                            
370         squashfs_cache_put(entry);                
371         return res;                               
372 }                                                 
373                                                   
374                                                   
375 /*                                                
376  * Look-up in the fragmment cache the fragment    
377  * filesystem.  If necessary read and decompre    
378  */                                               
379 struct squashfs_cache_entry *squashfs_get_frag    
380                                 u64 start_bloc    
381 {                                                 
382         struct squashfs_sb_info *msblk = sb->s    
383                                                   
384         return squashfs_cache_get(sb, msblk->f    
385                 length);                          
386 }                                                 
387                                                   
388                                                   
389 /*                                                
390  * Read and decompress the datablock located a    
391  * filesystem.  The cache is used here to avoi    
392  * read/decompress code.                          
393  */                                               
394 struct squashfs_cache_entry *squashfs_get_data    
395                                 u64 start_bloc    
396 {                                                 
397         struct squashfs_sb_info *msblk = sb->s    
398                                                   
399         return squashfs_cache_get(sb, msblk->r    
400 }                                                 
401                                                   
402                                                   
403 /*                                                
404  * Read a filesystem table (uncompressed seque    
405  */                                               
406 void *squashfs_read_table(struct super_block *    
407 {                                                 
408         int pages = (length + PAGE_SIZE - 1) >    
409         int i, res;                               
410         void *table, *buffer, **data;             
411         struct squashfs_page_actor *actor;        
412                                                   
413         table = buffer = kmalloc(length, GFP_K    
414         if (table == NULL)                        
415                 return ERR_PTR(-ENOMEM);          
416                                                   
417         data = kcalloc(pages, sizeof(void *),     
418         if (data == NULL) {                       
419                 res = -ENOMEM;                    
420                 goto failed;                      
421         }                                         
422                                                   
423         actor = squashfs_page_actor_init(data,    
424         if (actor == NULL) {                      
425                 res = -ENOMEM;                    
426                 goto failed2;                     
427         }                                         
428                                                   
429         for (i = 0; i < pages; i++, buffer +=     
430                 data[i] = buffer;                 
431                                                   
432         res = squashfs_read_data(sb, block, le    
433                 SQUASHFS_COMPRESSED_BIT_BLOCK,    
434                                                   
435         kfree(data);                              
436         kfree(actor);                             
437                                                   
438         if (res < 0)                              
439                 goto failed;                      
440                                                   
441         return table;                             
442                                                   
443 failed2:                                          
444         kfree(data);                              
445 failed:                                           
446         kfree(table);                             
447         return ERR_PTR(res);                      
448 }                                                 
449                                                   

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php