我是靠谱客的博主 魔幻高山,最近开发中收集的这篇文章主要介绍文件系统学习4 文件open read write等操作,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

系统调用接口

const struct file_operations ext2_file_operations = {
.llseek
= generic_file_llseek,
.read
= do_sync_read,
.write
= do_sync_write,
.aio_read	= generic_file_aio_read,
.aio_write	= generic_file_aio_write,
.ioctl
= ext2_ioctl,
.mmap
= generic_file_mmap,
.open
= generic_file_open,
.release	= ext2_release_file,
.fsync
= ext2_sync_file,
.splice_read	= generic_file_splice_read,
.splice_write	= generic_file_splice_write,
};

打开文件

asmlinkage long sys_open(const char __user *filename, int flags, int mode)
=>ret = do_sys_open(AT_FDCWD, filename, flags, mode);
=>fd = get_unused_fd_flags(flags);
=>fdt = files_fdtable(files);
fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds, files->next_fd);//将next_fd作为最新的fd
=>files->next_fd = fd + 1;
=>struct file *f = do_filp_open(dfd, tmp, flags, mode);
=>error = open_namei(dfd, filename, namei_flags, mode, &nd);
=>if (!(flag & O_CREAT))
==>error = path_lookup_open(dfd, pathname, lookup_flags(flag), nd, flag);
=>__path_lookup_intent_open(dfd, name, lookup_flags, nd, open_flags, 0);
=>struct file *filp = get_empty_filp();//申请新的file结构体
=>nd->intent.open.file = filp;
nd->intent.open.flags = open_flags;
nd->intent.open.create_mode = create_mode;
=>err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
=>retval = path_walk(name, nd);
==>goto ok;
=>error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);//如果open带不存在则创建新文件
=>error = open_namei_create(nd, &path, flag, mode);
=>error = vfs_create(dir->d_inode, path->dentry, mode, nd);
=>error = dir->i_op->create(dir, dentry, mode, nd);//inode方法
=>static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)//创建新文件写磁盘ext2文件系统
=>struct inode * inode = ext2_new_inode (dir, mode);
=>inode = new_inode(sb);
=>inode = alloc_inode(sb);
=>inode = sb->s_op->alloc_inode(sb);//超级块方法
=>static struct inode *ext2_alloc_inode(struct super_block *sb)
=>nameidata_to_filp(&nd, flags);
=>filp = nd->intent.open.file;
=>if (filp->f_path.dentry == NULL)
==>filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
=>f->f_flags = flags;
=>f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
=>inode = dentry->d_inode;
=>f->f_mapping = inode->i_mapping;
f->f_path.dentry = dentry;
f->f_path.mnt = mnt;
f->f_pos = 0;
f->f_op = fops_get(inode->i_fop);//从inode获取文件访问的方法
file_move(f, &inode->i_sb->s_files);
=>if (!open && f->f_op)
==>open = f->f_op->open;
=>if (open)
==>error = open(inode, f);//私有的Open方法调用
=>f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
=>else
==>path_release(nd);
=>fd_install(fd, f);//建立fd和file_struct的关联

读取文件

asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
=>file = fget_light(fd, &fput_needed);
=>loff_t pos = file_pos_read(file);//获取位置
=>ret = vfs_read(file, buf, count, &pos);
=>ret = file->f_op->read(file, buf, count, pos);
=>size_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
=>struct iovec iov = { .iov_base = buf, .iov_len = len };
=>init_sync_kiocb(&kiocb, filp);
kiocb.ki_pos = *ppos;
kiocb.ki_left = len;
=>for (;;) {
ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
=>do_generic_file_read(filp,ppos,&desc,file_read_actor);//.aio_read	= generic_file_aio_read
=>do_generic_mapping_read
=>for (;;) {//每一页循环一次
=>page = find_get_page(mapping, index);//如果磁盘高速缓存能搞定,则走捷径
=>if (!page)//否则从磁盘读取到磁盘高速缓存
==>page_cache_sync_readahead(mapping, &ra, filp, index, last_index - index);
==>page = find_get_page(mapping, index);
==>error = mapping->a_ops->readpage(filp, page);
=>mpage_readpage(page, ext2_get_block) //.readpage
= ext2_readpage
=>bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio, &map_bh, &first_logical_block, get_block);
=>如果页面的物理块连续,则一个bio搞定
=>否则,通过buffer_head用N个bio传输
=>mpage_bio_submit(READ, bio);
}
if (ret != -EIOCBRETRY)
break;
wait_on_retry_sync_kiocb(&kiocb);
}
=>*ppos = kiocb.ki_pos;
=>file_pos_write(file, pos);//更新位置
=>fput_light(file, fput_needed);//释放文件对象

