Commit bef17329 authored by Christoph Hellwig's avatar Christoph Hellwig

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: default avatarChristoph Hellwig <hch@lst.de>
Acked-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 899ac10c
...@@ -301,7 +301,7 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence) ...@@ -301,7 +301,7 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
} }
EXPORT_SYMBOL(vfs_llseek); EXPORT_SYMBOL(vfs_llseek);
off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence) static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
{ {
off_t retval; off_t retval;
struct fd f = fdget_pos(fd); struct fd f = fdget_pos(fd);
......
...@@ -1246,7 +1246,6 @@ int ksys_fchown(unsigned int fd, uid_t user, gid_t group); ...@@ -1246,7 +1246,6 @@ int ksys_fchown(unsigned int fd, uid_t user, gid_t group);
int ksys_getdents64(unsigned int fd, struct linux_dirent64 __user *dirent, int ksys_getdents64(unsigned int fd, struct linux_dirent64 __user *dirent,
unsigned int count); unsigned int count);
int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg); int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence);
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count); ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count);
void ksys_sync(void); void ksys_sync(void);
int ksys_unshare(unsigned long unshare_flags); int ksys_unshare(unsigned long unshare_flags);
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <linux/decompress/generic.h> #include <linux/decompress/generic.h>
static struct file *in_file, *out_file;
static loff_t in_pos, out_pos;
static int __init prompt_ramdisk(char *str) static int __init prompt_ramdisk(char *str)
{ {
...@@ -31,7 +33,7 @@ static int __init ramdisk_start_setup(char *str) ...@@ -31,7 +33,7 @@ static int __init ramdisk_start_setup(char *str)
} }
__setup("ramdisk_start=", ramdisk_start_setup); __setup("ramdisk_start=", ramdisk_start_setup);
static int __init crd_load(int in_fd, int out_fd, decompress_fn deco); static int __init crd_load(decompress_fn deco);
/* /*
* This routine tries to find a RAM disk image to load, and returns the * 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,7 +55,8 @@ static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
* lz4 * lz4
*/ */
static int __init static int __init
identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) identify_ramdisk_image(struct file *file, loff_t pos,
decompress_fn *decompressor)
{ {
const int size = 512; const int size = 512;
struct minix_super_block *minixsb; struct minix_super_block *minixsb;
...@@ -64,6 +67,7 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) ...@@ -64,6 +67,7 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
unsigned char *buf; unsigned char *buf;
const char *compress_name; const char *compress_name;
unsigned long n; unsigned long n;
int start_block = rd_image_start;
buf = kmalloc(size, GFP_KERNEL); buf = kmalloc(size, GFP_KERNEL);
if (!buf) if (!buf)
...@@ -78,8 +82,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) ...@@ -78,8 +82,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
/* /*
* Read block 0 to test for compressed kernel * Read block 0 to test for compressed kernel
*/ */
ksys_lseek(fd, start_block * BLOCK_SIZE, 0); pos = start_block * BLOCK_SIZE;
ksys_read(fd, buf, size); kernel_read(file, buf, size, &pos);
*decompressor = decompress_method(buf, size, &compress_name); *decompressor = decompress_method(buf, size, &compress_name);
if (compress_name) { if (compress_name) {
...@@ -124,8 +128,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) ...@@ -124,8 +128,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
/* /*
* Read 512 bytes further to check if cramfs is padded * Read 512 bytes further to check if cramfs is padded
*/ */
ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0); pos = start_block * BLOCK_SIZE + 0x200;
ksys_read(fd, buf, size); kernel_read(file, buf, size, &pos);
if (cramfsb->magic == CRAMFS_MAGIC) { if (cramfsb->magic == CRAMFS_MAGIC) {
printk(KERN_NOTICE printk(KERN_NOTICE
...@@ -138,8 +142,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) ...@@ -138,8 +142,8 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
/* /*
* Read block 1 to test for minix and ext2 superblock * Read block 1 to test for minix and ext2 superblock
*/ */
ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0); pos = (start_block + 1) * BLOCK_SIZE;
ksys_read(fd, buf, size); kernel_read(file, buf, size, &pos);
/* Try minix */ /* Try minix */
if (minixsb->s_magic == MINIX_SUPER_MAGIC || if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
...@@ -166,15 +170,22 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) ...@@ -166,15 +170,22 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
start_block); start_block);
done: done:
ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
kfree(buf); kfree(buf);
return nblocks; return nblocks;
} }
static unsigned long nr_blocks(struct file *file)
{
struct inode *inode = file->f_mapping->host;
if (!S_ISBLK(inode->i_mode))
return 0;
return i_size_read(inode) >> 10;
}
int __init rd_load_image(char *from) int __init rd_load_image(char *from)
{ {
int res = 0; int res = 0;
int in_fd, out_fd;
unsigned long rd_blocks, devblocks; unsigned long rd_blocks, devblocks;
int nblocks, i; int nblocks, i;
char *buf = NULL; char *buf = NULL;
...@@ -184,20 +195,21 @@ int __init rd_load_image(char *from) ...@@ -184,20 +195,21 @@ int __init rd_load_image(char *from)
char rotator[4] = { '|' , '/' , '-' , '\\' }; char rotator[4] = { '|' , '/' , '-' , '\\' };
#endif #endif
out_fd = ksys_open("/dev/ram", O_RDWR, 0); out_file = filp_open("/dev/ram", O_RDWR, 0);
if (out_fd < 0) if (IS_ERR(out_file))
goto out; goto out;
in_fd = ksys_open(from, O_RDONLY, 0); in_file = filp_open(from, O_RDONLY, 0);
if (in_fd < 0) if (IS_ERR(in_file))
goto noclose_input; goto noclose_input;
nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor); in_pos = rd_image_start * BLOCK_SIZE;
nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
if (nblocks < 0) if (nblocks < 0)
goto done; goto done;
if (nblocks == 0) { if (nblocks == 0) {
if (crd_load(in_fd, out_fd, decompressor) == 0) if (crd_load(decompressor) == 0)
goto successful_load; goto successful_load;
goto done; goto done;
} }
...@@ -206,11 +218,7 @@ int __init rd_load_image(char *from) ...@@ -206,11 +218,7 @@ int __init rd_load_image(char *from)
* NOTE NOTE: nblocks is not actually blocks but * NOTE NOTE: nblocks is not actually blocks but
* the number of kibibytes of data to load into a ramdisk. * the number of kibibytes of data to load into a ramdisk.
*/ */
if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0) rd_blocks = nr_blocks(out_file);
rd_blocks = 0;
else
rd_blocks >>= 1;
if (nblocks > rd_blocks) { if (nblocks > rd_blocks) {
printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n", printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
nblocks, rd_blocks); nblocks, rd_blocks);
...@@ -220,13 +228,10 @@ int __init rd_load_image(char *from) ...@@ -220,13 +228,10 @@ int __init rd_load_image(char *from)
/* /*
* OK, time to copy in the data * OK, time to copy in the data
*/ */
if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
devblocks = 0;
else
devblocks >>= 1;
if (strcmp(from, "/initrd.image") == 0) if (strcmp(from, "/initrd.image") == 0)
devblocks = nblocks; devblocks = nblocks;
else
devblocks = nr_blocks(in_file);
if (devblocks == 0) { if (devblocks == 0) {
printk(KERN_ERR "RAMDISK: could not determine device size\n"); printk(KERN_ERR "RAMDISK: could not determine device size\n");
...@@ -245,14 +250,11 @@ int __init rd_load_image(char *from) ...@@ -245,14 +250,11 @@ int __init rd_load_image(char *from)
if (i && (i % devblocks == 0)) { if (i && (i % devblocks == 0)) {
pr_cont("done disk #1.\n"); pr_cont("done disk #1.\n");
rotate = 0; rotate = 0;
if (ksys_close(in_fd)) { fput(in_file);
printk("Error closing the disk.\n");
goto noclose_input;
}
break; break;
} }
ksys_read(in_fd, buf, BLOCK_SIZE); kernel_read(in_file, buf, BLOCK_SIZE, &in_pos);
ksys_write(out_fd, buf, BLOCK_SIZE); kernel_write(out_file, buf, BLOCK_SIZE, &out_pos);
#if !defined(CONFIG_S390) #if !defined(CONFIG_S390)
if (!(i % 16)) { if (!(i % 16)) {
pr_cont("%c\b", rotator[rotate & 0x3]); pr_cont("%c\b", rotator[rotate & 0x3]);
...@@ -265,9 +267,9 @@ int __init rd_load_image(char *from) ...@@ -265,9 +267,9 @@ int __init rd_load_image(char *from)
successful_load: successful_load:
res = 1; res = 1;
done: done:
ksys_close(in_fd); fput(in_file);
noclose_input: noclose_input:
ksys_close(out_fd); fput(out_file);
out: out:
kfree(buf); kfree(buf);
ksys_unlink("/dev/ram"); ksys_unlink("/dev/ram");
...@@ -283,11 +285,10 @@ int __init rd_load_disk(int n) ...@@ -283,11 +285,10 @@ int __init rd_load_disk(int n)
static int exit_code; static int exit_code;
static int decompress_error; static int decompress_error;
static int crd_infd, crd_outfd;
static long __init compr_fill(void *buf, unsigned long len) static long __init compr_fill(void *buf, unsigned long len)
{ {
long r = ksys_read(crd_infd, buf, len); long r = kernel_read(in_file, buf, len, &in_pos);
if (r < 0) if (r < 0)
printk(KERN_ERR "RAMDISK: error while reading compressed data"); printk(KERN_ERR "RAMDISK: error while reading compressed data");
else if (r == 0) else if (r == 0)
...@@ -297,7 +298,7 @@ static long __init compr_fill(void *buf, unsigned long len) ...@@ -297,7 +298,7 @@ static long __init compr_fill(void *buf, unsigned long len)
static long __init compr_flush(void *window, unsigned long outcnt) static long __init compr_flush(void *window, unsigned long outcnt)
{ {
long written = ksys_write(crd_outfd, window, outcnt); long written = kernel_write(out_file, window, outcnt, &out_pos);
if (written != outcnt) { if (written != outcnt) {
if (decompress_error == 0) if (decompress_error == 0)
printk(KERN_ERR printk(KERN_ERR
...@@ -316,11 +317,9 @@ static void __init error(char *x) ...@@ -316,11 +317,9 @@ static void __init error(char *x)
decompress_error = 1; decompress_error = 1;
} }
static int __init crd_load(int in_fd, int out_fd, decompress_fn deco) static int __init crd_load(decompress_fn deco)
{ {
int result; int result;
crd_infd = in_fd;
crd_outfd = out_fd;
if (!deco) { if (!deco) {
pr_emerg("Invalid ramdisk decompression routine. " pr_emerg("Invalid ramdisk decompression routine. "
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment