zoukankan      html  css  js  c++  java
  • file结构体

    1.1 struct file
      struct file结构体定义在include/linux/fs.h中定义。文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file。它由内核在打开文件时创建,并传递给在文件上进行操作的任何函数。在文件的所有实例都关闭后,内核释放这个数据结构。在内核创建和驱动源码中,struct file的指针通常被命名为file或filp。

    struct file 的最重要成员在这展示.
    1.mode_t f_mode;
    文件模式确定文件是可读的或者是可写的(或者都是), 通过位 FMODE_READ 和FMODE_WRITE. 你可能想在你的 open 或者 ioctl 函数中检查这个成员的读写许可, 但是不需要检查读写许可, 因为内核在调用你的方法之前检查. 当文件还没有为那种存取而打开时读或写的企图被拒绝, 驱动甚至不知道这个情况.
    2.loff_t f_pos;
    当前读写位置. loff_t 在所有平台都是 64 位( 在 gcc 术语里是 long long ). 驱动可以读这个值,如果它需要知道文件中的当前位置, 但是正常地不应该改变它; 读和写应当使用它们作为最后参数而收到的指针来更新一个位置, 代替直接作用于 filp->f_pos. 这个规则的一个例外是在 llseek 方法中, 它的目的就是改变文件位置.
    3.unsigned int f_flags;
    这些是文件标志, 例如 O_RDONLY, O_NONBLOCK, 和 O_SYNC. 驱动应当检查O_NONBLOCK 标志来看是否是请求非阻塞操作; 其他标志很少使用. 特别地, 应当检查读/写许可, 使用 f_mode 而不是f_flags. 所有的标志在头文件

    ifdef __NEED_I_SIZE_ORDERED

    seqcount_t i_size_seqcount;

    endif

    struct timespec i_atime;
    struct timespec i_mtime;
    struct timespec i_ctime;
    unsigned int i_blkbits;
    blkcnt_t i_blocks;
    unsigned short i_bytes;
    umode_t i_mode;
    spinlock_t i_lock;
    struct mutex i_mutex;
    struct rw_semaphore i_alloc_sem;
    const struct inode_operations *i_op;
    const struct file_operations *i_fop;
    struct super_block *i_sb;
    struct file_lock *i_flock;
    struct address_space *i_mapping;
    struct address_space i_data;

    ifdef CONFIG_QUOTA

    struct dquot *i_dquot[MAXQUOTAS];

    endif

    struct list_head i_devices;
    union {
    struct pipe_inode_info *i_pipe;
    struct block_device *i_bdev;
    struct cdev *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,其中cdev结构是字符设备结构体。
    };
    int i_cindex;

    __u32 i_generation;

    ifdef CONFIG_DNOTIFY

    unsigned long i_dnotify_mask;
    struct dnotify_struct *i_dnotify;

    endif

    ifdef CONFIG_INOTIFY

    struct list_head inotify_watches;
    struct mutex inotify_mutex;

    endif

    unsigned long i_state;
    unsigned long dirtied_when;

    unsigned int i_flags;

    atomic_t i_writecount;

    ifdef CONFIG_SECURITY

    void *i_security;

    endif

    void *i_private;
    };

      我们在进程中打开一个文件F,实际上就是要在内存中建立F的dentry,和inode结构,并让它们与进程结构联系来,把VFS中定义的接口给接起来。我们来看一看这个经典的图:
    file结构体详解
    这里写图片描述

    下图为多个进程打开同一文件的情况:
    file结构体详解
    这里写图片描述

    技术不分国界
  • 相关阅读:
    Unix + OS IBM Aix System Director
    framework apache commons
    维吉尼亚密码
    破译换字式密码
    维吉尼亚密码表
    维吉尼亚密码表
    delete i;
    破译换字式密码
    破译换字式密码
    int *i = new int;
  • 原文地址:https://www.cnblogs.com/angels-yaoyao/p/12443618.html
Copyright © 2011-2022 走看看