Linux内核读写文件
来源:互联网 发布:儿童故事软件大全 编辑:程序博客网 时间:2024/05/19 13:45
drivers/mtd/nandsim.c
<1>:打开文件
cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
if (IS_ERR(cfile))
return PTR_ERR(cfile);
if (!cfile->f_op || (!cfile->f_op->read && !cfile->f_op->aio_read)) {
NS_ERR("alloc_device: cache file not readable/n");
err = -EINVAL;
goto err_close;
}
if (!cfile->f_op->write && !cfile->f_op->aio_write) {
NS_ERR("alloc_device: cache file not writeable/n");
err = -EINVAL;
goto err_close;
}
<2>:读文件
static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t *pos)
{
mm_segment_t old_fs;
ssize_t tx;
int err, memalloc;
err = get_pages(ns, file, count, *pos);
if (err)
return err;
old_fs = get_fs();
set_fs(get_ds()); //将地址空间设置为内核空间,防止vfs_read返回失败
memalloc = set_memalloc();
tx = vfs_read(file, (char __user *)buf, count, pos);
clear_memalloc(memalloc);
set_fs(old_fs);
put_pages(ns);
return tx;
}
<3>:写文件
static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t *pos)
{
mm_segment_t old_fs;
ssize_t tx;
int err, memalloc;
err = get_pages(ns, file, count, *pos);
if (err)
return err;
old_fs = get_fs();
set_fs(get_ds()); //#define get_ds() (KERNEL_DS)
memalloc = set_memalloc();
tx = vfs_write(file, (char __user *)buf, count, pos);
clear_memalloc(memalloc);
set_fs(old_fs);
put_pages(ns);
return tx;
}
- Linux内核读写文件
- Linux内核读写文件
- Linux内核读写文件
- linux内核读写文件
- linux内核读写文件
- Linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- Linux内核模块读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核文件读写操作
- linux内核中读写文件
- Linux内核中读写文件
- Linux内核中读写文件
- Hello World!
- Incorporating contextual information in recommender systems using a multidimensional approach
- Shell字符串长度
- 在电脑上安装Android模拟器-部分更新详解
- css圆角边框,包含滑动门技术
- Linux内核读写文件
- Gemfile
- JDK中的队列接口:BlockingQueue详解
- iso 4 中textFieldShouldBeginEditing调用两次
- 顺序栈1
- 顺序栈1
- 顺序栈1
- database.yml
- Debug Java Application