文件操作结构

将驱动程序操作连接到设备编号,结构定义在<linux/fs.h>,其中包含一组函数指针,每个打开的文件(在内部由一个file结构表示)和一组函数关联(通过包含指向一个file_operations结构的f_op字段)。

struct file_operations {//指向拥有该结构的模块的指针struct module *owner;//修改当前文件的读写位置loff_t (*llseek) (struct file *, loff_t, int);ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);int (*iterate) (struct file *, struct dir_context *);int (*iterate_shared) (struct file *, struct dir_context *);__poll_t (*poll) (struct file *, struct poll_table_struct *);long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);long (*compat_ioctl) (struct file *, unsigned int, unsigned long);int (*mmap) (struct file *, struct vm_area_struct *);unsigned long mmap_supported_flags;int (*open) (struct inode *, struct file *);int (*flush) (struct file *, fl_owner_t id);int (*release) (struct inode *, struct file *);int (*fsync) (struct file *, loff_t, loff_t, int datasync);int (*fasync) (int, struct file *, int);int (*lock) (struct file *, int, struct file_lock *);ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);int (*check_flags)(int);int (*flock) (struct file *, int, struct file_lock *);ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);int (*setlease)(struct file *, long, struct file_lock **, void **);long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMUunsigned (*mmap_capabilities)(struct file *);
#endifssize_t (*copy_file_range)(struct file *, loff_t, struct file *,loff_t, size_t, unsigned int);int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,u64);ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *,u64);
} __randomize_layout;

file结构

file结构与用户空间的FILE没有任何关联。file结构代表一个打开的文件,由内核在open时创建,并传递给在该文件上进行操作的所有函数。

struct file {union {struct llist_node    fu_llist;struct rcu_head    fu_rcuhead;} f_u;struct path        f_path;struct inode     *f_inode;   /* cached value *///与文件相关的操作const struct file_operations    *f_op;/** Protects f_ep_links, f_flags.* Must not be taken from IRQ context.*/spinlock_t        f_lock;enum rw_hint     f_write_hint;atomic_long_t      f_count;//文件标志,检查用户请求是否是非阻塞的操作unsigned int       f_flags;//文件模式,表示文件可读或可写fmode_t          f_mode;struct mutex     f_pos_lock;loff_t           f_pos;struct fown_struct    f_owner;const struct cred   *f_cred;struct file_ra_state    f_ra;u64            f_version;
#ifdef CONFIG_SECURITYvoid          *f_security;
#endif/* needed for tty driver, and maybe others */void         *private_data;#ifdef CONFIG_EPOLL/* Used by fs/eventpoll.c to link all the hooks to this file */struct list_head    f_ep_links;struct list_head f_tfile_llink;
#endif /* #ifdef CONFIG_EPOLL */struct address_space    *f_mapping;errseq_t     f_wb_err;
} __randomize_layout

inode结构

内核用inode结构在内部表示文件,它和file不同,后者表示打开的文件描述符,对于单个文件,可能会有许多表示打开的文件描述符的file结构,但它们都指向单个inode结构。

struct inode {umode_t            i_mode;unsigned short       i_opflags;kuid_t            i_uid;kgid_t            i_gid;unsigned int      i_flags;#ifdef CONFIG_FS_POSIX_ACLstruct posix_acl  *i_acl;struct posix_acl *i_default_acl;
#endifconst struct inode_operations *i_op;struct super_block    *i_sb;struct address_space  *i_mapping;#ifdef CONFIG_SECURITYvoid           *i_security;
#endif/* Stat data, not accessed from path walking */unsigned long      i_ino;/** Filesystems may only read i_nlink directly.  They shall use the* following functions for modification:**    (set|clear|inc|drop)_nlink*    inode_(inc|dec)_link_count*/union {const unsigned int i_nlink;unsigned int __i_nlink;};dev_t           i_rdev;loff_t           i_size;struct timespec64    i_atime;struct timespec64   i_mtime;struct timespec64   i_ctime;spinlock_t      i_lock; /* i_blocks, i_bytes, maybe i_size */unsigned short          i_bytes;unsigned int       i_blkbits;enum rw_hint      i_write_hint;blkcnt_t       i_blocks;#ifdef __NEED_I_SIZE_ORDEREDseqcount_t     i_size_seqcount;
#endif/* Misc */unsigned long       i_state;struct rw_semaphore i_rwsem;unsigned long       dirtied_when;   /* jiffies of first dirtying */unsigned long        dirtied_time_when;struct hlist_node i_hash;struct list_head i_io_list;  /* backing dev IO list */
#ifdef CONFIG_CGROUP_WRITEBACKstruct bdi_writeback  *i_wb;      /* the associated cgroup wb *//* foreign inode detection, see wbc_detach_inode() */int          i_wb_frn_winner;u16         i_wb_frn_avg_time;u16           i_wb_frn_history;
#endifstruct list_head  i_lru;      /* inode LRU list */struct list_head    i_sb_list;struct list_head  i_wb_list;  /* backing dev writeback list */union {struct hlist_head    i_dentry;struct rcu_head        i_rcu;};atomic64_t      i_version;atomic_t      i_count;atomic_t        i_dio_count;atomic_t        i_writecount;
#ifdef CONFIG_IMAatomic_t       i_readcount; /* struct files open RO */
#endifconst struct file_operations  *i_fop; /* former ->i_op->default_file_ops */struct file_lock_context *i_flctx;struct address_space   i_data;struct list_head i_devices;union {struct pipe_inode_info *i_pipe;struct block_device *i_bdev;struct cdev     *i_cdev;char            *i_link;unsigned        i_dir_seq;};__u32           i_generation;#ifdef CONFIG_FSNOTIFY__u32            i_fsnotify_mask; /* all events this inode cares about */struct fsnotify_mark_connector __rcu    *i_fsnotify_marks;
#endif#if IS_ENABLED(CONFIG_FS_ENCRYPTION)struct fscrypt_info   *i_crypt_info;
#endifvoid          *i_private; /* fs or device private pointer */
} __randomize_layout;