删除结点

Super.c (c:linuxlinux-2.6.23fsext2):	.delete_inode	= ext2_delete_inode,
void ext2_delete_inode (struct inode * inode)
=>truncate_inode_pages(&inode->i_data, 0);//释放磁盘页高速缓存
=>truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
=>两个大的循环,第一个循环发现页没有lock和write_back的话立即释放;否则Pass
=>第二个循环收拾剩下的lock和write_back页
=>mark_inode_dirty(inode);
=>__mark_inode_dirty(inode, I_DIRTY);
=>inode->i_state |= flags;//设置dirty标志位
=>list_move(&inode->i_list, &sb->s_dirty);//把inode移到dirty链表
=>ext2_update_inode(inode, inode_needs_sync(inode));
=>if (inode->i_blocks)
ext2_truncate (inode);
=>ext2_free_inode (inode);

写文件

ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
struct file *file = fget_light(fd, &fput_needed);
loff_t pos = file_pos_read(file);
ret = vfs_write(file, buf, count, &pos);
=>ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
ret = file->f_op->write(file, buf, count, pos);
=>ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
for (;;)
ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
=>ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long nr_segs, loff_t pos)
ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, &iocb->ki_pos);
=>ssize_t __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov, unsigned long nr_segs, loff_t *ppos)
struct file *file = iocb->ki_filp;
struct address_space * mapping = file->f_mapping;
struct inode
*inode = mapping->host;
//O_DIRECT不经过缓存
//非O_DIRECT的流程
written = generic_file_buffered_write(iocb, iov, nr_segs, pos, ppos, count, written);
=>ssize_t generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov, unsigned long nr_segs, loff_t pos, loff_t *ppos, size_t count, ssize_t written)
struct address_space *mapping = file->f_mapping;
const struct address_space_operations *a_ops = mapping->a_ops;
status = a_ops->write_begin(file, mapping, pos, bytes, flags, &page, &fsdata);
=>int ext2_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata)
status = a_ops->write_end(file, mapping, pos, bytes, copied, page, fsdata);
=>int generic_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata)
copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
=>int block_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata)
__block_commit_write(inode, page, start, start+copied);
=>int __block_commit_write(struct inode *inode, struct page *page, unsigned from, unsigned to)
unsigned block_start, block_end;
int partial = 0;
unsigned blocksize;
struct buffer_head *bh, *head;
blocksize = 1 << inode->i_blkbits;
for(bh = head = page_buffers(page), block_start = 0;
bh != head || !block_start;
block_start=block_end, bh = bh->b_this_page)
block_end = block_start + blocksize;
if (block_end <= from || block_start >= to)
if (!buffer_uptodate(bh))
partial = 1;
else
set_buffer_uptodate(bh);
mark_buffer_dirty(bh);//标记位脏
if (!partial)
SetPageUptodate(page);
if (pos+copied > inode->i_size)
i_size_write(inode, pos+copied);
i_size_changed = 1;
unlock_page(page);
page_cache_release(page);
if (i_size_changed)
mark_inode_dirty(inode);
balance_dirty_pages_ratelimited(mapping);//唤醒写脏页的线程
if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
err = sync_page_range(inode, mapping, pos, ret);
file_pos_write(file, pos);
fput_light(file, fput_needed);
int ext2_writepage(struct page *page, struct writeback_control *wbc)
return block_write_full_page(page, ext2_get_block, wbc);
=>int block_write_full_page(struct page *page, get_block_t *get_block, struct writeback_control *wbc)
return block_write_full_page_endio(page, get_block, wbc, end_buffer_async_write);
=>int block_write_full_page_endio(struct page *page, get_block_t *get_block, struct writeback_control *wbc, bh_end_io_t *handler)
return __block_write_full_page(inode, page, get_block, wbc, handler);
=>int __block_write_full_page(struct inode *inode, struct page *page, get_block_t *get_block, struct writeback_control *wbc, bh_end_io_t *handler)
err = get_block(inode, block, bh, 1);
=>int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
int ret = ext2_get_blocks(inode, iblock, max_blocks, bh_result, create);
=>int ext2_get_blocks(struct inode *inode, sector_t iblock, unsigned long maxblocks, struct buffer_head *bh_result, int create)
partial = ext2_get_branch(inode, depth, offsets, chain, &err);
=>Indirect *ext2_get_branch(struct inode *inode,
int depth,
int *offsets,
Indirect chain[4],
int *err)
bh = sb_bread(sb, le32_to_cpu(p->key));
=>struct buffer_head * sb_bread(struct super_block *sb, sector_t block)
return __bread(sb->s_bdev, block, sb->s_blocksize);
=>buffer_head *__bread(struct block_device *bdev, sector_t block, unsigned size)
struct buffer_head *bh = __getblk(bdev, block, size);
=>struct buffer_head *__getblk(struct block_device *bdev, sector_t block, unsigned size)
struct buffer_head *bh = __find_get_block(bdev, block, size);
if (bh == NULL)
bh = __getblk_slow(bdev, block, size);
=>struct buffer_head *__getblk_slow(struct block_device *bdev, sector_t block, int size)
for (;;)
struct buffer_head * bh;
int ret;
bh = __find_get_block(bdev, block, size);
if (bh)
return bh;
ret = grow_buffers(bdev, block, size);
if (ret < 0)
return NULL;
if (ret == 0)
free_more_memory();
return bh;
if (likely(bh) && !buffer_uptodate(bh))
bh = __bread_slow(bh);
return bh;
brelse(bh);

