{"id":2054,"date":"2010-11-27T17:16:14","date_gmt":"2010-11-27T09:16:14","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2054"},"modified":"2010-11-27T21:09:34","modified_gmt":"2010-11-27T13:09:34","slug":"%e8%be%b9%e5%ad%a6%e8%be%b9%e5%ae%9e%e8%b7%b5%ef%bc%9a%e6%89%93%e5%8d%b0vfs%e4%b8%ad%e7%9a%84%e7%bb%93%e6%9e%84%e4%bd%93","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2054","title":{"rendered":"\u8fb9\u5b66\u8fb9\u5b9e\u8df5\uff1a\u6253\u5370VFS\u4e2d\u7684\u7ed3\u6784\u4f53"},"content":{"rendered":"<p>\u5b66\u4e60\u4e86VFS\u7684\u57fa\u672c\u539f\u7406\uff0c\u6211\u4eec\u5f88\u6709\u5fc5\u8981\u5bf9\u8fd9\u4e9b\u7406\u8bba\u77e5\u8bc6\u8fdb\u884c\u9a8c\u8bc1\u548c\u5b9e\u8df5\u3002\u672c\u6587\u6240\u5206\u6790\u7684\u51e0\u4e2a\u5c0f\u7a0b\u5e8f\u5c06\u66f4\u5177\u4f53\u3001\u76f4\u89c2\u7684\u5c55\u73b0VFS\u4e2d\u4e00\u4e9b\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002<\/p>\n<h4>1.\u6253\u5370\u8d85\u7ea7\u5757\u548c\u7d22\u5f15\u7ed3\u70b9<\/h4>\n<p>\u901a\u8fc7\u524d\u9762\u7684<a href=\"http:\/\/edsionte.com\/techblog\/archives\/1984\" target=\"_blank\">\u5206\u6790<\/a>\u6211\u4eec\u77e5\u9053\uff0c\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8d85\u7ea7\u5757\u90fd\u5185\u5d4c\u4e00\u4e2astruct list_head\u7c7b\u578b\u7684\u5b57\u6bb5s_list\u3002\u901a\u8fc7\u8be5\u5b57\u6bb5\u5c06\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8d85\u7ea7\u5757\u94fe\u63a5\u6210\u4e00\u4e2a\u53cc\u8054\u8868\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u53cc\u8054\u8868\u7684\u5934\u6307\u9488\u4ee5\u53ca\u4e86\u89e3\u76f8\u5173\u4fbf\u5229\u5b8f\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u904d\u5386\u6574\u4e2a\u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u8d85\u7ea7\u5757\u4e86\u3002<br \/>\n\u4e3a\u4e86\u89e3\u91ca\u65b9\u4fbf\uff0c\u6211\u4eec\u5c06\u5185\u5d4c\u7684list_head\u7ed3\u6784\u4f53\u79f0\u4e3a\u5185\u90e8\u7ed3\u6784\u4f53\uff1b\u5c06super_block\u7ed3\u6784\u4f53\u79f0\u4e3a\u5916\u90e8\u7ed3\u6784\u4f53\uff1b<\/p>\n<p>\u5177\u4f53\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 list_for_each\u5b8f\u6765\u904d\u5386\u4e00\u4e2alist_head\u7c7b\u578b\u7684\u53cc\u8054\u8868\u3002\u8be5\u5b8f\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\"> 364#define list_for_each(pos, head) \\\r\n 365        for (pos = (head)-&gt;next; prefetch(pos-&gt;next), pos != (head); \\\r\n 366                pos = pos-&gt;next)<\/pre>\n<p>\u4f7f\u7528\u8be5\u5b8f\u65f6\uff0c\u9700\u8981\u5411head\u53c2\u6570\u4e2d\u4f20\u9012\u8981\u904d\u5386\u53cc\u8054\u8868\u7684\u5934\u6307\u9488\uff1b\u800c\u6bcf\u6b21\u904d\u5386\u5f97\u5230\u7684list_head\u7c7b\u578b\u7684\u7ed3\u70b9\u5730\u5740\u4f1a\u4fdd\u5b58\u5728pos\u8fd9\u4e2a\u53c2\u6570\u4e2d\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u5b8f\u53ea\u80fd\u904d\u5386\u5185\u5d4c\u4e8e\u8d85\u7ea7\u5757\u4e2d\u7684\u90a3\u4e2alist_head\u7ed3\u6784\u7684\u94fe\u8868\uff0c\u5e76\u4e0d\u80fd\u5f97\u5230\u6b63\u5728\u88ab\u904d\u5386\u7684\u90a3\u4e2a\u8d85\u7ea7\u5757\u7684\u5730\u5740\uff08\u4e5f\u5c31\u662f\u6307\u5411\u5f53\u524d\u6b63\u88ab\u904d\u5386\u7684\u8d85\u7ea7\u5757\u7684\u6307\u9488\uff09\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u6b21\u904d\u5386\u65f6\u5e76\u4e0d\u80fd\u5f97\u5230\u8d85\u7ea7\u5757\u4e2d\u7684\u5176\u4ed6\u5b57\u6bb5\u3002\u56e0\u6b64\uff0c\u8fd8\u5e94\u8be5\u4f7f\u7528 list_entry\u5b8f\u3002\u8be5\u5b8f\u901a\u8fc7\u6307\u5411list_head\u7ed3\u70b9\u7684\u5730\u5740\u6765\u5f97\u5230\u5916\u90e8\u8d85\u7ea7\u5757\u7684\u9996\u5730\u5740\u3002<\/p>\n<pre class=\"brush:c\">345#define list_entry(ptr, type, member) \\\r\n346        container_of(ptr, type, member)<\/pre>\n<p>\u8fd9\u4e2a\u5b8f\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6307\u5411\u5185\u90e8\u7ed3\u6784\u4f53list_head\u7684\u6307\u9488\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5916\u90e8\u7ed3\u6784\u4f53\u7684\u7c7b\u578b\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662flist_head\u7c7b\u578b\u7684\u53d8\u91cf\u5728\u5916\u90e8\u7ed3\u6784\u4f53\u4e2d\u7684\u540d\u79f0\u3002\u8fd9\u4e2a\u5b8f\u6700\u540e\u4f1a\u8fd4\u56de\u6307\u5411\u5f53\u524d\u5916\u90e8\u7ed3\u6784\u4f53\u7684\u6307\u9488\u3002\u6bd4\u5982\uff0c\u5728super_block\u7ed3\u6784\u4f53\u4e2d\uff0clist_head\u7ed3\u6784\u7c7b\u578b\u7684\u5b57\u6bb5\u540d\u79f0\u4e3as_list\uff0c\u56e0\u6b64\u53ef\u4ee5\u5982\u4e0b\u4f7f\u7528\u8be5\u5b8f\uff1a<\/p>\n<pre class=\"brush:c\">sb = list_entry(pos, struct super_block, s_list);<\/pre>\n<p>\u5bf9\u4e8e\u8d85\u7ea7\u5757\u5f62\u6210\u7684\u53cc\u8054\u8868\u6765\u8bf4\uff0c\u5b83\u7684\u5934\u6307\u9488\u662fsuper_blocks\u3002\u4f46\u662f\u5f88\u9057\u61be\uff0csuper_blocks\u8fd9\u4e2a\u53d8\u91cf\u5e76\u6ca1\u6709\u88ab\u5bfc\u51fa\u3002\u6240\u8c13\u5bfc\u51fa\uff0c\u5c31\u662f\u901a\u8fc7EXPORT_SYMBOL\u5c06\u67d0\u4e2a\u51fd\u6570\u6216\u8005\u53d8\u91cf\u5bf9\u5168\u90e8\u5185\u6838\u4ee3\u7801\u516c\u5f00\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f7f\u7528 EXPORT_SYMBOL\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u4ee5\u7b26\u53f7\u7684\u65b9\u5f0f\u5bfc\u51fa\u7ed9\u5176\u4ed6\u6a21\u5757\u4f7f\u7528 \u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5305\u542bsuper_blocks\u7684\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5c06\u8fd9\u4e2a\u53d8\u91cf\u5bfc\u51fa\uff0c\u5e76\u4e14\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\u3002\u5bf9\u4e8e\u6211\u4eec\u8fd9\u91cc\u7684\u8fd9\u4e2a\u5c0f\u7a0b\u5e8f\u800c\u8a00\uff0c\u8fd9\u6837\u505a\u6709\u4e9b\u4e0d\u503c\u5f97\u3002\u5e78\u597d\uff0c\u5728\/proc\/kallsyms\u6587\u4ef6\u4e2d\uff0c\u8bb0\u5f55\u4e86\u5185\u6838\u4e2d\u6240\u6709\u7b26\u53f7\u4ee5\u53ca\u7b26\u53f7\u7684\u5730\u5740\u3002\u56e0\u6b64\uff0c\u5728\u8be5\u6587\u4ef6\u4e2d\u67e5\u627e\u76f8\u5e94\u7b26\u53f7\u5c31\u53ef\u4ee5\u5f97\u5230\u5176\u5730\u5740\u3002<\/p>\n<p>\u6211\u4eec\u4f7f\u7528\u4e0b\u8ff0\u4e24\u6761\u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-desktop:~\/code\/vfs\/print_sb$ grep super_blocks \/proc\/kallsyms\r\nc0772a30 D super_blocks\r\nedsionte@edsionte-desktop:~\/code\/vfs\/print_sb$ grep \" sb_lock\" \/proc\/kallsyms\r\nc08c9d60 B sb_lock<\/pre>\n<p>\u5c31\u53ef\u4ee5\u5f97\u5230super_blocks\u53d8\u91cf\u7684\u5730\u5740\u3002\u53e6\u5916\uff0csb_lock\u8d85\u7ea7\u5757\u5bf9\u5e94\u7684\u81ea\u65cb\u9501\u3002<\/p>\n<p>\u4e0a\u8ff0\u90fd\u51c6\u5907\u597d\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u904d\u5386\u4e86\u3002\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">#define SUPER_BLOCKS_ADDRESS 0xc0772a30\r\n#define SB_LOCK_ADDRESS 0xc08c9d60\r\n\r\nstatic int __init my_init(void)\r\n{\r\nstruct super_block *sb;\r\nstruct list_head *pos;\r\nstruct list_head *linode;\r\nstruct inode *pinode;\r\nunsigned long long count = 0;\r\n\r\nprintk(\"print some fields of super blocks:\\n\");\r\nspin_lock((spinlock_t *)SB_LOCK_ADDRESS);\r\nlist_for_each(pos, (struct list_head *)SUPER_BLOCKS_ADDRESS){\r\n\r\nsb = list_entry(pos, struct super_block, s_list);\r\nprintk(\"dev_t:%d,%d \",MAJOR(sb-&gt;s_dev),MINOR(sb-&gt;s_dev));\r\nprintk(\"fs_name:%s\\n\",sb-&gt;s_type-&gt;name);\r\nprintk(\"\\n\");\r\n}\r\n\r\nspin_unlock((spinlock_t *)SB_LOCK_ADDRESS);\r\nprintk(\"the number of inodes:%llu\\n\",sizeof(struct inode)*count);\r\n\r\nreturn 0;\r\n}<\/pre>\n<p>\u53e6\u5916\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6bcf\u6b21\u91cd\u542f\u7535\u8111\u540e\uff0c\u90fd\u8981\u91cd\u65b0\u67e5\u627e\u4e0a\u8ff0\u4e24\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u8d85\u7ea7\u5757\u4e2d\u6240\u6709\u7684inode\uff0c\u6709\u4e13\u95e8\u4e00\u4e2a\u94fe\u8868\u5c06\u6240\u6709\u7684inode\u94fe\u63a5\u8d77\u6765\u3002\u8fd9\u4e2a\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u662f\u8d85\u7ea7\u5757\u4e2d\u7684s_inode\u5b57\u6bb5\u3002\u800cinode\u4e4b\u95f4\u662f\u5176\u5185\u90e8\u7684i_sb_list\u5b57\u6bb5\u8fdb\u884c\u94fe\u63a5\u7684\u3002\u4e86\u89e3\u4e86\u8fd9\u4e9b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0a\u8ff0\u7a0b\u5e8f\u7684\u57fa\u7840\u4e0a\uff0c\u518d\u6253\u5370\u6bcf\u4e2a\u8d85\u7ea7\u5757\u4e2d\u7684\u6240\u6709inode\uff1a<\/p>\n<pre class=\"brush:c\">\tlist_for_each(pos, (struct list_head *)SUPER_BLOCKS_ADDRESS){\r\n\r\n\t\tsb = list_entry(pos, struct super_block, s_list);\r\n\t\tprintk(\"dev_t:%d,%d \",MAJOR(sb-&gt;s_dev),MINOR(sb-&gt;s_dev));\r\n\t\tprintk(\"fs_name:%s\\n\",sb-&gt;s_type-&gt;name);\r\n\r\n\t\tlist_for_each(linode, &amp;sb-&gt;s_inodes){\r\n\r\n\t\t\tpinode = list_entry(linode, struct inode, i_sb_list);\r\n\t\t\tcount ++;\r\n\t\t\tprintk(\"%lu\\t\",pinode-&gt;i_ino);\r\n\t\t}\r\n\r\n\t\tprintk(\"\\n\");\r\n\t}<\/pre>\n<p>\u5728\u4e0a\u9762\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u518d\u52a0\u6df1\u4e00\u6b65\u3002\u4e00\u4e2a\u7d22\u5f15\u7ed3\u70b9\u53ef\u80fd\u5bf9\u5e94\u82e5\u5e72\u4e2adentry\uff0c\u8fd9\u4e9bdentry\u81ea\u8eab\u901a\u8fc7\u5176\u5185\u90e8\u7684d_alias\u94fe\u63a5\u5728\u4e00\u8d77\uff1b\u800c\u6574\u4e2a\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u662finode\u4e2d\u7684i_dentry\u5b57\u6bb5\u3002\u56e0\u6b64\uff0c\u6839\u636e\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u904d\u5386\u6bcf\u4e2ainode\u7684\u540c\u65f6\uff0c\u7ee7\u7eed\u904d\u5386\u8fd9\u4e2ainode\u5bf9\u5e94\u7684\u6240\u6709dentry\u3002\u90e8\u5206\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">\r\n\tlist_for_each(pos, (struct list_head *)SUPER_BLOCKS_ADDRESS){\r\n\t\tsb = list_entry(pos, struct super_block, s_list);\r\n\t\tprintk(\"dev_t:%d,%d \",MAJOR(sb->s_dev),MINOR(sb->s_dev));\r\n\t\tprintk(\"fs_name:%s\\n\",sb->s_type->name);\r\n\r\n\t\tlist_for_each(linode, &sb->s_inodes){\r\n\t\t\tpinode = list_entry(linode, struct inode, i_sb_list);\r\n\t\t\tcount ++;\r\n\t\t\tprintk(\"%lu[\",pinode->i_ino);\r\n\r\n\t\t\tlist_for_each(ldentry, &pinode->i_dentry){\r\n\t\t\t\tpdentry = list_entry(ldentry, struct dentry, d_alias);\r\n\t\t\t\tprintk(\"%s->\",pdentry->d_name.name);\r\n\t\t\t}\r\n\t\t\tprintk(\"]\\t\");\r\n\t\t}\r\n\t\t\r\n\t\tprintk(\"\\n\");\r\n\t}\r\n<\/pre>\n<p>\u4e0a\u51fa\u7a0b\u5e8f\u7684\u5b8c\u6574\u7684\u4ee3\u7801\u5728<a href=\"http:\/\/edsionte.download.csdn.net\/\" target=\"_blank\">\u8fd9\u91cc<\/a>\u3002<\/p>\n<h4>2.\u6253\u5370\u6587\u4ef6\u7c7b\u578b\u7ed3\u6784\u4f53<\/h4>\n<p>\u540c\u6837\u7684\u9053\u7406\uff0c\u901a\u8fc7\u4e0b\u8ff0\u7684\u4ee3\u7801\u53ef\u4ee5\u6253\u5370file_system_type\u7ed3\u6784\u4f53\u3002<\/p>\n<pre class=\"brush:c\">#define FILE_SYSTEM_ADDRESS 0xc08ca3a4 \/* grep file_systems \/proc\/kallsyms *\/\r\n#define FILE_SYSTEM_LOCK_ADDRESS 0xc0772de0 \/* grep file_systems_lock \/proc\/kallsyms *\/\r\n\r\nstatic int __init printfs_init(void)\r\n{\r\n\tstruct file_system_type **pos;\r\n\r\n\tprintk(\"\\n\\nprint file_system_type:\\n\");\r\n\r\n\tread_lock((rwlock_t *)FILE_SYSTEM_LOCK_ADDRESS);\r\n\tpos = (struct file_system_type **)FILE_SYSTEM_ADDRESS;\r\n\r\n\twhile(*pos){\r\n\t\tprintk(\"name: %s\\n\",(*pos)-&gt;name);\r\n\t\tpos = &amp;((*pos)-&gt;next);\r\n\t}\r\n\r\n\tread_unlock((rwlock_t *)FILE_SYSTEM_LOCK_ADDRESS);\r\n\r\n\treturn 0;\r\n}<\/pre>\n<p>\u66f4\u591a\u7684\u6253\u5370\u4fe1\u606f\u53ef\u4ee5\u6309\u7167\u4e0a\u8ff0\u65b9\u6cd5\u7ee7\u7eed\u6dfb\u52a0\u3002\u5f00\u59cb\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u4e86VFS\u7684\u57fa\u672c\u539f\u7406\uff0c\u6211\u4eec\u5f88\u6709\u5fc5\u8981\u5bf9\u8fd9\u4e9b\u7406\u8bba\u77e5\u8bc6\u8fdb\u884c\u9a8c\u8bc1\u548c\u5b9e\u8df5\u3002\u672c\u6587\u6240\u5206\u6790\u7684\u51e0\u4e2a\u5c0f\u7a0b\u5e8f\u5c06\u66f4\u5177\u4f53\u3001\u76f4\u89c2\u7684\u5c55\u73b0V [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[134,174,148],"class_list":["post-2054","post","type-post","status-publish","format-standard","hentry","category-linux","tag-kernel","tag-vfs","tag-148"],"views":8996,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=2054"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2054\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2054"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}