fs:regfs: add register easy filesystem
PATCH 1/2 fs:regfs: add register easy filesystem
register filesystem is mapping the register into file dentry, it will use the io readio to get the register val. DBT file is use to decript the register tree, you can use it as follow:
mount -t regfs -o dtb=test.dtb none /mnt
test.dts:
/ {
compatible = "hisilicon,hi6220-hikey", "hisilicon,hi6220";
#address-cells = <0x2>;
#size-cells = <0x2>;
model = "HiKey Development Board";
gic-v3-dist{
reg = <0x0 0x8000000 0x0 0x10000>;
GIC_CTRL {
offset = <0x0>;
};
GICD_TYPER {
offset = <0x4>;
};
};
};it will create all regiter dentry file in /mnt
--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/regfs/Kconfig | 7 + fs/regfs/Makefile | 8 ++ fs/regfs/file.c | 107 +++++++++++++++ fs/regfs/inode.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/regfs/internal.h | 32 +++++ fs/regfs/regfs_inode.h | 32 +++++ fs/regfs/supper.c | 71 ++++++++++ 9 files changed, 613 insertions(+) create mode 100644 fs/regfs/Kconfig create mode 100644 fs/regfs/Makefile create mode 100644 fs/regfs/file.c create mode 100644 fs/regfs/inode.c create mode 100644 fs/regfs/internal.h create mode 100644 fs/regfs/regfs_inode.h create mode 100644 fs/regfs/supper.c
fs/Kconfig +1 -0
@@ -324,6 +324,7 @@ endif # NETWORK_FILESYSTEMS source "fs/nls/Kconfig" source "fs/dlm/Kconfig" source "fs/unicode/Kconfig" +source "fs/regfs/Kconfig" config IO_WQ bool
fs/Makefile +1 -0
@@ -136,3 +136,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/ obj-$(CONFIG_EROFS_FS) += erofs/ obj-$(CONFIG_VBOXSF_FS) += vboxsf/ obj-$(CONFIG_ZONEFS_FS) += zonefs/ +obj-$(CONFIG_REGFS_FS) += zonefs/
fs/regfs/Kconfig new file +7 -0
@@ -0,0 +1,7 @@ +config REGFS_FS + tristate "registers filesystem support" + depends on ARM64 + help + regfs support the read and write register of device resource by + dentry filesystem, it is more easy to support bsp debug. it also + support to printk the register val when panic
fs/regfs/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +#Makefile for the linux ramfs routines. +# + +obj-y += regfs.o + +regfs-objs += inode.o file.o supper.o
fs/regfs/file.c new file +107 -0
107 lines not on this page · raw message
fs/regfs/inode.c new file +354 -0
354 lines not on this page · raw message
fs/regfs/internal.h new file +32 -0
@@ -0,0 +1,32 @@ +#ifndef __INTERNAL_H__ +#define __INTERNAL_H__ + +#define loc_debug (fmt, ...) \ + do { \ + if (regfs_debug) \ + printk(fmt, ##__VA_ARGS__); \ + } while (0) + +struct regfs_fs_info { +struct super_block *sb; +void *dtb_buf; +u64 dtb_len; +u64 iomem; +u64 size; +// supper ifs list +struct list_head regfs_head; +// io map list of inode +struct list_head list; +}; + +#define RAMFS_DEFAULT_MODE 0755 + +extern int regfs_debug; +extern const struct address_space_operations regfs_aops; +extern const struct inode_operations regfs_file_inode_operations; +extern const struct file_operations regfs_file_operations; +extern const struct super_operations regfs_ops; +extern struct kmem_cache *regfs_inode_cachep; +int regfs_supper_init (void ); + +#endif
fs/regfs/regfs_inode.h new file +32 -0
@@ -0,0 +1,32 @@ +#ifndef __REGFS_INODE_H__ +#define __REGFS_INODE_H__ + +enum res_type { +RES_TYPE_NONE =0 , +RES_TYPE_RANGE , +RES_TYPE_ITEM , +}; + +struct regfs_inode_info { +unsigned long flag; +struct inode vfs_inode; +void __iomem *base; +u64 offset; +u64 val;//for panic save +struct list_head list;//inode list +enum res_type type; +}; + +struct res_data { +enum res_type type; +u64 base; +u64 offset; +}; + +static inline struct regfs_inode_info *REGFS_I (struct inode *inode) +{ + return container_of(inode, struct regfs_inode_info, vfs_inode); +} + +#endif +
fs/regfs/supper.c new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 2/2 fs:regfs: add panic notifier callback for saving regs
register panic notifier callback for saveing regs, add a module param regfs_panic to enable the show reg info when panic.
--- fs/regfs/inode.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-)
fs/regfs/inode.c +38 -1
@@ -46,10 +46,41 @@static LIST_HEAD (regfs_head);static const struct inode_operations regfs_dir_inode_operations; -int regfs_debug; +int regfs_debug =1 ;module_param (regfs_debug, int,S_IRUGO );MODULE_PARM_DESC (regfs_debug,"enable regfs debug mode" ); +static int regfs_panic =1 ; +module_param (regfs_panic, int,S_IRUGO ); +MODULE_PARM_DESC (regfs_debug,"printk the register when panic" ); + +//save all register val when panic +static int regfs_panic_event (struct notifier_block *self, +unsigned long val,void *data) +{ +struct regfs_fs_info *fsi; +struct inode *inode, *next; + + +list_for_each_entry (fsi, ®fs_head, regfs_head) { +list_for_each_entry_safe (inode, next, &fsi->sb->s_inodes, i_sb_list) { +struct regfs_inode_info *info =REGFS_I (inode);; +//save the regs val +if (info->type ==RES_TYPE_ITEM ) { + info->val =readl_relaxed (info->base + info->offset); +if (regfs_panic) +printk ("%llx:%llx\n" , (u64)(info->base + info->offset), info->val); + } + } + } + +return NOTIFY_DONE ; +} + +static struct notifier_block regfs_panic_event_nb = { + .notifier_call = regfs_panic_event, +}; +struct inode *regfs_get_inode (struct super_block *sb,const struct inode *dir,umode_t mode,dev_t dev) {struct inode *inode =new_inode (sb);
@@ -328,6 +359,7 @@ static void init_once(void *foo)static int __init init_regfs_fs (void ) { +int ret; regfs_inode_cachep = kmem_cache_create_usercopy("regfs_inode_cache", sizeof(struct regfs_inode_info), 0,
@@ -337,11 +369,16 @@ static int __init init_regfs_fs(void)if (!regfs_inode_cachep)return -ENOMEM ; + ret =atomic_notifier_chain_register (&panic_notifier_list, ®fs_panic_event_nb); +if (ret) +pr_warn ("regfs regiter panic notifier failed\n" ); +return register_filesystem (®fs_fs_type); } static void __exit exit_regfs_fs(void) { +atomic_notifier_chain_unregister (&panic_notifier_list, ®fs_panic_event_nb);unregister_filesystem (®fs_fs_type);rcu_barrier ();kmem_cache_destroy (regfs_inode_cachep);
Signature · 1 line
1.8.3.1
Hi viro:
Through regfs is very sample and easy, but i think it is a Interest , could give some suggestions?
Regards,
zc
Quoted 721 lines · Zou Cao
register filesystem is mapping the register into file dentry, it will use the io readio to get the register val. DBT file is use to decript the register tree, you can use it as follow:
mount -t regfs -o dtb=test.dtb none /mnt test.dts: / { compatible = "hisilicon,hi6220-hikey", "hisilicon,hi6220"; #address-cells = <0x2>; #size-cells = <0x2>; model = "HiKey Development Board"; gic-v3-dist{ reg = <0x0 0x8000000 0x0 0x10000>; GIC_CTRL { offset = <0x0>; }; GICD_TYPER { offset = <0x4>; }; }; };it will create all regiter dentry file in /mnt
Signed-off-by Zou Cao <[email protected]>--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/regfs/Kconfig | 7 + fs/regfs/Makefile | 8 ++ fs/regfs/file.c | 107 +++++++++++++++ fs/regfs/inode.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/regfs/internal.h | 32 +++++ fs/regfs/regfs_inode.h | 32 +++++ fs/regfs/supper.c | 71 ++++++++++ 9 files changed, 613 insertions(+)create mode 100644
fs/regfs/Kconfig
create mode 100644fs/regfs/Makefile
create mode 100644fs/regfs/file.c
create mode 100644fs/regfs/inode.c
create mode 100644fs/regfs/internal.h
create mode 100644fs/regfs/regfs_inode.h
create mode 100644fs/regfs/supper.cfs/Kconfig +1
-0@@ -324,6 +324,7 @@ endif # NETWORK_FILESYSTEMS source "fs/nls/Kconfig" source "fs/dlm/Kconfig" source "fs/unicode/Kconfig" +source "fs/regfs/Kconfig" config IO_WQ boolfs/Makefile +1
-0@@ -136,3 +136,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/ obj-$(CONFIG_EROFS_FS) += erofs/ obj-$(CONFIG_VBOXSF_FS) += vboxsf/ obj-$(CONFIG_ZONEFS_FS) += zonefs/ +obj-$(CONFIG_REGFS_FS) += zonefs/fs/regfs/Kconfig new file +7
-0@@ -0,0 +1,7 @@ +config REGFS_FS + tristate "registers filesystem support" + depends on ARM64 + help + regfs support the read and write register of device resource by + dentry filesystem, it is more easy to support bsp debug. it also + support to printk the register val when panicfs/regfs/Makefile new file +8
-0@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +#Makefile for the linux ramfs routines. +# + +obj-y += regfs.o + +regfs-objs += inode.o file.o supper.ofs/regfs/file.c new file +107
-0@@ -0,0 +1,107 @@ +#include <linux/fs.h> +#include <linux/mm.h> +#include <linux/mpage.h> +#include <linux/writeback.h> +#include <linux/buffer_head.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/memcontrol.h> +#include <linux/iomap.h> +#include <linux/pagemap.h> +#include <linux/uio.h> +#include <linux/buffer_head.h> +#include <linux/dax.h> +#include <linux/writeback.h> +#include <linux/swap.h> +#include <linux/bio.h> +#include <linux/sched/signal.h> +#include <linux/migrate.h> + +#include "regfs_inode.h" +#include "internal.h" + +ssize_t regfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode = file->f_mapping->host; +ssize_t ret; + +inode_lock (inode); + ret =generic_write_checks (iocb, from); +if (ret >0 ) + ret =__generic_file_write_iter (iocb, from); +inode_unlock (inode); + +if (ret >0 ) + ret =generic_write_sync (iocb, ret); +return ret; +} + +static ssize_t regfs_file_read (struct file *file,char __user *buf,size_t len,loff_t *ppos) +{ +struct address_space *mapping = file->f_mapping; +struct regfs_inode_info *info =REGFS_I (mapping->host); +char str[64 ]; +unsigned long val; + + val =readl_relaxed (info->base + info->offset); + +loc_debug ("name:%s base:%p val:%lx\n" + , file->f_path.dentry->d_iname + , info->base + info->offset + , val); + +snprintf (str,64 ,"%lx" , val); + +return simple_read_from_buffer (buf, len, ppos, str,strlen (str)); +} + +static ssize_t regfs_file_write (struct file *file,const char __user *buf,size_t len,loff_t *ppos) +{ +struct address_space *mapping = file->f_mapping; +struct regfs_inode_info *info =REGFS_I (mapping->host); +char str[67 ]; +unsigned long val =0 ; +loff_t pos = *ppos; +size_t res; + +if (pos <0 ) +return -EINVAL ; +if (pos >= len || len >66 ) +return 0 ; + + res =copy_from_user (str, buf, len); +if (res) +return -EFAULT ; + str[len] =0 ; + +if (kstrtoul (str,16 , &val) <0 ) +return -EINVAL ; + +writel_relaxed (val, info->base + info->offset); + +loc_debug ("name:%s base:%p val:%lx\n" + , file->f_path.dentry->d_iname + , info->base + info->offset + , val); + +return len; +} + +const struct file_operations regfs_file_operations = { + .read = regfs_file_read, + .write = regfs_file_write, +}; + +const struct inode_operations regfs_file_inode_operations = { + .setattr = simple_setattr, + .getattr = simple_getattr, +}; + +const struct address_space_operations regfs_aops = { + .readpage = simple_readpage, + .write_begin = simple_write_begin, + .write_end = simple_write_end, + .set_page_dirty = __set_page_dirty_buffers, +}; +fs/regfs/inode.c new file +354
-0@@ -0,0 +1,354 @@ +/* +* Resizable simple ram filesystem for Linux. +* +* Copyright (C) 2000 Linus Torvalds. +* 2000 Transmeta Corp. +* +* Usage limits added by David Gibson, Linuxcare Australia. +* This file is released under the GPL. +*/ + +/* +* NOTE! This filesystem is probably most useful +* not as a real filesystem, but as an example of +* how virtual filesystems can be written. +* +* It doesn't get much simpler than this. Consider +* that this file implements the full semantics of +* a POSIX-compliant read-write filesystem. +* +* Note in particular how the filesystem does not +* need to implement any data structures of its own +* to keep track of the virtual data: using the VFS +* caches is sufficient. +*/ + +#include <linux/fs.h> +#include <linux/pagemap.h> +#include <linux/highmem.h> +#include <linux/time.h> +#include <linux/init.h> +#include <linux/string.h> +#include <linux/backing-dev.h> +#include <linux/sched.h> +#include <linux/parser.h> +#include <linux/magic.h> +#include <linux/slab.h> +#include <linux/kernel.h> +#include <linux/of.h> +#include <linux/of_fdt.h> +#include <linux/libfdt.h> +#include <asm/uaccess.h> +#include <linux/module.h> +#include "regfs_inode.h" +#include "internal.h" + +static LIST_HEAD (regfs_head); + +static const struct inode_operations regfs_dir_inode_operations; +int regfs_debug; +module_param (regfs_debug, int,S_IRUGO ); +MODULE_PARM_DESC (regfs_debug,"enable regfs debug mode" ); + +struct inode *regfs_get_inode (struct super_block *sb,const struct inode *dir,umode_t mode,dev_t dev) +{ +struct inode *inode =new_inode (sb); + +if (inode) { + inode->i_ino =get_next_ino (); +inode_init_owner (inode, dir, mode); + inode->i_mapping->a_ops = ®fs_aops; +//inode->i_mapping->backing_dev_info = ®fs_backing_dev_info; +mapping_set_gfp_mask (inode->i_mapping,GFP_HIGHUSER ); +mapping_set_unevictable (inode->i_mapping); + inode->i_atime = inode->i_mtime = inode->i_ctime =current_time (inode); +switch (mode &S_IFMT ) { +default : +init_special_inode (inode, mode, dev); +break ; +case S_IFREG : + inode->i_op = ®fs_file_inode_operations; + inode->i_fop = ®fs_file_operations; +break ; +case S_IFDIR : + inode->i_op = ®fs_dir_inode_operations; + inode->i_fop = &simple_dir_operations; + +/* directory inodes start off with i_nlink == 2 (for "." entry) */ +inc_nlink (inode); +break ; +case S_IFLNK : + inode->i_op = &page_symlink_inode_operations; +break ; + } + } + +return inode; +} + +static const struct inode_operations regfs_dir_inode_operations = { + .lookup = simple_lookup, +}; + +static struct dentry *new_dentry_create (struct super_block *sb,struct dentry *parent, +const char *name,bool is_dir,struct res_data *res) +{ +struct dentry *dentry; +struct inode *inode; +struct regfs_inode_info *ei; +struct regfs_fs_info *fsi = sb->s_fs_info; + + dentry =d_alloc_name (parent, name); +if (!dentry) +return NULL ; + + inode =new_inode (sb); +if (!inode) + goto out; + + ei =REGFS_I (inode); + inode->i_ino =get_next_ino ();; + inode->i_mtime = inode->i_atime = inode->i_ctime =current_time (inode); + inode->i_uid =GLOBAL_ROOT_UID ; + inode->i_gid =GLOBAL_ROOT_GID ; +if (is_dir) { + inode->i_mode =S_IFDIR |S_IRUGO |S_IWUSR ; + inode->i_op = ®fs_dir_inode_operations; + inode->i_fop = &simple_dir_operations; +list_add (&ei->list, &fsi->list); + }else { + inode->i_mode =S_IFREG |S_IRUGO |S_IWUSR ; + inode->i_op = ®fs_file_inode_operations; + inode->i_fop = ®fs_file_operations; +inc_nlink (inode); + } + ei->base = (void *)res->base; + ei->offset = res->offset; + ei->type = res->type; + +d_add (dentry, inode); + +loc_debug ("new dentry io base:%llx offset:%llx ei:%llx\n" , (u64 )ei->base, (u64 )ei->offset, (u64 )ei); +return dentry; +out: +dput (dentry); +return NULL ; +} + +static void node_transfer_dentry (struct super_block *sb) +{ +struct regfs_fs_info *fsi = sb->s_fs_info; +void *blob = fsi->dtb_buf; +const char *pathp; +int node_offset, depth =-1 ; +struct dentry *parent =NULL ; +u64 parent_base; + +for (node_offset =fdt_next_node (blob,-1 , &depth); + node_offset >=0 && depth >=0 ; + node_offset =fdt_next_node (blob, node_offset, &depth)) { + +const struct fdt_property *prop; +struct res_data res; + + pathp =fdt_get_name (blob, node_offset,NULL ); + prop = (void *)fdt_getprop (blob, node_offset,"reg" ,NULL ); + +if (prop) { +unsigned long phys; + + phys =fdt32_to_cpu (((const __be32 *)prop)[1 ]); + res.type =RES_TYPE_RANGE ; + res.offset =fdt32_to_cpu (((const __be32 *)prop)[3 ]); + res.base = (u64 )ioremap (phys, res.offset); + +if (!res.base) { + parent =NULL ; + parent_base =0 ; +continue ; + } + +loc_debug ("%s reg:%lx size:%lx map:%llx\n\n" , pathp + , (unsigned long )fdt32_to_cpu (((const __be32 *)prop)[1 ]) + , (unsigned long )fdt32_to_cpu (((const __be32 *)prop)[3 ]) + , (u64 )res.base); + + parent =new_dentry_create (sb, sb->s_root, (const char *)pathp, true, &res); + parent_base = res.base; + + }else { +// parent dentry is create failed, igonre all child dentry +if (!parent) +continue ; + + prop = (void *)fdt_getprop (blob, node_offset,"offset" ,NULL ); +if (prop) { + + res.offset =fdt32_to_cpu (*(const __be32 *)prop); + res.base = parent_base; + res.type =RES_TYPE_ITEM ; + +new_dentry_create (sb, parent, (const char *) pathp, false, &res); +loc_debug ("%s offset:%lx\n" , pathp, (unsigned long )fdt32_to_cpu (*(const __be32 *)prop)); + } + } + } +} + +static int parse_options (char *options,struct super_block *sb) +{ +char *p; +int ret = -EINVAL ; +struct regfs_fs_info *fsi; +size_t msize =INT_MAX ; + + fsi = sb->s_fs_info; + +if (!options) +return -EINVAL ; + +while ((p =strsep (&options,"," )) !=NULL ) { +char *name, *name_val; + + name =strsep (&p,"=" ); +if (name ==NULL ) + goto failed; + + name_val =strsep (&p,"=" ); +if (name_val ==NULL ) + goto failed; + +//get resource address +if (!strcmp (name,"dtb" )) { + ret =kernel_read_file_from_path (name_val, &fsi->dtb_buf, &fsi->dtb_len, msize,READING_UNKNOWN ); +if (ret) { +pr_err ("load %s failed\n" , name_val); + goto failed; + } + }else + goto failed; + }; + +return 0 ; + +failed: +return ret; +} + +int regfs_fill_super (struct super_block *sb,void *data,int silent) +{ +struct regfs_fs_info *fsi; +struct inode *inode; +int err; + + fsi =kzalloc (sizeof (struct regfs_fs_info ),GFP_KERNEL ); +if (!fsi) +return -ENOMEM ; + + sb->s_fs_info = fsi; + fsi->sb = sb; + + err =parse_options ((char *)data, sb); +if (err) + goto out; + + sb->s_maxbytes =MAX_LFS_FILESIZE ; + sb->s_blocksize =PAGE_SIZE ; + sb->s_blocksize_bits =PAGE_SHIFT ; + sb->s_magic =RAMFS_MAGIC ; + sb->s_op = ®fs_ops; + sb->s_time_gran =1 ; + + inode =regfs_get_inode (sb,NULL ,S_IFDIR ,0 ); + sb->s_root =d_make_root (inode); +if (!sb->s_root) + goto out; + +INIT_LIST_HEAD (&fsi->list); +INIT_LIST_HEAD (&fsi->regfs_head); +list_add (&fsi->regfs_head, ®fs_head); + +return 0 ; + +out: +if (fsi) +kfree (fsi); + +return err; +} + +struct dentry *regfs_mount (struct file_system_type *fs_type, +int flags,const char *dev_name,void *data) +{ +struct dentry *root_dentry; +struct super_block *sb; + + root_dentry =mount_nodev (fs_type, flags, data, regfs_fill_super); + + sb = root_dentry->d_sb; + +if (sb->s_root) { +node_transfer_dentry (sb); + }else +return NULL ; + +return root_dentry; +} + +static void regfs_kill_sb (struct super_block *sb) +{ +struct regfs_fs_info *fsi = sb->s_fs_info; +struct regfs_inode_info *info_res; + +list_for_each_entry (info_res, &fsi->list, list) +iounmap (info_res->base); + +if (fsi) { +if (fsi->dtb_buf) +vfree (fsi->dtb_buf); +list_del (&fsi->regfs_head); +kfree (sb->s_fs_info); + } +kill_litter_super (sb); +} + +static struct file_system_type regfs_fs_type = { + .name ="regfs" , + .mount = regfs_mount, + .kill_sb = regfs_kill_sb, + .fs_flags =FS_USERNS_MOUNT , +}; + +static void init_once (void *foo) +{ +struct regfs_inode_info *ei = (struct regfs_inode_info *) foo; + +inode_init_once (&ei->vfs_inode); +} + +static int __init init_regfs_fs (void ) +{ + + regfs_inode_cachep =kmem_cache_create_usercopy ("regfs_inode_cache" , +sizeof (struct regfs_inode_info ),0 , + (SLAB_RECLAIM_ACCOUNT |SLAB_MEM_SPREAD |SLAB_ACCOUNT ), +0 ,0 , init_once); + +if (!regfs_inode_cachep) +return -ENOMEM ; + +return register_filesystem (®fs_fs_type); +} + +static void __exit exit_regfs_fs(void) +{ +unregister_filesystem (®fs_fs_type); +rcu_barrier (); +kmem_cache_destroy (regfs_inode_cachep); +} + +module_init (init_regfs_fs); +module_exit (exit_regfs_fs); + +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Zou Cao<[email protected]>" );fs/regfs/internal.h new file +32
-0@@ -0,0 +1,32 @@ +#ifndef __INTERNAL_H__ +#define __INTERNAL_H__ + +#define loc_debug (fmt, ...) \ + do { \ + if (regfs_debug) \ + printk(fmt, ##__VA_ARGS__); \ + } while (0) + +struct regfs_fs_info { +struct super_block *sb; +void *dtb_buf; +u64 dtb_len; +u64 iomem; +u64 size; +// supper ifs list +struct list_head regfs_head; +// io map list of inode +struct list_head list; +}; + +#define RAMFS_DEFAULT_MODE 0755 + +extern int regfs_debug; +extern const struct address_space_operations regfs_aops; +extern const struct inode_operations regfs_file_inode_operations; +extern const struct file_operations regfs_file_operations; +extern const struct super_operations regfs_ops; +extern struct kmem_cache *regfs_inode_cachep; +int regfs_supper_init (void ); + +#endif fs/regfs/regfs_inode.h new file +32
-0@@ -0,0 +1,32 @@ +#ifndef __REGFS_INODE_H__ +#define __REGFS_INODE_H__ + +enum res_type { +RES_TYPE_NONE =0 , +RES_TYPE_RANGE , +RES_TYPE_ITEM , +}; + +struct regfs_inode_info { +unsigned long flag; +struct inode vfs_inode; +void __iomem *base; +u64 offset; +u64 val;//for panic save +struct list_head list;//inode list +enum res_type type; +}; + +struct res_data { +enum res_type type; +u64 base; +u64 offset; +}; + +static inline struct regfs_inode_info *REGFS_I (struct inode *inode) +{ + return container_of(inode, struct regfs_inode_info, vfs_inode); +} + +#endif +fs/regfs/supper.c new file +71
-0@@ -0,0 +1,71 @@ +#include <linux/fs.h> +#include <linux/pagemap.h> +#include <linux/highmem.h> +#include <linux/time.h> +#include <linux/init.h> +#include <linux/string.h> +#include <linux/backing-dev.h> +#include <linux/sched.h> +#include <linux/parser.h> +#include <linux/magic.h> +#include <linux/slab.h> +#include <linux/kernel.h> +#include <linux/uaccess.h> +#include <linux/module.h> +#include <linux/iversion.h> +#include "regfs_inode.h" +#include "internal.h" + +struct kmem_cache *regfs_inode_cachep; + +/* +* Display the mount options in /proc/mounts. +*/ + +static struct inode *regfs_alloc_inode (struct super_block *sb) +{ +struct regfs_inode_info *ei; + + ei =kmem_cache_alloc (regfs_inode_cachep,GFP_NOFS ); +if (!ei) +return NULL ; + +inode_set_iversion (&ei->vfs_inode,1 ); + ei->type =RES_TYPE_NONE ; + +return &ei->vfs_inode; +} + +static void regfs_i_callback (struct rcu_head *head) +{ + struct inode *inode = container_of(head, struct inode, i_rcu); + +kmem_cache_free (regfs_inode_cachep,REGFS_I (inode)); +} + +static void regfs_destroy_inode (struct inode *inode) +{ +call_rcu (&inode->i_rcu, regfs_i_callback); +} + +static int regfs_write_inode (struct inode *inode,struct writeback_control *wbc) +{ +return 0 ; +} + +static int regfs_drop_inode (struct inode *inode) +{ +return generic_drop_inode (inode); +} + +const struct super_operations regfs_ops = { + .alloc_inode = regfs_alloc_inode, + .destroy_inode = regfs_destroy_inode, + .write_inode = regfs_write_inode, + .drop_inode = regfs_drop_inode, +}; + +int regfs_supper_init (void ) +{ +return 0 ; +}
pending ?
Quoted 750 lines · zc
Hi viro:
Through regfs is very sample and easy, but i think it is a Interest , could give some suggestions?
Regards,
zc
Quoted 740 lines · Zou Cao
register filesystem is mapping the register into file dentry, it will use the io readio to get the register val. DBT file is use to decript the register tree, you can use it as follow:
mount -t regfs -o dtb=test.dtb none /mnt
test.dts:
/ {compatible = "hisilicon,hi6220-hikey", "hisilicon,hi6220"; #address-cells = <
0x2>;
#size-cells = <0x2>;
model = "HiKey Development Board";gic-v3-dist{
reg = <0x00x80000000x00x10000>;
GIC_CTRL{
offset = <0x0>;
};
GICD_TYPER{
offset = <0x4>;
};
};
};it will create all regiter dentry file in /mnt
Signed-off-by Zou Cao <[email protected]>---
fs/Kconfig | 1 +
fs/Makefile | 1 +
fs/regfs/Kconfig | 7 +
fs/regfs/Makefile | 8 ++
fs/regfs/file.c | 107 +++++++++++++++
fs/regfs/inode.c | 354
+++++++++++++++++++++++++++++++++++++++++++++++++
fs/regfs/internal.h | 32 +++++
fs/regfs/regfs_inode.h| 32 +++++
fs/regfs/supper.c | 71 ++++++++++9 files changed, 613 insertions(+)create mode 100644
fs/regfs/Kconfig
create mode 100644fs/regfs/Makefile
create mode 100644fs/regfs/file.c
create mode 100644fs/regfs/inode.c
create mode 100644fs/regfs/internal.h
create mode 100644fs/regfs/regfs_inode.h
create mode 100644fs/regfs/supper.cfs/Kconfig +0
-0@@ -324,6 +324,7 @@ endif # NETWORK_FILESYSTEMSsource "
fs/nls/Kconfig"
source "fs/dlm/Kconfig"
source "fs/unicode/Kconfig"
+source "fs/regfs/Kconfig"
configIO_WQ
boolfs/Makefile +0
-0@@ -136,3 +136,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/obj-$(CONFIG_EROFS_FS) += erofs/
obj-$(CONFIG_VBOXSF_FS) += vboxsf/
obj-$(CONFIG_ZONEFS_FS) += zonefs/fs/Makefile +1
-0+obj-$(CONFIG_REGFS_FS) += zonefs/fs/regfs/Kconfig new file +7
-0@@ -0,0 +1,7 @@ +config REGFS_FS + tristate "registers filesystem support" + depends on ARM64 + help + regfs support the read and write register of device resource by + dentry filesystem, it is more easy to support bsp debug. it also + support to printk the register val when panicfs/regfs/Makefile new file +8
-0@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +#Makefile for the linux ramfs routines. +# + +obj-y += regfs.o + +regfs-objs += inode.o file.o supper.ofs/regfs/file.c new file +23
-0@@ -0,0 +1,107 @@ +#include <linux/fs.h> +#include <linux/mm.h> +#include <linux/mpage.h> +#include <linux/writeback.h> +#include <linux/buffer_head.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/memcontrol.h> +#include <linux/iomap.h> +#include <linux/pagemap.h> +#include <linux/uio.h> +#include <linux/buffer_head.h> +#include <linux/dax.h> +#include <linux/writeback.h> +#include <linux/swap.h> +#include <linux/bio.h> +#include <linux/sched/signal.h> +#include <linux/migrate.h> + +#include "regfs_inode.h" +#include "internal.h" + +ssize_t regfs_file_write_iter(struct kiocb *iocb, struct iov_iter*from)
fs/regfs/file.c new file +17
-0+{ + struct file *file = iocb->ki_filp; + struct inode *inode = file->f_mapping->host; + ssize_t ret; + + inode_lock(inode); + ret = generic_write_checks(iocb, from); + if (ret > 0) + ret = __generic_file_write_iter(iocb, from); + inode_unlock(inode); + + if (ret > 0) + ret = generic_write_sync(iocb, ret); + return ret; +} + +static ssize_t regfs_file_read(struct file *file, char __user *buf,
size_tlen,loff_t*ppos)fs/regfs/file.c new file +19
-0+{ + struct address_space *mapping = file->f_mapping; + struct regfs_inode_info *info = REGFS_I(mapping->host); + char str[64]; + unsigned long val; + + val = readl_relaxed(info->base + info->offset); + + loc_debug("name:%s base:%p val:%lx\n" + , file->f_path.dentry->d_iname + , info->base + info->offset + , val); + + snprintf(str, 64, "%lx", val); + + return simple_read_from_buffer(buf, len, ppos, str, strlen(str)); +} + +static ssize_t regfs_file_write(struct file *file, const char __user*buf,
size_tlen,loff_t*ppos)fs/regfs/file.c new file +48
-0+{ + struct address_space *mapping = file->f_mapping; + struct regfs_inode_info *info = REGFS_I(mapping->host); + char str[67]; + unsigned long val = 0; + loff_t pos = *ppos; + size_t res; + + if (pos < 0) + return -EINVAL; + if (pos >= len || len > 66) + return 0; + + res = copy_from_user(str, buf, len); + if (res) + return -EFAULT; + str[len] = 0; + + if (kstrtoul(str, 16, &val) < 0) + return -EINVAL; + + writel_relaxed(val, info->base + info->offset); + + loc_debug("name:%s base:%p val:%lx\n" + , file->f_path.dentry->d_iname + , info->base + info->offset + , val); + + return len; +} + +const struct file_operations regfs_file_operations = { + .read = regfs_file_read, + .write = regfs_file_write, +}; + +const struct inode_operations regfs_file_inode_operations = { + .setattr = simple_setattr, + .getattr = simple_getattr, +}; + +const struct address_space_operations regfs_aops = { + .readpage = simple_readpage, + .write_begin = simple_write_begin, + .write_end = simple_write_end, + .set_page_dirty = __set_page_dirty_buffers, +}; +fs/regfs/inode.c new file +53
-0@@ -0,0 +1,354 @@ +/* +* Resizable simple ram filesystem for Linux. +* +* Copyright (C) 2000 Linus Torvalds. +* 2000 Transmeta Corp. +* +* Usage limits added by David Gibson, Linuxcare Australia. +* This file is released under the GPL. +*/ + +/* +* NOTE! This filesystem is probably most useful +* not as a real filesystem, but as an example of +* how virtual filesystems can be written. +* +* It doesn't get much simpler than this. Consider +* that this file implements the full semantics of +* a POSIX-compliant read-write filesystem. +* +* Note in particular how the filesystem does not +* need to implement any data structures of its own +* to keep track of the virtual data: using the VFS +* caches is sufficient. +*/ + +#include <linux/fs.h> +#include <linux/pagemap.h> +#include <linux/highmem.h> +#include <linux/time.h> +#include <linux/init.h> +#include <linux/string.h> +#include <linux/backing-dev.h> +#include <linux/sched.h> +#include <linux/parser.h> +#include <linux/magic.h> +#include <linux/slab.h> +#include <linux/kernel.h> +#include <linux/of.h> +#include <linux/of_fdt.h> +#include <linux/libfdt.h> +#include <asm/uaccess.h> +#include <linux/module.h> +#include "regfs_inode.h" +#include "internal.h" + +static LIST_HEAD (regfs_head); + +static const struct inode_operations regfs_dir_inode_operations; +int regfs_debug; +module_param (regfs_debug, int,S_IRUGO ); +MODULE_PARM_DESC (regfs_debug,"enable regfs debug mode" ); + +struct inode *regfs_get_inode(struct super_block *sb, const structinode *dir,
umode_tmode,dev_tdev)fs/regfs/inode.c new file +11
-0+{ + struct inode *inode = new_inode(sb); + + if (inode) { + inode->i_ino = get_next_ino(); + inode_init_owner(inode, dir, mode); + inode->i_mapping->a_ops = ®fs_aops; + //inode->i_mapping->backing_dev_info = ®fs_backing_dev_info; + mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); + mapping_set_unevictable(inode->i_mapping); + inode->i_atime = inode->i_mtime = inode->i_ctime =
current_time(inode);
+ switch (mode &S_IFMT) {
+ default:
+init_special_inode(inode, mode, dev);
+ break;
+ caseS_IFREG:
+inode->i_op=®fs_file_inode_operations;
+inode->i_fop=®fs_file_operations;
+ break;
+ caseS_IFDIR:
+inode->i_op=®fs_dir_inode_operations;
+inode->i_fop=&simple_dir_operations;
+
+ /* directory inodes start off withi_nlink== 2 (for "." entry) */
+inc_nlink(inode);
+ break;
+ caseS_IFLNK:
+inode->i_op=&page_symlink_inode_operations; + break;
+ }
+ }fs/regfs/inode.c new file +9
-0+ + return inode; +} + +static const struct inode_operations regfs_dir_inode_operations = { + .lookup = simple_lookup, +}; + +static struct dentry *new_dentry_create(struct super_block *sb,struct dentry *parent,
+ const char *name, boolis_dir, structres_data*res)fs/regfs/inode.c new file +17
-0+{ + struct dentry *dentry; + struct inode *inode; + struct regfs_inode_info *ei; + struct regfs_fs_info *fsi = sb->s_fs_info; + + dentry = d_alloc_name(parent, name); + if (!dentry) + return NULL; + + inode = new_inode(sb); + if (!inode) + goto out; + + ei = REGFS_I(inode); + inode->i_ino = get_next_ino();; + inode->i_mtime = inode->i_atime = inode->i_ctime =
current_time(inode);
+inode->i_uid=GLOBAL_ROOT_UID;
+inode->i_gid=GLOBAL_ROOT_GID;
+ if (is_dir) {
+inode->i_mode=S_IFDIR|S_IRUGO|S_IWUSR; +inode->i_op=®fs_dir_inode_operations; +inode->i_fop=&simple_dir_operations; +list_add(&ei->list, &fsi->list);
+ } else {
+inode->i_mode=S_IFREG|S_IRUGO|S_IWUSR; +inode->i_op=®fs_file_inode_operations; +inode->i_fop=®fs_file_operations; +inc_nlink(inode);
+ }
+ei->base= (void *)res->base;
+ei->offset=res->offset;
+ei->type=res->type;fs/regfs/inode.c new file +4
-0+ + d_add(dentry, inode); + + loc_debug("new dentry io base:%llx offset:%llx ei:%llx\n",(u64)ei->base, (u64)ei->offset, (u64)ei);
+ return dentry;fs/regfs/inode.c new file +40
-0+out: + dput(dentry); + return NULL; +} + +static void node_transfer_dentry(struct super_block *sb) +{ + struct regfs_fs_info *fsi = sb->s_fs_info; + void *blob = fsi->dtb_buf; + const char *pathp; + int node_offset, depth = -1; + struct dentry *parent = NULL; + u64 parent_base; + + for (node_offset = fdt_next_node(blob, -1, &depth); + node_offset >= 0 && depth >= 0; + node_offset = fdt_next_node(blob, node_offset, &depth)) { + + const struct fdt_property *prop; + struct res_data res; + + pathp = fdt_get_name(blob, node_offset, NULL); + prop = (void *)fdt_getprop(blob, node_offset, "reg", NULL); + + if (prop) { + unsigned long phys; + + phys = fdt32_to_cpu(((const __be32 *)prop)[1]); + res.type = RES_TYPE_RANGE; + res.offset = fdt32_to_cpu(((const __be32 *)prop)[3]); + res.base = (u64)ioremap(phys, res.offset); + + if (!res.base) { + parent = NULL; + parent_base = 0; + continue; + } + + loc_debug("%s reg:%lx size:%lx map:%llx\n\n", pathp + , (unsigned long) fdt32_to_cpu(((const __be32*)prop)[1])
+ , (unsigned long)fdt32_to_cpu(((const __be32 *)prop)[3])
+ , (u64)res.base);
+
+ parent =new_dentry_create(sb, sb->s_root, (const char *)pathp, true, &res);
+parent_base= res.base;fs/regfs/inode.c new file +7
-0+ + } else { + // parent dentry is create failed, igonre all child dentry + if (!parent) + continue; + + prop = (void *)fdt_getprop(blob, node_offset, "offset",NULL);
+ if (prop) {fs/regfs/inode.c new file +6
-0+ + res.offset = fdt32_to_cpu(*(const __be32 *)prop); + res.base = parent_base; + res.type = RES_TYPE_ITEM; + + new_dentry_create(sb, parent, (const char *) pathp,false, &res);
+loc_debug("%s offset:%lx\n", pathp, (unsigned long)fdt32_to_cpu(*(const __be32 *)prop));
+ }
+ }
+ }fs/regfs/inode.c new file +28
-0+} + +static int parse_options (char *options,struct super_block *sb) +{ + char *p; + int ret = -EINVAL; + struct regfs_fs_info *fsi; + size_t msize = INT_MAX; + + fsi = sb->s_fs_info; + + if (!options) + return -EINVAL; + + while ((p = strsep(&options, ",")) != NULL) { + char *name, *name_val; + + name = strsep(&p, "="); + if (name == NULL) + goto failed; + + name_val = strsep(&p, "="); + if (name_val == NULL) + goto failed; + + //get resource address + if (!strcmp(name, "dtb")) { + ret = kernel_read_file_from_path(name_val,
&fsi->dtb_buf,&fsi->dtb_len, msize,READING_UNKNOWN); + if (ret) {
+pr_err("load %s failed\n", name_val); + goto failed;
+ }
+ } else
+ goto failed;
+ };fs/regfs/inode.c new file +102
-0+ + return 0; + +failed: + return ret; +} + +int regfs_fill_super (struct super_block *sb,void *data,int silent) +{ + struct regfs_fs_info *fsi; + struct inode *inode; + int err; + + fsi = kzalloc(sizeof(struct regfs_fs_info), GFP_KERNEL); + if (!fsi) + return -ENOMEM; + + sb->s_fs_info = fsi; + fsi->sb = sb; + + err = parse_options((char *)data, sb); + if (err) + goto out; + + sb->s_maxbytes = MAX_LFS_FILESIZE; + sb->s_blocksize = PAGE_SIZE; + sb->s_blocksize_bits = PAGE_SHIFT; + sb->s_magic = RAMFS_MAGIC; + sb->s_op = ®fs_ops; + sb->s_time_gran = 1; + + inode = regfs_get_inode(sb, NULL, S_IFDIR, 0); + sb->s_root = d_make_root(inode); + if (!sb->s_root) + goto out; + + INIT_LIST_HEAD(&fsi->list); + INIT_LIST_HEAD(&fsi->regfs_head); + list_add(&fsi->regfs_head, ®fs_head); + + return 0; + +out: + if (fsi) + kfree(fsi); + + return err; +} + +struct dentry *regfs_mount (struct file_system_type *fs_type, + int flags, const char *dev_name, void *data) +{ + struct dentry *root_dentry; + struct super_block *sb; + + root_dentry = mount_nodev(fs_type, flags, data, regfs_fill_super); + + sb = root_dentry->d_sb; + + if (sb->s_root) { + node_transfer_dentry(sb); + } else + return NULL; + + return root_dentry; +} + +static void regfs_kill_sb (struct super_block *sb) +{ + struct regfs_fs_info *fsi = sb->s_fs_info; + struct regfs_inode_info *info_res; + + list_for_each_entry(info_res, &fsi->list, list) + iounmap(info_res->base); + + if (fsi) { + if (fsi->dtb_buf) + vfree(fsi->dtb_buf); + list_del(&fsi->regfs_head); + kfree(sb->s_fs_info); + } + kill_litter_super(sb); +} + +static struct file_system_type regfs_fs_type = { + .name = "regfs", + .mount = regfs_mount, + .kill_sb = regfs_kill_sb, + .fs_flags = FS_USERNS_MOUNT, +}; + +static void init_once (void *foo) +{ + struct regfs_inode_info *ei = (struct regfs_inode_info *) foo; + + inode_init_once(&ei->vfs_inode); +} + +static int __init init_regfs_fs (void ) +{ + + regfs_inode_cachep =kmem_cache_create_usercopy("regfs_inode_cache",
+sizeof(struct regfs_inode_info), 0, + (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|SLAB_ACCOUNT),
+ 0, 0,init_once);fs/regfs/inode.c new file +19
-0+ + if (!regfs_inode_cachep) + return -ENOMEM; + + return register_filesystem(®fs_fs_type); +} + +static void __exit exit_regfs_fs(void) +{ + unregister_filesystem(®fs_fs_type); + rcu_barrier(); + kmem_cache_destroy(regfs_inode_cachep); +} + +module_init (init_regfs_fs); +module_exit (exit_regfs_fs); + +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Zou Cao<[email protected]>" );fs/regfs/internal.h new file +32
-0@@ -0,0 +1,32 @@ +#ifndef __INTERNAL_H__ +#define __INTERNAL_H__ + +#define loc_debug (fmt, ...) \ + do { \ + if (regfs_debug) \ + printk(fmt, ##__VA_ARGS__); \ + } while (0) + +struct regfs_fs_info { + struct super_block *sb; + void *dtb_buf; + u64 dtb_len; + u64 iomem; + u64 size; + // supper ifs list + struct list_head regfs_head; + // io map list of inode + struct list_head list; +}; + +#define RAMFS_DEFAULT_MODE 0755 + +extern int regfs_debug; +extern const struct address_space_operations regfs_aops; +extern const struct inode_operations regfs_file_inode_operations; +extern const struct file_operations regfs_file_operations; +extern const struct super_operations regfs_ops; +extern struct kmem_cache *regfs_inode_cachep; +int regfs_supper_init (void ); + +#endif fs/regfs/regfs_inode.h new file +32
-0@@ -0,0 +1,32 @@ +#ifndef __REGFS_INODE_H__ +#define __REGFS_INODE_H__ + +enum res_type { + RES_TYPE_NONE = 0, + RES_TYPE_RANGE, + RES_TYPE_ITEM, +}; + +struct regfs_inode_info { + unsigned long flag; + struct inode vfs_inode; + void __iomem *base; + u64 offset; + u64 val; //for panic save + struct list_head list; //inode list + enum res_type type; +}; + +struct res_data { + enum res_type type; + u64 base; + u64 offset; +}; + +static inline struct regfs_inode_info *REGFS_I (struct inode *inode) +{ + return container_of(inode, struct regfs_inode_info, vfs_inode); +} + +#endif +fs/regfs/supper.c new file +51
-0@@ -0,0 +1,71 @@ +#include <linux/fs.h> +#include <linux/pagemap.h> +#include <linux/highmem.h> +#include <linux/time.h> +#include <linux/init.h> +#include <linux/string.h> +#include <linux/backing-dev.h> +#include <linux/sched.h> +#include <linux/parser.h> +#include <linux/magic.h> +#include <linux/slab.h> +#include <linux/kernel.h> +#include <linux/uaccess.h> +#include <linux/module.h> +#include <linux/iversion.h> +#include "regfs_inode.h" +#include "internal.h" + +struct kmem_cache *regfs_inode_cachep; + +/* +* Display the mount options in /proc/mounts. +*/ + +static struct inode *regfs_alloc_inode (struct super_block *sb) +{ + struct regfs_inode_info *ei; + + ei = kmem_cache_alloc(regfs_inode_cachep, GFP_NOFS); + if (!ei) + return NULL; + + inode_set_iversion(&ei->vfs_inode, 1); + ei->type = RES_TYPE_NONE; + + return &ei->vfs_inode; +} + +static void regfs_i_callback (struct rcu_head *head) +{ + struct inode *inode = container_of(head, struct inode, i_rcu); + + kmem_cache_free(regfs_inode_cachep, REGFS_I(inode)); +} + +static void regfs_destroy_inode (struct inode *inode) +{ + call_rcu(&inode->i_rcu, regfs_i_callback); +} + +static int regfs_write_inode(struct inode *inode, struct
writeback_control*wbc)fs/regfs/supper.c new file +20
-0+{ + return 0; +} + +static int regfs_drop_inode (struct inode *inode) +{ + return generic_drop_inode(inode); +} + +const struct super_operations regfs_ops = { + .alloc_inode = regfs_alloc_inode, + .destroy_inode = regfs_destroy_inode, + .write_inode = regfs_write_inode, + .drop_inode = regfs_drop_inode, +}; + +int regfs_supper_init (void ) +{ + return 0; +}
A message that is not in the archive (sent off-list or not yet received): [email protected]
Al Viro
maintainerQuoted 3 lines · zc
Hi viro:
have time for reviewing this?
Start with removing unused boilerplate. When quite a chunk of the codebase is simply never used, filtering _that_ out is on the author, not reviewers.
Quoted 7 lines · Al Viro
Quoted 3 lines · zc
Hi viro:
have time for reviewing this?
Start with removing unused boilerplate. When quite a chunk of the codebase is simply never used, filtering _that_ out is on the author, not reviewers.
sorry, i will remove these unused boilerplat.
Regsards,
zoucao
Al Viro
maintainerQuoted 6 lines · Zou Cao
fs/regfs/file.c new file +6
-0+ssize_t regfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode = file->f_mapping->host; +ssize_t ret; +
fs/regfs/file.c line 38
Quoted 10 lines
fs/regfs/file.c new file +10
-0+inode_lock (inode); + ret =generic_write_checks (iocb, from); +if (ret >0 ) + ret =__generic_file_write_iter (iocb, from); +inode_unlock (inode); + +if (ret >0 ) + ret =generic_write_sync (iocb, ret); +return ret; +}
Huh? How is that different from generic_file_write_iter()? And who's using it, anyway?
fs/regfs/file.c line 71
Quoted 10 lines
fs/regfs/file.c new file +10
-0+struct regfs_inode_info *info =REGFS_I (mapping->host); +char str[67 ]; +unsigned long val =0 ; +loff_t pos = *ppos; +size_t res; + +if (pos <0 ) +return -EINVAL ; +if (pos >= len || len >66 ) +return 0 ;
This is completely bogus. "If current position is greater than the length of string we are asking to write, quietly return 0"?
fs/regfs/file.c line 79
Quoted 7 lines
fs/regfs/file.c new file +7
-0+ res =copy_from_user (str, buf, len); +if (res) +return -EFAULT ; + str[len] =0 ; + +if (kstrtoul (str,16 , &val) <0 ) +return -EINVAL ;
Where does 67 come from? If you are expecting a hexadecimal representation of a unsigned long on arm64, you should have at most 16 digits. 67 looks rather odd...
fs/regfs/file.c line 81
Quoted 1 line
fs/regfs/file.c new file +1
-0+writel_relaxed (val, info->base + info->offset);
... and you are promptly discarding the upper 32 bits, since writel_relaxed() takes u32:
((void)__raw_writel((__force u32)cpu_to_le32(v),(c)))
is going to truncate to 32bit, no matter what. Quietly truncate, at that...
fs/regfs/file.c line 106
Quoted 6 lines
fs/regfs/file.c new file +6
-0+const struct address_space_operations regfs_aops = { + .readpage = simple_readpage, + .write_begin = simple_write_begin, + .write_end = simple_write_end, + .set_page_dirty = __set_page_dirty_buffers, +};
Again, huh? What would use the page cache there, anyway?
Quoted 1 line
+static
LIST_HEAD(regfs_head);
Protected by...?
Quoted 30 lines
fs/regfs/inode.c new file +30
-0+static const struct inode_operations regfs_dir_inode_operations; +int regfs_debug; +module_param (regfs_debug, int,S_IRUGO ); +MODULE_PARM_DESC (regfs_debug,"enable regfs debug mode" ); + +struct inode *regfs_get_inode (struct super_block *sb,const struct inode *dir,umode_t mode,dev_t dev) +{ +struct inode *inode =new_inode (sb); + +if (inode) { + inode->i_ino =get_next_ino (); +inode_init_owner (inode, dir, mode); + inode->i_mapping->a_ops = ®fs_aops; +//inode->i_mapping->backing_dev_info = ®fs_backing_dev_info; +mapping_set_gfp_mask (inode->i_mapping,GFP_HIGHUSER ); +mapping_set_unevictable (inode->i_mapping); + inode->i_atime = inode->i_mtime = inode->i_ctime =current_time (inode); +switch (mode &S_IFMT ) { +default : +init_special_inode (inode, mode, dev); +break ; +case S_IFREG : + inode->i_op = ®fs_file_inode_operations; + inode->i_fop = ®fs_file_operations; +break ; +case S_IFDIR : + inode->i_op = ®fs_dir_inode_operations; + inode->i_fop = &simple_dir_operations; + +/* directory inodes start off with i_nlink == 2 (for "." entry) */
fs/regfs/inode.c line 87
Quoted 10 lines
fs/regfs/inode.c new file +10
-0+inc_nlink (inode); +break ; +case S_IFLNK : + inode->i_op = &page_symlink_inode_operations; +break ; + } + } + +return inode; +}
Seriously? Where would symlinks, device nodes, FIFOs and sockets come from? And you are open-coding the regular file case in the new_dentry_create() anyway, so the only thing this is actually used for is the root directory.
fs/regfs/inode.c line 91
Quoted 3 lines
fs/regfs/inode.c new file +3
-0+static const struct inode_operations regfs_dir_inode_operations = { + .lookup = simple_lookup, +};
... and simple_dir_inode_operations is wrong, because...?
Quoted 16 lines
fs/regfs/inode.c new file +16
-0+static struct dentry *new_dentry_create (struct super_block *sb,struct dentry *parent, +const char *name,bool is_dir,struct res_data *res) +{ +struct dentry *dentry; +struct inode *inode; +struct regfs_inode_info *ei; +struct regfs_fs_info *fsi = sb->s_fs_info; + + dentry =d_alloc_name (parent, name); +if (!dentry) +return NULL ; + + inode =new_inode (sb); +if (!inode) + goto out; +
fs/regfs/inode.c line 118
Quoted 10 lines
fs/regfs/inode.c new file +10
-0+ ei =REGFS_I (inode); + inode->i_ino =get_next_ino ();; + inode->i_mtime = inode->i_atime = inode->i_ctime =current_time (inode); + inode->i_uid =GLOBAL_ROOT_UID ; + inode->i_gid =GLOBAL_ROOT_GID ; +if (is_dir) { + inode->i_mode =S_IFDIR |S_IRUGO |S_IWUSR ; + inode->i_op = ®fs_dir_inode_operations; + inode->i_fop = &simple_dir_operations; +list_add (&ei->list, &fsi->list);
where's the matching removal from the list?
Quoted 95 lines
fs/regfs/inode.c new file +95
-0+ }else { + inode->i_mode =S_IFREG |S_IRUGO |S_IWUSR ; + inode->i_op = ®fs_file_inode_operations; + inode->i_fop = ®fs_file_operations; +inc_nlink (inode); + } + ei->base = (void *)res->base; + ei->offset = res->offset; + ei->type = res->type; + +d_add (dentry, inode); + +loc_debug ("new dentry io base:%llx offset:%llx ei:%llx\n" , (u64 )ei->base, (u64 )ei->offset, (u64 )ei); +return dentry; +out: +dput (dentry); +return NULL ; +} + +static void node_transfer_dentry (struct super_block *sb) +{ +struct regfs_fs_info *fsi = sb->s_fs_info; +void *blob = fsi->dtb_buf; +const char *pathp; +int node_offset, depth =-1 ; +struct dentry *parent =NULL ; +u64 parent_base; + +for (node_offset =fdt_next_node (blob,-1 , &depth); + node_offset >=0 && depth >=0 ; + node_offset =fdt_next_node (blob, node_offset, &depth)) { + +const struct fdt_property *prop; +struct res_data res; + + pathp =fdt_get_name (blob, node_offset,NULL ); + prop = (void *)fdt_getprop (blob, node_offset,"reg" ,NULL ); + +if (prop) { +unsigned long phys; + + phys =fdt32_to_cpu (((const __be32 *)prop)[1 ]); + res.type =RES_TYPE_RANGE ; + res.offset =fdt32_to_cpu (((const __be32 *)prop)[3 ]); + res.base = (u64 )ioremap (phys, res.offset); + +if (!res.base) { + parent =NULL ; + parent_base =0 ; +continue ; + } + +loc_debug ("%s reg:%lx size:%lx map:%llx\n\n" , pathp + , (unsigned long )fdt32_to_cpu (((const __be32 *)prop)[1 ]) + , (unsigned long )fdt32_to_cpu (((const __be32 *)prop)[3 ]) + , (u64 )res.base); + + parent =new_dentry_create (sb, sb->s_root, (const char *)pathp, true, &res); + parent_base = res.base; + + }else { +// parent dentry is create failed, igonre all child dentry +if (!parent) +continue ; + + prop = (void *)fdt_getprop (blob, node_offset,"offset" ,NULL ); +if (prop) { + + res.offset =fdt32_to_cpu (*(const __be32 *)prop); + res.base = parent_base; + res.type =RES_TYPE_ITEM ; + +new_dentry_create (sb, parent, (const char *) pathp, false, &res); +loc_debug ("%s offset:%lx\n" , pathp, (unsigned long )fdt32_to_cpu (*(const __be32 *)prop)); + } + } + } +} + +static int parse_options (char *options,struct super_block *sb) +{ +char *p; +int ret = -EINVAL ; +struct regfs_fs_info *fsi; +size_t msize =INT_MAX ; + + fsi = sb->s_fs_info; + +if (!options) +return -EINVAL ; + +while ((p =strsep (&options,"," )) !=NULL ) { +char *name, *name_val; + + name =strsep (&p,"=" );
fs/regfs/inode.c line 223
Quoted 10 lines
fs/regfs/inode.c new file +10
-0+if (name ==NULL ) + goto failed; + + name_val =strsep (&p,"=" ); +if (name_val ==NULL ) + goto failed; + +//get resource address +if (!strcmp (name,"dtb" )) { + ret =kernel_read_file_from_path (name_val, &fsi->dtb_buf, &fsi->dtb_len, msize,READING_UNKNOWN );
Why bother doing that in the kernel?
fs/regfs/inode.c line 291
Quoted 12 lines
fs/regfs/inode.c new file +12
-0+struct dentry *regfs_mount (struct file_system_type *fs_type, +int flags,const char *dev_name,void *data) +{ +struct dentry *root_dentry; +struct super_block *sb; + + root_dentry =mount_nodev (fs_type, flags, data, regfs_fill_super); + + sb = root_dentry->d_sb; + +if (sb->s_root) { +node_transfer_dentry (sb);
Er... Why not do that in regfs_fill_super()?
Al, not going any further for now...
Quoted 11 lines · Al Viro
Quoted 10 lines · Zou Cao
fs/regfs/file.c new file +10
-0+ssize_t regfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode = file->f_mapping->host; +ssize_t ret; + +inode_lock (inode); + ret =generic_write_checks (iocb, from); +if (ret >0 ) + ret =__generic_file_write_iter (iocb, from);
Quoted 8 lines
fs/regfs/file.c line 38
Quoted 6 lines
fs/regfs/file.c new file +6
-0+inode_unlock (inode); + +if (ret >0 ) + ret =generic_write_sync (iocb, ret); +return ret; +}Huh? How is that different from
generic_file_write_iter()? And who's using it, anyway?
my bug, remove it.
Quoted 4 lines
Quoted 4 lines
fs/regfs/file.c new file +4
-0+struct regfs_inode_info *info =REGFS_I (mapping->host); +char str[67 ]; +unsigned long val =0 ; +loff_t pos = *ppos;
Quoted 8 lines
fs/regfs/file.c line 71
Quoted 6 lines
fs/regfs/file.c new file +6
-0+size_t res; + +if (pos <0 ) +return -EINVAL ; +if (pos >= len || len >66 ) +return 0 ;This is completely bogus. "If current position is greater than the length of string we are asking to write, quietly return 0"?
Yes, fixed it.
Quoted 11 lines
fs/regfs/file.c line 79
Quoted 7 lines
fs/regfs/file.c new file +7
-0+ res =copy_from_user (str, buf, len); +if (res) +return -EFAULT ; + str[len] =0 ; + +if (kstrtoul (str,16 , &val) <0 ) +return -EINVAL ;Where does 67 come from? If you are expecting a hexadecimal representation of a unsigned long on arm64, you should have at most 16 digits. 67 looks rather odd...
Yes, it is only 16. thank you so much.
Quoted 6 lines
fs/regfs/file.c line 81
Quoted 1 line
fs/regfs/file.c new file +1
-0+writel_relaxed (val, info->base + info->offset);... and you are promptly discarding the upper 32 bits, since
writel_relaxed()takes u32:((void)__raw_writel((__force u32)cpu_to_le32(v),(c)))is going to truncate to 32bit, no matter what. Quietly truncate, at that...
Quoted 7 lines
fs/regfs/file.c line 106
Quoted 6 lines
fs/regfs/file.c new file +6
-0+const struct address_space_operations regfs_aops = { + .readpage = simple_readpage, + .write_begin = simple_write_begin, + .write_end = simple_write_end, + .set_page_dirty = __set_page_dirty_buffers, +};Again, huh? What would use the page cache there, anyway?
actually it not need, it will be remove
Quoted 2 lines
Quoted 1 line
+static
LIST_HEAD(regfs_head);Protected by...?
It is better to add a protect for multpite mount, i will do more test for it.
Quoted 35 lines
Quoted 35 lines
fs/regfs/inode.c new file +35
-0+static const struct inode_operations regfs_dir_inode_operations; +int regfs_debug; +module_param (regfs_debug, int,S_IRUGO ); +MODULE_PARM_DESC (regfs_debug,"enable regfs debug mode" ); + +struct inode *regfs_get_inode (struct super_block *sb,const struct inode *dir,umode_t mode,dev_t dev) +{ +struct inode *inode =new_inode (sb); + +if (inode) { + inode->i_ino =get_next_ino (); +inode_init_owner (inode, dir, mode); + inode->i_mapping->a_ops = ®fs_aops; +//inode->i_mapping->backing_dev_info = ®fs_backing_dev_info; +mapping_set_gfp_mask (inode->i_mapping,GFP_HIGHUSER ); +mapping_set_unevictable (inode->i_mapping); + inode->i_atime = inode->i_mtime = inode->i_ctime =current_time (inode); +switch (mode &S_IFMT ) { +default : +init_special_inode (inode, mode, dev); +break ; +case S_IFREG : + inode->i_op = ®fs_file_inode_operations; + inode->i_fop = ®fs_file_operations; +break ; +case S_IFDIR : + inode->i_op = ®fs_dir_inode_operations; + inode->i_fop = &simple_dir_operations; + +/* directory inodes start off with i_nlink == 2 (for "." entry) */ +inc_nlink (inode); +break ; +case S_IFLNK : + inode->i_op = &page_symlink_inode_operations; +break ;
Quoted 8 lines
fs/regfs/inode.c line 87
Quoted 5 lines
fs/regfs/inode.c new file +5
-0+ } + } + +return inode; +}Seriously? Where would symlinks, device nodes, FIFOs and sockets come from? And you are open-coding the regular file case in the
new_dentry_create()anyway, so the only thing this is actually used for is the root directory.
yes, i have limited the rm/create/link in regfs in inode_operations, so i need to remove all these and only leave the
inode->i_op = ®fs_file_inode_operations; ?
Quoted 5 lines
fs/regfs/inode.c line 91
Quoted 3 lines
fs/regfs/inode.c new file +3
-0+static const struct inode_operations regfs_dir_inode_operations = { + .lookup = simple_lookup, +};... and
simple_dir_inode_operationsis wrong, because...?
Here i limit the inode create/rm/link and so on, only support the read and write, it isn't useful for new inode, because these inode have not register addr to bind its dentry,
i think these create/rm/simlink are invalidm, only use simple_lookup.
Quoted 19 lines
Quoted 19 lines
fs/regfs/inode.c new file +19
-0+static struct dentry *new_dentry_create (struct super_block *sb,struct dentry *parent, +const char *name,bool is_dir,struct res_data *res) +{ +struct dentry *dentry; +struct inode *inode; +struct regfs_inode_info *ei; +struct regfs_fs_info *fsi = sb->s_fs_info; + + dentry =d_alloc_name (parent, name); +if (!dentry) +return NULL ; + + inode =new_inode (sb); +if (!inode) + goto out; + + ei =REGFS_I (inode); + inode->i_ino =get_next_ino ();; + inode->i_mtime = inode->i_atime = inode->i_ctime =current_time (inode);
Quoted 8 lines
fs/regfs/inode.c line 118
Quoted 7 lines
fs/regfs/inode.c new file +7
-0+ inode->i_uid =GLOBAL_ROOT_UID ; + inode->i_gid =GLOBAL_ROOT_GID ; +if (is_dir) { + inode->i_mode =S_IFDIR |S_IRUGO |S_IWUSR ; + inode->i_op = ®fs_dir_inode_operations; + inode->i_fop = &simple_dir_operations; +list_add (&ei->list, &fsi->list);where's the matching removal from the list?
Current regfs doesn't support inode create/del in regfs, sorry i am lazy to free it, all will be free when
umounting, if inode can't del, it will never need to remove from list, but it is better to add the remove list.
Quoted 98 lines
Quoted 98 lines
fs/regfs/inode.c new file +98
-0+ }else { + inode->i_mode =S_IFREG |S_IRUGO |S_IWUSR ; + inode->i_op = ®fs_file_inode_operations; + inode->i_fop = ®fs_file_operations; +inc_nlink (inode); + } + ei->base = (void *)res->base; + ei->offset = res->offset; + ei->type = res->type; + +d_add (dentry, inode); + +loc_debug ("new dentry io base:%llx offset:%llx ei:%llx\n" , (u64 )ei->base, (u64 )ei->offset, (u64 )ei); +return dentry; +out: +dput (dentry); +return NULL ; +} + +static void node_transfer_dentry (struct super_block *sb) +{ +struct regfs_fs_info *fsi = sb->s_fs_info; +void *blob = fsi->dtb_buf; +const char *pathp; +int node_offset, depth =-1 ; +struct dentry *parent =NULL ; +u64 parent_base; + +for (node_offset =fdt_next_node (blob,-1 , &depth); + node_offset >=0 && depth >=0 ; + node_offset =fdt_next_node (blob, node_offset, &depth)) { + +const struct fdt_property *prop; +struct res_data res; + + pathp =fdt_get_name (blob, node_offset,NULL ); + prop = (void *)fdt_getprop (blob, node_offset,"reg" ,NULL ); + +if (prop) { +unsigned long phys; + + phys =fdt32_to_cpu (((const __be32 *)prop)[1 ]); + res.type =RES_TYPE_RANGE ; + res.offset =fdt32_to_cpu (((const __be32 *)prop)[3 ]); + res.base = (u64 )ioremap (phys, res.offset); + +if (!res.base) { + parent =NULL ; + parent_base =0 ; +continue ; + } + +loc_debug ("%s reg:%lx size:%lx map:%llx\n\n" , pathp + , (unsigned long )fdt32_to_cpu (((const __be32 *)prop)[1 ]) + , (unsigned long )fdt32_to_cpu (((const __be32 *)prop)[3 ]) + , (u64 )res.base); + + parent =new_dentry_create (sb, sb->s_root, (const char *)pathp, true, &res); + parent_base = res.base; + + }else { +// parent dentry is create failed, igonre all child dentry +if (!parent) +continue ; + + prop = (void *)fdt_getprop (blob, node_offset,"offset" ,NULL ); +if (prop) { + + res.offset =fdt32_to_cpu (*(const __be32 *)prop); + res.base = parent_base; + res.type =RES_TYPE_ITEM ; + +new_dentry_create (sb, parent, (const char *) pathp, false, &res); +loc_debug ("%s offset:%lx\n" , pathp, (unsigned long )fdt32_to_cpu (*(const __be32 *)prop)); + } + } + } +} + +static int parse_options (char *options,struct super_block *sb) +{ +char *p; +int ret = -EINVAL ; +struct regfs_fs_info *fsi; +size_t msize =INT_MAX ; + + fsi = sb->s_fs_info; + +if (!options) +return -EINVAL ; + +while ((p =strsep (&options,"," )) !=NULL ) { +char *name, *name_val; + + name =strsep (&p,"=" ); +if (name ==NULL ) + goto failed; +
Quoted 8 lines
fs/regfs/inode.c line 223
Quoted 7 lines
fs/regfs/inode.c new file +7
-0+ name_val =strsep (&p,"=" ); +if (name_val ==NULL ) + goto failed; + +//get resource address +if (!strcmp (name,"dtb" )) { + ret =kernel_read_file_from_path (name_val, &fsi->dtb_buf, &fsi->dtb_len, msize,READING_UNKNOWN );Why bother doing that in the kernel?
Do you mean why using kernel_read_file_from_path and bind these dtb into dentry? i want to support different devices registers to mount
different path by using itself dtb file. i introduce mount param: dtb=test1.dtb, load these dtb to module and explain them, later these registers
will bind into different dentrys:
mount -t regfs -o dtb=test.dtb none /mnt
also you can mount others device dts to different dirtecotry.
mount -t regfs -o dtb=test1.dtb none /mnt1
mount -t regfs -o dtb=test2.dtb none /mnt2
I meet a hateful problem is devmem, for debugging some divices registers in user level, I can't remember so many registers and to find their addr by read device spec,
if it can be list and more easy to write/read and don't let me to reconfig these map of dentry in next time, i think dts file is suitably.
Quoted 5 lines
Quoted 5 lines
fs/regfs/inode.c new file +5
-0+struct dentry *regfs_mount (struct file_system_type *fs_type, +int flags,const char *dev_name,void *data) +{ +struct dentry *root_dentry; +struct super_block *sb;
Quoted 8 lines
fs/regfs/inode.c line 291
Quoted 7 lines
fs/regfs/inode.c new file +7
-0+ + root_dentry =mount_nodev (fs_type, flags, data, regfs_fill_super); + + sb = root_dentry->d_sb; + +if (sb->s_root) { +node_transfer_dentry (sb);Er... Why not do that in
regfs_fill_super()?
Yes, it is betters to move it into regfs_fill_super.
Quoted 1 line
Al, not going any further for now...
the master problem is i need to support inode create, del and so on? event i provide these opereations, these new inodes haven't bind the dts register addr,
it just a page cache inode, like ramfs inode, these inodes are invalid for regfs.