欢迎来到kaffeel的博客

点滴积累,快乐分享-kaffeel.org
  首页  :: 新随笔  :: 订阅 订阅  :: 管理

Yaffs文件系统的重要数据结构

Posted on 2013-03-30 17:11  kaffeel  阅读(2423)  评论(0编辑  收藏  举报

1. file_system_to_install

fsinst是结构体file_system_to_install类型的指针,指向需要向内核注册(install)的文件系统相关信息。

  1. /* Stuff to handle installation of file systems */
  2. struct file_system_to_install {
  3.      struct file_system_type *fst;
  4.      int installed;
  5. };

    fst是struct file_system_type结构体类型的指针,指向需要向内核文件系统链表结构注册的文件系统的必要信息,其定义在fs.h中。

installed是文件系统是否注册的标识,当fst指向的文件系统向内核注册成功后,该位置为1,否则置为0;

2. file_system_type

下面看一下file_system_type结构提的定义(一下定义来自ray项目android/kernel/include/linux/fs.h)。

  1. struct file_system_type {
  2.      const char *name;
  3.      int fs_flags;
  4.      int (*get_sb) (struct file_system_type *, int,
  5.                  const char *, void *, struct vfsmount *);
  6.      void (*kill_sb) (struct super_block *);
  7.      struct module *owner;
  8.      struct file_system_type * next;
  9.      struct list_head fs_supers;
  10.      struct lock_class_key s_lock_key;
  11.      struct lock_class_key s_umount_key;
  12.      struct lock_class_key i_lock_key;
  13.      struct lock_class_key i_mutex_key;
  14.      struct lock_class_key i_mutex_dir_key;
  15.      struct lock_class_key i_alloc_sem_key;
  16. };

简要介绍一下file_system_type比较重要的几个成员:

    owner:指向实现文件系统的模块的指针;

    name :文件系统类型名称,如yaffs/yaffs2

    fs_flags:文件系统类型标志,如FS_FAT/FS_NTFS…

    mount:具体文件系统mount函数的函数指针,当我们执行mount 命令时会调用;

    struct file_system_type *next: 用于构建内核file system结构链表,便于插入(insmod)、删除(rmmod)等操作,对于具体文件系统而言,该成员需要置NULL;

    fs_supers:具有相同文件系统类型的超级快对象链表的头;

3. fs_to_install

fs_to_install数组是在mount时向内核注册的文件系统列表,我们看一下其定义:

  1. static struct file_system_to_install fs_to_install[] = {
  2.      {&yaffs_fs_type, 0},
  3.      {&yaffs2_fs_type, 0},
  4.      {NULL, 0}
  5. };
  6. static struct file_system_type yaffs_fs_type = {
  7.      .owner = THIS_MODULE,
  8.      .name = "yaffs",
  9.      .mount = yaffs_mount,
  10.      .kill_sb = kill_block_super,
  11.      .fs_flags = FS_REQUIRES_DEV,
  12. };
  13. static struct file_system_type yaffs2_fs_type = {
  14.      .owner = THIS_MODULE,
  15.      .name = "yaffs2",
  16.      .mount = yaffs2_mount,
  17.      .kill_sb = kill_block_super,
  18.      .fs_flags = FS_REQUIRES_DEV,
  19. };

        从上述代码可以看出,fs_to_install数组有两个元素,分别为yaffs_fs_type和yaffs2_fs_type,两个文件系统分别对file_system_type结构体的owner,name,mount,kill_sb,fs_flags进行赋值。

4. 目录项对象(dentry)

Linux用数据结构dentry来描述文件系统中与某个文件索引节点相链接的一个目录项(可以是文件,也可以是目录)。 每个目录项对象可以处于一下四种状态之一:

1)  空闲状态

处于该状态的目录项对象不包含有效的信息,且还没有被VFS使用。对应的内存区由slab分配器进行处理。

2)  未使用状态

处于该状态的目录项对象当前还没有被内核使用。该对象的引用计数器d_count的值为0,但其d_inode字段仍然指向关联的索引节点。该目录项对象包含有效的信息,但为了在必要时回收,他的内容可能被丢弃。

3)  正在使用状态

处于该状态的目录项对象当前正在被内核使用。该对象的引用计数器d_count的值为正数,其d_inode字段指向关联的索引节点对象。该目录项对象包含有效的信息,并且不能被丢弃。

4)  负状态

与目录项关联的索引节点不复存在,那是因为相应的磁盘索引节点已经被删除,或者因为目录项对象是通过解析一个不存在的文件的路径名创建的。目录项对象的d_inode字段被置为NULL,但该对象仍然被保存在目录项高速缓存中,以便后续对同一文件目录名查找操作能够快速完成。

其定义如下:

  1. struct dentry {
  2.      atomic_t d_count;
  3.      unsigned int d_flags;          /* protected by d_lock */
  4.      spinlock_t d_lock;          /* per dentry lock */
  5.      struct inode *d_inode;          /* Where the name belongs to - NULL is
  6.                           * negative */
  7.      /*
  8.       * The next three fields are touched by __d_lookup.  Place them here
  9.       * so they all fit in a 16-byte range, with 16-byte alignment.
  10.       */
  11.      struct dentry *d_parent;     /* parent directory */
  12.      struct qstr d_name;
  13.      struct list_head d_lru;          /* LRU list */
  14.      struct list_head d_child;     /* child of parent list */
  15.      struct list_head d_subdirs;     /* our children */
  16.      struct list_head d_alias;     /* inode alias list */
  17.      unsigned long d_time;          /* used by d_revalidate */
  18.      struct dentry_operations *d_op;
  19.      struct super_block *d_sb;     /* The root of the dentry tree */
  20.      void *d_fsdata;               /* fs-specific data */
  21.      struct rcu_head d_rcu;
  22.      struct dcookie_struct *d_cookie; /* cookie, if any */
  23.      struct hlist_node d_hash;     /* lookup hash list */     
  24.      int d_mounted;
  25.      unsigned char d_iname[DNAME_INLINE_LEN_MIN];     /* small names */
  26. };

dentry 的主要字段的含义解释如下:

  •     d_flags:    目录项高速缓存标志;
  •     d_count:    目录项对象引用计数器;
  •     d_lock:     保护目录项对象的自旋锁;
  •     d_inode: 与文件名关联的索引节点;
  •     d_parent:   父目录的目录项对象;
  •     d_name: 文件名;
  •     d_lru:      用于未使用目录项链表的指针;
  •     d_child:    对目录而言,用于同一父目录中的目录项链表的指针;
  •     d_subdirs:  对目录而言,子目录项链表的头;
  •     d_alias: 用于与统一索引节点(别名)相关的目录项链表的指针;
  •     d_op: 目录项Operation方法;
  •     d_sb: 文件的超级块对象;
  •     d_fsdata: 依赖于文件系统的数据;
  •     d_rcu:      回收目录项对象时,由RCU描述符使用;

5. dentry_operations

