Skip to content

Commit bef1732

Browse files
author
Christoph Hellwig
committed
initrd: switch initrd loading to struct file based APIs
There is no good reason to mess with file descriptors from in-kernel code, switch the initrd loading to struct file based read and writes instead. Also Pass an explicit offset instead of ->f_pos, and to make that easier, use file scope file structs and offsets everywhere except for identify_ramdisk_image instead of the current strange mix. Signed-off-by: Christoph Hellwig <[email protected]> Acked-by: Linus Torvalds <[email protected]>
1 parent 899ac10 commit bef1732

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

fs/read_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
301301
}
302302
EXPORT_SYMBOL(vfs_llseek);
303303

304-
off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
304+
static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
305305
{
306306
off_t retval;
307307
struct fd f = fdget_pos(fd);

include/linux/syscalls.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,6 @@ int ksys_fchown(unsigned int fd, uid_t user, gid_t group);
12461246
int ksys_getdents64(unsigned int fd, struct linux_dirent64 __user *dirent,
12471247
unsigned int count);
12481248
int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
1249-
off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence);
12501249
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count);
12511250
void ksys_sync(void);
12521251
int ksys_unshare(unsigned long unshare_flags);

init/do_mounts_rd.c

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include <linux/decompress/generic.h>
1616

17+
static struct file *in_file, *out_file;
18+
static loff_t in_pos, out_pos;
1719

1820
static int __init prompt_ramdisk(char *str)
1921
{
@@ -31,7 +33,7 @@ static int __init ramdisk_start_setup(char *str)
3133
}
3234
__setup("ramdisk_start=", ramdisk_start_setup);
3335

34-
static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
36+
static int __init crd_load(decompress_fn deco);
3537

3638
/*
3739
* 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);
5355
* lz4
5456
*/
5557
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)
5760
{
5861
const int size = 512;
5962
struct minix_super_block *minixsb;
@@ -64,6 +67,7 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
6467
unsigned char *buf;
6568
const char *compress_name;
6669
unsigned long n;
70+
int start_block = rd_image_start;
6771

6872
buf = kmalloc(size, GFP_KERNEL);
6973
if (!buf)
@@ -78,8 +82,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
7882
/*
7983
* Read block 0 to test for compressed kernel
8084
*/
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);
8387

8488
*decompressor = decompress_method(buf, size, &compress_name);
8589
if (compress_name) {
@@ -124,8 +128,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
124128
/*
125129
* Read 512 bytes further to check if cramfs is padded
126130
*/
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);
129133

130134
if (cramfsb->magic == CRAMFS_MAGIC) {
131135
printk(KERN_NOTICE
@@ -138,8 +142,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
138142
/*
139143
* Read block 1 to test for minix and ext2 superblock
140144
*/
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);
143147

144148
/* Try minix */
145149
if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
@@ -166,15 +170,22 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
166170
start_block);
167171

168172
done:
169-
ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
170173
kfree(buf);
171174
return nblocks;
172175
}
173176

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+
174186
int __init rd_load_image(char *from)
175187
{
176188
int res = 0;
177-
int in_fd, out_fd;
178189
unsigned long rd_blocks, devblocks;
179190
int nblocks, i;
180191
char *buf = NULL;
@@ -184,20 +195,21 @@ int __init rd_load_image(char *from)
184195
char rotator[4] = { '|' , '/' , '-' , '\\' };
185196
#endif
186197

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))
189200
goto out;
190201

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))
193204
goto noclose_input;
194205

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);
196208
if (nblocks < 0)
197209
goto done;
198210

199211
if (nblocks == 0) {
200-
if (crd_load(in_fd, out_fd, decompressor) == 0)
212+
if (crd_load(decompressor) == 0)
201213
goto successful_load;
202214
goto done;
203215
}
@@ -206,11 +218,7 @@ int __init rd_load_image(char *from)
206218
* NOTE NOTE: nblocks is not actually blocks but
207219
* the number of kibibytes of data to load into a ramdisk.
208220
*/
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);
214222
if (nblocks > rd_blocks) {
215223
printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
216224
nblocks, rd_blocks);
@@ -220,13 +228,10 @@ int __init rd_load_image(char *from)
220228
/*
221229
* OK, time to copy in the data
222230
*/
223-
if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
224-
devblocks = 0;
225-
else
226-
devblocks >>= 1;
227-
228231
if (strcmp(from, "/initrd.image") == 0)
229232
devblocks = nblocks;
233+
else
234+
devblocks = nr_blocks(in_file);
230235

231236
if (devblocks == 0) {
232237
printk(KERN_ERR "RAMDISK: could not determine device size\n");
@@ -245,14 +250,11 @@ int __init rd_load_image(char *from)
245250
if (i && (i % devblocks == 0)) {
246251
pr_cont("done disk #1.\n");
247252
rotate = 0;
248-
if (ksys_close(in_fd)) {
249-
printk("Error closing the disk.\n");
250-
goto noclose_input;
251-
}
253+
fput(in_file);
252254
break;
253255
}
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);
256258
#if !defined(CONFIG_S390)
257259
if (!(i % 16)) {
258260
pr_cont("%c\b", rotator[rotate & 0x3]);
@@ -265,9 +267,9 @@ int __init rd_load_image(char *from)
265267
successful_load:
266268
res = 1;
267269
done:
268-
ksys_close(in_fd);
270+
fput(in_file);
269271
noclose_input:
270-
ksys_close(out_fd);
272+
fput(out_file);
271273
out:
272274
kfree(buf);
273275
ksys_unlink("/dev/ram");
@@ -283,11 +285,10 @@ int __init rd_load_disk(int n)
283285

284286
static int exit_code;
285287
static int decompress_error;
286-
static int crd_infd, crd_outfd;
287288

288289
static long __init compr_fill(void *buf, unsigned long len)
289290
{
290-
long r = ksys_read(crd_infd, buf, len);
291+
long r = kernel_read(in_file, buf, len, &in_pos);
291292
if (r < 0)
292293
printk(KERN_ERR "RAMDISK: error while reading compressed data");
293294
else if (r == 0)
@@ -297,7 +298,7 @@ static long __init compr_fill(void *buf, unsigned long len)
297298

298299
static long __init compr_flush(void *window, unsigned long outcnt)
299300
{
300-
long written = ksys_write(crd_outfd, window, outcnt);
301+
long written = kernel_write(out_file, window, outcnt, &out_pos);
301302
if (written != outcnt) {
302303
if (decompress_error == 0)
303304
printk(KERN_ERR
@@ -316,11 +317,9 @@ static void __init error(char *x)
316317
decompress_error = 1;
317318
}
318319

319-
static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
320+
static int __init crd_load(decompress_fn deco)
320321
{
321322
int result;
322-
crd_infd = in_fd;
323-
crd_outfd = out_fd;
324323

325324
if (!deco) {
326325
pr_emerg("Invalid ramdisk decompression routine. "

0 commit comments

Comments
 (0)