因之前工作需要在kernel里存取数据到文件中,特意研究了一下怎么做,我们应尽可能避免直接操作文件I/O,选择通过调用VFS(虚拟文件系统)的函数来实现,我的实现demo如下:
##Includes
1 2 3 4
| #include <linux/fs.h> #include <asm/segment.h> #include <asm/uaccess.h> #include <linux/buffer_head.h>
|
##Opening a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| struct file* file_open(const char* path, int flags, int rights) { struct file* filp = NULL; mm_segment_t oldfs; int err = 0;
oldfs = get_fs(); set_fs(get_ds()); filp = filp_open(path, flags, rights); set_fs(oldfs); if(IS_ERR(filp)) { err = PTR_ERR(filp); return NULL; } return filp; }
|
##Close a file
1 2 3
| void file_close(struct file* file) { filp_close(file, NULL); }
|
##Reading from a file
1 2 3 4 5 6 7 8 9 10 11 12
| int file_read(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size) { mm_segment_t oldfs; int ret;
oldfs = get_fs(); set_fs(get_ds());
ret = vfs_read(file, data, size, &offset);
set_fs(oldfs); return ret; }
|
##Writing to a file
1 2 3 4 5 6 7 8 9 10 11 12
| int file_write(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size) { mm_segment_t oldfs; int ret;
oldfs = get_fs(); set_fs(get_ds());
ret = vfs_write(file, data, size, &offset);
set_fs(oldfs); return ret; }
|
##Syncing changes a file
1 2 3 4
| int file_sync(struct file* file) { vfs_fsync(file, 0); return 0; }
|