与目录项对象关联的方法(const struct dentry_operations *d_op;)称为目录项操作。这些方法由dentry_operations结构体来描述,该结构的地址存放在d_op字段中。

  1. struct dentry_operations {
  2.      int (*d_revalidate)(struct dentry *, struct nameidata *);
  3.      int (*d_hash) (struct dentry *, struct qstr *);
  4.      int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
  5.      int (*d_delete)(struct dentry *);
  6.      void (*d_release)(struct dentry *);
  7.      void (*d_iput)(struct dentry *, struct inode *);
  8. };
  • d_revalidate:把目录项对象转换为一个文件路径名之前,判断该目录项对象是否仍然有效,缺省的VFS函数什么都不做。
  • d_hash:生成一个散列值,这是用于目录项散列表的,特定于具体文件系统的散列函数。参数dentry标识包含路径分量的目录,参数name指向一个结构,该结构包含要查找的路径名分量以及由散列函数生成的散列值。
  • d_compare:比较两个文件名。name1应该属于dir所指向的目录。缺省的VFS函数是常用的字符串匹配函数。
  • d_delete:对当前目录项对象的最后一个索引被删除(d_count变为0)时,调用该方法。缺省的VFS函数什么都不做。
  • d_release:当要释放一个目录项对象时(放入slab分配器),调用该方法。
  • d_iput:当一个目录项对象变为负状态(即丢弃它的索引节点)时,带哦用该方法。缺省的VFS函数调用input()释放索引节点对象。

6. 超级块对象(super_block)

对具体文件系统的超级块是文件系统中最重要的数据结构,它用来描述整个文件系统信息(组织结构和管理信息)。不涉及文件系统的内容。VFS超级块是各种具体文件系统在安装时建立的,并在这些文件系统卸载时自动删除。VFS超级块实际上应该说成是某个具体文件系统的VFS超级块。超级块对象由super_block结构组成。

  1. struct super_block {
  2.      struct list_head     s_list;          /* Keep this first */
  3.      dev_t               s_dev;          /* search index; _not_ kdev_t */
  4.      unsigned long          s_blocksize;
  5.      unsigned long          s_old_blocksize;
  6.      unsigned char          s_blocksize_bits;
  7.      unsigned char          s_dirt;
  8.      unsigned long long     s_maxbytes;     /* Max file size */
  9.      struct file_system_type     *s_type;
  10.      struct super_operations     *s_op;
  11.      struct dquot_operations     *dq_op;
  12.      struct quotactl_ops     *s_qcop;
  13.      struct export_operations *s_export_op;
  14.      unsigned long          s_flags;
  15.      unsigned long          s_magic;
  16.      struct dentry          *s_root;
  17.      struct rw_semaphore     s_umount;
  18.      struct semaphore     s_lock;
  19.      int               s_count;
  20.      int               s_syncing;
  21.      int               s_need_sync_fs;
  22.      atomic_t          s_active;
  23.      void                    *s_security;
  24.      struct xattr_handler     **s_xattr;
  25.      struct list_head     s_inodes;     /* all inodes */
  26.      struct list_head     s_dirty;     /* dirty inodes */
  27.      struct list_head     s_io;          /* parked for writeback */
  28.      struct hlist_head     s_anon;          /* anonymous dentries for (nfs) exporting */
  29.      struct list_head     s_files;
  30.      struct block_device     *s_bdev;
  31.      struct list_head     s_instances;
  32.      struct quota_info     s_dquot;     /* Diskquota specific options */
  33.      int               s_frozen;
  34.      wait_queue_head_t     s_wait_unfrozen;
  35.      char s_id[32];                    /* Informational name */
  36.      void                *s_fs_info;     /* Filesystem private info */
  37.      /*
  38.       * The next field is for VFS *only*. No filesystems have any business
  39.       * even looking at it. You had been warned.
  40.       */
  41.      struct semaphore s_vfs_rename_sem;     /* Kludge */
  42.      /* Granuality of c/m/atime in ns.
  43.         Cannot be worse than a second */
  44.      u32             s_time_gran;
  45. };

所有超级块对象都以双向循环链表的形式链接在一起,链表中第一个元素用super_block变量来表示,而超级块对象的s_list字段存放指向链表相邻元素的指针。在yaffs文件系统中s_fs_info指向和该超级块关联的yaffs_dev结构(2.6: sb->s_fs_info  points to the struct yaffs_dev associated with this superblock)。

7. 超级块操作(super_operations)

    和超级块相关联的方法是超级块操作,这些操作由数据结构super_operations来描述,该结构的起始地址存放在super_block的s_op指针中。每一个具体的文件系统都可以定义自己的超级块操作,当VFS需要带哦用其中一个操作时,如read_inode(),执行路径是:sb->s_op->read_inode(inode);

    super_operations数据结构的定义如下:

  1. /*
  2. * NOTE: write_inode, delete_inode, clear_inode, put_inode can be called
  3. * without the big kernel lock held in all filesystems.
  4. */
  5. struct super_operations {
  6.         struct inode *(*alloc_inode)(struct super_block *sb);
  7.      void (*destroy_inode)(struct inode *);
  8.      void (*read_inode) (struct inode *);
  9.         void (*dirty_inode) (struct inode *);
  10.      int (*write_inode) (struct inode *, int);
  11.      void (*put_inode) (struct inode *);
  12.      void (*drop_inode) (struct inode *);
  13.      void (*delete_inode) (struct inode *);
  14.      void (*put_super) (struct super_block *);
  15.      void (*write_super) (struct super_block *);
  16.      int (*sync_fs)(struct super_block *sb, int wait);
  17.      void (*write_super_lockfs) (struct super_block *);
  18.      void (*unlockfs) (struct super_block *);
  19.      int (*statfs) (struct super_block *, struct kstatfs *);
  20.      int (*remount_fs) (struct super_block *, int *, char *);
  21.      void (*clear_inode) (struct inode *);
  22.      void (*umount_begin) (struct super_block *);
  23.      int (*show_options)(struct seq_file *, struct vfsmount *);
  24.      ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  25.      ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  26. };
  • alloc_inode(sb)

为索引节点对象分配空间,包括具体文件系统的数据所需要的空间。

  • destroy_inode(inode)

撤销索引节点对象,包括具体文件系统的数据。

  • dirty_inode(inode,flags)

当索引节点标记为修改(脏)时调用。Ext3文件系统用它来更新新盘上的文件系统日志。

  • write_inode(inode,wbc)

用通过传递参数指定的索引节点对象的内容更新一个文件系统的索引节点。索引节点对象的i_ino字段标识所涉及磁盘上文件系统的索引节点。

  • drop_inode(inode)

在即将撤销索引节点时调用,即:当最后一个用户释放该索引节点时;实现该方法的文件系统通常使用generic_drop_inode()函数,该函数从VFS数据结构中一走对索引节点的每一个索引,如果索引节点不再出现在任何目录中,则调用超级块方法delete_inode将它从文件系统中删除。

  • evict_inode(inode)
  • put_super(sb)

释放通过传递的参数指定的超级块对象(因为相应的文件系统被卸载)。

  • sync_fs(sb,wait)

在清除文件系统来更新磁盘上的具体文件系统数据结构时调用(由如yaffs等日志文件系统使用)。

  • freeze_fs(sb)
  • unfreeze_fs(sb)
  • statfs(dentry,kstatfs)

将文件系统的统计信息返回,填写在buf缓冲区中。

  • remount_fs(sb)

用新的选项重新挂载文件系统(当某个挂载选项必须被修改是该函数被调用)。

  • umount_begin(sb)

