Skip to content

Commit 22216c2

Browse files
committed
修复 modfs 因为文件属性问题导致无法挂载 rootfs
1 parent 49219f5 commit 22216c2

File tree

8 files changed

+66
-74
lines changed

8 files changed

+66
-74
lines changed

module/squashfs/squashfs.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ int squashfs_lookup_child(
390390
int squashfs_create_mount(
391391
const char *src, squashfs_mount_t **out, sqfs_inode_generic_t **root_inode, sqfs_u64 *root_ref
392392
) {
393-
394393
*out = NULL;
395394
if (root_inode != NULL) {
396395
*root_inode = NULL;

src/arch/x86_64/acpi/apic.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
#include "term/klog.h"
99
#include "timer.h"
1010

11-
bool x2apic_mode = false;
12-
uint64_t lapic_address;
11+
static bool x2apic_mode = false;
12+
static uint64_t lapic_address;
1313

14-
uint64_t calibrated_timer_initial = 0;
14+
static uint64_t calibrated_timer_initial = 0;
1515

1616
static struct ioapic_info found_ioapics[MAX_IOAPICS];
1717
static struct iso_info found_isos[MAX_ISO];
1818

1919
static size_t found_iso_count = 0;
2020
static size_t found_ioapic_count = 0;
2121

22-
void disable_pic() {
22+
static void disable_pic() {
2323
io_out8(0x21, 0xff);
2424
io_out8(0xa1, 0xff);
2525

@@ -35,19 +35,19 @@ void lapic_write(uint32_t reg, uint32_t value) {
3535
wrmsr(0x800 + (reg >> 4), value);
3636
return;
3737
}
38-
*(volatile uint32_t *)((uint64_t)lapic_address + reg) = value;
38+
*(volatile uint32_t *)(lapic_address + reg) = value;
3939
}
4040

4141
uint32_t lapic_read(uint32_t reg) {
4242
if (x2apic_mode) {
4343
return rdmsr(0x800 + (reg >> 4));
4444
}
45-
return *(volatile uint32_t *)((uint64_t)lapic_address + reg);
45+
return *(volatile uint32_t *)(lapic_address + reg);
4646
}
4747

4848
uint64_t lapic_id() {
4949
uint32_t phy_id = lapic_read(LAPIC_REG_ID);
50-
return x2apic_mode ? phy_id : (phy_id >> 24);
50+
return x2apic_mode ? phy_id : phy_id >> 24;
5151
}
5252

5353
static void ioapic_mmio_write(uintptr_t base, uint32_t reg, uint32_t value) {
@@ -82,12 +82,13 @@ static struct ioapic_info *find_ioapic(uint32_t gsi) {
8282

8383
void ioapic_add(uint8_t vector, uint32_t irq) {
8484
struct ioapic_info *io = find_ioapic(isa_irq_to_gsi(vector));
85-
if (!io)
85+
if (!io) {
8686
return;
87+
}
8788

88-
uint32_t irq0 = irq - io->gsi_base;
89-
uint32_t ioredtbl = 0x10 + irq0 * 2;
90-
uint64_t redirect = vector | ((uint64_t)lapic_id() << 56);
89+
const uint32_t irq0 = irq - io->gsi_base;
90+
const uint32_t ioredtbl = 0x10 + irq0 * 2;
91+
const uint64_t redirect = vector | lapic_id() << 56;
9192

9293
ioapic_mmio_write(io->mmio_base, ioredtbl, (uint32_t)redirect);
9394
ioapic_mmio_write(io->mmio_base, ioredtbl + 1, (uint32_t)(redirect >> 32));
@@ -99,10 +100,10 @@ void ioapic_enable(uint8_t vector) {
99100
printk("Cannot found ioapic for vector %d gsi:%d ENABLE\n", vector, isa_irq_to_gsi(vector));
100101
return;
101102
}
102-
uint64_t index = 0x10 + ((isa_irq_to_gsi(vector) - ioapic->gsi_base) * 2);
103+
const uint64_t index = 0x10 + (isa_irq_to_gsi(vector) - ioapic->gsi_base) * 2;
103104
uint64_t value = (uint64_t)ioapic_mmio_read(ioapic->mmio_base, index + 1) << 32
104105
| (uint64_t)ioapic_mmio_read(ioapic->mmio_base, index);
105-
value &= (~0x10000UL);
106+
value &= ~0x10000UL;
106107
ioapic_mmio_write(ioapic->mmio_base, index, (uint32_t)(value & 0xFFFFFFFF));
107108
ioapic_mmio_write(ioapic->mmio_base, index + 1, (uint32_t)(value >> 32));
108109
}

src/arch/x86_64/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ USED _Noreturn void kmain() {
138138
extern void mount_modfs();
139139
extern errno_t mount_boot_rootfs();
140140
mount_modfs();
141-
mount_boot_rootfs();
142141

143142
init_console_symlink();
144143

src/fs/fssys.c

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,23 @@ syscall_(chdir, char *s) {
548548
}
549549

550550
syscall_(fcntl, int fd, int cmd, uint64_t arg) {
551-
if (fd < 0 || cmd < 0)
551+
if (fd < 0 || cmd < 0) {
552552
return SYSCALL_FAULT_(EINVAL);
553+
}
553554
fd_t *handle = get_fd(get_current_task()->process->fdts, fd);
554-
if (handle == NULL)
555+
if (handle == NULL) {
555556
return SYSCALL_FAULT_(EBADF);
557+
}
556558

557559
switch (cmd) {
558560
case F_GETFD:
559-
return (handle->flags & O_CLOEXEC) ? 1 : 0;
561+
return handle->flags & O_CLOEXEC ? 1 : 0;
560562
case F_SETFD:
561-
if (arg & 1)
563+
if (arg & 1) {
562564
handle->flags |= O_CLOEXEC;
563-
else
565+
} else {
564566
handle->flags &= ~O_CLOEXEC;
567+
}
565568
return EOK;
566569
case F_DUPFD_CLOEXEC:
567570
return dup_with_minfd(handle, (int)arg, true);
@@ -570,7 +573,7 @@ syscall_(fcntl, int fd, int cmd, uint64_t arg) {
570573
case F_GETFL:
571574
return handle->flags;
572575
case F_SETFL:;
573-
uint32_t valid_flags = O_APPEND | O_DIRECT | O_NOATIME | O_NONBLOCK;
576+
const uint32_t valid_flags = O_APPEND | O_DIRECT | O_NOATIME | O_NONBLOCK;
574577
handle->flags &= ~valid_flags;
575578
handle->flags |= arg & valid_flags;
576579
handle->node->flags &= ~valid_flags;
@@ -587,8 +590,8 @@ syscall_(mount, char *dev_name, char *dir_name, char *type, uint64_t flags, void
587590
return SYSCALL_FAULT_(EINVAL);
588591
}
589592

590-
char *ndir_name = vfs_cwd_path_build(dir_name);
591-
vfs_node_t dir = vfs_open(ndir_name);
593+
char *ndir_name = vfs_cwd_path_build(dir_name);
594+
const vfs_node_t dir = vfs_open(ndir_name);
592595
if (!dir) {
593596
free(ndir_name);
594597
return SYSCALL_FAULT_(ENOENT);
@@ -599,35 +602,39 @@ syscall_(mount, char *dev_name, char *dir_name, char *type, uint64_t flags, void
599602
free(ndir_name);
600603
return SYSCALL_FAULT_(EINVAL);
601604
}
602-
char *old_root_p = vfs_cwd_path_build(dev_name);
603-
vfs_node_t old_root = vfs_open(old_root_p);
605+
char *old_root_p = vfs_cwd_path_build(dev_name);
606+
const vfs_node_t old_root = vfs_open(old_root_p);
604607
free(old_root_p);
605-
if (old_root == NULL || !old_root->is_mount)
608+
if (old_root == NULL || !old_root->is_mount) {
606609
return SYSCALL_FAULT_(EINVAL);
607-
if (dir != rootdir)
610+
}
611+
if (dir != rootdir) {
608612
list_append(dir->parent->child, old_root);
613+
}
609614
char *nb = old_root->name;
610615
old_root->name = dir->name;
611616
dir->name = nb;
612617
list_append(old_root->parent->child, dir);
613618

614619
list_delete(old_root->parent->child, old_root);
615-
if (dir != rootdir)
620+
if (dir != rootdir) {
616621
list_delete(dir->parent->child, dir);
617-
else
622+
} else {
618623
rootdir = old_root;
624+
}
619625

620-
vfs_node_t parent = dir->parent;
621-
dir->parent = old_root->parent;
622-
old_root->parent = parent;
626+
const vfs_node_t parent = dir->parent;
627+
dir->parent = old_root->parent;
628+
old_root->parent = parent;
623629

624630
vfs_close(old_root);
625631
vfs_close(dir);
626632
return EOK;
627633
}
628634

629-
if (type == NULL)
635+
if (type == NULL) {
630636
return SYSCALL_FAULT_(EINVAL);
637+
}
631638

632639
char *ndev_name = vfs_cwd_path_build(dev_name);
633640
errno_t mret = EOK;
@@ -636,8 +643,9 @@ syscall_(mount, char *dev_name, char *dir_name, char *type, uint64_t flags, void
636643
if (mret != EOK) {
637644
free(ndir_name);
638645
free(ndev_name);
639-
if (mret < 0)
646+
if (mret < 0) {
640647
return (uint64_t)mret;
648+
}
641649
return SYSCALL_FAULT_(EIO);
642650
}
643651
free(ndir_name);
@@ -647,12 +655,10 @@ syscall_(mount, char *dev_name, char *dir_name, char *type, uint64_t flags, void
647655

648656
syscall_(poll, struct pollfd *fds_user, size_t nfds, size_t timeout) {
649657
int ready = 0;
650-
uint64_t start_time = nano_time();
658+
const uint64_t start_time = nano_time();
651659
bool sigexit = false;
652-
tcb_t current = get_current_task();
653-
fdt_t *fdt = current->process->fdts;
654-
655-
extern vfs_callback_t fs_callbacks[256];
660+
const tcb_t current = get_current_task();
661+
const fdt_t *fdt = current->process->fdts;
656662

657663
do {
658664
ready = 0;
@@ -663,7 +669,8 @@ syscall_(poll, struct pollfd *fds_user, size_t nfds, size_t timeout) {
663669

664670
// 检查每个文件描述符
665671
for (size_t i = 0; i < nfds; i++) {
666-
fd_t *handle = get_fd(fdt, fds_user[i].fd);
672+
extern vfs_callback_t fs_callbacks[256];
673+
const fd_t *handle = get_fd(fdt, fds_user[i].fd);
667674
if (handle == NULL) {
668675
fds_user[i].revents = POLLNVAL;
669676
ready++;

src/fs/procfs/proc_dpatch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void load_procfs_root() {
6565
}
6666

6767
size_t procfs_read_dispatch(proc_handle_t *handle, void *addr, size_t offset, size_t size) {
68-
uint64_t hash = hash_dp(handle->name);
68+
const uint64_t hash = hash_dp(handle->name);
6969
for (size_t i = 0; i < dp_index; i++) {
7070
if (hash == dispatch_array[i]->hash) {
7171
return dispatch_array[i]->read_entry(handle, addr, offset, size);
@@ -75,7 +75,7 @@ size_t procfs_read_dispatch(proc_handle_t *handle, void *addr, size_t offset, si
7575
}
7676

7777
void procfs_stat_dispatch(proc_handle_t *handle, vfs_node_t node) {
78-
uint64_t hash = hash_dp(handle->name);
78+
const uint64_t hash = hash_dp(handle->name);
7979
for (size_t i = 0; i < dp_index; i++) {
8080
if (hash == dispatch_array[i]->hash) {
8181
node->size = dispatch_array[i]->stat_entry(handle);

src/fs/tmpfs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ errno_t tmpfs_stat(void *file, vfs_node_t node) {
103103
return -ENOENT;
104104
node->type = file0->type == tp_file_symlink ? file_symlink
105105
: file0->type == tp_file_dir ? file_dir
106+
: file0->type == tp_file_blk ? file_block
107+
: file0->type == tp_file_char ? file_stream
106108
: file_none;
107109
node->size = file0->type == file_dir ? 0 : file0->size;
108110
return EOK;
@@ -233,7 +235,7 @@ errno_t tmpfs_mknod(void *parent, const char *name, vfs_node_t node, uint16_t mo
233235
node->type = file_block;
234236
handle->type = tp_file_blk;
235237
}
236-
if ((mode & S_IFMT) == S_IFCHR) {
238+
else if ((mode & S_IFMT) == S_IFCHR) {
237239
node->type = file_stream;
238240
handle->type = tp_file_char;
239241
} else {

src/mod/module.c

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "mod/module.h"
22
#include "boot.h"
33
#include "errno.h"
4+
#include "fs/tmpfs.h"
45
#include "fs/vfs.h"
56
#include "krlibc.h"
67
#include "mem/heap.h"
@@ -23,6 +24,10 @@ static const char *find_boot_rootfs_name(void) {
2324
return NULL;
2425
}
2526

27+
static bool is_boot_rootfs_module(const char *name) {
28+
return strcmp(name, "cp_rootfs") == 0 || strcmp(name, "rootfs") == 0;
29+
}
30+
2631
void extract_name(const char *input, char *output, size_t output_size) {
2732
const char *name = strrchr(input, '/');
2833
if (!name) {
@@ -100,35 +105,11 @@ void mount_modfs() {
100105
continue;
101106
}
102107
vfs_write(mod_node, module0.data, 0, module0.size);
108+
if (is_boot_rootfs_module(module0.name) && mod_node->handle != NULL) {
109+
tmpfs_file_t *tmpfile = mod_node->handle;
110+
tmpfile->type = tp_file_blk;
111+
mod_node->type = file_block;
112+
mod_node->mode = 0644;
113+
}
103114
}
104115
}
105-
106-
errno_t mount_boot_rootfs() {
107-
const char *rootfs_name = find_boot_rootfs_name();
108-
if (rootfs_name == NULL) {
109-
logkf("rootfs: boot module not found\n\r");
110-
return -ENOENT;
111-
}
112-
113-
char rootfs_path[64];
114-
sprintf(rootfs_path, "/mod/%s", rootfs_name);
115-
116-
vfs_mkdir("/new_root");
117-
118-
vfs_node_t new_root = vfs_open("/new_root");
119-
if (new_root == NULL) {
120-
logkf("rootfs: cannot open /new_root\n\r");
121-
return -ENOENT;
122-
}
123-
124-
errno_t ret = vfs_mount(rootfs_path, "squashfs", new_root, NULL);
125-
if (ret != EOK) {
126-
logkf("rootfs: squashfs mount from %s failed: %d\n\r", rootfs_path, ret);
127-
vfs_close(new_root);
128-
return ret;
129-
}
130-
131-
logkf("rootfs: mounted %s on /new_root\n\r", rootfs_path);
132-
vfs_close(new_root);
133-
return EOK;
134-
}

src/task/poll.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ syscall_(
314314
const uint64_t start_time = nano_time();
315315
int ready = 0;
316316

317+
arch_open_interrupt();
318+
scheduler_enable();
317319
do {
318320
ready = 0;
319321
spin_lock(ep->lock);
@@ -356,7 +358,8 @@ syscall_(
356358
}
357359

358360
scheduler_yield();
359-
} while (timeout < 0 || (nano_time() - start_time) < (uint64_t)timeout * 1000000ULL);
361+
} while (timeout < 0 || nano_time() - start_time < (uint64_t)timeout * 1000000ULL);
362+
arch_close_interrupt();
360363

361364
return 0;
362365
}

0 commit comments

Comments
 (0)