作为常规,只有下面两个字段对驱动程序有用:
dev_t    i_rdev;
对表示设备文件的inode结构,包含了真正的设备编号。
struct cdev *i_cdev;
struct cdev表示字符设备的内核内部数据结构,当inode指向一个字符设备文件,该字段包含指向struct cdev结构的指针

下面两个宏可用来从inode中获得主设备号和次设备号:
unsigned int iminor(struct inode *inode);
unsigned int imajor(struct inode *inode);

linux 内核 数据结构 file_operations、file、inode相关推荐

  1. 索引节点inode: Linux内核文件系统之(inode)

    inode译成中文就是索引节点,它用来存放档案及目录的基本信息,包含时间.档名.使用者及群组等. static int eachreg_open(struct inode *inode, struct ...

  2. go移植linux内核书名叫啥,Go语言移植Linux内核数据结构hlist

    hlist(哈希链表)可以通过相应的Hash算法,迅速找到相关的链表Head及节点. 在有些应用场景,比Go标准库提供的list(一种双向链表)更合适. 依照list.h中的源码,我实现了一个Go语言 ...

  3. linux内核数据结构之链表

    1.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该代码来自linux内核,在li ...

  4. linux内核数据结构实现--链表、队列和哈希

    C是面向过程的语言,但是linux内核却用C实现了一套面向对象的设计模式,linux内核中处处体现着面向对象的思想. 1. 内核链表和list_entry 1.1 普通链表实现 我们在语法书上学到的链 ...

  5. 【Linux】Linux内核数据结构:IDR(redix树)

    1. 引言 最近在系统里遇到了IDR结构体,后来看了一下,是内核的一个基础结构. 这个是怎么引入的,引入是为了什么呢? 最早的时候,我们的结构体是一个类似于大结构体套小结构体. struct A {i ...

  6. Linux内核数据结构——链表

    目录 目录 简介 单向链表 双向链表 环形链表 Linux内核中的链表实现 offsetof container_of container_of 第一部分 container_of 第二部分 链表初始 ...

  7. FIFO - linux内核数据结构

    Linux kernal 鬼斧神工,博大精深,让人叹为观止,拍手叫绝.然匠心独运的设计并非扑朔迷离.盘根错节,真正的匠心独运乃辞简理博.化繁为简,在简洁中昭显优雅和智慧,kfifo就是这样一种数据结构 ...

  8. linux 内核 struct file_operations中 ioctl 变为 unlocked_ioctl

    1.消失的确切时间     ioctl的消失到底是从哪个版本开始的?网上给出的时间是2.6.36开始.网上就是这么说,但是自己必须找到代码中的证据.于是我通过git搜索主线内核代码,找到的删除ioct ...

  9. Linux 内核数据结构解析--无锁链表

    无锁链表定义在include/linux/llist.h.无锁链表是一种单向链表 定义: struct llist_head {struct llist_node *first; };struct l ...

最新文章

  1. 张高兴的 UWP 开发笔记:横向 ListView
  2. 你也可以设计简单的内容管理系统
  3. python3 线程间通讯(借助queue)
  4. redis windows
  5. .cpp 编译成.a或是 .so
  6. docker 安装nginx_docker安装nginx搭建简单文件共享服务
  7. .net httpwebrequest 并发慢_go语言并发之MPG模型
  8. H.264学习历程(天之骄子)
  9. 系统调用和库函数的区别
  10. redis复制key的数据_Redis常见面试题
  11. Gitbook/Markdown中插入复杂(合并单元格)的表格
  12. 关于修改密码后,远程服务器项目连接不上数据库的处理办法
  13. sql 更新一些特殊要求字段(批量)
  14. spring boot task实现动态创建定时任务
  15. Mysql 日期函数 增加周天年月时分秒
  16. C语言数据结构知识点小结(全)
  17. kepware怎么读modbus/tcp数据_注塑机设备联网?EUROMAP 63?Kepware快速帮您搞定!
  18. 突破考易系统1.0切换窗口封锁
  19. 项目管理论坛_活动预告|2019年“VUCA时代项目管理与项目治理”论坛通知
  20. 扫雷——关于展开空格的算法

热门文章

  1. Keil编译产生的RO,RW和ZI是什么
  2. STM32使用DMA从串口读数据到内存
  3. [20170513]update结果集.txt
  4. win10内建子系统Linux
  5. 全排列之递归与非递归算法实现总结
  6. gnome Nautilus cannot handle computer locations
  7. linux文本处理工具之grep与正则表达式语法
  8. openlayers之style符号化
  9. weexapp 开发流程(一)开发环境配置
  10. 百度编辑器(ueditor)@功能之获取坐标