14
14
15
15
#include <linux/decompress/generic.h>
16
16
17
+ static struct file * in_file , * out_file ;
18
+ static loff_t in_pos , out_pos ;
17
19
18
20
static int __init prompt_ramdisk (char * str )
19
21
{
@@ -31,7 +33,7 @@ static int __init ramdisk_start_setup(char *str)
31
33
}
32
34
__setup ("ramdisk_start=" , ramdisk_start_setup );
33
35
34
- static int __init crd_load (int in_fd , int out_fd , decompress_fn deco );
36
+ static int __init crd_load (decompress_fn deco );
35
37
36
38
/*
37
39
* This routine tries to find a RAM disk image to load, and returns the
@@ -53,7 +55,8 @@ static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
53
55
* lz4
54
56
*/
55
57
static int __init
56
- identify_ramdisk_image (int fd , int start_block , decompress_fn * decompressor )
58
+ identify_ramdisk_image (struct file * file , loff_t pos ,
59
+ decompress_fn * decompressor )
57
60
{
58
61
const int size = 512 ;
59
62
struct minix_super_block * minixsb ;
@@ -64,6 +67,7 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
64
67
unsigned char * buf ;
65
68
const char * compress_name ;
66
69
unsigned long n ;
70
+ int start_block = rd_image_start ;
67
71
68
72
buf = kmalloc (size , GFP_KERNEL );
69
73
if (!buf )
@@ -78,8 +82,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
78
82
/*
79
83
* Read block 0 to test for compressed kernel
80
84
*/
81
- ksys_lseek ( fd , start_block * BLOCK_SIZE , 0 ) ;
82
- ksys_read ( fd , buf , size );
85
+ pos = start_block * BLOCK_SIZE ;
86
+ kernel_read ( file , buf , size , & pos );
83
87
84
88
* decompressor = decompress_method (buf , size , & compress_name );
85
89
if (compress_name ) {
@@ -124,8 +128,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
124
128
/*
125
129
* Read 512 bytes further to check if cramfs is padded
126
130
*/
127
- ksys_lseek ( fd , start_block * BLOCK_SIZE + 0x200 , 0 ) ;
128
- ksys_read ( fd , buf , size );
131
+ pos = start_block * BLOCK_SIZE + 0x200 ;
132
+ kernel_read ( file , buf , size , & pos );
129
133
130
134
if (cramfsb -> magic == CRAMFS_MAGIC ) {
131
135
printk (KERN_NOTICE
@@ -138,8 +142,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
138
142
/*
139
143
* Read block 1 to test for minix and ext2 superblock
140
144
*/
141
- ksys_lseek ( fd , (start_block + 1 ) * BLOCK_SIZE , 0 ) ;
142
- ksys_read ( fd , buf , size );
145
+ pos = (start_block + 1 ) * BLOCK_SIZE ;
146
+ kernel_read ( file , buf , size , & pos );
143
147
144
148
/* Try minix */
145
149
if (minixsb -> s_magic == MINIX_SUPER_MAGIC ||
@@ -166,15 +170,22 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
166
170
start_block );
167
171
168
172
done :
169
- ksys_lseek (fd , start_block * BLOCK_SIZE , 0 );
170
173
kfree (buf );
171
174
return nblocks ;
172
175
}
173
176
177
+ static unsigned long nr_blocks (struct file * file )
178
+ {
179
+ struct inode * inode = file -> f_mapping -> host ;
180
+
181
+ if (!S_ISBLK (inode -> i_mode ))
182
+ return 0 ;
183
+ return i_size_read (inode ) >> 10 ;
184
+ }
185
+
174
186
int __init rd_load_image (char * from )
175
187
{
176
188
int res = 0 ;
177
- int in_fd , out_fd ;
178
189
unsigned long rd_blocks , devblocks ;
179
190
int nblocks , i ;
180
191
char * buf = NULL ;
@@ -184,20 +195,21 @@ int __init rd_load_image(char *from)
184
195
char rotator [4 ] = { '|' , '/' , '-' , '\\' };
185
196
#endif
186
197
187
- out_fd = ksys_open ("/dev/ram" , O_RDWR , 0 );
188
- if (out_fd < 0 )
198
+ out_file = filp_open ("/dev/ram" , O_RDWR , 0 );
199
+ if (IS_ERR ( out_file ) )
189
200
goto out ;
190
201
191
- in_fd = ksys_open (from , O_RDONLY , 0 );
192
- if (in_fd < 0 )
202
+ in_file = filp_open (from , O_RDONLY , 0 );
203
+ if (IS_ERR ( in_file ) )
193
204
goto noclose_input ;
194
205
195
- nblocks = identify_ramdisk_image (in_fd , rd_image_start , & decompressor );
206
+ in_pos = rd_image_start * BLOCK_SIZE ;
207
+ nblocks = identify_ramdisk_image (in_file , in_pos , & decompressor );
196
208
if (nblocks < 0 )
197
209
goto done ;
198
210
199
211
if (nblocks == 0 ) {
200
- if (crd_load (in_fd , out_fd , decompressor ) == 0 )
212
+ if (crd_load (decompressor ) == 0 )
201
213
goto successful_load ;
202
214
goto done ;
203
215
}
@@ -206,11 +218,7 @@ int __init rd_load_image(char *from)
206
218
* NOTE NOTE: nblocks is not actually blocks but
207
219
* the number of kibibytes of data to load into a ramdisk.
208
220
*/
209
- if (ksys_ioctl (out_fd , BLKGETSIZE , (unsigned long )& rd_blocks ) < 0 )
210
- rd_blocks = 0 ;
211
- else
212
- rd_blocks >>= 1 ;
213
-
221
+ rd_blocks = nr_blocks (out_file );
214
222
if (nblocks > rd_blocks ) {
215
223
printk ("RAMDISK: image too big! (%dKiB/%ldKiB)\n" ,
216
224
nblocks , rd_blocks );
@@ -220,13 +228,10 @@ int __init rd_load_image(char *from)
220
228
/*
221
229
* OK, time to copy in the data
222
230
*/
223
- if (ksys_ioctl (in_fd , BLKGETSIZE , (unsigned long )& devblocks ) < 0 )
224
- devblocks = 0 ;
225
- else
226
- devblocks >>= 1 ;
227
-
228
231
if (strcmp (from , "/initrd.image" ) == 0 )
229
232
devblocks = nblocks ;
233
+ else
234
+ devblocks = nr_blocks (in_file );
230
235
231
236
if (devblocks == 0 ) {
232
237
printk (KERN_ERR "RAMDISK: could not determine device size\n" );
@@ -245,14 +250,11 @@ int __init rd_load_image(char *from)
245
250
if (i && (i % devblocks == 0 )) {
246
251
pr_cont ("done disk #1.\n" );
247
252
rotate = 0 ;
248
- if (ksys_close (in_fd )) {
249
- printk ("Error closing the disk.\n" );
250
- goto noclose_input ;
251
- }
253
+ fput (in_file );
252
254
break ;
253
255
}
254
- ksys_read ( in_fd , buf , BLOCK_SIZE );
255
- ksys_write ( out_fd , buf , BLOCK_SIZE );
256
+ kernel_read ( in_file , buf , BLOCK_SIZE , & in_pos );
257
+ kernel_write ( out_file , buf , BLOCK_SIZE , & out_pos );
256
258
#if !defined(CONFIG_S390 )
257
259
if (!(i % 16 )) {
258
260
pr_cont ("%c\b" , rotator [rotate & 0x3 ]);
@@ -265,9 +267,9 @@ int __init rd_load_image(char *from)
265
267
successful_load :
266
268
res = 1 ;
267
269
done :
268
- ksys_close ( in_fd );
270
+ fput ( in_file );
269
271
noclose_input :
270
- ksys_close ( out_fd );
272
+ fput ( out_file );
271
273
out :
272
274
kfree (buf );
273
275
ksys_unlink ("/dev/ram" );
@@ -283,11 +285,10 @@ int __init rd_load_disk(int n)
283
285
284
286
static int exit_code ;
285
287
static int decompress_error ;
286
- static int crd_infd , crd_outfd ;
287
288
288
289
static long __init compr_fill (void * buf , unsigned long len )
289
290
{
290
- long r = ksys_read ( crd_infd , buf , len );
291
+ long r = kernel_read ( in_file , buf , len , & in_pos );
291
292
if (r < 0 )
292
293
printk (KERN_ERR "RAMDISK: error while reading compressed data" );
293
294
else if (r == 0 )
@@ -297,7 +298,7 @@ static long __init compr_fill(void *buf, unsigned long len)
297
298
298
299
static long __init compr_flush (void * window , unsigned long outcnt )
299
300
{
300
- long written = ksys_write ( crd_outfd , window , outcnt );
301
+ long written = kernel_write ( out_file , window , outcnt , & out_pos );
301
302
if (written != outcnt ) {
302
303
if (decompress_error == 0 )
303
304
printk (KERN_ERR
@@ -316,11 +317,9 @@ static void __init error(char *x)
316
317
decompress_error = 1 ;
317
318
}
318
319
319
- static int __init crd_load (int in_fd , int out_fd , decompress_fn deco )
320
+ static int __init crd_load (decompress_fn deco )
320
321
{
321
322
int result ;
322
- crd_infd = in_fd ;
323
- crd_outfd = out_fd ;
324
323
325
324
if (!deco ) {
326
325
pr_emerg ("Invalid ramdisk decompression routine. "
0 commit comments