{"id":269370,"date":"2023-04-12T04:04:53","date_gmt":"2023-04-11T20:04:53","guid":{"rendered":"https:\/\/lrxjmw.cn\/ypsyijddljkh.html"},"modified":"2023-04-12T04:04:53","modified_gmt":"2023-04-11T20:04:53","slug":"ypsyijddljkh","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ypsyijddljkh.html","title":{"rendered":"\u786c\u76d8\u4f7f\u7528i\u8282\u70b9\u7684\u903b\u8f91\u5757\u53f7\u6587\u4ef6\u6570\u636e\u5b58\u653e\u7684\u539f\u7406"},"content":{"rendered":"

_bmap()<\/p>\n

1\u3001_bmap()\u51fd\u6570\u7528\u4e8e\u628a\u4e00\u4e2a\u6587\u4ef6\u6570\u636e\u5757\u6620\u5c04\u5230\u76d8\u5757\u7684\u5904\u7406\u64cd\u4f5c<\/p>\n

linux\u6587\u4ef6inode\u5931\u6548_linux\u6587\u4ef6\u7cfb\u7edf\u7684inode_linux\u7cfb\u7edf close\u5934\u6587\u4ef6<\/p>\n

\u7531\u4e8e\u4e00\u4e2ai\u8282\u70b9\u5bf9\u5e94\u4e00\u4e2a\u6587\u4ef6\uff0c\u6240\u4ee5\u524d\u9762\u7684i\u8282\u70b9\u6620\u5c04\u7684\u903b\u8f91\u5757\u53f7\u5c31\u662f\u6587\u4ef6\u6570\u636e\u50a8\u5b58\u7684\u903b\u8f91\u5757\u53f7\uff1bi_zone[0]\u5230i_zone[6]\u662f\u76f4\u63a5\u903b\u8f91\u5757\u53f7\uff0ci_zone[7]\u662f\u4e00\u6b21\u95f4\u63a5\u903b\u8f91\u5757\u53f7\uff0ci_zone[8]\u662f\u4e8c\u6b21\u95f4\u63a5\u903b\u8f91\u5757\u53f7\uff1b\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u50a8\u5b58\u5728\u90a3\u4e2a\u786c\u789f\u4e0a\u7684\u903b\u8f91\u5757\u4e0a\u5c31\u662f\u7531\u8fd9\u4e2a\u94fe\u8868\u6765\u6620\u5c04\u7684\uff0c\u6309\u7167\u8fd9\u4e2a\u4e5f\u53ef\u4ee5\u6653\u5f97\u4e00\u4e2a\u6587\u4ef6\u7684\u6700\u5927\u50a8\u5b58\u662f\u591a\u5c11\uff1f<\/p>\n

linux\u6587\u4ef6inode\u5931\u6548_linux\u7cfb\u7edf close\u5934\u6587\u4ef6_linux\u6587\u4ef6\u7cfb\u7edf\u7684inode<\/p>\n

\n

\/\/\u628a\u6587\u4ef6\u4e0a\u7684\u6570\u636e\u5757\u6620\u5c04\u5230\u78c1\u76d8\u4e0a\uff0cinode \u6587\u4ef6i\u8282\u70b9\uff1bblock \u6587\u4ef6\u4e2d\u6570\u636e\u5757\u53f7\uff0ccreate\u662f\u5426\u521b\u5efa\u6807\u5fd7\nstatic int _bmap(struct m_inode * inode,int block,int create)\n{\n\tstruct buffer_head * bh;\n\tint i;\n\/\/\u5224\u65ad\u6587\u4ef6\u6570\u636e\u5757\u53f7block\u662f\u5426\u8d85\u51fa\u8303\u56f4\n\tif (block<0)\n\t\tpanic("_bmap: block= 7+512+512*512)\/\/\u6587\u4ef6\u903b\u8f91\u5757\u7684\u8303\u56f4\n\t\tpanic(\"_bmap: block>big\");\n\t\/\/\u4f7f\u7528\u76f4\u63a5\u5757\n\tif (blocki_zone[block])\/\/\u521b\u5efa\u6807\u5fd7\u7f6e\u4f4d\uff0ci\u8282\u70b9\u5bf9\u5e94\u7684\u903b\u8f91\u5757\u5b57\u6bb5\u4e3a0\n\t\t\tif (inode->i_zone[block]=new_block(inode->i_dev)) {\/\/\u7533\u8bf7\u4e00\u4e2a\u65b0\u78c1\u76d8\u903b\u8f91\u5757\uff0c\u8fd4\u56de\u903b\u8f91\u53f7\n\t\t\t\tinode->i_ctime=CURRENT_TIME;\n\t\t\t\tinode->i_dirt=1;\n\t\t\t}\n\t\treturn inode->i_zone[block];\/\/\u8fd4\u56de\u8bbe\u5907\u4e0a\u7684\u903b\u8f91\u5757\u53f7\n\t}\n\t\/\/\u4f7f\u7528\u4e00\u6b21\u95f4\u63a5\u5757\n\tblock -= 7;\n\tif (blocki_zone[7])\/\/\u8868\u660e\u6587\u4ef6\u662f\u9996\u6b21\u4f7f\u7528\u95f4\u63a5\u5757\uff0c\u5219\u7533\u8bf7\u4e00\u4e2a\u78c1\u76d8\u5757\u6765\u5b58\u653e\u95f4\u63a5\u5757\u4fe1\u606f\n\t\t\tif (inode->i_zone[7]=new_block(inode->i_dev)) {\n\t\t\t\tinode->i_dirt=1;\n\t\t\t\tinode->i_ctime=CURRENT_TIME;\n\t\t\t}\n\t\tif (!inode->i_zone[7])\/\/\u8868\u793a\u521b\u5efa\u95f4\u63a5\u5757\u78c1\u76d8\u5931\u8d25\uff0c\u6216\u8005create\u672a\u7f6e1\n\t\t\treturn 0;\n\t\tif (!(bh = bread(inode->i_dev,inode->i_zone[7])))\/\/\u8bfb\u53d6\u95f4\u63a5\u5757\u7684\u4fe1\u606f\n\t\t\treturn 0;\n\t\ti = ((unsigned short *) (bh->b_data))[block];\/\/\u5f97\u5230\u95f4\u63a5\u5757\u4e0ablock\u6240\u5904\u7684\u4f4d\u7f6e\uff0c\u5224\u65ad\u662f\u5426\u4e3a0\n\t\tif (create && !i)\/\/\u5982\u679c\u95f4\u63a5\u5757\u53f7\u4e0a\u7684block\u4f4d\u7f6e\u4e3a0\uff0ccreate\u7f6e\u4f4d\n\t\t\tif (i=new_block(inode->i_dev)) {\/\/\u7533\u8bf7\u4e00\u4e2a\u65b0\u7684\u903b\u8f91\u5757\u7ed9block\n\t\t\t\t((unsigned short *) (bh->b_data))[block]=i;\n\t\t\t\tbh->b_dirt=1;\n\t\t\t}\n\t\tbrelse(bh);\n\t\treturn i;\n\t}\n\t\/\/\u4f7f\u7528\u4e8c\u6b21\u95f4\u63a5\u5757\n\tblock -= 512;\n\t\/\/\u4e3a\u4e8c\u6b21\u95f4\u63a5\u5757\u7533\u8bf7\u903b\u8f91\u5757\n\tif (create && !inode->i_zone[8])\n\t\tif (inode->i_zone[8]=new_block(inode->i_dev)) {\/\/\u6620\u5c04\u5230\u4e8c\u7ea7\u95f4\u63a5\u5757\u4e2d\u7684\u4e00\u7ea7\u903b\u8f91\u5757\u4e0a\n\t\t\tinode->i_dirt=1;\n\t\t\tinode->i_ctime=CURRENT_TIME;\n\t\t}\n\tif (!inode->i_zone[8])\n\t\treturn 0;\n\t\/\/\u8bfb\u53d6\u4e8c\u7ea7\u95f4\u63a5\u5757\u4fe1\u606f\n\tif (!(bh=bread(inode->i_dev,inode->i_zone[8])))\n\t\treturn 0;\n\t\/\/\u83b7\u53d6block\u4e8c\u7ea7\u95f4\u63a5\u5757\u53f7\u5728\u6307\u5b9a\u7684\u54ea\u4e2a\u4e8c\u7ea7\u95f4\u63a5\u5757\u7684\u4e00\u7ea7\u5757\u53f7\n\ti = ((unsigned short *)bh->b_data)[block>>9];\/\/\u53f3\u79fb9\u4f4d\u8868\u793a\u6574\u9664512\uff0c\u5f97\u5230\u4e00\u7ea7\u5757\u53f7\n\tif (create && !i)\/\/\u5982\u679c\u4e8c\u7ea7\u95f4\u63a5\u5757\u4e2d\u4e8c\u7ea7\u5757\u4e0d\u5b58\u5728\uff0c\u5219\u8981\u4e3a\u8be5\u4e8c\u7ea7\u5757\u7533\u8bf7\u4e00\u4e2a\u903b\u8f91\u5757\u53f7\n\t\tif (i=new_block(inode->i_dev)) {\/\/\u5f97\u5230\u4e00\u4e2a\u903b\u8f91\u5757\u7528\u6765\u5b58\u653e\u4e8c\u7ea7\u95f4\u63a5\u5757\u4e2d\u7684\u4e8c\u7ea7\u95f4\u63a5\u5757\u53f7\n\t\t\t((unsigned short *) (bh->b_data))[block>>9]=i;\/\/block>>9 === block\/512 \u4e3a\u4e8c\u7ea7\u5757\u6620\u5c04\u4e00\u4e2a\u903b\u8f91\u5757\n\t\t\tbh->b_dirt=1;\n\t\t}\n\tbrelse(bh);\n\tif (!i)\n\t\treturn 0;\n\tif (!(bh=bread(inode->i_dev,i)))\/\/\u8bfb\u53d6\u4e8c\u7ea7\u95f4\u63a5\u5757\u4e2d\u7684\u4e8c\u7ea7\u5757\u6620\u5c04\u7684\u7f13\u5b58\u5757\n\t\treturn 0;\n\ti = ((unsigned short *)bh->b_data)[block&511];\/\/\u8868\u793a\u5728\u4e8c\u7ea7\u95f4\u63a5\u5757\u4e2d\u7684\u4e8c\u7ea7\u5757\u4e2d\u7684\u7b2c\u51e0\u4e2a\u6570\u7ec4\u5143\u7d20\n\tif (create && !i)\n\t\tif (i=new_block(inode->i_dev)) {\/\/\u7533\u8bf7\u4e00\u4e2a\u903b\u8f91\u5757\u6765\u5b58\u653e\u6700\u7ec8\u7684\u6570\u636e\n\t\t\t((unsigned short *) (bh->b_data))[block&511]=i;\/\/\u6620\u5c04\u4e0b\u6587\u4ef6\u4e2d\u7684\u4e8c\u7ea7\u5757\n\t\t\tbh->b_dirt=1;\n\t\t}\n\tbrelse(bh);\n\treturn i;\/\/\u8fd4\u56de\u903b\u8f91\u5757\u53f7\n}<\/code><\/pre>\n<\/p>\n

input()<\/p>\n

2\u3001iput()\u51fd\u6570\u662f\u628ai\u8282\u70b9\u7684\u5f15\u7528\u9012\u51cf\uff0c\u5047\u5982i\u8282\u70b9\u5f15\u7528\u4e3a1\uff0c\u5219\u9012\u51cf\u5220\u6389i\u8282\u70b9\u7ea2\u65d7linux6.0\uff0c\u4f46\u662f\u8bbe\u7f6e\u76f8\u5173\u5c5e\u6027\uff1b\u5047\u5982\u662f\u7ba1\u7ebf\u6587\u4ef6\u6216\u5219\u5757\u8bbe\u5907\u6587\u4ef6linux\u6587\u4ef6\u7cfb\u7edf<\/a>\u7684inode<\/strong>\uff0c\u5219\u53e6\u5916\u8fdb\u884c\u5904\u7406\uff1b<\/p>\n

\n

\/\/\u653e\u7f6e\u4e00\u4e2ai\u8282\u70b9\uff0c\u51cf\u53bb1\u4e2ai\u8282\u70b9\u7684\u5f15\u7528\nvoid iput(struct m_inode * inode)\n{\n\tif (!inode)\n\t\treturn;\n\twait_on_inode(inode);\n\tif (!inode->i_count)\/\/\u5982\u679ci\u8282\u70b9\u5df2\u7ecf\u662f0\uff0c\u90a3\u4e48\u6b7b\u673a\n\t\tpanic(\"iput: trying to free free inode\");\n\tif (inode->i_pipe) {\/\/\u5982\u679c\u662f\u7ba1\u9053\u6587\u4ef6\n\t\twake_up(&inode->i_wait);\/\/\u5524\u9192\u7b49\u5f85\u8be5\u7ba1\u9053\u7684\u8fdb\u7a0b\n\t\tif (--inode->i_count)\/\/\u5f15\u7528\u51cf\u53bb1\n\t\t\treturn;\/\/\u5982\u679c\u8fd8\u88ab\u5176\u4ed6\u7a0b\u5e8f\u5f15\u7528\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\uff1b\u5426\u5219\u5c31\u8bbe\u7f6e\u4e00\u4e9b\u5c5e\u6027\u548c\u91ca\u653e\u5185\u5b58\n\t\tfree_page(inode->i_size);\/\/\u91ca\u653e\u7ba1\u9053\u4f7f\u7528\u7684\u5185\u5b58\u9875\u9762\n\t\tinode->i_count=0;\/\/\u5bf9i\u8282\u70b9\u5c5e\u6027\u5b57\u6bb5\u8bbe\u7f6e\n\t\tinode->i_dirt=0;\n\t\tinode->i_pipe=0;\n\t\treturn;\n\t}\n\t\/\/i\u8282\u70b9\u5bf9\u5e94\u7684\u8bbe\u5907\u53f7\u4e3a0\uff0c\u5f15\u7528\u9012\u51cf\u540e\u8fd4\u56de\uff1b\u5982\uff0c\u7ba1\u9053\u64cd\u4f5ci\u8282\u70b9\n\tif (!inode->i_dev) {\n\t\tinode->i_count--;\n\t\treturn;\n\t}\n\t\/\/\u5982\u679c\u662f\u5757\u8bbe\u5907\u6587\u4ef6\u7684i\u8282\u70b9\uff0c\u5237\u65b0\u8be5\u8bbe\u5907\n\tif (S_ISBLK(inode->i_mode)) {\n\t\tsync_dev(inode->i_zone[0]);\/\/zone[0]\u662f\u8bbe\u5907\u53f7\n\t\twait_on_inode(inode);\n\t}\nrepeat:\n\tif (inode->i_count>1) {\/\/\u5f15\u7528\u9012\u51cf  \u8fd4\u56de\n\t\tinode->i_count--;\n\t\treturn;\n\t}\n\tif (!inode->i_nlinks) {\/\/\u94fe\u63a5\u6570\u4e3a0\uff0c \u8868\u793a\u6587\u4ef6\u5df2\u7ecf\u5220\u9664\uff0c\u7f6e\u6587\u4ef6\u957f\u5ea6\u4e3a0\uff0c\u91ca\u653ei\u8282\u70b9\n\t\ttruncate(inode);\n\t\tfree_inode(inode);\n\t\treturn;\n\t}\n\tif (inode->i_dirt) {\/\/\u5df2\u7ecf\u66f4\u6539\u8fc7\n\t\twrite_inode(inode);\t\/* we can sleep - so do again *\/\/\/\u628ai\u8282\u70b9\u4fe1\u606f\u5199\u5165\u7f13\u5b58\u4e2d\n\t\twait_on_inode(inode);\n\t\tgoto repeat;\/\/\u7ecf\u8fc7\u7761\u7720\uff0c\u8981\u518d\u4e00\u6b21\u786e\u8ba4\n\t}\n\tinode->i_count--;\/\/\u9012\u51cf\u8fd4\u56de\uff0c\u6587\u4ef6\u8fd8\u5b58\u5728\n\treturn;\n}\n<\/code><\/pre>\n<\/p>\n

get_empty_inode()3\u3001get_empty_inode(void)\u51fd\u6570\u8868\u793a\u5728i\u8282\u70b9\u94fe\u8868\u4e2d\u67e5\u627e\u4e00\u4e2a\u7a7a\u95f2\u7684i\u8282\u70b9\uff0c\u8fdb\u884c\u8bbe\u7f6e\uff0c\u4e4b\u540e\u8fd4\u56de\u8be5i\u8282\u70b9\uff1b<\/p>\n

\n

\/\/\u4ecei\u8282\u70b9\u8868\u4e2d\u83b7\u53d6\u4e00\u4e2a\u7a7a\u95f2i\u8282\u70b9\u9879\nstruct m_inode * get_empty_inode(void)\n{\n\tstruct m_inode * inode;\n\tstatic struct m_inode * last_inode = inode_table;\n\tint i;\n\tdo {\n\t\tinode = NULL;\n\t\tfor (i = NR_INODE; i ; i--) {\n\t\t\tif (++last_inode >= inode_table + NR_INODE)\/\/\u5982\u679clast_inode\u5df2\u7ecf\u5230\u4e86\u6700\u540e\u4e00\u9879\uff0c\u5219\u91cd\u65b0\u626b\u63cf\n\t\t\t\tlast_inode = inode_table;\n\t\t\tif (!last_inode->i_count) {\/\/\u5f15\u7528\u4e3a0\uff0c\n\t\t\t\tinode = last_inode;\n\t\t\t\tif (!inode->i_dirt && !inode->i_lock)\/\/\u6ca1\u6709\u4fee\u6539\uff0c\u6ca1\u6709\u4e0a\u9501\uff0c\u5c31\u9009\u62e9\u8fd9\u4e2ai\u8282\u70b9\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t\n\t\t\/\/\u6ca1\u6709\u627e\u5230i\u8282\u70b9\uff0c\u6253\u5370i\u8282\u70b9\u5217\u8868\uff0c\u4f9b\u8c03\u8bd5\uff0c\u6b7b\u673a\n\t\tif (!inode) {\n\t\t\tfor (i=0 ; ii_dirt) {\n\t\t\twrite_inode(inode);\n\t\t\twait_on_inode(inode);\n\t\t}\n\t} while (inode->i_count);\/\/\u5982\u679ci\u8282\u70b9\u53c8\u88ab\u522b\u7684\u8fdb\u7a0b\u4f7f\u7528\uff0c\u90a3\u4e48\u518d\u627e\u4e00\u4e2ai\u8282\u70b9\n\t\/\/\u5bf9\u7a7a\u95f2i\u8282\u70b9\u8be6\u5185\u5bb9\u6e05\u96f6\uff0c\u8bbe\u7f6e\u5f15\u7528\uff0c\u8fd4\u56dei\u8282\u70b9\u6307\u9488\n\tmemset(inode,0,sizeof(*inode));\n\tinode->i_count = 1;\n\treturn inode;\n}\n<\/code><\/pre>\n<\/p>\n

linux\u7cfb\u7edf close\u5934\u6587\u4ef6_linux\u6587\u4ef6\u7cfb\u7edf\u7684inode_linux\u6587\u4ef6inode\u5931\u6548<\/p>\n

iget()<\/p>\n

4\u3001iget(intdev,intnr)\u51fd\u6570\u4f9d\u636e\u8bbe\u5907\u53f7\u548ci\u8282\u70b9\u53f7\u5f97\u5230i\u8282\u70b9\u4fe1\u606f\uff1b\u5176\u4e2d\u6709\u4e2a\u91cd\u70b9\u5224\u5b9a\u7684\u662f\u8be5i\u8282\u70b9\u662f\u5426\u662f\u67d0\u4e2a\u6587\u4ef6\u7cfb\u7edf\u7684\u5b89\u88c5\u8282\u70b9\u3002\u5047\u5982\u662f\uff0c\u5219\u628ai\u8282\u70b9\u91ca\u653e\u4e00\u4e2a\u5f15\u7528\uff1b\u800c\u4e14\u91cd\u65b0\u5728\u8be5\u6587\u4ef6\u7cfb\u7edf\u4e2d\u67e5\u627ei\u8282\u70b9\uff08\u4e5f\u5c31\u662f\u6839\u8282\u70b9\uff09<\/p>\n

\n

\/\/\u4ece\u8bbe\u5907\u4e0a\u8bfb\u53d6\u6307\u5b9ai\u8282\u70b9\u53f7\u7684i\u8282\u70b9\u4fe1\u606f\n\/\/\u53c2\u6570 dev\u8bbe\u5907\u53f7\uff0cnr  i\u8282\u70b9\u53f7\n\/\/1\u3001\u5728\u5185\u5b58\u4e2d\u7684i\u8282\u70b9\u5217\u8868\u4e2d\u626b\u63cf\uff0c\u82e5\u67e5\u627e\u5230\u4e86\uff0c\u5219\u8fd4\u56dei\u8282\u70b9\u6307\u9488\n\/\/2\u3001\u5982\u679c\u4e0a\u9762\u6ca1\u6709\u627e\u5230\uff0c\u5219\u4ece\u8bbe\u5907\u4e0a\u8bfb\u53d6\u6307\u5b9ai\u8282\u70b9\u53f7\u7684i\u8282\u70b9\u4fe1\u606f\u653e\u5165\u5185\u5b58i\u8282\u70b9\u8868\u4e2d\uff0c\u8fd4\u56dei\u8282\u70b9\u6307\u9488\nstruct m_inode * iget(int dev,int nr)\n{\n\tstruct m_inode * inode, * empty;\n\tif (!dev)\n\t\tpanic(\"iget with dev==0\");\n\tempty = get_empty_inode();\/\/\u5f97\u5230\u4e00\u4e2a\u7a7a\u95f2i\u8282\u70b9\n\tinode = inode_table;\/\/i\u8282\u70b9\u5217\u8868\u6570\u7ec4\n\t\n\twhile (inode i_dev != dev || inode->i_num != nr) {\/\/\u8bbe\u5907\u53f7\u6216\u8005i\u8282\u70b9\u53f7\u4e0d\u76f8\u7b49\uff0c\u5219\u770b\u4e0b\u4e00\u4e2a\n\t\t\tinode++;\n\t\t\tcontinue;\n\t\t}\n\t\t\/\/\u89e3\u9501\uff0c\u7136\u540e\u518d\u786e\u8ba4\u4e0b\u8bbe\u5907\u53f7\u548ci\u8282\u70b9\u53f7\u662f\u5426\u6539\u53d8\u4e86\n\t\twait_on_inode(inode);\n\t\tif (inode->i_dev != dev || inode->i_num != nr) {\n\t\t\tinode = inode_table;\n\t\t\tcontinue;\n\t\t}\n\t\tinode->i_count++;\/\/\u5f15\u7528\n\t\t\/\/\u5982\u679c\u627e\u5230\u7684i\u8282\u70b9\u662f\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u7684\u5b89\u88c5\u8282\u70b9\n\t\tif (inode->i_mount) {\n\t\t\tint i;\n\t\t\t\n\t\t\tfor (i = 0 ; i= NR_SUPER) {\n\t\t\t\tprintk(\"Mounted inode hasn't got sbn\");\n\t\t\t\tif (empty)\n\t\t\t\t\tiput(empty);\/\/\u91ca\u653e\u7a7a\u95f2i\u8282\u70b9\n\t\t\t\treturn inode;\n\t\t\t}\n\t\t\t\/\/\u627e\u5230i\u8282\u70b9\u5b89\u88c5\u7684\u8d85\u7ea7\u5757\uff0c\u83b7\u53d6\u5230\u8be5\u6587\u4ef6\u7cfb\u7edf\u7684\u8bbe\u5907\u53f7\uff0c\u5f97\u5230nr\u4e3a\u6839\u8282\u70b91\uff0c\u91cd\u65b0\u5728\u8be5\u6587\u4ef6\u7cfb\u7edf\u4e2d\u67e5\u627e\u6839i\u8282\u70b9\n\t\t\tiput(inode);\n\t\t\tdev = super_block[i].s_dev;\n\t\t\tnr = ROOT_INO;\n\t\t\tinode = inode_table;\n\t\t\tcontinue;\n\t\t}\n\t\t\/\/\u5982\u679c\u627e\u5230\u7684i\u8282\u70b9\u4e0d\u662f\uff0c\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u7684\u5b89\u88c5\u8282\u70b9\uff0c\n\t\t\/\/\u5219\u91ca\u653e\u7a7a\u8282\u70b9\uff0c\u8fd4\u56dei\u8282\u70b9\n\t\tif (empty)\n\t\t\tiput(empty);\n\t\treturn inode;\n\t}\n\t\/\/\u5982\u679c\u5728i\u8282\u70b9\u5217\u8868\u4e2d\uff0c\u6ca1\u6709\u627e\u5230\u6307\u5b9ai\u8282\u70b9\uff0c\u5219\u81ea\u5df1\u65b0\u5efa\u4e00\u4e2a\n\tif (!empty)\n\t\treturn (NULL);\n\tinode=empty;\n\tinode->i_dev = dev;\n\tinode->i_num = nr;\n\tread_inode(inode);\/\/\u4ece\u8bbe\u5907\u4e0a\u8bfb\u53d6\u8be5i\u8282\u70b9\n\treturn inode;\n}<\/code><\/pre>\n<\/p>\n

linux\u6587\u4ef6\u7cfb\u7edf\u7684inode_linux\u7cfb\u7edf close\u5934\u6587\u4ef6_linux\u6587\u4ef6inode\u5931\u6548<\/p>\n

read_inode()<\/p>\n

5\u3001\u8bfb\u53d6\u8bbe\u5907\u4e0a\u6307\u5b9ai\u8282\u70b9\u7684\u4fe1\u606f\u5230\u7f13\u5b58\u4e2d\uff0c\u5176\u4e2d\u8981\u5206\u6e05\u695a\uff0c\u53c2\u6570\u4e2d\u7684i\u8282\u70b9\u662f\u663e\u5b58\u4e2d\u4f7f\u7528\u7684i\u8282\u70b9\uff0c\u800c\u7a0b\u5e8f\u65c1\u8fb9\u662f\u8bfb\u53d6\u5230\u786c\u789f\u4e0a\u7684i\u8282\u70b9\uff1b\u8fd9\u5e94\u5f53\u662f\u4ece\u786c\u789f\u4e0a\u7684i\u8282\u70b9\u4e2d\u52a0\u8f7d\u5176\u4fe1\u606f\u5230\u663e\u5b58\u4e2d\u7684i\u8282\u70b9\u5904\uff0c\u4e5f\u5c31\u662f\u66f4\u65b0i\u8282\u70b9\u4fe1\u606f\uff1b\u6240\u4ee5\u7528\u8bfbi\u8282\u70b9\u4fe1\u606f\uff08\u628ai\u8282\u70b9\u4fe1\u606f\u4ece\u786c\u789f\u4e0a\u8bfb\u53d6\u5230\u7f13\u5b58\u533a\u4e2d\uff0c\u4ece\u786c\u789f\u4f7f\u7528\u7684i\u8282\u70b9\u66f4\u65b0\u5230\u663e\u5b58\u4f7f\u7528\u7684i\u8282\u70b9\uff09<\/p>\n

\n

\/\/\u4ece\u8bbe\u5907\u4e0a\u8bfb\u53d6\u6307\u5b9ai\u8282\u70b9\u7684\u4fe1\u606f\u5230\u7f13\u5b58\u4e2d\nstatic void read_inode(struct m_inode * inode)\n{\n\tstruct super_block * sb;\n\tstruct buffer_head * bh;\n\tint block;\n\tlock_inode(inode);\n\tif (!(sb=get_super(inode->i_dev)))\/\/\u6839\u636e\u8bbe\u5907\u53f7\u83b7\u53d6\u5230\u8d85\u7ea7\u5757\n\t\tpanic(\"trying to read inode without dev\");\n\t\/\/\u4e0b\u9762\u662f\u8ba1\u7b97i\u8282\u70b9\u903b\u8f91\u53f7\uff0c\u5f15\u5bfc\u5757 \u8d85\u7ea7\u5757  \u4e24\u4e2a\u4f4d\u56fe \u7136\u540e\u5c31\u662f\uff08i\u8282\u70b9\u53f7\/\u6bcf\u5757\u6240\u62e5\u6709\u7684i\u8282\u70b9\u6570\uff09\n\tblock = 2 + sb->s_imap_blocks + sb->s_zmap_blocks +\n\t\t(inode->i_num-1)\/INODES_PER_BLOCK;\n\t\/\/\u4ece\u78c1\u76d8\u4e0a\u83b7\u53d6\u5230i\u8282\u70b9\u4fe1\u606f\n\tif (!(bh=bread(inode->i_dev,block)))\n\t\tpanic(\"unable to read i-node block\");\n\t*(struct d_inode *)inode =\n\t\t((struct d_inode *)bh->b_data)\n\t\t\t[(inode->i_num-1)%INODES_PER_BLOCK];\/\/\u8fd9\u662f\u4ece\u786c\u76d8\u4e2di\u8282\u70b9\u4e0a\u83b7\u53d6\u5230\u6570\u636e\uff0c\u66f4\u65b0\u5185\u5b58\u4e2di\u8282\u70b9\u5185\u5bb9\n\tbrelse(bh);\n\tunlock_inode(inode);\n}<\/code><\/pre>\n<\/p>\n

write_inode()6\u3001\u5c06\u6307\u5b9ai\u8282\u70b9\u4fe1\u606f\u5199\u5165c\u76d8\u4e2d\uff0c\u548c\u524d\u9762\u7684read_inode()\u51fd\u6570\u6070\u597d\u76f8\u53cd\uff0c\u8be5\u51fd\u6570\u662f\u7531\u663e\u5b58\u4e2d\u4f7f\u7528\u7684i\u8282\u70b9\u5199\u5165\u5230\u786c\u789f\u4e0a\u4f7f\u7528\u7684i\u8282\u70b9\uff1b\u628a\u663e\u5b58\u4e2d\u66f4\u6539\u8fc7\u7684i\u8282\u70b9\u4fe1\u606f\u66f4\u65b0\u5230\u786c\u789f\u4e0a\u4f7f\u7528\u7684i\u8282\u70b9\u4fe1\u606f\uff1b<\/p>\n

\n

\/\/\u5c06i\u8282\u70b9\u4fe1\u606f\u5199\u5165\u7f13\u5b58\u533a\u4e2d\nstatic void write_inode(struct m_inode * inode)\/\/\u53c2\u6570\u662f\u5185\u5b58\u4e2d\u4f7f\u7528\u7684i\u8282\u70b9\u7ed3\u6784\n{\n\tstruct super_block * sb;\n\tstruct buffer_head * bh;\n\tint block;\n\tlock_inode(inode);\n\tif (!inode->i_dirt || !inode->i_dev) {\n\t\tunlock_inode(inode);\n\t\treturn;\n\t}\n\tif (!(sb=get_super(inode->i_dev)))\n\t\tpanic(\"trying to write inode without device\");\n\tblock = 2 + sb->s_imap_blocks + sb->s_zmap_blocks +\n\t\t(inode->i_num-1)\/INODES_PER_BLOCK;\/\/\u6839\u636ei\u8282\u70b9\u53f7\uff0c\u627e\u5230\u78c1\u76d8\u903b\u8f91\u5757\u4e0ai\u8282\u70b9\u4f4d\u7f6e\n\tif (!(bh=bread(inode->i_dev,block)))\/\/\u628a\u8be5i\u8282\u70b9\u78c1\u76d8\u5757\u8bfb\u53d6\u5230\u7f13\u5b58\u533a\n\t\tpanic(\"unable to read i-node block\");\n\t((struct d_inode *)bh->b_data)\/\/\u628a\u8be5\u7f13\u5b58\u533a\u5f3a\u8f6c\u4e3a \u78c1\u76d8\u4f7f\u7528\u7684i\u8282\u70b9\u7ed3\u6784\n\t\t[(inode->i_num-1)%INODES_PER_BLOCK] =\/\/\u6839\u636ei\u8282\u70b9\u53f7\uff0c\u53d6\u4f59\u5f97\u5230\u5728\u8be5\u5757\u78c1\u76d8\u4e0a\u7b2c\u51e0\u4e2a\u4f4d\u7f6e\u4e3ai\u8282\u70b9\n\t\t\t*(struct d_inode *)inode;\/\/\u7531\u5185\u5b58i\u8282\u70b9\u5f3a\u8f6c\u4e3a\u78c1\u76d8i\u8282\u70b9\u7ed3\u6784\uff0c\u56e0\u4e3a\u78c1\u76d8i\u8282\u70b9\u7ed3\u6784\u548c\u5185\u5b58i\u8282\u70b9\u7ed3\u6784\u524d\u51e0\u4f4d\u4e00\u6837\n\tbh->b_dirt=1;\n\tinode->i_dirt=0;\n\tbrelse(bh);\n\tunlock_inode(inode);\n}\n<\/code><\/pre>\n<\/p>\n

\u867d\u7136\u91cc\u9762\u7684\u8bfb\u5199i\u8282\u70b9\u4fe1\u606f\uff0c\u53ea\u662f\u5728\u65c1\u8fb9\u7684\u8f6c\u6362\u6b21\u5e8f\u4e0d\u4e00\u6837\u3002\u662f\u5728d_inode\u548cm_inode\u7ed3\u6784\u4f53\u4e4b\u95f4\u505a\u6587\u7ae0\u7684linux\u7cfb\u7edf\u4e0b\u8f7d\u5b98\u7f51\uff0c\u7531\u4e8e\u4e24\u4e2a\u7ed3\u6784\u4f53\u4e2d\u524d7\u9879\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0a\u97627\u9879\u662f\u53ef\u4ee5\u5171\u7528\uff0c\u53ef\u4ee5\u6539\u53d8\u7684linux\u6587\u4ef6\u7cfb\u7edf\u7684inode<\/strong>\uff0c\u800cm_inode\u7ed3\u6784\u4f53\u4e2d\u524d\u9762\u51e0\u9879d_inode\u7ed3\u6784\u4f53\u4e2d\u662f\u6ca1\u6709\u7684\uff08\u867d\u7136\u8fd9\u4e5f\u662f\u786e\u5b9ai\u8282\u70b9\u7684\u60df\u4e00\u6027\uff09\uff0c\u6240\u4ee5\u4e5f\u5c31\u4e0d\u5b58\u5728\u662f\u5426\u540c\u6b65\uff0c\u662f\u5426\u4e00\u6837\u3002<\/p>\n

\u8bfbi\u8282\u70b9\u6b21\u5e8f\u662f\uff1a*(structd_inode*)inode=((structd_inode*)bh->b_data)[(inode->i_num-1)%INODES_PER_BLOCK];\u4ece\u786c\u789f\u6620\u5c04\u7684\u7f13\u5b58\u533a\u4e2d\u8bfb\u53d6\u5230i\u8282\u70b9\u4fe1\u606f\u5230\u663e\u5b58i\u8282\u70b9\u7ed3\u6784\u4f53\u4e2d\uff1b<\/p>\n

\u5199i\u8282\u70b9\u6b21\u5e8f\u662f\uff1a((structd_inode*)bh->b_data)[(inode->i_num-1)%INODES_PER_BLOCK]=*(structd_inode*)inode;\u628ai\u8282\u70b9\u7ed3\u6784\u4f53\u5185\u5bb9\u4ece\u663e\u5b58\u4e2d\u5199\u5165\u5230\u786c\u789f\u6620\u5c04\u7684\u7f13\u5b58\u533a\u4e2d\u4f7f\u7528\u7684i\u8282\u70b9\u7ed3\u6784\u4f53\uff08\u867d\u7136\u4e5f\u5c31\u662f\u786c\u789f\u4e0a\u4f7f\u7528\u7684i\u8282\u70b9\uff09\uff1b<\/p>\n","protected":false},"excerpt":{"rendered":"

1\u3001_bmap()\u51fd\u6570\u7528\u4e8e\u628a\u4e00\u4e2a\u6587\u4ef6\u6570\u636e\u5757\u6620\u5c04\u5230\u76d8\u5757\u7684\u5904\u7406\u64cd\u4f5c\u56e0\u4e3a\u4e00\u4e2ai\u8282\u70b9\u5bf9\u5e94\u4e00\u4e2a\u6587\u4ef6\uff0c\u6240\u4ee5\u4e0a\u9762\u7684i\u8282\u70b9\u6620\u5c04\u7684\u903b\u8f91\u5757\u53f7\u5c31\u662f\u6587\u4ef6\u6570\u636e\u5b58\u653e\u7684\u903b\u8f91\u5757\u53f7\uff1bi_zone[0]\u5230i_zone[6]\u662f\u76f4\u63a5\u903b\u8f91\u5757\u53f7\uff0ci_zone[7]\u662f\u4e00\u6b21\u95f4\u63a5\u903b\u8f91\u5757\u53f7\uff0ci_zone[8]\u662f\u4e8c\u6b21\u95f4\u63a5\u903b\u8f91\u5757\u53f7\uff1b\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u5b58\u653e\u5728\u54ea\u4e2a\u786c\u76d8\u4e0a\u7684\u903b\u8f91\u5757\u4e0a\u5c31\u662f\u7531\u8fd9\u4e2a\u6570\u7ec4\u6765\u6620\u5c04\u7684\uff0c\u6839\u636e\u8fd9\u4e2a\u4e5f\u53ef\u4ee5\u77e5\u9053\u4e00\u4e2a\u6587\u4ef6\u7684\u6700\u5927\u5b58\u50a8\u662f\u591a\u5c11\uff1f<\/p>\n","protected":false},"author":1,"featured_media":269371,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-269370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/269370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=269370"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/269370\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/269371"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=269370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=269370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=269370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}