Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4242036
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: LINUX

2011-12-19 12:37:36



 inode 译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。 
      做个比喻,比如一本书,存储设备或分区就相当于这本书,Block相当于书中的每一页,inode 就相当于这本书前面的目录,一本书有很多的内容,如果想查找某部份的内容,我们可以先查目录,通过目录能最快的找到我们想要看的内容。
      当我们用ls 查看某个目录或文件时,如果加上-i 参数,就可以看到inode节点了;比如ls -li lsfile.sh ,最前面的数值就是inode信息

在《linux设备驱动程序》p59页,这样描述inode结构体:
内核用 inode 结构在内部表示文件,因此他和file结构不同,后者表示打开的我文件描述符。对单个问及那,可能会有很多表示打开的字符描述符的file结构,但它们都指向单个inode结构。
inode结构包含了大量有关文件的信息。作为常规,只有两个字段重要。

通过inode结构体获取主次设备号
unsigned int iminor(struct inode *inode);
unsigend int imajor(struct inode *inode);

src/include/linux/fs.h 2.6.35

  1. 725 struct inode {
  2.  726 struct hlist_node i_hash;
  3.  727 struct list_head i_list; /* backing dev IO list */
  4.  728 struct list_head i_sb_list;
  5.  729 struct list_head i_dentry;
  6.  730 unsigned long i_ino;
  7.  731 atomic_t i_count;
  8.  732 unsigned int i_nlink;
  9.  733 uid_t i_uid;
  10.  734 gid_t i_gid;
  11.  735 dev_t i_rdev; 对表示设备文件的inode结构,该字段包含了真正的设备编号
  12.  736 unsigned int i_blkbits;
  13.  737 u64 i_version;
  14.  738 loff_t i_size;
  15.  739#ifdef __NEED_I_SIZE_ORDERED
  16.  740 seqcount_t i_size_seqcount;
  17.  741#endif
  18.  742 struct timespec i_atime;
  19.  743 struct timespec i_mtime;
  20.  744 struct timespec i_ctime;
  21.  745 blkcnt_t i_blocks;
  22.  746 unsigned short i_bytes;
  23.  747 umode_t i_mode;
  24.  748 spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
  25.  749 struct mutex i_mutex;
  26.  750 struct rw_semaphore i_alloc_sem;
  27.  751 const struct inode_operations *i_op;
  28.  752 const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
  29.  753 struct super_block *i_sb;
  30.  754 struct file_lock *i_flock;
  31.  755 struct address_space *i_mapping;
  32.  756 struct address_space i_data;
  33.  757#ifdef CONFIG_QUOTA
  34.  758 struct dquot *i_dquot[MAXQUOTAS];
  35.  759#endif
  36.  760 struct list_head i_devices;
  37.  761 union {
  38.  762 struct pipe_inode_info *i_pipe;
  39.  763 struct block_device *i_bdev;
  40.  764 struct cdev *i_cdev;表示字符设备的内核的内部结构。当inode指向一个字符设备文件时,该字段包含了指向struct cdev结构的指针。
  41.  765 };
  42.  766
  43.  767 __u32 i_generation;
  44.  768
  45.  769#ifdef CONFIG_FSNOTIFY
  46.  770 __u32 i_fsnotify_mask; /* all events this inode cares about */
  47.  771 struct hlist_head i_fsnotify_mark_entries; /* fsnotify mark entries */
  48.  772#endif
  49.  773
  50.  774#ifdef CONFIG_INOTIFY
  51.  775 struct list_head inotify_watches; /* watches on this inode */
  52.  776 struct mutex inotify_mutex; /* protects the watches list */
  53.  777#endif
  54.  778
  55.  779 unsigned long i_state;
  56.  780 unsigned long dirtied_when; /* jiffies of first dirtying */
  57.  781
  58.  782 unsigned int i_flags;
  59.  783
  60.  784 atomic_t i_writecount;
  61.  785#ifdef CONFIG_SECURITY
  62.  786 void *i_security;
  63.  787#endif
  64.  788#ifdef CONFIG_FS_POSIX_ACL
  65.  789 struct posix_acl *i_acl;
  66.  790 struct posix_acl *i_default_acl;
  67.  791#endif
  68.  792 void *i_private; /* fs or device private pointer */
  69.  793};

file结构  
  1. src/include/linux/fs.h

  2.  914struct file {
  3.  915 /*
  4.  916 * fu_list becomes invalid after file_free is called and queued via
  5.  917 * fu_rcuhead for RCU freeing
  6.  918 */
  7.  919 union {
  8.  920 struct list_head fu_list;
  9.  921 struct rcu_head fu_rcuhead;
  10.  922 } f_u;
  11.  923 struct path f_path;
  12.  924#define f_dentry f_path.dentry
  13.  925#define f_vfsmnt f_path.mnt
  14.  926 const struct file_operations *f_op; 
  15.  927 spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */
  16.  928 atomic_long_t f_count;
  17.  929 unsigned int f_flags; //文件标识 O_RDONLY 只读
  18.  930 fmode_t f_mode;//文件权限值,00666 rwx
  19.  931 loff_t f_pos; // 当前的读写位置
  20.  932 struct fown_struct f_owner;
  21.  933 const struct cred *f_cred;
  22.  934 struct file_ra_state f_ra;
  23.  935
  24.  936 u64 f_version;
  25.  937#ifdef CONFIG_SECURITY
  26.  938 void *f_security;
  27.  939#endif
  28.  940 /* needed for tty driver, and maybe others */
  29.  941 void *private_data;
  30.  942
  31.  943#ifdef CONFIG_EPOLL
  32.  944 /* Used by fs/eventpoll.c to link all the hooks to this file */
  33.  945 struct list_head f_ep_links;
  34.  946#endif /* #ifdef CONFIG_EPOLL */
  35.  947 struct address_space *f_mapping;
  36.  948#ifdef CONFIG_DEBUG_WRITECOUNT
  37.  949 unsigned long f_mnt_write_state;
  38.  950#endif
  39.  951};
在lld3 p57页描述
file结构代表一个打开的文件(它并不仅仅限定于设备驱动程序,系统中每个打开的文件在内核空间都有一个对应的file结构)。它由内核在open时创建,并传递给该文件上进行操作的所有函数,直到最后的close函数。

在内核源码中,这项struct file的指针通常被称为filp。

struct file_opearations *f_op;
对于主设备号为1多的 /dev/null、 /dev/zero等等。的open代码根据要打开的次设备号替换为filp->f_op中的操作。这种技巧允许相同主设备号下的设备实现多种操作行为,而不会增加系统调用的负担。

void *private_data:
open系统调用在调用驱动程序的open方法前将这个指针置为NULL。驱动程序可以讲这个字段用于任何目的或者忽虑这个字段。






阅读(1247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~