From: acevest Date: Sat, 28 Sep 2024 09:25:06 +0000 (+0800) Subject: 弃用ERR_PTR PTR_ERR IS_ERR的错误码返回方式 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;p=kernel.git 弃用ERR_PTR PTR_ERR IS_ERR的错误码返回方式 --- diff --git a/fs/dentry.c b/fs/dentry.c index 7bf7d71..04081a2 100644 --- a/fs/dentry.c +++ b/fs/dentry.c @@ -140,44 +140,45 @@ void dentry_add(dentry_t *dentry, inode_t *inode) { dentry_rehash(dentry); } -dentry_t *dentry_cached_lookup(dentry_t *parent, qstr_t *s) { +int dentry_cached_lookup(dentry_t *parent, qstr_t *s, dentry_t **dentry) { dentry_hash_entry_t *dhe = dentry_hash(parent, s->hash); - dentry_t *dentry = NULL; + *dentry = NULL; mutex_lock(&dhe->mutex); list_head_t *p; list_for_each(p, &dhe->list) { - dentry = list_entry(p, dentry_t, d_hash); - assert(dentry != NULL); + *dentry = list_entry(p, dentry_t, d_hash); + assert(*dentry != NULL); - if (dentry->d_name.hash != s->hash) { + if ((*dentry)->d_name.hash != s->hash) { continue; } - if (dentry->d_name.len != s->len) { + if ((*dentry)->d_name.len != s->len) { continue; } - if (dentry->d_parent != parent) { + if ((*dentry)->d_parent != parent) { continue; } - if (memcmp(dentry->d_name.name, s->name, s->len) != 0) { + if (memcmp((*dentry)->d_name.name, s->name, s->len) != 0) { continue; } - dentry_get_locked(dentry); + dentry_get_locked(*dentry); mutex_unlock(&dhe->mutex); - return dentry; + return 0; } mutex_unlock(&dhe->mutex); - return NULL; + return 0; } + int dentry_real_lookup(dentry_t *parent, qstr_t *s, dentry_t **dentry) { *dentry = NULL; int ret = 0; @@ -189,7 +190,8 @@ int dentry_real_lookup(dentry_t *parent, qstr_t *s, dentry_t **dentry) { // 在获得信号量后,需要再上cache中查找一遍 // 因为这个过程中当前进程可能会睡眠,当被唤醒后,其它进程已经在内存准备好了 - *dentry = dentry_cached_lookup(parent, s); + ret = dentry_cached_lookup(parent, s, dentry); + assert(0 == ret); if (NULL != *dentry) { up(&dir->i_sem); diff --git a/fs/fssysc.c b/fs/fssysc.c index 255b964..22435b7 100644 --- a/fs/fssysc.c +++ b/fs/fssysc.c @@ -26,12 +26,14 @@ __attribute__((regparm(0))) long sysc_mkdir(const char *path, int mode) { } } - dentry_t *dentry; - dentry = path_lookup_create(&ni); - ret = PTR_ERR(dentry); - if (!IS_ERR(dentry)) { + dentry_t *dentry = NULL; + ret = path_lookup_create(&ni, &dentry); + if (0 != ret) { + assert(dentry == NULL); ret = vfs_mkdir(ni.path.dentry->d_inode, dentry, mode); dentry_put(dentry); + } else { + assert(dentry != NULL); } up(&ni.path.dentry->d_inode->i_sem); diff --git a/fs/path.c b/fs/path.c index 25dfb3a..48d2caa 100644 --- a/fs/path.c +++ b/fs/path.c @@ -201,7 +201,8 @@ int path_walk(const char *path, namei_t *ni) { compute_qstr_hash(&this); // 根据该名字,先上dentry cache里找 - dentry = dentry_cached_lookup(ni->path.dentry, &this); + ret = dentry_cached_lookup(ni->path.dentry, &this, &dentry); + assert(0 == ret); // 如果找不到就上实际存储设备中去找 if (NULL == dentry) { @@ -281,7 +282,8 @@ int path_walk(const char *path, namei_t *ni) { // printk("HASH %s %lu\n", this.name, this.hash); // 根据该名字,先上dentry cache里找 - dentry = dentry_cached_lookup(ni->path.dentry, &this); + ret = dentry_cached_lookup(ni->path.dentry, &this, &dentry); + assert(0 == ret); // 如果找不到就上实际存储设备中去找 if (NULL == dentry) { @@ -328,50 +330,49 @@ end: return ret; } -dentry_t *path_lookup_hash(dentry_t *base, qstr_t *name) { - dentry_t *dentry = NULL; - +int path_lookup_hash(dentry_t *base, qstr_t *name, dentry_t **dentry) { + int ret = 0; inode_t *inode = base->d_inode; - dentry = dentry_cached_lookup(base, name); - if (dentry != NULL) { - return dentry; + ret = dentry_cached_lookup(base, name, dentry); + assert(0 == ret); + if (*dentry != NULL) { + return 0; } dentry_t *dentry_new = dentry_alloc(base, name); if (dentry_new == NULL) { - dentry = ERR_PTR(-ENOMEM); - return dentry; + return ENOMEM; } - dentry = inode->i_ops->lookup(inode, dentry_new); + *dentry = inode->i_ops->lookup(inode, dentry_new); - if (dentry == NULL) { // 返回 lookup 没有再分配一个dentry - dentry = dentry_new; + if (*dentry == NULL) { // 返回 lookup 没有再分配一个dentry + *dentry = dentry_new; } else { // 否则就释放dentry_new使用lookup返回的dentry dentry_put(dentry_new); } - return dentry; + return 0; } -dentry_t *path_lookup_create(namei_t *ni) { - dentry_t *dentry = NULL; +int path_lookup_create(namei_t *ni, dentry_t **dentry) { + int err = 0; // 在调用完path_lookup_create后调用 up 操作 down(&ni->path.dentry->d_inode->i_sem); - dentry = ERR_PTR(-EEXIST); + // if (ni->last_type != LAST_NORMAL) { - return dentry; + return EEXIST; } - dentry = path_lookup_hash(ni->path.dentry, &ni->last); - if (IS_ERR(dentry)) { - return dentry; + err = path_lookup_hash(ni->path.dentry, &ni->last, dentry); + if (err != 0) { + return err; } - return dentry; + return err; } int path_open_namei(const char *path, int flags, int mode, namei_t *ni) { @@ -406,9 +407,8 @@ int path_open_namei(const char *path, int flags, int mode, namei_t *ni) { down(&dir->d_inode->i_sem); - dentry = dentry_cached_lookup(dir, &ni->last); - if (IS_ERR(dentry)) { - ret = PTR_ERR(dentry); + ret = path_lookup_hash(dir, &ni->last, &dentry); + if (0 != ret) { up(&dir->d_inode->i_sem); goto end; } diff --git a/fs/super.c b/fs/super.c index 0f8f334..2fffc62 100644 --- a/fs/super.c +++ b/fs/super.c @@ -39,57 +39,59 @@ int set_anonymous_super(superblock_t *s, void *data) { } int read_super_for_nodev(fs_type_t *type, int flags, void *data, fill_super_cb_t fill_super, vfsmount_t *mnt) { - int ret = 0; + int err = 0; superblock_t *s = 0; // 分配superblock - s = sget(type, NULL, set_anonymous_super, NULL); + err = sget(type, NULL, set_anonymous_super, NULL, &s); assert(s != NULL); // s->sb_flags = flags; - ret = fill_super(s, data); - if (0 != ret) { - panic("ret: %d", ret); + err = fill_super(s, data); + if (0 != err) { + panic("err: %d", err); } mnt->mnt_sb = s; mnt->mnt_root = dentry_get(s->sb_root); - return ret; + return err; } -superblock_t *sget(fs_type_t *type, // - int (*test)(superblock_t *, void *), // - int (*set)(superblock_t *, void *), // - void *data // +int sget(fs_type_t *type, // + int (*test)(superblock_t *, void *), // + int (*set)(superblock_t *, void *), // + void *data, // + superblock_t **s // ) { - int ret = 0; - superblock_t *s = 0; + int err = 0; + + *s = 0; if (0 != test) { panic("not implemented"); } // TOOD REMOVE - assert(0 == s); + assert(0 == *s); - s = alloc_super(type); - if (0 == s) { - return ERR_PTR(-ENOMEM); + *s = alloc_super(type); + if (0 == *s) { + return ENOMEM; } assert(0 != set); - ret = set(s, data); - assert(0 == ret); + err = set(*s, data); + assert(0 == err); uint32_t eflags; irq_save(eflags); - list_add_tail(&s->sb_list, &g_superblocks); + list_add_tail(&(*s)->sb_list, &g_superblocks); irq_restore(eflags); - return s; + return err; } diff --git a/fs/vfs.h b/fs/vfs.h index 9902063..d215530 100644 --- a/fs/vfs.h +++ b/fs/vfs.h @@ -285,8 +285,16 @@ inode_t *alloc_inode(superblock_t *sb); void init_special_inode(inode_t *inode, umode_t mode, dev_t rdev); //// -dentry_t *dentry_cached_lookup(dentry_t *parent, qstr_t *s); -int dentry_real_lookup(dentry_t *parent, qstr_t *s, dentry_t **dentry); +int dentry_cached_lookup(dentry_t *parent, // + qstr_t *s, // + dentry_t **dentry // OUT +); + +int dentry_real_lookup(dentry_t *parent, // + qstr_t *s, // + dentry_t **dentry // OUT +); + dentry_t *dentry_alloc_root(inode_t *root_inode); dentry_t *dentry_alloc(dentry_t *parent, const qstr_t *s); void dentry_add(dentry_t *dentry, inode_t *inode); @@ -306,4 +314,6 @@ extern const file_operations_t simple_dir_operations; // bool path_init(const char *path, unsigned int flags, namei_t *ni); int path_walk(const char *path, namei_t *ni); -dentry_t *path_lookup_create(namei_t *ni); +int path_lookup_create(namei_t *ni, // + dentry_t **dentry // OUT +); diff --git a/include/fs.h b/include/fs.h index 5db8166..3e21fb0 100644 --- a/include/fs.h +++ b/include/fs.h @@ -59,10 +59,11 @@ extern chrdev_t *chrdev[]; typedef int (*fill_super_cb_t)(superblock_t *sb, void *data); int read_super_for_nodev(fs_type_t *type, int flags, void *data, fill_super_cb_t fill_super, vfsmount_t *mnt); -superblock_t *sget(fs_type_t *type, // - int (*test)(superblock_t *, void *), // - int (*set)(superblock_t *, void *), // - void *data // +int sget(fs_type_t *type, // + int (*test)(superblock_t *, void *), // + int (*set)(superblock_t *, void *), // + void *data, // + superblock_t **s // OUT ); file_t *get_file(int fd); diff --git a/include/system.h b/include/system.h index d0fd0a0..b61f772 100644 --- a/include/system.h +++ b/include/system.h @@ -272,18 +272,4 @@ void boot_delay(int ticks); #endif -#define MAX_ERRNO 4095 - -#ifndef ASM - -#define IS_ERR_VALUE(x) (((unsigned long)(x)) >= ((unsigned long)(-MAX_ERRNO))) - -static inline void *ERR_PTR(long err) { return ((void *)(err)); } - -static inline long PTR_ERR(void *p) { return ((long)(p)); } - -static inline bool IS_ERR(void *p) { return IS_ERR_VALUE(p); } - -#endif - #endif //_SYSTEM_H diff --git a/kernel/task_root.c b/kernel/task_root.c index 95d5477..7b4c1b0 100644 --- a/kernel/task_root.c +++ b/kernel/task_root.c @@ -49,7 +49,7 @@ void kernel_task(char *name, void *entry, void *arg) { regs.ss = SELECTOR_KRNL_DS; regs.fs = SELECTOR_KRNL_DS; regs.gs = SELECTOR_KRNL_DS; -#if 0 +#if 1 get_eflags(regs.eflags); #else regs.eflags = 0x200; @@ -74,12 +74,10 @@ void root_task_entry() { } #endif - // sti(); + sti(); kernel_task("init", init_task_entry, NULL); - sti(); - current->priority = 1; while (1) { asm("hlt;");