~ [ 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-4.9.337)


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

~ [ 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