Linux tmpfs的使用問題解析

Linux tmpfs的使用問題解析

在不同的版本系統查看tmpfs分區的情況,有的可以查詢到,有的則不行且不報錯。如df命令,對於正常的結果如下:

tmpfs 15360 96 15264 1% /var

異常情況,則不會有該條目。

嘗試編譯測試程序利用statfs獲取掛載點的空間使用情況,在異常的情況下空間數據爲0。

測試程序過程爲:

1)stat獲取掛載點文件類型 if ((s.st_mode & S_IFMT) == S_IFBLK) mountDevice = s.st_rdev; else mountDevice = s.st_dev;

2)通過setmntent、getmntent、endmntent組合函數從/proc/mounts下找尋匹配的mntent節點,確定掛載點是在合法範圍的

3)statfs通過掛載點路徑,獲取掛載分區的屬性數據測試程序本身應該沒有問題,在正常版本下可以獲取正確的數據。跟蹤statfs的調用流程:SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs__user *, buf)->static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)->int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)->retval = dentry->d_sb->s_op->statfs(dentry, buf);

從這裏,有理由懷疑異常版本的tmpfs實現有問題,實現在shmem.c中,打開會發現其中有很多宏,有些實現是按宏區分爲2份,比如與本問題相關的shmem_ops,

``` C

1)static const struct super_operations shmem_ops;/*只是1個定義未初始化*/

2)/*statfs被實現且被CONFIG_TMPFS管理*/

static const struct super_operations shmem_ops = {

.alloc_inode = shmem_alloc_inode,

.destroy_inode = shmem_destroy_inode,

#ifdef CONFIG_TMPFS

.statfs = shmem_statfs,

.remount_fs = shmem_remount_fs,

.show_options = shmem_show_options,

#endif

.delete_inode = shmem_delete_inode,

.drop_inode = generic_delete_inode,

.put_super = shmem_put_super,

};

``` 

找到另外1個相關的宏CONFIG_SHMEM,並打開,重新編譯版本,驗證OK。shmem.c是被默認編入內核的,但其具體功能可能會由於控制宏開關的不同而表現不同。比如:在CONFIG_SHMEM和CONFIG_TMPFS均關閉的情況下,我們是可以使用tmpfs進行內存分區掛載的,但即使我們制定其掛載大小,對掛載分區的使用也是不會受限,會一致消耗內存直至無內存可用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章