1 / 12

内核中文件操作

内核中文件操作. 用户空间与内核空间文件操作区别 用户空间 open() , close() , read() , write() 内核空间 sys_open () , sys_close () , sys_read () , sys_write () filp_open () , filp_close () , filp_read () , filp_write (). 内核中文件操作函数. struct file * filp_open ( const char *filename, int flags, int mode ) f ilename :文件名

yonah
Download Presentation

内核中文件操作

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 内核中文件操作 • 用户空间与内核空间文件操作区别 用户空间 open(),close(),read(),write() 内核空间 sys_open(),sys_close(),sys_read(),sys_write() filp_open(),filp_close(),filp_read(),filp_write()

  2. 内核中文件操作函数 • struct file *filp_open(constchar *filename, int flags, int mode) • filename:文件名 • flags:文件打开方式,如:O_RDONLY、O_WRONLY、O_RDWR、O_CREAT、O_APPEND • mode:新创建文件的权限,包括文件拥有者权限、组用户权限、其它用户权限 • intfilp_close(struct file *filp, fl_owner_t id) • filp:文件指针 • id:POSIX(可移植操作系统接口)线程的id

  3. 内核中文件操作函数 • ssize_t (*read) (struct file *filp, char __user *buf, size_t size, loff_t*ppos) • filp:读取的文件的指针 • buf:读取的缓存 • size:读取的字节数量 • ppos:在该文件中当前的位置 • 用法: filp->f_op->read(filp,(char *)buf,strlen(buf),&filp->f_pos); • f_op:file_operations结构体 read、write、llseek、ioctl等

  4. 内核中文件操作函数 • ssize_t (*write) (struct file *filp, const char __user *buf, size_t size, loff_t *ppos) • filp:写入的文件的指针 • buf:写入数据的缓存 • size:写入的字节数量 • ppos:在该文件中当前的位置 • 用法: filp->f_op->write(filp,(char *)buf,strlen(buf),&filp->f_pos);

  5. 内核中文件操作函数 • loff_t (*llseek) (struct file *filp, loff_t offset, int origin) • filp:操作的文件的指针 • offset:偏移量 • origin:偏移量相对位置,0表示绝对位置,1表示相对当前位置 • 用法: filp->f_op->llseek(filp,strlen(buf)*2,0)

  6. 内核中文件操作 • <asm/uaccess.h>文件 #define KERNEL_DS    MAKE_MM_SEG(-1UL)#define USER_DS        MAKE_MM_SEG(PAGE_OFFSET)#define get_ds()    (KERNEL_DS)#define get_fs()    (current_thread_info()->addr_limit)#define set_fs(x)    (current_thread_info()->addr_limit = (x))

  7. 内核中文件操作 • 示例 filp = filp_open(FILE_NAME,O_RDWR|O_CREAT,0644); old_fs = get_fs(); set_fs(get_ds()); filp->f_op->llseek(filp,0,0); printk("filp->f_pos=%llu\n",filp->f_pos); filp->f_op->read(filp,buf,sizeof(buf),&filp->f_pos); printk("filp->f_pos=%llu\n",filp->f_pos); printk("buf=%s\n",buf); sprintf(buf,"%s","The Messages.\n"); filp->f_op->write(filp,(char *)buf,sizeof(buf),&filp->f_pos); printk("filp->f_pos=%llu\n",filp->f_pos); set_fs(old_fs);

  8. 数据映射方式 • 数据结构 structlbatopba{ unsigned long longlba; unsigned long longpba; }

  9. 数据映射方式 • 直接映射 24 176 235 17 36

  10. 数据映射方式 • 顺序存放 4/15 2/254 5/19 4/35 16/61 2/78 3/39 …

  11. 数据映射方式 • 混合存放 3/25 3/28 3/25/28/null 3/25/null/null 7/573/null/null 2/73/36/198 2/73/36/null 2/73/null/null 4/178/null/null 8/14/null/null ……

  12. 要求 • 实现一个类似于loopback块设备,能够将一个块设备与文件正确关联映射。 • 数据:存放在一个文件中; • LBA->PBA映射表:存放在一个文件中。 • 资料 • 内核源码drivers/block/loop.c • http://blog.chinaunix.net/uid-712024-id-2678927.html • http://blog.csdn.net/proware/article/details/5959842

More Related