首先给出filp的结构体:struct file

struct file {

  union {

        struct list_head fu_list; //文件对象链表指针linux/include/linux/list.h

        struct rcu_head fu_rcuhead; //RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制

   } f_u;

  struct path f_path; //包含dentry和mnt两个成员,用于确定文件路径

  #define f_dentry f_path.dentry //f_path的成员之一,当前文件的dentry结构

  #define f_vfsmnt f_path.mnt //表示当前文件所在文件系统的挂载根目录

  const struct file_operations *f_op; //与该文件相关联的操作函数

  atomic_t f_count; //文件的引用计数(有多少进程打开该文件)

  unsigned int f_flags; //对应于open时指定的flag

  mode_t f_mode; //读写模式:open的mod_t mode参数

       loff_t     f_pos;//当前文件指针位置

  off_t f_pos; //该文件在当前进程中的文件偏移量

  struct fown_struct f_owner; //该结构的作用是通过信号进行I/O时间通知的数据。

  unsigned int f_uid, f_gid;// 文件所有者id,所有者组id

  struct file_ra_state f_ra; //在linux/include/linux/fs.h中定义,文件预读相关

  unsigned long f_version;//记录文件的版本号,每次使用之后递增

  #ifdef CONFIG_SECURITY

       void *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;

      spinlock_t f_ep_lock;

  #endif /* #ifdef CONFIG_EPOLL */

  struct address_space *f_mapping;

  };

找到private成员(前后已用空白行隔开),显然该成员是void*类型,也就是说我们可以将任意的地址赋给该指针变量

在Linux驱动字符设备开发中:

首先:

struct file *filp

定义结构体:

struct cdev 
{
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};

继续定义结构

struct test_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
};

再定义:

struct test_dev testdev;

此时:在test_open()应用函数里取指:&testdev并赋值给filp->private_data即    

static int test_open(struct inode *inode, struct file *filp)
{
filp->private_data = &testdev; /* 设置私有数据 */
return 0;
}

也就是说此时的filp->private为一个指向结构体test_dev的指针

此时将:

struct   test_dev *dev = filp->private_data;

此时也就是说dev存放的就是该私有数据的结构体指针了

通过dev就可以直接访问该私有test的私有数据了。

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