2010-12-10 22 views
5

在我看來,tmpfs並不是重新使用inode號碼,而是每次需要一個空閒的inode時,通過+1順序創建一個新的inode號碼。linux tmpfs中如何生成inode數字?

你知道這是如何實現/你可以指向我的一些源代碼,我可以檢查在tmpfs中使用的算法嗎?

我需要理解這一點,以便繞過使用inode編號作爲其緩存鍵的緩存系統中的限制(因此當inode被重複使用太頻繁時會導致罕見的碰撞,但會發生碰撞)。如果我能證明它可以持續創建獨特的inode號碼,tmpfs可以節省我的一天。

感謝你的幫助,

傑羅姆·瓦格納

回答

3

大部分的tmpfs代碼在mm/shmem.c。新的inode由

static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 
           int mode, dev_t dev, unsigned long flags) 

創建,但它將幾乎所有東西委託給通用文件系統代碼。

特別是,本場i_ino填充在fs/inode.c

/** 
*  new_inode  - obtain an inode 
*  @sb: superblock 
* 
*  Allocates a new inode for given superblock. The default gfp_mask 
*  for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE. 
*  If HIGHMEM pages are unsuitable or it is known that pages allocated 
*  for the page cache are not reclaimable or migratable, 
*  mapping_set_gfp_mask() must be called with suitable flags on the 
*  newly created inode's mapping 
* 
*/ 
struct inode *new_inode(struct super_block *sb) 
{ 
     /* 
     * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW 
     * error if st_ino won't fit in target struct field. Use 32bit counter 
     * here to attempt to avoid that. 
     */ 
     static unsigned int last_ino; 
     struct inode *inode; 

     spin_lock_prefetch(&inode_lock); 

     inode = alloc_inode(sb); 
     if (inode) { 
       spin_lock(&inode_lock); 
       __inode_add_to_lists(sb, NULL, inode); 
       inode->i_ino = ++last_ino; 
       inode->i_state = 0; 
       spin_unlock(&inode_lock); 
     } 
     return inode; 
} 

而且它確實只使用一個遞增計數器(last_ino)。

大多數其他文件系統使用來自磁盤文件的信息來稍後覆蓋i_ino字段。

請注意,這完全可能包裹所有。內核還有一個以不同方式填充的「生成」字段。 mm/shmem.c使用當前時間。

+0

感謝您挖掘出來。 「一路包裹」是什麼意思? – 2010-12-10 23:10:12

+1

發生溢出時回零 – slezica 2010-12-11 00:14:00

7

我不會直接回答你的問題,所以我提前對此表示道歉。

tmpfs的想法很好,但我不會讓我的程序依賴於生成密鑰的更多或更少的模糊實現細節。爲什麼不嘗試其他方法,如將inode號碼與其他信息結合起來?也許修改日期:除非系統日期更改,否則兩個文件不可能在密鑰生成時獲得相同的inode編號和修改日期。

乾杯!

+0

我同意依靠這樣一個impl。細節似乎並不合理,未來的證據。事實上,密鑰已經依賴於(inode,mtime),但由於mtime具有1秒的粒度,我已經學會了碰撞發生的困難方式。在密鑰中使用文件名和文件大小也會降低碰撞概率。我認爲最好的辦法是在發佈inode時使用某種內核通知來刪除緩存。 tmpfs'hack'可能會給我的問題帶來快速和骯髒的解決方案,直到真正的修復程序開發和測試。感謝您的建議 – 2010-12-10 23:06:36

+0

哦,那麼,很抱歉告訴你什麼,你已經知道,並已測試xD – slezica 2010-12-11 00:14:54