终端一个挂载操作,因为相应的卸载操作已经开始(只在网络文件系统中使用)。

  • show_options(seq_file, dentry)

用来显示特定文件系统的选项。

  • show_devname(seq_file, dentry)
  • show_path(seq_file, dentry)
  • show_stats(seq_file, dentry)
  • quota_read(sb)

限额系统使用该方法从文件系统中读取数据,该文件详细说明了所在文件系统的限制。

  • quota_write(sb)

限额系统使用该方法将数据写入文件,该文件详细说明了所在文件系统的限制。

  • bdev_try_to_free_page(sb,page,gfp_t)
  • nr_cached_objects(sb)
  • free_cached_objects(sb, int)

以上方法对是对所有可能的文件系统类型均是可用的。具体某一个文件系统,某些方法可能不需要,这是将对应的字段置为NULL即可。

8. 块设备结构(block_device)

块设备是Yaffs操作的设备,块设备驱动程序是Linux块子系统中的最底层组件。每一个块设备都有一个block_device结构的描述符表示.

  1. struct block_device {
  2.      dev_t               bd_dev;  /* not a kdev_t - it's a search key */
  3.      struct inode *          bd_inode;     /* will die */
  4.      int               bd_openers;
  5.      struct semaphore     bd_sem;     /* open/close mutex */
  6.      struct semaphore     bd_mount_sem;     /* mount mutex */
  7.      struct list_head     bd_inodes;
  8.      void *               bd_holder;
  9.      int               bd_holders;
  10.      struct block_device *     bd_contains;
  11.      unsigned          bd_block_size;
  12.      struct hd_struct *     bd_part;
  13.      /* number of times partitions within this device have been opened. */
  14.      unsigned          bd_part_count;
  15.      int               bd_invalidated;
  16.      struct gendisk *     bd_disk;
  17.      struct list_head     bd_list;
  18.      struct backing_dev_info *bd_inode_backing_dev_info;
  19.      /*
  20.       * Private data.  You must have bd_claim'ed the block_device
  21.       * to use this.  NOTE:  bd_claim allows an owner to claim
  22.       * the same device multiple times, the owner must take special
  23.       * care to not mess up bd_private for that case.
  24.       */
  25.      unsigned long          bd_private;
  26. };

 

