在linux内核中读写文件
转载: http://blog.csdn.net/tommy_wxie/article/details/8194276
1. 序曲
在用户态,读写文件可以通过read和write这两个系统调用来完成(C库函数实际上是对系统调用的封装)。 但是,在内核态没有这样的系统调用,我们又该如何读写文件呢?
阅读Linux内核源码,可以知道陷入内核执行的是实际执行的是sys_read和sys_write这两个函数,但是这两个函数没有使用EXPORT_SYMBOL导出,也就是说其他模块不能使用。
在fs/open.c中系统调用具体实现如下(内核版本2.6.34.1):
1 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode) 2 { 3 long ret; 4 if (force_o_largefile()) 5 flags |= O_LARGEFILE; 6 7 ret = do_sys_open(AT_FDCWD, filename, flags, mode); 8 9 /* avoid REGPARM breakage on x86: */ 10 asmlinkage_protect(3, ret, filename, flags, mode); 11 return ret; 12 }
跟踪do_sys_open()函数,就会发现它主要使用了do_filp_open()函数该函数在fs/namei.c中,而在该文件中,filp_open函数也是调用了do_filp_open函数,并且接口和sys_open函数极为相似,调用参数也和sys_open一样,并且使用EXPORT_SYMBOL导出了,所以我们猜想该函数可以打开文件,功能和open一样。
使用同样的方法,找出了一组在内核操作文件的函数,如下:
|
功能 |
函数原型 |
|
打开文件 |
struct file *filp_open(const char *filename, int flags, int mode) |
|
读文件 |
ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
|
写文件 |
ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
|
关闭文件 |
int filp_close(struct file *filp, fl_owner_t id) |
2. 内核空间与用户空间
在vfs_read和vfs_write函数中,其参数buf指向的用户空间的内存地址,如果我们直接使用内核空间的指针,则会返回-EFALUT。这是因为使用的缓冲区超过了用户空间的地址范围。一般系统调用会要求你使用的缓冲区不能在内核区。这个可以用set_fs()、get_fs()来解决。
在include/asm/uaccess.h中,有如下定义:
1 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 2 3 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) 4 5 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET) 6 7 #define get_ds() (KERNEL_DS) 8 9 #define get_fs() (current->addr_limit) 10 11 #define set_fs(x) (current->addr_limit = (x))
如果使用,如下:
1 mm_segment_t fs = get_fs(); 2 3 set_fs(KERNEL_FS); 4 5 //vfs_write(); 6 7 vfs_read(); 8 9 set_fs(fs);
详尽解释:系统调用本来是提供给用户空间的程序访问的,所以,对传递给它的参数(比如上面的buf),它默认会认为来自用户空间,在read或write()函数中,为了保护内核空间,一般会用get_fs()得到的值来和USER_DS进行比较,从而防止用户空间程序“蓄意”破坏内核空间;而现在要在内核空间使用系统调用,此时传递给read或write()的参数地址就是内核空间的地址了,在USER_DS之上(USER_DS ~ KERNEL_DS),如果不做任何其它处理,在write()函数中,会认为该地址超过了USER_DS范围,所以会认为是用户空间的“蓄意破坏”,从而不允许进一步的执行;为了解决这个问题; set_fs(KERNEL_DS);将其能访问的空间限制扩大到KERNEL_DS,这样就可以在内核顺利使用系统调用了!
3、示例:
1 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ 2 3 #include <linux/init.h> 4 #include <linux/module.h> 5 #include <linux/fs.h> 6 #include <linux/uaccess.h> 7 8 #define FILE_READ "/tmp/read.txt" 9 #define FILE_WRITE "/tmp/write.txt" 10 11 static int __init file_rw_init(void) 12 { 13 struct file *fp_r; 14 struct file *fp_w; 15 mm_segment_t fs; 16 loff_t pos = 0; 17 int ret = 0; 18 char buf[20] = {0}; 19 int count = 0; 20 21 22 pr_info("enter.\n"); 23 24 fp_r = filp_open(FILE_READ, O_RDONLY, 0644); 25 if (IS_ERR(fp_r)) { 26 pr_err("faild to open %s\n", FILE_READ); 27 ret = -ENODEV; 28 goto out; 29 } 30 31 fp_w = filp_open(FILE_WRITE, O_RDWR|O_CREAT, 0644); 32 if (IS_ERR(fp_w)) { 33 pr_err("faild to open %s\n", FILE_WRITE); 34 ret = -ENODEV; 35 goto out1; 36 } 37 38 fs = get_fs(); 39 set_fs(KERNEL_DS); 40 pos = 0; 41 count = vfs_read(fp_r, buf, sizeof(buf), &pos); 42 if (count < 0) { 43 pr_err("failed to read: %d\n", count); 44 goto out2; 45 } 46 pr_info("read %s from %s\n", buf, FILE_READ); 47 48 pos = 0; 49 count = vfs_write(fp_w, buf, count, &pos); 50 if (count < 0) { 51 pr_err("failed write: %d\n", count); 52 goto out2; 53 } 54 pr_info("write %s to %s\n", buf, FILE_WRITE); 55 56 filp_close(fp_r, NULL); 57 filp_close(fp_w, NULL); 58 59 set_fs(fs); 60 61 return 0; 62 out2: 63 set_fs(fs); 64 filp_close(fp_w, NULL); 65 out1: 66 filp_close(fp_r, NULL); 67 out: 68 return ret; 69 } 70 module_init(file_rw_init); 71 72 static void __exit file_rw_exit(void) 73 { 74 pr_info("enter.\n"); 75 } 76 module_exit(file_rw_exit); 77 78 MODULE_LICENSE("GPL");
实验:
[root@tiny4412 ]# mount -t nfs -o nolock 192.168.2.6:/nfsroot /mnt [root@tiny4412 ]# cd mnt/ [root@tiny4412 mnt]# ls fdt gpio_demo.ko interrupt_demo.ko shadow3 file_rw.ko group passwd trace [root@tiny4412 mnt]# insmod file_rw.ko [ 46.952611] file_rw: file_rw_init: enter. [ 46.952723] file_rw: file_rw_init: faild to open /tmp/read.txt insmod: can't insert 'file_rw.ko': No such device [root@tiny4412 mnt]# lsmod [root@tiny4412 mnt]# echo "Hello World" > /tmp/read.txt [root@tiny4412 mnt]# insmod file_rw.ko [ 94.510880] file_rw: file_rw_init: enter. [root@tiny4412 mnt]# cat /tmp/write.txt Hello World
完。
本文来自博客园,作者:dolinux,未经同意,禁止转载

浙公网安备 33010602011771号