NFS: nfs_refresh_inode should clear cache_validity flags on success
[linux-2.6] / fs / isofs / compress.c
1 /* -*- linux-c -*- ------------------------------------------------------- *
2  *   
3  *   Copyright 2001 H. Peter Anvin - All Rights Reserved
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
8  *   USA; either version 2 of the License, or (at your option) any later
9  *   version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12
13 /*
14  * linux/fs/isofs/compress.c
15  *
16  * Transparent decompression of files on an iso9660 filesystem
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21
22 #include <linux/vmalloc.h>
23 #include <linux/zlib.h>
24
25 #include "isofs.h"
26 #include "zisofs.h"
27
28 /* This should probably be global. */
29 static char zisofs_sink_page[PAGE_CACHE_SIZE];
30
31 /*
32  * This contains the zlib memory allocation and the mutex for the
33  * allocation; this avoids failures at block-decompression time.
34  */
35 static void *zisofs_zlib_workspace;
36 static struct semaphore zisofs_zlib_semaphore;
37
38 /*
39  * When decompressing, we typically obtain more than one page
40  * per reference.  We inject the additional pages into the page
41  * cache as a form of readahead.
42  */
43 static int zisofs_readpage(struct file *file, struct page *page)
44 {
45         struct inode *inode = file->f_path.dentry->d_inode;
46         struct address_space *mapping = inode->i_mapping;
47         unsigned int maxpage, xpage, fpage, blockindex;
48         unsigned long offset;
49         unsigned long blockptr, blockendptr, cstart, cend, csize;
50         struct buffer_head *bh, *ptrbh[2];
51         unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
52         unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
53         unsigned long bufmask  = bufsize - 1;
54         int err = -EIO;
55         int i;
56         unsigned int header_size = ISOFS_I(inode)->i_format_parm[0];
57         unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
58         /* unsigned long zisofs_block_size = 1UL << zisofs_block_shift; */
59         unsigned int zisofs_block_page_shift = zisofs_block_shift-PAGE_CACHE_SHIFT;
60         unsigned long zisofs_block_pages = 1UL << zisofs_block_page_shift;
61         unsigned long zisofs_block_page_mask = zisofs_block_pages-1;
62         struct page *pages[zisofs_block_pages];
63         unsigned long index = page->index;
64         int indexblocks;
65
66         /* We have already been given one page, this is the one
67            we must do. */
68         xpage = index & zisofs_block_page_mask;
69         pages[xpage] = page;
70  
71         /* The remaining pages need to be allocated and inserted */
72         offset = index & ~zisofs_block_page_mask;
73         blockindex = offset >> zisofs_block_page_shift;
74         maxpage = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
75         maxpage = min(zisofs_block_pages, maxpage-offset);
76
77         for ( i = 0 ; i < maxpage ; i++, offset++ ) {
78                 if ( i != xpage ) {
79                         pages[i] = grab_cache_page_nowait(mapping, offset);
80                 }
81                 page = pages[i];
82                 if ( page ) {
83                         ClearPageError(page);
84                         kmap(page);
85                 }
86         }
87
88         /* This is the last page filled, plus one; used in case of abort. */
89         fpage = 0;
90
91         /* Find the pointer to this specific chunk */
92         /* Note: we're not using isonum_731() here because the data is known aligned */
93         /* Note: header_size is in 32-bit words (4 bytes) */
94         blockptr = (header_size + blockindex) << 2;
95         blockendptr = blockptr + 4;
96
97         indexblocks = ((blockptr^blockendptr) >> bufshift) ? 2 : 1;
98         ptrbh[0] = ptrbh[1] = NULL;
99
100         if ( isofs_get_blocks(inode, blockptr >> bufshift, ptrbh, indexblocks) != indexblocks ) {
101                 if ( ptrbh[0] ) brelse(ptrbh[0]);
102                 printk(KERN_DEBUG "zisofs: Null buffer on reading block table, inode = %lu, block = %lu\n",
103                        inode->i_ino, blockptr >> bufshift);
104                 goto eio;
105         }
106         ll_rw_block(READ, indexblocks, ptrbh);
107
108         bh = ptrbh[0];
109         if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
110                 printk(KERN_DEBUG "zisofs: Failed to read block table, inode = %lu, block = %lu\n",
111                        inode->i_ino, blockptr >> bufshift);
112                 if ( ptrbh[1] )
113                         brelse(ptrbh[1]);
114                 goto eio;
115         }
116         cstart = le32_to_cpu(*(__le32 *)(bh->b_data + (blockptr & bufmask)));
117
118         if ( indexblocks == 2 ) {
119                 /* We just crossed a block boundary.  Switch to the next block */
120                 brelse(bh);
121                 bh = ptrbh[1];
122                 if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
123                         printk(KERN_DEBUG "zisofs: Failed to read block table, inode = %lu, block = %lu\n",
124                                inode->i_ino, blockendptr >> bufshift);
125                         goto eio;
126                 }
127         }
128         cend = le32_to_cpu(*(__le32 *)(bh->b_data + (blockendptr & bufmask)));
129         brelse(bh);
130
131         if (cstart > cend)
132                 goto eio;
133                 
134         csize = cend-cstart;
135
136         if (csize > deflateBound(1UL << zisofs_block_shift))
137                 goto eio;
138
139         /* Now page[] contains an array of pages, any of which can be NULL,
140            and the locks on which we hold.  We should now read the data and
141            release the pages.  If the pages are NULL the decompressed data
142            for that particular page should be discarded. */
143         
144         if ( csize == 0 ) {
145                 /* This data block is empty. */
146
147                 for ( fpage = 0 ; fpage < maxpage ; fpage++ ) {
148                         if ( (page = pages[fpage]) != NULL ) {
149                                 memset(page_address(page), 0, PAGE_CACHE_SIZE);
150                                 
151                                 flush_dcache_page(page);
152                                 SetPageUptodate(page);
153                                 kunmap(page);
154                                 unlock_page(page);
155                                 if ( fpage == xpage )
156                                         err = 0; /* The critical page */
157                                 else
158                                         page_cache_release(page);
159                         }
160                 }
161         } else {
162                 /* This data block is compressed. */
163                 z_stream stream;
164                 int bail = 0, left_out = -1;
165                 int zerr;
166                 int needblocks = (csize + (cstart & bufmask) + bufmask) >> bufshift;
167                 int haveblocks;
168                 struct buffer_head *bhs[needblocks+1];
169                 struct buffer_head **bhptr;
170
171                 /* Because zlib is not thread-safe, do all the I/O at the top. */
172
173                 blockptr = cstart >> bufshift;
174                 memset(bhs, 0, (needblocks+1)*sizeof(struct buffer_head *));
175                 haveblocks = isofs_get_blocks(inode, blockptr, bhs, needblocks);
176                 ll_rw_block(READ, haveblocks, bhs);
177
178                 bhptr = &bhs[0];
179                 bh = *bhptr++;
180
181                 /* First block is special since it may be fractional.
182                    We also wait for it before grabbing the zlib
183                    semaphore; odds are that the subsequent blocks are
184                    going to come in in short order so we don't hold
185                    the zlib semaphore longer than necessary. */
186
187                 if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
188                         printk(KERN_DEBUG "zisofs: Hit null buffer, fpage = %d, xpage = %d, csize = %ld\n",
189                                fpage, xpage, csize);
190                         goto b_eio;
191                 }
192                 stream.next_in  = bh->b_data + (cstart & bufmask);
193                 stream.avail_in = min(bufsize-(cstart & bufmask), csize);
194                 csize -= stream.avail_in;
195
196                 stream.workspace = zisofs_zlib_workspace;
197                 down(&zisofs_zlib_semaphore);
198                 
199                 zerr = zlib_inflateInit(&stream);
200                 if ( zerr != Z_OK ) {
201                         if ( err && zerr == Z_MEM_ERROR )
202                                 err = -ENOMEM;
203                         printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
204                                zerr);
205                         goto z_eio;
206                 }
207
208                 while ( !bail && fpage < maxpage ) {
209                         page = pages[fpage];
210                         if ( page )
211                                 stream.next_out = page_address(page);
212                         else
213                                 stream.next_out = (void *)&zisofs_sink_page;
214                         stream.avail_out = PAGE_CACHE_SIZE;
215
216                         while ( stream.avail_out ) {
217                                 int ao, ai;
218                                 if ( stream.avail_in == 0 && left_out ) {
219                                         if ( !csize ) {
220                                                 printk(KERN_WARNING "zisofs: ZF read beyond end of input\n");
221                                                 bail = 1;
222                                                 break;
223                                         } else {
224                                                 bh = *bhptr++;
225                                                 if ( !bh ||
226                                                      (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
227                                                         /* Reached an EIO */
228                                                         printk(KERN_DEBUG "zisofs: Hit null buffer, fpage = %d, xpage = %d, csize = %ld\n",
229                                                                fpage, xpage, csize);
230                                                                
231                                                         bail = 1;
232                                                         break;
233                                                 }
234                                                 stream.next_in = bh->b_data;
235                                                 stream.avail_in = min(csize,bufsize);
236                                                 csize -= stream.avail_in;
237                                         }
238                                 }
239                                 ao = stream.avail_out;  ai = stream.avail_in;
240                                 zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
241                                 left_out = stream.avail_out;
242                                 if ( zerr == Z_BUF_ERROR && stream.avail_in == 0 )
243                                         continue;
244                                 if ( zerr != Z_OK ) {
245                                         /* EOF, error, or trying to read beyond end of input */
246                                         if ( err && zerr == Z_MEM_ERROR )
247                                                 err = -ENOMEM;
248                                         if ( zerr != Z_STREAM_END )
249                                                 printk(KERN_DEBUG "zisofs: zisofs_inflate returned %d, inode = %lu, index = %lu, fpage = %d, xpage = %d, avail_in = %d, avail_out = %d, ai = %d, ao = %d\n",
250                                                        zerr, inode->i_ino, index,
251                                                        fpage, xpage,
252                                                        stream.avail_in, stream.avail_out,
253                                                        ai, ao);
254                                         bail = 1;
255                                         break;
256                                 }
257                         }
258
259                         if ( stream.avail_out && zerr == Z_STREAM_END ) {
260                                 /* Fractional page written before EOF.  This may
261                                    be the last page in the file. */
262                                 memset(stream.next_out, 0, stream.avail_out);
263                                 stream.avail_out = 0;
264                         }
265
266                         if ( !stream.avail_out ) {
267                                 /* This page completed */
268                                 if ( page ) {
269                                         flush_dcache_page(page);
270                                         SetPageUptodate(page);
271                                         kunmap(page);
272                                         unlock_page(page);
273                                         if ( fpage == xpage )
274                                                 err = 0; /* The critical page */
275                                         else
276                                                 page_cache_release(page);
277                                 }
278                                 fpage++;
279                         }
280                 }
281                 zlib_inflateEnd(&stream);
282
283         z_eio:
284                 up(&zisofs_zlib_semaphore);
285
286         b_eio:
287                 for ( i = 0 ; i < haveblocks ; i++ ) {
288                         if ( bhs[i] )
289                                 brelse(bhs[i]);
290                 }
291         }
292
293 eio:
294
295         /* Release any residual pages, do not SetPageUptodate */
296         while ( fpage < maxpage ) {
297                 page = pages[fpage];
298                 if ( page ) {
299                         flush_dcache_page(page);
300                         if ( fpage == xpage )
301                                 SetPageError(page);
302                         kunmap(page);
303                         unlock_page(page);
304                         if ( fpage != xpage )
305                                 page_cache_release(page);
306                 }
307                 fpage++;
308         }                       
309
310         /* At this point, err contains 0 or -EIO depending on the "critical" page */
311         return err;
312 }
313
314 const struct address_space_operations zisofs_aops = {
315         .readpage = zisofs_readpage,
316         /* No sync_page operation supported? */
317         /* No bmap operation supported */
318 };
319
320 static int initialized;
321
322 int __init zisofs_init(void)
323 {
324         if ( initialized ) {
325                 printk("zisofs_init: called more than once\n");
326                 return 0;
327         }
328
329         zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
330         if ( !zisofs_zlib_workspace )
331                 return -ENOMEM;
332         init_MUTEX(&zisofs_zlib_semaphore);
333
334         initialized = 1;
335         return 0;
336 }
337
338 void zisofs_cleanup(void)
339 {
340         if ( !initialized ) {
341                 printk("zisofs_cleanup: called without initialization\n");
342                 return;
343         }
344
345         vfree(zisofs_zlib_workspace);
346         initialized = 0;
347 }