Skip to content

Commit bf6419e

Browse files
author
Christoph Hellwig
committed
initramfs: switch initramfs unpacking to struct file based APIs
There is no good reason to mess with file descriptors from in-kernel code, switch the initramfs unpacking to struct file based write instead. Signed-off-by: Christoph Hellwig <[email protected]> Acked-by: Linus Torvalds <[email protected]>
1 parent b2a74d5 commit bf6419e

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

init/initramfs.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
#include <linux/memblock.h>
1414
#include <linux/namei.h>
1515

16-
static ssize_t __init xwrite(int fd, const char *p, size_t count)
16+
static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
17+
loff_t *pos)
1718
{
1819
ssize_t out = 0;
1920

2021
/* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
2122
while (count) {
22-
ssize_t rv = ksys_write(fd, p, count);
23+
ssize_t rv = kernel_write(file, p, count, pos);
2324

2425
if (rv < 0) {
2526
if (rv == -EINTR || rv == -EAGAIN)
@@ -317,7 +318,8 @@ static int __init maybe_link(void)
317318
return 0;
318319
}
319320

320-
static __initdata int wfd;
321+
static __initdata struct file *wfile;
322+
static __initdata loff_t wfile_pos;
321323

322324
static int __init do_name(void)
323325
{
@@ -334,16 +336,17 @@ static int __init do_name(void)
334336
int openflags = O_WRONLY|O_CREAT;
335337
if (ml != 1)
336338
openflags |= O_TRUNC;
337-
wfd = ksys_open(collected, openflags, mode);
338-
339-
if (wfd >= 0) {
340-
ksys_fchown(wfd, uid, gid);
341-
ksys_fchmod(wfd, mode);
342-
if (body_len)
343-
ksys_ftruncate(wfd, body_len);
344-
vcollected = kstrdup(collected, GFP_KERNEL);
345-
state = CopyFile;
346-
}
339+
wfile = filp_open(collected, openflags, mode);
340+
if (IS_ERR(wfile))
341+
return 0;
342+
wfile_pos = 0;
343+
344+
vfs_fchown(wfile, uid, gid);
345+
vfs_fchmod(wfile, mode);
346+
if (body_len)
347+
vfs_truncate(&wfile->f_path, body_len);
348+
vcollected = kstrdup(collected, GFP_KERNEL);
349+
state = CopyFile;
347350
}
348351
} else if (S_ISDIR(mode)) {
349352
ksys_mkdir(collected, mode);
@@ -365,16 +368,16 @@ static int __init do_name(void)
365368
static int __init do_copy(void)
366369
{
367370
if (byte_count >= body_len) {
368-
if (xwrite(wfd, victim, body_len) != body_len)
371+
if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
369372
error("write error");
370-
ksys_close(wfd);
373+
fput(wfile);
371374
do_utime(vcollected, mtime);
372375
kfree(vcollected);
373376
eat(body_len);
374377
state = SkipIt;
375378
return 0;
376379
} else {
377-
if (xwrite(wfd, victim, byte_count) != byte_count)
380+
if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
378381
error("write error");
379382
body_len -= byte_count;
380383
eat(byte_count);
@@ -576,21 +579,23 @@ static inline bool kexec_free_initrd(void)
576579
static void __init populate_initrd_image(char *err)
577580
{
578581
ssize_t written;
579-
int fd;
582+
struct file *file;
583+
loff_t pos = 0;
580584

581585
unpack_to_rootfs(__initramfs_start, __initramfs_size);
582586

583587
printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
584588
err);
585-
fd = ksys_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
586-
if (fd < 0)
589+
file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
590+
if (IS_ERR(file))
587591
return;
588592

589-
written = xwrite(fd, (char *)initrd_start, initrd_end - initrd_start);
593+
written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
594+
&pos);
590595
if (written != initrd_end - initrd_start)
591596
pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
592597
written, initrd_end - initrd_start);
593-
ksys_close(fd);
598+
fput(file);
594599
}
595600
#endif /* CONFIG_BLK_DEV_RAM */
596601

0 commit comments

Comments
 (0)