9. yaffs文件系统参数(yaffs_param

数据结构yaffs_param由yaffs文件系统设备相关的参数组成,如chunk大小,block大小,起始block和结束block,保留block数等等,其定义如下:

  1. struct yaffs_param {
  2.      const YCHAR *name;
  3.      /*
  4.       * Entry parameters set up way early. Yaffs sets up the rest.
  5.       * The structure should be zeroed out before use so that unused
  6.       * and default values are zero.
  7.       */
  8.      int inband_tags;     /* Use unband tags */
  9.      u32 total_bytes_per_chunk;     /* Should be >= 512, does not need to
  10.                           be a power of 2 */
  11.      int chunks_per_block;     /* does not need to be a power of 2 */
  12.      int spare_bytes_per_chunk;     /* spare area size */
  13.      int start_block;     /* Start block we're allowed to use */
  14.      int end_block;          /* End block we're allowed to use */
  15.      int n_reserved_blocks;     /* Tuneable so that we can reduce
  16.                      * reserved blocks on NOR and RAM. */
  17.      int n_caches;          /* If <= 0, then short op caching is disabled,
  18.                      * else the number of short op caches.
  19.                      */
  20.      int cache_bypass_aligned; /* If non-zero then bypass the cache for
  21.                        * aligned writes.
  22.                        */
  23.      int use_nand_ecc;     /* Flag to decide whether or not to use
  24.                      * NAND driver ECC on data (yaffs1) */
  25.      int tags_9bytes;     /* Use 9 byte tags */
  26.      int no_tags_ecc;     /* Flag to decide whether or not to do ECC
  27.                      * on packed tags (yaffs2) */
  28.      int is_yaffs2;          /* Use yaffs2 mode on this device */
  29.      int empty_lost_n_found;     /* Auto-empty lost+found directory on mount */
  30.      int refresh_period;     /* How often to check for a block refresh */
  31.      /* Checkpoint control. Can be set before or after initialisation */
  32.      u8 skip_checkpt_rd;
  33.      u8 skip_checkpt_wr;
  34.      int enable_xattr;     /* Enable xattribs */
  35.      int max_objects;     /*
  36.                      * Set to limit the number of objects created.
  37.                      * 0 = no limit.
  38.                     */
  39.      /* The remove_obj_fn function must be supplied by OS flavours that
  40.       * need it.
  41.       * yaffs direct uses it to implement the faster readdir.
  42.       * Linux uses it to protect the directory during unlocking.
  43.       */
  44.      void (*remove_obj_fn) (struct yaffs_obj *obj);
  45.      /* Callback to mark the superblock dirty */
  46.      void (*sb_dirty_fn) (struct yaffs_dev *dev);
  47.      /*  Callback to control garbage collection. */
  48.      unsigned (*gc_control_fn) (struct yaffs_dev *dev);
  49.      /* Debug control flags. Don't use unless you know what you're doing */
  50.      int use_header_file_size;     /* Flag to determine if we should use
  51.                           * file sizes from the header */
  52.      int disable_lazy_load;     /* Disable lazy loading on this device */
  53.      int wide_tnodes_disabled;     /* Set to disable wide tnodes */
  54.      int disable_soft_del;     /* yaffs 1 only: Set to disable the use of
  55.                      * softdeletion. */
  56.      int defered_dir_update;     /* Set to defer directory updates */
  57. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  58.      int auto_unicode;
  59. #endif
  60.      int always_check_erased;     /* Force chunk erased check always on */
  61.      int disable_summary;
  62. };

 

  • name:保存mtd设备的名称;
  • n_reserved_blocks: 设备的保留块,用于后续坏块??
  • n_caches :该字段决定yaffs文件系统是否使用告诉缓存块;
  • inband_tags:标志位,默认值为0,即采用OOB(Out Of Band)的方式存储tags,可以通过挂载时指定inband_tags选项改变默认值;
  • total_bytes_per_chunk:每个chunk总的字节数,该字段的值需要大于等于512,但不必是2的次方数;
  • chunks_per_block:每个block的chunk数;
  • spare_bytes_per_chunk:每个chunk包含的OOB区域字节数;
  • start_block:第一个可以使用的block;
  • end_block:最后一个可以使用的block;
  • n_reserved_blocks:为GC保留的block的阀值;
  • n_caches:高速缓存的个数,YAFFS2为了减少数据的碎片以及提高性能为每个文件的读写操作提供了cache,一般小于10个;
  • no_tags_ecc:标志为,默认为0,即tags中包含ECC校验信息,可以通过内核配置(CONFIG_YAFFS_DISABLE_TAGS_ECC)改变默认值。
  • is_yaffs2 :标志位,默认为0,即文件系统版本为yaffs,该字段值会在mount过程中根据条件及mtd设备的writesize进行自动转换。
  • refresh_period:block刷新周期,刷新目的主要找出最旧的处于YAFFS_BLOCK_STATE_FULL状态的block,供GC作为gc_block使用;
  • skip_checkpt_rd: 标志位,默认为0,支持读取checkpoint,可以提高挂载速度。可以通过挂载时指定挂载选项no-checkpoint-read或no-checkpoint修改默认值;
  • skip_checkpt_wr:标志位,默认为0,支持写入checkpoint。可以通过挂载是指定选项no-checkpoint-write或no-checkpoint修改默认值。
  • wide_tnodes_disabled:标志位,默认为0,采用动态位宽,通过内核配置修改CONFIG_YAFFS_DISABLE_WIDE_TNODES配置项可以采用动态位宽。

10.yaffs tag处理函数(tagger)

yaffs_tags_handler结构由yaffs tag的处理函数指针组成,其定义如下:

  1. struct yaffs_tags_handler {
  2.      int (*write_chunk_tags_fn) (struct yaffs_dev *dev,
  3.                         int nand_chunk, const u8 *data,
  4.                         const struct yaffs_ext_tags *tags);
  5.      int (*read_chunk_tags_fn) (struct yaffs_dev *dev,
  6.                        int nand_chunk, u8 *data,
  7.                        struct yaffs_ext_tags *tags);
  8.      int (*query_block_fn) (struct yaffs_dev *dev, int block_no,
  9.                       enum yaffs_block_state *state,
  10.                       u32 *seq_number);
  11.      int (*mark_bad_fn) (struct yaffs_dev *dev, int block_no);
  12. };

 

11.yaffs设备(yaffs_dev)

数据结构struct yaffs_dev描述了yaffs文件系统很多信息,如与MTD层交互的接口、NAND的参数(page大小、block数和分区起始位置等)、文件tnode节点的位宽、所有block的使用情况、所有chunk的bitmap、垃圾回收和checkpoint的运行时信息等。

一个设备只能挂载一个yaffs文件系统,后续的垃圾回收等操作,都是基于该yaffs_dev来做的,yaffs_dev是一个RAM-resident的数据结构。其定义如下:

  1. struct yaffs_dev {
  2.      struct yaffs_param param;
  3.      struct yaffs_driver drv;
  4.      struct yaffs_tags_handler tagger;
  5.      /* Context storage. Holds extra OS specific data for this device */
  6.      void *os_context;
  7.      void *driver_context;
  8.      struct list_head dev_list;
  9.      /* Runtime parameters. Set up by YAFFS. */
  10.      int data_bytes_per_chunk;
  11.      /* Non-wide tnode stuff */
  12.      u16 chunk_grp_bits;     /* Number of bits that need to be resolved if
  13.                      * the tnodes are not wide enough.
  14.                      */
  15.      u16 chunk_grp_size;     /* == 2^^chunk_grp_bits */
  16.      /* Stuff to support wide tnodes */
  17.      u32 tnode_width;
  18.      u32 tnode_mask;
  19.      u32 tnode_size;
  20.      /* Stuff for figuring out file offset to chunk conversions */
  21.      u32 chunk_shift;     /* Shift value */
  22.      u32 chunk_div;          /* Divisor after shifting: 1 for 2^n sizes */
  23.      u32 chunk_mask;          /* Mask to use for power-of-2 case */
  24.      int is_mounted;
  25.      int read_only;
  26.      int is_checkpointed;
  27.      /* Stuff to support block offsetting to support start block zero */
  28.      int internal_start_block;
  29.      int internal_end_block;
  30.      int block_offset;
  31.      int chunk_offset;
  32.      /* Runtime checkpointing stuff */
  33.      int checkpt_page_seq;     /* running sequence number of checkpt pages */
  34.      int checkpt_byte_count;
  35.      int checkpt_byte_offs;
  36.      u8 *checkpt_buffer;
  37.      int checkpt_open_write;
  38.      int blocks_in_checkpt;
  39.      int checkpt_cur_chunk;
  40.      int checkpt_cur_block;
  41.      int checkpt_next_block;
  42.      int *checkpt_block_list;
  43.      int checkpt_max_blocks;
  44.      u32 checkpt_sum;
  45.      u32 checkpt_xor;
  46.      int checkpoint_blocks_required;     /* Number of blocks needed to store
  47.                           * current checkpoint set */
  48.      /* Block Info */
  49.      struct yaffs_block_info *block_info;
  50.      u8 *chunk_bits;          /* bitmap of chunks in use */
  51.      unsigned block_info_alt:1;     /* allocated using alternative alloc */
  52.      unsigned chunk_bits_alt:1;     /* allocated using alternative alloc */
  53.      int chunk_bit_stride;     /* Number of bytes of chunk_bits per block.
  54.                      * Must be consistent with chunks_per_block.
  55.                      */
  56.      int n_erased_blocks;
  57.      int alloc_block;     /* Current block being allocated off */
  58.      u32 alloc_page;
  59.      int alloc_block_finder;     /* Used to search for next allocation block */
  60.      /* Object and Tnode memory management */
  61.      void *allocator;
  62.      int n_obj;
  63.      int n_tnodes;
  64.      int n_hardlinks;
  65.      struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
  66.      u32 bucket_finder;
  67.      int n_free_chunks;
  68.      /* Garbage collection control */
  69.      u32 *gc_cleanup_list;     /* objects to delete at the end of a GC. */
  70.      u32 n_clean_ups;
  71.      unsigned has_pending_prioritised_gc;     /* We think this device might
  72.                               have pending prioritised gcs */
  73.      unsigned gc_disable;
  74.      unsigned gc_block_finder;
  75.      unsigned gc_dirtiest;
  76.      unsigned gc_pages_in_use;
  77.      unsigned gc_not_done;
  78.      unsigned gc_block;
  79.      unsigned gc_chunk;
  80.      unsigned gc_skip;
  81.      struct yaffs_summary_tags *gc_sum_tags;
  82.      /* Special directories */
  83.      struct yaffs_obj *root_dir;
  84.      struct yaffs_obj *lost_n_found;
  85.      int buffered_block;     /* Which block is buffered here? */
  86.      int doing_buffered_block_rewrite;
  87.      struct yaffs_cache *cache;
  88.      int cache_last_use;
  89.      /* Stuff for background deletion and unlinked files. */
  90.      struct yaffs_obj *unlinked_dir;     /* Directory where unlinked and deleted
  91.                           files live. */
  92.      struct yaffs_obj *del_dir;     /* Directory where deleted objects are
  93.                          sent to disappear. */
  94.      struct yaffs_obj *unlinked_deletion;     /* Current file being
  95.                                    background deleted. */
  96.      int n_deleted_files;     /* Count of files awaiting deletion; */
  97.      int n_unlinked_files;     /* Count of unlinked files. */
  98.      int n_bg_deletions;     /* Count of background deletions. */
  99.      /* Temporary buffer management */
  100.      struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
  101.      int max_temp;
  102.      int temp_in_use;
  103.      int unmanaged_buffer_allocs;
  104.      int unmanaged_buffer_deallocs;
  105.      /* yaffs2 runtime stuff */
  106.      unsigned seq_number;     /* Sequence number of currently
  107.                          allocating block */
  108.      unsigned oldest_dirty_seq;
  109.      unsigned oldest_dirty_block;
  110.      /* Block refreshing */
  111.      int refresh_skip;     /* A skip down counter.
  112.                      * Refresh happens when this gets to zero. */
  113.      /* Dirty directory handling */
  114.      struct list_head dirty_dirs;     /* List of dirty directories */
  115.      /* Summary */
  116.      int chunks_per_summary;
  117.      struct yaffs_summary_tags *sum_tags;
  118.      /* Statistics */
  119.      u32 n_page_writes;
  120.      u32 n_page_reads;
  121.      u32 n_erasures;
  122.      u32 n_bad_markings;
  123.      u32 n_erase_failures;
  124.      u32 n_gc_copies;
  125.      u32 all_gcs;
  126.      u32 passive_gc_count;
  127.      u32 oldest_dirty_gc_count;
  128.      u32 n_gc_blocks;
  129.      u32 bg_gcs;
  130.      u32 n_retried_writes;
  131.      u32 n_retired_blocks;
  132.      u32 n_ecc_fixed;
  133.      u32 n_ecc_unfixed;
  134.      u32 n_tags_ecc_fixed;
  135.      u32 n_tags_ecc_unfixed;
  136.      u32 n_deletions;
  137.      u32 n_unmarked_deletions;
  138.      u32 refresh_count;
  139.      u32 cache_hits;
  140.      u32 tags_used;
  141.      u32 summary_used;
  142. };

 

  • struct yaffs_param param: yaffs文件系统和设备相关的参数;
  • struct yaffs_driver drv: yaffs设备驱动结构,由yaffs文件系统对MTD设备操作的函数指针组成,包括读、写、擦除、标记坏块、坏块检查、驱动初始化和驱动注销等操作MTD设备的接口函数,其初始化是在yaffs_mtd_drv_install()函数中完成。其定义如下:
  1. struct yaffs_driver {
  2.      int (*drv_write_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
  3.                        const u8 *data, int data_len,
  4.                        const u8 *oob, int oob_len);
  5.      int (*drv_read_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
  6.                        u8 *data, int data_len,
  7.                        u8 *oob, int oob_len,
  8.                        enum yaffs_ecc_result *ecc_result);
  9.      int (*drv_erase_fn) (struct yaffs_dev *dev, int block_no);
  10.      int (*drv_mark_bad_fn) (struct yaffs_dev *dev, int block_no);
  11.      int (*drv_check_bad_fn) (struct yaffs_dev *dev, int block_no);
  12.      int (*drv_initialise_fn) (struct yaffs_dev *dev);
  13.      int (*drv_deinitialise_fn) (struct yaffs_dev *dev);
  14. };
  • struct yaffs_tags_handler tagger: yaffs文件系统tag相关的处理函数;
  • void *os_context:指向yaffs_linux_context数据结构的指针,该数据结构存储yaffs的运行环境,定义如下:
  1. struct yaffs_linux_context {
  2.      struct list_head context_list;     /* List of these we have mounted */
  3.      struct yaffs_dev *dev;
  4.      struct super_block *super;
  5.      struct task_struct *bg_thread;     /* Background thread for this device */
  6.      int bg_running;
  7.      struct mutex gross_lock;     /* Gross locking mutex*/
  8.      u8 *spare_buffer;     /* For mtdif2 use. Don't know the buffer size
  9.                      * at compile time so we have to allocate it.
  10.                      */
  11.      struct list_head search_contexts;
  12.      struct task_struct *readdir_process;
  13.      unsigned mount_id;
  14.      int dirty;
  15. };
    •  context_list: 通过该字段加入到yaffs_context_list全局链表中;
    •  dev:  指向YAFFS文件系统超级块的设备的指针;
    •  super: 指向VFS层超级块的指针;
    •  bg_thread: YAFFS2后台来即回收线程的指针;
    •  bg_running: 启动和停止垃圾回收线程标志位,1为启动,0为停止;
    •  gross_lock: 互斥锁,保护整个超级块关键字段的互斥访问,粒度比较大;
    •  spare_buffer: OOB块的缓冲区;
    •  search_contexts: 通过该字段把所有Directory Search Context组成链表;
    •  readdir_process: 解决使用NFS思索问题而加入的字段;
    •  mount_id: 每个NAND Flash分区挂载Yaffs2都分配不同的ID号;
  • driver_context: 指向mtd_info结构体的指针,mtd_info是MTD子系统的核心数据结构,主要对底层硬件进行封装;
  • data_bytes_per_chunk: 每个chunk总的字节数,和前面提到的total_bytes_per_chunk一样;
  • chunk_grp_bits: 采用静态位宽时超过tnode_width宽度后的位数,采用动态位宽时,其值恒为0;
  • chunk_grp_size: 由chunk_grp_bits转化而来的大小;
  • tnode_width: 位宽的mask,主要用于快速获取chunk id号;
  • tnode_size: YAFFS_NTNODES_LEVEL0节点所占用的内存大小,单位是byte;
  • chunk_shift:主要用来计算logical chunk index以及logical chunk offset;
  • chunk_div:作用同chunk_shift,主要用于chunk大小不是2次幂的情况;
  • chunk_mask:作用同chunk_shift,组合起来计算logical chunk offset;
  • is_mounted:标志位,文件系统挂载时被置位checkpoint是为提高挂载速度而引入的功能,作用同JFFS2的EBS,以空间换取时间,卸载时通过在NAND Flash上保存文件系统超级块快照,挂载时获取快照信息可以快速还原系统状态。
  • checkpt_page_seq: 控制checkpoint的写入或者读出时的chunk序列号;
  • checkpt_byte_count: 写入或读出checkpoint信息的字节数;
  • checkpt_byte_offs: checkpt_buffer缓冲区的偏移量;
  • checkpt_buffer:写入或读出checkpoint的缓冲区,大小为data_bytes_per_chunk字节;
  • checkpt_open_write:标志位,yaffs2_checkpt_open时传入的,决定checkpoint读写属性;
  • blocks_in_checkpt:chenkpoint使用的block的数量;
  • checkpt_cur_chunk:当前block已经使用掉的chunk数量;
  • checkpt_cur_block:当前正在使用的block编;
  • checkpt_next_block:当前正在使用的下一个block编号;
  • checkpt_block_list:在读checkpoint时使用,该数组中保留使用的block编号,checkpoint信息读取完毕,根据这个数组保存的block编号,更改block的状态为YAFFS_BLOCK_TATE_ CHECKPOINT,以便卸载时检测到这样状态的block时擦除;
  • checkpt_max_blocks:checkpoint能使用的block最大数量;
  • checkpoint_blocks_required:checkpoint信息所需的block数量,看该字段的计算大致就知道checkpoint需要保存文件系统的哪些信息;
  • chunk_bits:指向擦出块位图的指针,每一位对应一个chunk,置0表示没有被使用,置1表示在使用中;
  • block_info_alt:标志位,采用kmalloc分配block_info所使用的内存则置为0,采用vmalloc分配的内存则置为1;
  • chunk_bits_alt:标志位,采用kmalloc分配chunk_bits所使用的内存则置为0,采用vmalloc分配的内存则置为1;
  • chunk_bit_stride:位宽,擦出块中每一个chunk占一位,总的位宽应等于chunks_per_block,要求按byte对其;
  • n_erased_blocks:空闲擦出块的数目;
  • alloc_block:当前正在写入的擦出块在block_info指针数组中的下标;
  • alloc_page:当前正在写入的擦出块中chunk的序列号;
  • alloc_block_finder:记录下一个被选择写入的擦出块在block_info指针数组的下标;
  • block_info:指向yaffs_block_info类型的指针数组,数组每一项表示一块擦除块的统计信息,由yaffs_block_info表示,该信息在运行时只存在于内存中,当YAFFS2被卸载时,该数据当作checkpoint信息被记录下来,在下一次挂载时被读出并恢复,具体定义如下:
  1. struct yaffs_block_info {
  2.      int soft_del_pages:10;     /* number of soft deleted pages */
  3.      int pages_in_use:10;     /* number of pages in use */
  4.      unsigned block_state:4;     /* One of the above block states. */
  5.                     /* NB use unsigned because enum is sometimes
  6.                      * an int */
  7.      u32 needs_retiring:1;     /* Data has failed on this block, */
  8.                     /*need to get valid data off and retire*/
  9.      u32 skip_erased_check:1;/* Skip the erased check on this block */
  10.      u32 gc_prioritise:1;     /* An ECC check or blank check has failed.
  11.                        Block should be prioritised for GC */
  12.      u32 chunk_error_strikes:3;     /* How many times we've had ecc etc
  13.                     failures on this block and tried to reuse it */
  14.      u32 has_summary:1;     /* The block has a summary */
  15.      u32 has_shrink_hdr:1;     /* This block has at least one shrink header */
  16.      u32 seq_number;          /* block sequence number for yaffs2 */
  17. };
    •  soft_del_pages:在YAFFS2包含两种类型的删除,delete和soft delete。前者用于文件内容的更新,比如修改文件中的部分内容,这时YAFFS2会分配新的chunk,将修改后的内容写入新的chunk中,原来chunk的内容自然就没用了,所有将pages_in_use减1,并修改chunk_bits。后者用于文件的删除,YAFFS2在删除文件时,只是删除该文件在内存中的一些描述结构,而被删除文件所占用的chunk不会立即释放,不会删除文件的内容,在后续的文件系统操作中一般也不会把这些chunk分配出去,直到系统进行垃圾收集时才有选择地释放这些chunk。soft_del_pages就表示soft delete的chunk数目;
    •  pages_in_use:该擦除块中被使用的chunk数目,包括已经被soft delete的chunk;
    •  block_state:该擦除块的状态,比如, YAFFS_BLOCK_STATE_FULL表示该擦除块中所有的chunk已经被分配完,YAFFS_BLOCK_STATE_DIRTY表示该擦除块中所有的chunk已经被delete可以被擦除了,YAFFS_BLOCK_STATE_CHECK POINT表示该擦除块保存的是checkpoint信息,YAFFS_BLOCK_STATE_EMPTY表示空闲的擦除块;
    •  needs_retiring:标志位,chunk_error_strikes次数超过3次以上就会置位,该擦除块需要重新回收擦除;
    •  skip_erased_check:标志位,置0时需要对擦除块的进行检测,一般只检测该擦除块的第一个chunk,置1时跳过对擦除块的检测,可以通过CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED设置;
    •  gc_prioritise:标志位,该块发生过ECC校验错误或check失败,需要在垃圾回收时优先擦除;
    •  chunk_error_strikes:发生ECC校验错误的次数;
    •  has_shrink_hdr:标志位,置0时表示该擦除块上的存储的文件被没有发生过截断truncate,即文件大小没有发生过变化resize,否则必须在文件的objectheader标识,同时该object header所在擦除块上也标识,即has_shrink_hdr置1;
    •  seq_number:序列号,表示擦除块被使用的先后顺序,序号越小越早被使用,在挂载时起到非常重要的作用;
  •    n_boj:文件系统中已经分配的struct yaffs_obj的数量,计算checkpoint信息时使用;
  •    n_tnodes:文件系统中已经分配的struct yaffs_tnode的数量,计算checkpoint信息时使用;
  •    obj_bucket:struct yaffs_obj对象的散列表,以obj_id为键,便于文件查找和操作;
  •    bucket_finder: 散列表中最短链表的索引;
  •    n_free_chunks:flash中空闲的chunk数量;
  •    allocator:指向struct yaffs_allocator结构的指针,YAFFS2文件系统实现的一个内存分配器,主要为struct yaffs_allocator、struct yaffs_tnode分配高速缓存替代原有的slab,struct yaffs_allocator定义如下:
  1. struct yaffs_allocator {
  2.      int n_tnodes_created;
  3.      struct yaffs_tnode *free_tnodes;
  4.      int n_free_tnodes;
  5.      struct yaffs_tnode_list *alloc_tnode_list;
  6.      int n_obj_created;
  7.      struct list_head free_objs;
  8.      int n_free_objects;
  9.      struct yaffs_obj_list *allocated_obj_list;
  10. };
    • n_tnodes_created:统计值,分配过总的struct yaffs_tnode数目;
    • free_tnodes:指向一块连续的内存区域,这块连续内存被分成YAFFS_ALLOCA TION_NTNODE个struct yaffs_tnode结构大小的内存;
    • n_free_tnodes:分配器中空闲tnodes的数目;
    • alloc_tnode_list:把yaffs_tnode连续的内存区域存放到此链表中;
    • n_obj_created:统计值,分配过总的struct yaffs_obj数目;
    • free_objs:指向一块连续的内存区域,这块连续内存被分成YAFFS_ALLO CATION_NOBJECTS个struct yaffs_obj结构大小的内存;
    • n_free_objects:分配器中空闲objects的数目;
    • allocated_obj_list:把yaffs_obj连续的内存区域存放到此链表中;

这里引入了两个最核心的数据结构struct yaffs_tnode和struct yaffs_obj。

     struct yaffs_tnode结构体的定义如下:

  1. /*
  2. * Tnodes form a tree with the tnodes in "levels"
  3. * Levels greater than 0 hold 8 slots which point to other tnodes.
  4. * Those at level 0 hold 16 slots which point to chunks in NAND.
  5. * A maximum level of 8 thust supports files of size up to:  2^(3*MAX_LEVEL+4)
  6. *
  7. * Thus a max level of 8 supports files with up to 2^^28 chunks which gives
  8. * a maximum file size of around 512Gbytees with 2k chunks.
  9. */
  10. #define YAFFS_NTNODES_LEVEL0          16
  11. #define YAFFS_NTNODES_INTERNAL          (YAFFS_NTNODES_LEVEL0 / 2)
  12. /*--------------------------- Tnode -------------------------- */
  13. struct yaffs_tnode {
  14.      struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
  15. };

yaffs_tnode是一个长度为YAFFS_NTNODES_INTERNAL的yaffs_tnode结构的指针数组。据此结构创建的节点树最底层的节点称为叶子节点,中间的是非叶子节点。树的所有节点都是yaffs_tnode结构类型。当节点为非叶节点时,数组中的每个元素都指向下一层子节点;当节点为叶子节点时,该数组拆分为YAFFS_NTNODES_LEVEL0个tnode_width位长的短整数,该短整数就是文件内容在flash 上的存储位置。最底层节点即叶节点中tnode_width的位宽决定了文件系统所能寻址的FLASH最大空间,假设tnode_width=16,即可以表示65 536 个chunk。对于chunk的大小为8KB,这种情况下所能寻址的最大FLASH空间是512MB。 文件系统通过节点树查找文件是非常简单和方便的。中间节点使用的Tnode每组有8个指针,需要3位二进制代码对其进行索引,同时,叶节点使用的Tnode每组有16个指针,所以,需要4 位二进制代码对其进行索引。当节点树结构Tnode刚开始建立时,仅建立最底层Lowest-Level Tnode,当File所配置的Chunk数超过16个时,此Tree会建立一个中间节点internal Tnode,并将其第0 个internal[0]指向原本的Lowest-Level Tnode。当读取的Chunk数愈来愈多时,会一直新增Tnode,且节点树也会越来越高。

Tnode节点树建立了文件内偏移到物理chunk之间的映射,YAFFS2靠logical chunk index 来检索Tnode tree,从而得到physical chunk index。比如说读文件时,使用者给出文件内的offset,YAFFS2根据chunk size计算出该offset在文件的第几个chunk中,即计算出logical chunk index,然后在Tnode tree 中找到相应的physical chunk index。

在yaffs文件系统中,不论是文件、目录还是链接(soft linke 或是hard link),在内存中都是以struct yaffs_obj数据结构来描述,因此yaffs_obj是一个RAM-Resident的数据结构。每一个yaffs_obj都对应到一个yaffs_obj_hdr,而yaffs_obj_hdr则是用以储存此yaffs_Object 的一些属性值,,例如该yaffs_Object 是File 或是Directory,此yaffs_Object 的parentObjectId 为何…等等 。其定义如下:

  1. struct yaffs_obj {
  2.      u8 deleted:1;          /* This should only apply to unlinked files. */
  3.      u8 soft_del:1;          /* it has also been soft deleted */
  4.      u8 unlinked:1;          /* An unlinked file.*/
  5.      u8 fake:1;          /* A fake object has no presence on NAND. */
  6.      u8 rename_allowed:1;     /* Some objects cannot be renamed. */
  7.      u8 unlink_allowed:1;
  8.      u8 dirty:1;          /* the object needs to be written to flash */
  9.      u8 valid:1;          /* When the file system is being loaded up, this
  10.                      * object might be created before the data
  11.                      * is available
  12.                      * ie. file data chunks encountered before
  13.                     * the header.
  14.                      */
  15.      u8 lazy_loaded:1;     /* This object has been lazy loaded and
  16.                      * is missing some detail */
  17.      u8 defered_free:1;     /* Object is removed from NAND, but is
  18.                      * still in the inode cache.
  19.                      * Free of object is defered.
  20.                      * until the inode is released.
  21.                      */
  22.      u8 being_created:1;     /* This object is still being created
  23.                      * so skip some verification checks. */
  24.      u8 is_shadowed:1;     /* This object is shadowed on the way
  25.                      * to being renamed. */
  26.      u8 xattr_known:1;     /* We know if this has object has xattribs
  27.                      * or not. */
  28.      u8 has_xattr:1;          /* This object has xattribs.
  29.                      * Only valid if xattr_known. */
  30.      u8 serial;          /* serial number of chunk in NAND.*/
  31.      u16 sum;          /* sum of the name to speed searching */
  32.      struct yaffs_dev *my_dev;     /* The device I'm on */
  33.      struct list_head hash_link;     /* list of objects in hash bucket */
  34.      struct list_head hard_links;     /* hard linked object chain*/
  35.      /* directory structure stuff */
  36.      /* also used for linking up the free list */
  37.      struct yaffs_obj *parent;
  38.      struct list_head siblings;
  39.      /* Where's my object header in NAND? */
  40.      int hdr_chunk;
  41.      int n_data_chunks;     /* Number of data chunks for this file. */
  42.      u32 obj_id;          /* the object id value */
  43.      u32 yst_mode;
  44.      YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
  45. #ifdef CONFIG_YAFFS_WINCE
  46.      u32 win_ctime[2];
  47.      u32 win_mtime[2];
  48.      u32 win_atime[2];
  49. #else
  50.      u32 yst_uid;
  51.      u32 yst_gid;
  52.      u32 yst_atime;
  53.      u32 yst_mtime;
  54.      u32 yst_ctime;
  55. #endif
  56.      u32 yst_rdev;
  57.      void *my_inode;
  58.      enum yaffs_obj_type variant_type;
  59.      union yaffs_obj_var variant;
  60. };

下面介绍yaffs_obj中的几个关键字:

    • deleted,soft_del,ulinked:这三个字段用于描述该文件对象在删除过程中所处的阶段。在删除文件时,首先要将文件从原目录移至一个特殊的系统目录/unlinked,一次拒绝应用程序对该文件的访问,此时将unlinked置为1;然后判断该文件长度是否为0,如果为0,该文件可以直接删除,此时将deleted置为1;如果不为0,将deleted和soft_del都置为1,表明该文件数据所占的chunk还没有释放,要留待后续垃圾回收处理;
    • fake:创建fake directory时被置为1;有两种目录,一种是普通的目录,有object header,另外一种是fake directory,没有object header,只存在内存中。他们是root,lost_found,deleted域unlinked;
    • dirty:文件被修改时置为1;
    • valid:文件object header被写入时置1,没有object header时置为0,表示文件并未真正生效;
    • hash_link:通过该字段把文件对象挂接到散列表中;
    • parent:指向父目录的yaffs_obj对象;
    • siblings:在同一个目录的yaffs_obj对象通过该字段组成双向循环链表;
    • hdr_chunk:每个文件在flash上都有一个object header,存储着该文件的大小、所有者、创建修改时间等信息,hdr_chunk就是该文件头在flash上的chunk号, object header一旦被写入chunk,就不能再修改,只能在另一个chunk 中写入一个新的object header;
    • obj_id:每一个文件系统对象都被赋予一个唯一的编号,作为对象标识,也用于将该对象挂入一个散列表,加快对象的搜索速度;
    • variant_type、variant:前者表示该对象的类型,是目录、普通文件还是链接文件,后者是一个联合体,根据对象类型的不同有不同的解释,双向循环链表, obj->varient. dir_variant.children 记录着该目录下的所有子目录和文件;
  • gc_cleanup_list:数组,保存垃圾回收时可以删除的yaffs_obj对象obj_id;
  • n_clean_ups: gc_cleanup_list数组的下标;
  • has_pending_prioritised_gc:标志位,设备存在优先回收的擦除块;
  • gc_disable:标志位,置1禁止垃圾回收,置0使能垃圾回收,主要用于垃圾回收时的同步,防止垃圾回收的重入,进入垃圾流程置1,退出时置0;
  • gc_block_finder:存储可以进行垃圾回收擦除块的编号;
  • gc_dirtiest:存储最脏的擦除块的编号;
  • gc_pages_in_use:被选中垃圾回收的擦除块有效数据使用的page数目不能超过一定阈值,否则代价太大,需要把有效数据搬移到空闲擦除的page中;
  • gc_not_done:跳过垃圾回收的次数;
  • gc_block:正在被垃圾回收的擦除块;
  • gc_chunk:垃圾回收时会判断每个chunk是否有效,gc_chunk表示正在被检查的那个chunk,有效的话需要把该chunk的数据搬移到其他空闲擦除块的chunk上;
  • gc_skip:没有使用该字段;
  • root_dir、lost_n_found、unlinked_dir、del_dir、unlinked_deletion:这几种目录都是fake directory,只存在于内存中,没有object header,该类目录禁止改名、禁止删除,文件对象的fake的字段置1;
  • cache:指向yaffs_cache数据结构的指针,当param->n_caches大于0时,在yaffs_guts_initialise()函数中将初始化cache[dev->cache = kmalloc(cache_bytes, GFP_NOFS);]。现实的文件操作中存在大量的short sequential reads, and short sequential writes,页缓存能提高short sequential reads类型的读性能,而为了提高short sequential writes类型的写性能,YAFFS2文件系统实现了自己的文件缓存。yaffs_guts_initialise函数对文件系统要使用的cache进行初始化,缓存的数目有dev->param.n_caches决定,缓存的大小由dev->param.total_ bytes_per_chunk决定,写入的数据小于total_bytes_per_chunk时就暂时缓存到cache中,达到total_bytes_per_chunk再写入到FLASH。 struct yaffs_cache的定义如下:
  1. /* ChunkCache is used for short read/write operations.*/
  2. struct yaffs_cache {
  3.      struct yaffs_obj *object;
  4.      int chunk_id;
  5.      int last_use;
  6.      int dirty;
  7.      int n_bytes;          /* Only valid if the cache is dirty */
  8.      int locked;          /* Can't push out or flush while locked. */
  9.      u8 *data;
  10. };

 

  • cache_last_use:cache被使用的次数,通过该字段可以看出文件操作是否存在频繁的short sequential writes;
  • temp_buffer:YAFFS2实现了一个临时的buffer,buffer大小为total_bytes_per_chunk,主要用来临时存放从chunk中读出的数据;
  • seq_number:当被用作序列号时,sequnce Number表示擦除块被使用的前后顺序,越小则表示被使用的越早,反之亦然。擦除块的sequnce number在gc与上电scan时起到了非常重要的作用;
  • oldest_dirty_seq:最小的seq_number号也就是最老的seq_number号;
  • oldest_dirty_block:最小的seq_number号对应的擦除块编号;
  • dirty_dirs: 需要同步更新的文件对象挂到这个链表下;

12.yaffs_obj_hdr

struct yaffs_obj_hdr是表示object header的数据结构,该结构只存在与Flash,每个文件都有一个object header,存储着该文件的大小、所有者、创建修改时间等信息。object header一旦被写入chunk,就不能被修改,如果文件被修改,只能在另一个chunk中写入一个新的object header。该结构体的定义如下:

  1. /* This is the object structure as stored on NAND */
  2. struct yaffs_obj_hdr {
  3.      enum yaffs_obj_type type;
  4.      /* Apply to everything  */
  5.      int parent_obj_id;
  6.      u16 sum_no_longer_used;     /* checksum of name. No longer used */
  7.      YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  8.      /* The following apply to all object types except for hard links */
  9.      u32 yst_mode;          /* protection */
  10.      u32 yst_uid;
  11.      u32 yst_gid;
  12.      u32 yst_atime;
  13.      u32 yst_mtime;
  14.      u32 yst_ctime;
  15.      /* File size  applies to files only */
  16.      u32 file_size_low;
  17.      /* Equivalent object id applies to hard links only. */
  18.      int equiv_id;
  19.      /* Alias is for symlinks only. */
  20.      YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  21.      u32 yst_rdev;     /* stuff for block and char devices (major/min) */
  22.      u32 win_ctime[2];
  23.      u32 win_atime[2];
  24.      u32 win_mtime[2];
  25.      u32 inband_shadowed_obj_id;
  26.      u32 inband_is_shrink;
  27.      u32 file_size_high;
  28.      u32 reserved[1];
  29.      int shadows_obj;     /* This object header shadows the specified object if > 0 */
  30.      /* is_shrink applies to object headers written when wemake a hole. */
  31.      u32 is_shrink;
  32. };

 

  • type: 关联文件的类型,表示该文件是普通文件、目录还是链接(SoftLink/HardLink)文件等;
  • name:yaffs object的名称;
  • is_shrink: 标识所关联的文件是否发生过resize。如果文件发生过resize,必须在file的object header上标识出这一点;同事也必须在object header所在的block上也标识出这一点。关于shrink header的说明见[Yaffs] Improving/ignoring yaffs2 shrink header handling http://www.yaffs.net/lurker/message/20070430.103622.962952a8.fr.html
  • n_obj:文件系统中已经分配的struct yaffs_obj的数量,计算checkpoint信息时使用;
  • yst_mode:表示yaffs object的权限;
  • yst_uid:文件的User ID;
  • yst_gid:文件的Group ID;
  • yst_atime:文件的最后访问时间;
  • yst_mtime:文件的最后修改时间;
  • yst_ctime:文件的创建时间;
  • file_size_low:文件的大小;
  • equiv_id:hardlink对应的Object ID;
  • alias:符号链接的别名;

13.yaffs_ext_tags

  1. struct yaffs_ext_tags {
  2.      unsigned chunk_used;     /*  Status of the chunk: used or unused */
  3.      unsigned obj_id;     /* If 0 this is not used */
  4.      unsigned chunk_id;     /* If 0 this is a header, else a data chunk */
  5.      unsigned n_bytes;     /* Only valid for data chunks */
  6.      /* The following stuff only has meaning when we read */
  7.      enum yaffs_ecc_result ecc_result;
  8.      unsigned block_bad;
  9.      /* YAFFS 1 stuff */
  10.      unsigned is_deleted;     /* The chunk is marked deleted */
  11.      unsigned serial_number;     /* Yaffs1 2-bit serial number */
  12.      /* YAFFS2 stuff */
  13.      unsigned seq_number;     /* The sequence number of this block */
  14.      /* Extra info if this is an object header (YAFFS2 only) */
  15.      unsigned extra_available;     /* Extra info available if not zero */
  16.      unsigned extra_parent_id;     /* The parent object */
  17.      unsigned extra_is_shrink;     /* Is it a shrink header? */
  18.      unsigned extra_shadows;     /* Does this shadow another object? */
  19.      enum yaffs_obj_type extra_obj_type;     /* What object type? */
  20.      loff_t extra_file_size;          /* Length if it is a file */
  21.      unsigned extra_equiv_id;     /* Equivalent object for a hard link */
  22. };

 

struct yaffs_ext_tags数据结构是用来表示chunk的归属信息的,其定义如下:

  • chunk_used:为0表示该chunk未分配,为1表示该chunk已经分配使用;
  • obj_id: 为0表示该chunk未分配使用,非0表示文件的obj_id信息,通过该字段可以知道该chunk属于哪个文件;
  • chunk_id: 为0表示该chunk存储的是一个文件的object header;非0表示该chunk存储的是data chunk,表示文件logical chunk index,可以根据这个字段计算所保存的数据在文件内的偏移;
  • n_bytes:表示该chunk包含的数据量,单位是byte;
  • seq_number:等同于擦除块的seq_number,表示擦除块被使用的先后顺序,block中所有chunk的该字段是一样的,所以扫描时可以根据此信息对block进行排序;
  • extra_obj_type:表示该chunk中存储的文件类型, extra_available为1才能使用这个信息;


文章来源:http://kaffeel.org/20130330/yaffs%e6%96%87%e4%bb%b6%e7%b3%bb%e7%bb%9f%e7%9a%84%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84/