参考文章
linux驱动之–fops的关联
https://blog.csdn.net/qwaszx523/article/details/54377742?utm_source=itdadao&utm_medium=referral

系统调用Open()函数的内核追踪
http://blog.chinaunix.net/uid-24585858-id-2125501.html

linux设备驱动模型–open系统调用(创建文件)
https://blog.csdn.net/new_abc/article/details/8788911?

linux文件系统写过程简析
https://www.cnblogs.com/linghuchong0605/p/4515542.html

Linux文件系统(五)—三大缓冲区之buffer块缓冲区
https://blog.csdn.net/wh8_2011/article/details/49883427

浅尝辄止70-内核那些“非显式定义”宏
https://www.jianshu.com/p/9746745a97d2

rename代码阅读(linux 3.10.104)
https://blog.csdn.net/geshifei/article/details/81482660

VFS之open调用的理解
http://blog.chinaunix.net/uid-23062171-id-4709078.html

VFS之write调用的理解
http://blog.chinaunix.net/uid-23062171-id-4709077.html

do_mpage_readpage函数详细分析
http://blog.chinaunix.net/uid-28236237-id-4028521.html

普通文件的readpage方法(do_mpage_readpage)
https://blog.csdn.net/weixin_36145588/article/details/74990382
跟踪sys_mkdir的系统调用过程
https://blog.csdn.net/a363344923/article/details/44812293

最后

以上就是魔幻高山为你收集整理的文件系统学习4 文件open read write等操作的全部内容,希望文章能够帮你解决文件系统学习4 文件open read write等操作所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部