DSP

uclinux-2008R1-RC8(bf561)到VDSP5的移植(47): d_alloc引出的

2019-07-13 17:27发布

  快乐虾 http://blog.csdn.net/lights_joy/ lights@hb165.com      本文适用于 ADI bf561 DSP 优视BF561EVB开发板 uclinux-2008r1-rc8 (移植到vdsp5) Visual DSP++ 5.0    欢迎转载,但请保留作者信息   在内核中有一个d_alloc函数,用于分配一个dentry的结构体并进行初始化。 /**  * d_alloc    -    allocate a dcache entry  * @parent: parent of entry to allocate  * @name: qstr of the name  *  * Allocates a dentry. It returns %NULL if there is insufficient memory  * available. On a success the dentry is returned. The name passed in is  * copied and the copy passed in may be reused after this call.  */   struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) {      struct dentry *dentry;      char *dname;        dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);      if (!dentry)          return NULL;        if (name->len > DNAME_INLINE_LEN-1) {          dname = kmalloc(name->len + 1, GFP_KERNEL);          if (!dname) {               kmem_cache_free(dentry_cache, dentry);               return NULL;          }      } else {          dname = dentry->d_iname;      }         dentry->d_name.name = dname;        dentry->d_name.len = name->len;      dentry->d_name.hash = name->hash;      memcpy(dname, name->name, name->len);      dname[name->len] = 0;        dentry->d_count.lock = 0; // 新加的语句      atomic_set(&dentry->d_count, 1);      dentry->d_flags = DCACHE_UNHASHED;      spin_lock_init(&dentry->d_lock);      dentry->d_inode = NULL;      dentry->d_parent = NULL;      dentry->d_sb = NULL;      dentry->d_op = NULL;      dentry->d_fsdata = NULL;      dentry->d_mounted = 0; #ifdef CONFIG_PROFILING      dentry->d_cookie = NULL; #endif      INIT_HLIST_NODE(&dentry->d_hash);      INIT_LIST_HEAD(&dentry->d_lru);      INIT_LIST_HEAD(&dentry->d_subdirs);      INIT_LIST_HEAD(&dentry->d_alias);        if (parent) {          dentry->d_parent = dget(parent);          dentry->d_sb = parent->d_sb;      } else {          INIT_LIST_HEAD(&dentry->d_u.d_child);      }        spin_lock(&dcache_lock);      if (parent)          list_add(&dentry->d_u.d_child, &parent->d_subdirs);      dentry_stat.nr_dentry++;      spin_unlock(&dcache_lock);        return dentry; } 在原始文件中,没有红 {MOD}标出的语句。 d_count是dentry结构体中的一个成员,其类型是atomic_t,在原内核中不支持SMP,所以没有什么问题,但是为了使用SMP,将atomic_t的定义改为了: typedef struct {      int counter;      testset_t lock; } atomic_t; 其中lock是为了双核同步而设置的,要求初始值为0,但是在原始文件中并没有对它进行初始化,故而很可能造成锁定的失败。再深入一点思考,突然发现这种初始化的方法似乎特别愚笨,每次都必须对lock成员进行特别的操作。不过在内核中每次初始化时都会调用atomic_set这个宏,在此修改这个宏,使之成为: static __inline__ void atomic_set(atomic_t * v, int value) {      v->counter = value;      v->lock = 0; }