{"id":1651,"date":"2012-04-12T23:31:19","date_gmt":"2012-04-12T14:31:19","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1651"},"modified":"2012-04-12T23:37:56","modified_gmt":"2012-04-12T14:37:56","slug":"xv6-63-chapter-5-file-system-code-inode-contents","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1651","title":{"rendered":"[xv6 #63] Chapter 5 &#8211; File system &#8211; Code: Inode contents"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e71\u301c73\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u30c7\u30a3\u30b9\u30af\u4e0a\u306einode\u306e\u69cb\u9020\u3092\u8868\u3059dinode\u69cb\u9020\u4f53\u306f\u3001\u30b5\u30a4\u30ba\u3068\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u306e\u914d\u5217\u3092\u542b\u3093\u3067\u3044\u308b\uff08\u56f35-4\u53c2\u7167\uff09\u3002<br \/>\ninode\u306e\u30c7\u30fc\u30bf\u306f\u3001dinode\u69cb\u9020\u4f53\u306eaddrs\u914d\u5217\u306b\u8a18\u9332\u3055\u308c\u3066\u3044\u308b\u30d6\u30ed\u30c3\u30af\u7fa4\u304b\u3089\u898b\u3064\u3051\u308b\u4e8b\u304c\u3067\u304d\u308b\u3002<br \/>\n\u30c7\u30fc\u30bf\u306e\u6700\u521d\u306eNDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001\u305d\u306eaddrs\u914d\u5217\u306e\u6700\u521d\u306eNDIRECT\u500b\u306e\u30a8\u30f3\u30c8\u30ea\u306b\u8a18\u9332\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u305d\u306e\u3088\u3046\u306a\u30d6\u30ed\u30c3\u30af\u7fa4\u306f\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\uff08direct blocks\uff09\u3068\u547c\u3070\u308c\u308b\u3002<br \/>\n\u30c7\u30fc\u30bf\u306e\u6b21\u306eNINDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001\u305d\u306einode\u306b\u306f\u76f4\u63a5\u8a18\u9332\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u306b\u3042\u308b\u3002<br \/>\n\u305d\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u306f\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\uff08indirect block\uff09\u3068\u547c\u3070\u308c\u308b\u3002<br \/>\naddrs\u914d\u5217\u306e\u6700\u5f8c\u306e\u30a8\u30f3\u30c8\u30ea\u306f\u3001\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u30a2\u30c9\u30ec\u30b9\u7528\u3067\u3042\u308b\u3002<br \/>\n\u4e0a\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u521d\u306e6kB\uff08NDIRECT * BSIZE\uff09\u5206\u306f\u3001inode\u306b\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u30d6\u30ed\u30c3\u30af\u7fa4\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u51fa\u6765\u308b\u304c\u3001\u6b21\u306e64kB\uff08NINDIRECT * BSIZE\uff09\u5206\u306f\u3001\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3092\u8aad\u307f\u8fbc\u3093\u3060\u5f8c\u306b\u3057\u304b\u8aad\u307f\u8fbc\u3080\u3053\u3068\u306f\u51fa\u6765\u306a\u3044\u3002<br \/>\n\u3053\u308c\u306f\u30c7\u30a3\u30b9\u30af\u4e0a\u3067\u306e\u826f\u3044\u8868\u73fe\u6cd5\u3060\u304c\u3001\u3053\u306e\u69cb\u9020\u3092\u5229\u7528\u3059\u308b\u30b3\u30fc\u30c9\u306b\u3068\u3063\u3066\u306f\u8907\u96d1\u3067\u3042\u308b\u3002<br \/>\nbmap\u95a2\u6570\u306f\u3001readi\u95a2\u6570\u3084writei\u95a2\u6570\uff08\u3059\u3050\u5f8c\u3067\u898b\u308b\u3053\u3068\u306b\u306a\u308b\u3060\u308d\u3046\uff09\u306a\u3069\u306e\u3088\u308a\u9ad8\u3044\u5c64\u306e\u30eb\u30fc\u30c1\u30f3\u306e\u305f\u3081\u306b\u3001\u3053\u306e\u69cb\u9020\u3092\u7ba1\u7406\u3059\u308b\u3002<br \/>\nbmap\u95a2\u6570\u306f\u3001inode\u3067\u3042\u308bip\u3092\u53d7\u3051\u53d6\u308a\u3001\u305d\u306einode\u306ebn\u756a\u76ee\u306e\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u3092\u8fd4\u3059\u3002<br \/>\n\u3082\u3057ip\u304c\u305d\u306e\u3088\u3046\u306a\u30d6\u30ed\u30c3\u30af\u3092\u307e\u3060\u6301\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001bmap\u95a2\u6570\u304c\u305d\u308c\u3092\u5272\u308a\u5f53\u3066\u308b\u3002<\/p>\n<p><a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19-300x258.png\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-04-12 22.43.19\" width=\"300\" height=\"258\" class=\"aligncenter size-medium wp-image-1652\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19-300x258.png 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19.png 619w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u56f35-4 \u30c7\u30a3\u30b9\u30af\u4e0a\u306b\u304a\u3051\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u8868\u73fe<\/p>\n<p>fs.c\u306ebmap\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Inode contents\r\n\/\/\r\n\/\/ The contents (data) associated with each inode is stored\r\n\/\/ in a sequence of blocks on the disk.  The first NDIRECT blocks\r\n\/\/ are listed in ip-&gt;addrs&#x5B;].  The next NINDIRECT blocks are \r\n\/\/ listed in the block ip-&gt;addrs&#x5B;NDIRECT].\r\n\r\n\/\/ Return the disk block address of the nth block in inode ip.\r\n\/\/ If there is no such block, bmap allocates one.\r\nstatic uint\r\nbmap(struct inode *ip, uint bn)\r\n{\r\n  uint addr, *a;\r\n  struct buf *bp;\r\n\r\n  if(bn &lt; NDIRECT){\r\n    if((addr = ip-&gt;addrs&#x5B;bn]) == 0)\r\n      ip-&gt;addrs&#x5B;bn] = addr = balloc(ip-&gt;dev);\r\n    return addr;\r\n  }\r\n  bn -= NDIRECT;\r\n\r\n  if(bn &lt; NINDIRECT){\r\n    \/\/ Load indirect block, allocating if necessary.\r\n    if((addr = ip-&gt;addrs&#x5B;NDIRECT]) == 0)\r\n      ip-&gt;addrs&#x5B;NDIRECT] = addr = balloc(ip-&gt;dev);\r\n    bp = bread(ip-&gt;dev, addr);\r\n    a = (uint*)bp-&gt;data;\r\n    if((addr = a&#x5B;bn]) == 0){\r\n      a&#x5B;bn] = addr = balloc(ip-&gt;dev);\r\n      log_write(bp);\r\n    }\r\n    brelse(bp);\r\n    return addr;\r\n  }\r\n\r\n  panic(&quot;bmap: out of range&quot;);\r\n}<\/pre>\n<p>bmap\u95a2\u6570\u306f\u3001\u7c21\u5358\u306a\u4ed5\u4e8b\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u6700\u521d\u306eNDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001inode\u305d\u308c\u81ea\u8eab\u306b\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u6b21\u306eNINDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001ip-&gt;addrs[NDIRECT]\u3067\u793a\u3055\u308c\u308b\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306b\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\nbmap\u95a2\u6570\u306f\u3001\u305d\u306e\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3092\u8aad\u307f\u8fbc\u307f\u3001\u305d\u3057\u3066\u305d\u306e\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u4e2d\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u304b\u3089\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u3092\u8aad\u307f\u51fa\u3059\u3002<br \/>\n\u3082\u3057\u305d\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u304c\u3001NDIRECT + NINDIRECT\u3092\u8d8a\u3048\u3066\u3044\u308b\u5834\u5408\u3001bmap\u95a2\u6570\u306fpanic\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\n\u547c\u3073\u51fa\u3057\u5074\u306f\u3001\u7bc4\u56f2\u5916\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u306b\u3064\u3044\u3066\u554f\u3044\u5408\u308f\u305b\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u8cac\u4efb\u304c\u3042\u308b\u3002<\/p>\n<p>bmap\u95a2\u6570\u306f\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u30d6\u30ed\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u308b\u3002<br \/>\n\u672a\u5272\u308a\u5f53\u3066\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001\u30d6\u30ed\u30c3\u30af\u756a\u53f70\u3068\u3057\u3066\u793a\u3055\u308c\u308b\u3002<br \/>\n\u3088\u3063\u3066bmap\u95a2\u6570\u306f0\u306b\u906d\u9047\u3057\u305f\u3089\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u65b0\u3057\u3044\u30d6\u30ed\u30c3\u30af\u306e\u756a\u53f7\u3067\u305d\u308c\u3092\u7f6e\u304d\u63db\u3048\u308b\u3002<\/p>\n<p>bmap\u95a2\u6570\u306f\u3001\u305d\u306einode\u304c\u6210\u9577\u3059\u308b\u3068\u304d\u306b\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u30d6\u30ed\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u308b\u3002<br \/>\nitrunc\u95a2\u6570\u306f\u3001\u305d\u308c\u3089\u3092\u89e3\u653e\u3057\u3001inode\u306e\u30b5\u30a4\u30ba\u30920\u306b\u30ea\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nitrunc\u95a2\u6570\u306f\u3001\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3092\u89e3\u653e\u3059\u308b\u3053\u3068\u304b\u3089\u521d\u3081\u3001\u305d\u3057\u3066\u305d\u308c\u304b\u3089\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u5185\u5bb9\u3092\u89e3\u653e\u3057\u3001\u305d\u3057\u3066\u6700\u5f8c\u306b\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u305d\u306e\u3082\u306e\u3092\u89e3\u653e\u3059\u308b\u3002<\/p>\n<p>fs.c\u306eitrunc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Truncate inode (discard contents).\r\n\/\/ Only called after the last dirent referring\r\n\/\/ to this inode has been erased on disk.\r\nstatic void\r\nitrunc(struct inode *ip)\r\n{\r\n  int i, j;\r\n  struct buf *bp;\r\n  uint *a;\r\n\r\n  for(i = 0; i &lt; NDIRECT; i++){\r\n    if(ip-&gt;addrs&#x5B;i]){\r\n      bfree(ip-&gt;dev, ip-&gt;addrs&#x5B;i]);\r\n      ip-&gt;addrs&#x5B;i] = 0;\r\n    }\r\n  }\r\n  \r\n  if(ip-&gt;addrs&#x5B;NDIRECT]){\r\n    bp = bread(ip-&gt;dev, ip-&gt;addrs&#x5B;NDIRECT]);\r\n    a = (uint*)bp-&gt;data;\r\n    for(j = 0; j &lt; NINDIRECT; j++){\r\n      if(a&#x5B;j])\r\n        bfree(ip-&gt;dev, a&#x5B;j]);\r\n    }\r\n    brelse(bp);\r\n    bfree(ip-&gt;dev, ip-&gt;addrs&#x5B;NDIRECT]);\r\n    ip-&gt;addrs&#x5B;NDIRECT] = 0;\r\n  }\r\n\r\n  ip-&gt;size = 0;\r\n  iupdate(ip);\r\n}<\/pre>\n<p>bmap\u95a2\u6570\u306f\u3001readi\u95a2\u6570\u3084writei\u95a2\u6570\u306e\u3088\u3046\u306a\u3001inode\u306e\u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u3078\u30a2\u30af\u30bb\u30b9\u3059\u308b\u95a2\u6570\u3092\u66f8\u304d\u3084\u3059\u304f\u3059\u308b\u3002<br \/>\nreadi\u95a2\u6570\u306f\u3001inode\u304b\u3089\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u3002<br \/>\nreadi\u95a2\u6570\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u30aa\u30d5\u30bb\u30c3\u30c8\u3068\u30ab\u30a6\u30f3\u30c8\u304c\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u8d85\u3048\u306a\u3044\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u4e8b\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u8d85\u3048\u305f\u3068\u3053\u308d\u304b\u3089\u59cb\u307e\u308b\u3088\u3046\u306a\u8aad\u307f\u8fbc\u307f\u306b\u306f\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u307e\u305f\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u307e\u305f\u3050\u3088\u3046\u306a\u6307\u793a\u304c\u3042\u3063\u305f\u5834\u5408\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u3088\u308a\u3082\u5c11\u306a\u3044\u30c7\u30fc\u30bf\u3092\u8fd4\u3059\u3002<br \/>\n\u30e1\u30a4\u30f3\u306e\u30eb\u30fc\u30d7\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u5404\u30d6\u30ed\u30c3\u30af\u3092\u51e6\u7406\u3057\u3001\u30d0\u30c3\u30d5\u30a1\u304b\u3089\u30c7\u30fc\u30bf\u3092dst\u306b\u683c\u7d0d\u3059\u308b\u3002<br \/>\nwritei\u95a2\u6570\u306f\u30013\u3064\u306e\u4f8b\u5916\u3092\u9664\u3044\u3066readi\u95a2\u6570\u3068\u3068\u3066\u3082\u3088\u304f\u4f3c\u3066\u3044\u308b\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u304b\u3089\u3001\u3082\u3057\u304f\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u3092\u307e\u305f\u3050\u3088\u3046\u306a\u66f8\u304d\u8fbc\u307f\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u5927\u30b5\u30a4\u30ba\u3092\u4e0a\u9650\u3068\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u5927\u304d\u304f\u3059\u308b\u3002<br \/>\n\u30e1\u30a4\u30f3\u306e\u30eb\u30fc\u30d7\u3067\u306f\u3001\u30c7\u30fc\u30bf\u3092\u30d0\u30c3\u30d5\u30a1\u304b\u3089\u53d6\u308a\u51fa\u3059\u306e\u3067\u306f\u306a\u304f\u3001\u30d0\u30c3\u30d5\u30a1\u306b\u683c\u7d0d\u3059\u308b\u3002<br \/>\n\u305d\u3057\u3066\u3001\u66f8\u304d\u8fbc\u307f\u304c\u30d5\u30a1\u30a4\u30eb\u3092\u62e1\u5f35\u3057\u305f\u5834\u5408\u3001writei\u95a2\u6570\u306f\u305d\u306e\u30b5\u30a4\u30ba\u3092\u66f4\u65b0\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>fs.c\u306ereadi, writei\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Read data from inode.\r\nint\r\nreadi(struct inode *ip, char *dst, uint off, uint n)\r\n{\r\n  uint tot, m;\r\n  struct buf *bp;\r\n\r\n  if(ip-&gt;type == T_DEV){\r\n    if(ip-&gt;major &lt; 0 || ip-&gt;major &gt;= NDEV || !devsw&#x5B;ip-&gt;major].read)\r\n      return -1;\r\n    return devsw&#x5B;ip-&gt;major].read(ip, dst, n);\r\n  }\r\n\r\n  if(off &gt; ip-&gt;size || off + n &lt; off)\r\n    return -1;\r\n  if(off + n &gt; ip-&gt;size)\r\n    n = ip-&gt;size - off;\r\n\r\n  for(tot=0; tot&lt;n; tot+=m, off+=m, dst+=m){\r\n    bp = bread(ip-&gt;dev, bmap(ip, off\/BSIZE));\r\n    m = min(n - tot, BSIZE - off%BSIZE);\r\n    memmove(dst, bp-&gt;data + off%BSIZE, m);\r\n    brelse(bp);\r\n  }\r\n  return n;\r\n}\r\n\r\n\/\/ PAGEBREAK!\r\n\/\/ Write data to inode.\r\nint\r\nwritei(struct inode *ip, char *src, uint off, uint n)\r\n{\r\n  uint tot, m;\r\n  struct buf *bp;\r\n\r\n  if(ip-&gt;type == T_DEV){\r\n    if(ip-&gt;major &lt; 0 || ip-&gt;major &gt;= NDEV || !devsw&#x5B;ip-&gt;major].write)\r\n      return -1;\r\n    return devsw&#x5B;ip-&gt;major].write(ip, src, n);\r\n  }\r\n\r\n  if(off &gt; ip-&gt;size || off + n &lt; off)\r\n    return -1;\r\n  if(off + n &gt; MAXFILE*BSIZE)\r\n    return -1;\r\n\r\n  for(tot=0; tot&lt;n; tot+=m, off+=m, src+=m){\r\n    bp = bread(ip-&gt;dev, bmap(ip, off\/BSIZE));\r\n    m = min(n - tot, BSIZE - off%BSIZE);\r\n    memmove(bp-&gt;data + off%BSIZE, src, m);\r\n    log_write(bp);\r\n    brelse(bp);\r\n  }\r\n\r\n  if(n &gt; 0 &amp;&amp; off &gt; ip-&gt;size){\r\n    ip-&gt;size = off;\r\n    iupdate(ip);\r\n  }\r\n  return n;\r\n}<\/pre>\n<p>readi\u95a2\u6570\u3068writei\u95a2\u6570\u306e\u4e21\u65b9\u5171\u3001ip-&gt;type == T_DEV\u306e\u30c1\u30a7\u30c3\u30af\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u306b\u30c7\u30fc\u30bf\u304c\u306a\u3044\u3001\u7279\u6b8a\u306a\u30c7\u30d0\u30a4\u30b9\u3092\u5236\u5fa1\u3059\u308b\u305f\u3081\u306b\u3042\u308b\u3002<br \/>\n\u305d\u306e\u5834\u5408\u306b\u3064\u3044\u3066\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u306e\u5c64\u306e\u7bc0\u3067\u8aac\u660e\u3059\u308b\u3060\u308d\u3046\u3002<\/p>\n<p>stati\u95a2\u6570\u306f\u3001inode\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u3092stat\u69cb\u9020\u4f53\u306b\u30b3\u30d4\u30fc\u3059\u308b\u3002<br \/>\n\u3053\u306e\u69cb\u9020\u4f53\u306f\u3001stat\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u7d4c\u7531\u3067\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u305d\u306e\u307e\u307e\u63d0\u4f9b\u3055\u308c\u308b\u3002<\/p>\n<p>fs.c\u306estati\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Copy stat information from inode.\r\nvoid\r\nstati(struct inode *ip, struct stat *st)\r\n{\r\n  st-&gt;dev = ip-&gt;dev;\r\n  st-&gt;ino = ip-&gt;inum;\r\n  st-&gt;type = ip-&gt;type;\r\n  st-&gt;nlink = ip-&gt;nlink;\r\n  st-&gt;size = ip-&gt;size;\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30d5\u30a1\u30a4\u30eb\u304c\u30c7\u30a3\u30b9\u30af\u4e0a\u3067\u3069\u306e\u3088\u3046\u306b\u8868\u73fe\u3055\u308c\u3066\u3044\u308b\u304b\u3068\u3001\u305d\u306e\u5468\u8fba\u306e\uff08\u3068\u3044\u3063\u3066\u3082\u3069\u308c\u3082\u5fc5\u8981\u4e0d\u53ef\u6b20\uff09\u95a2\u6570\u306b\u3064\u3044\u3066\u306e\u8aac\u660e\u3067\u3059\u3002<\/p>\n<p>bmap\u306e\u5b9f\u88c5\u304b\u3089\u3057\u30661\u30d5\u30a1\u30a4\u30eb70kB\uff08NDIRECT * BSIZE + NINDIRECT * BSIZE\uff09\u307e\u3067\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\nwritei\u3092\u898b\u3066\u3082\u3001MAXFILE * BSIZE\u307e\u3067\u3068\u306a\u3063\u3066\u3044\u3066\u3001MAXFILE\u306fNDIRECT + NINDIRECT\u3068\u5b9a\u7fa9\u3055\u308c\u3066\u308b\u306e\u3067\u591a\u5206\u305d\u3046\u3067\u3057\u3087\u3046\u3002<br \/>\n\u3053\u308c\u3092\u62e1\u5f35\u3059\u308b\u306b\u306f\u3001\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3067\u3082\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u3088\u3046\u306b\u3001addrs\u914d\u5217\u306e\u6700\u5f8c\u306b\u6b21\u306e\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u3092\u542b\u3081\u308b\u3088\u3046\u306b\u3059\u308c\u3070\u3044\u3044\u306e\u304b\u306a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e71\u301c73\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u30c7\u30a3\u30b9\u30af\u4e0a\u306einode\u306e\u69cb\u9020\u3092\u8868\u3059dinode\u69cb\u9020\u4f53\u306f\u3001\u30b5\u30a4\u30ba\u3068\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u306e\u914d\u5217\u3092\u542b\u3093\u3067\u3044\u308b\uff08\u56f35-4\u53c2\u7167\uff09\u3002<br \/>\ninode\u306e\u30c7\u30fc\u30bf\u306f\u3001dinode\u69cb\u9020\u4f53\u306eaddrs\u914d\u5217\u306b\u8a18\u9332\u3055\u308c\u3066\u3044\u308b\u30d6\u30ed\u30c3\u30af\u7fa4\u304b\u3089\u898b\u3064\u3051\u308b\u4e8b\u304c\u3067\u304d\u308b\u3002<br \/>\n\u30c7\u30fc\u30bf\u306e\u6700\u521d\u306eNDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001\u305d\u306eaddrs\u914d\u5217\u306e\u6700\u521d\u306eNDIRECT\u500b\u306e\u30a8\u30f3\u30c8\u30ea\u306b\u8a18\u9332\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u305d\u306e\u3088\u3046\u306a\u30d6\u30ed\u30c3\u30af\u7fa4\u306f\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\uff08direct blocks\uff09\u3068\u547c\u3070\u308c\u308b\u3002<br \/>\n\u30c7\u30fc\u30bf\u306e\u6b21\u306eNINDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001\u305d\u306einode\u306b\u306f\u76f4\u63a5\u8a18\u9332\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u306b\u3042\u308b\u3002<br \/>\n\u305d\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u306f\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\uff08indirect block\uff09\u3068\u547c\u3070\u308c\u308b\u3002<br \/>\naddrs\u914d\u5217\u306e\u6700\u5f8c\u306e\u30a8\u30f3\u30c8\u30ea\u306f\u3001\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u30a2\u30c9\u30ec\u30b9\u7528\u3067\u3042\u308b\u3002<br \/>\n\u4e0a\u3067\u8ff0\u3079\u305f\u3088\u3046\u306b\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u521d\u306e6kB\uff08NDIRECT * BSIZE\uff09\u5206\u306f\u3001inode\u306b\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u30d6\u30ed\u30c3\u30af\u7fa4\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u51fa\u6765\u308b\u304c\u3001\u6b21\u306e64kB\uff08NINDIRECT * BSIZE\uff09\u5206\u306f\u3001\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3092\u8aad\u307f\u8fbc\u3093\u3060\u5f8c\u306b\u3057\u304b\u8aad\u307f\u8fbc\u3080\u3053\u3068\u306f\u51fa\u6765\u306a\u3044\u3002<br \/>\n\u3053\u308c\u306f\u30c7\u30a3\u30b9\u30af\u4e0a\u3067\u306e\u826f\u3044\u8868\u73fe\u6cd5\u3060\u304c\u3001\u3053\u306e\u69cb\u9020\u3092\u5229\u7528\u3059\u308b\u30b3\u30fc\u30c9\u306b\u3068\u3063\u3066\u306f\u8907\u96d1\u3067\u3042\u308b\u3002<br \/>\nbmap\u95a2\u6570\u306f\u3001readi\u95a2\u6570\u3084writei\u95a2\u6570\uff08\u3059\u3050\u5f8c\u3067\u898b\u308b\u3053\u3068\u306b\u306a\u308b\u3060\u308d\u3046\uff09\u306a\u3069\u306e\u3088\u308a\u9ad8\u3044\u5c64\u306e\u30eb\u30fc\u30c1\u30f3\u306e\u305f\u3081\u306b\u3001\u3053\u306e\u69cb\u9020\u3092\u7ba1\u7406\u3059\u308b\u3002<br \/>\nbmap\u95a2\u6570\u306f\u3001inode\u3067\u3042\u308bip\u3092\u53d7\u3051\u53d6\u308a\u3001\u305d\u306einode\u306ebn\u756a\u76ee\u306e\u30c7\u30fc\u30bf\u30d6\u30ed\u30c3\u30af\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u3092\u8fd4\u3059\u3002<br \/>\n\u3082\u3057ip\u304c\u305d\u306e\u3088\u3046\u306a\u30d6\u30ed\u30c3\u30af\u3092\u307e\u3060\u6301\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001bmap\u95a2\u6570\u304c\u305d\u308c\u3092\u5272\u308a\u5f53\u3066\u308b\u3002<\/p>\n<p><a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19-300x258.png\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-04-12 22.43.19\" width=\"300\" height=\"258\" class=\"aligncenter size-medium wp-image-1652\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19-300x258.png 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/04\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-04-12-22.43.19.png 619w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u56f35-4 \u30c7\u30a3\u30b9\u30af\u4e0a\u306b\u304a\u3051\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u8868\u73fe<\/p>\n<p>fs.c\u306ebmap\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Inode contents\r\n\/\/\r\n\/\/ The contents (data) associated with each inode is stored\r\n\/\/ in a sequence of blocks on the disk.  The first NDIRECT blocks\r\n\/\/ are listed in ip-&gt;addrs&#x5B;].  The next NINDIRECT blocks are \r\n\/\/ listed in the block ip-&gt;addrs&#x5B;NDIRECT].\r\n\r\n\/\/ Return the disk block address of the nth block in inode ip.\r\n\/\/ If there is no such block, bmap allocates one.\r\nstatic uint\r\nbmap(struct inode *ip, uint bn)\r\n{\r\n  uint addr, *a;\r\n  struct buf *bp;\r\n\r\n  if(bn &lt; NDIRECT){\r\n    if((addr = ip-&gt;addrs&#x5B;bn]) == 0)\r\n      ip-&gt;addrs&#x5B;bn] = addr = balloc(ip-&gt;dev);\r\n    return addr;\r\n  }\r\n  bn -= NDIRECT;\r\n\r\n  if(bn &lt; NINDIRECT){\r\n    \/\/ Load indirect block, allocating if necessary.\r\n    if((addr = ip-&gt;addrs&#x5B;NDIRECT]) == 0)\r\n      ip-&gt;addrs&#x5B;NDIRECT] = addr = balloc(ip-&gt;dev);\r\n    bp = bread(ip-&gt;dev, addr);\r\n    a = (uint*)bp-&gt;data;\r\n    if((addr = a&#x5B;bn]) == 0){\r\n      a&#x5B;bn] = addr = balloc(ip-&gt;dev);\r\n      log_write(bp);\r\n    }\r\n    brelse(bp);\r\n    return addr;\r\n  }\r\n\r\n  panic(&quot;bmap: out of range&quot;);\r\n}<\/pre>\n<p>bmap\u95a2\u6570\u306f\u3001\u7c21\u5358\u306a\u4ed5\u4e8b\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u6700\u521d\u306eNDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001inode\u305d\u308c\u81ea\u8eab\u306b\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u6b21\u306eNINDIRECT\u500b\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001ip-&gt;addrs[NDIRECT]\u3067\u793a\u3055\u308c\u308b\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306b\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\nbmap\u95a2\u6570\u306f\u3001\u305d\u306e\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3092\u8aad\u307f\u8fbc\u307f\u3001\u305d\u3057\u3066\u305d\u306e\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u4e2d\u306e\u6b63\u3057\u3044\u4f4d\u7f6e\u304b\u3089\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u3092\u8aad\u307f\u51fa\u3059\u3002<br \/>\n\u3082\u3057\u305d\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u304c\u3001NDIRECT + NINDIRECT\u3092\u8d8a\u3048\u3066\u3044\u308b\u5834\u5408\u3001bmap\u95a2\u6570\u306fpanic\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\n\u547c\u3073\u51fa\u3057\u5074\u306f\u3001\u7bc4\u56f2\u5916\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u306b\u3064\u3044\u3066\u554f\u3044\u5408\u308f\u305b\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u8cac\u4efb\u304c\u3042\u308b\u3002<\/p>\n<p>bmap\u95a2\u6570\u306f\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u30d6\u30ed\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u308b\u3002<br \/>\n\u672a\u5272\u308a\u5f53\u3066\u306e\u30d6\u30ed\u30c3\u30af\u306f\u3001\u30d6\u30ed\u30c3\u30af\u756a\u53f70\u3068\u3057\u3066\u793a\u3055\u308c\u308b\u3002<br \/>\n\u3088\u3063\u3066bmap\u95a2\u6570\u306f0\u306b\u906d\u9047\u3057\u305f\u3089\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u65b0\u3057\u3044\u30d6\u30ed\u30c3\u30af\u306e\u756a\u53f7\u3067\u305d\u308c\u3092\u7f6e\u304d\u63db\u3048\u308b\u3002<\/p>\n<p>bmap\u95a2\u6570\u306f\u3001\u305d\u306einode\u304c\u6210\u9577\u3059\u308b\u3068\u304d\u306b\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u30d6\u30ed\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u308b\u3002<br \/>\nitrunc\u95a2\u6570\u306f\u3001\u305d\u308c\u3089\u3092\u89e3\u653e\u3057\u3001inode\u306e\u30b5\u30a4\u30ba\u30920\u306b\u30ea\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nitrunc\u95a2\u6570\u306f\u3001\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3092\u89e3\u653e\u3059\u308b\u3053\u3068\u304b\u3089\u521d\u3081\u3001\u305d\u3057\u3066\u305d\u308c\u304b\u3089\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u5185\u5bb9\u3092\u89e3\u653e\u3057\u3001\u305d\u3057\u3066\u6700\u5f8c\u306b\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u305d\u306e\u3082\u306e\u3092\u89e3\u653e\u3059\u308b\u3002<\/p>\n<p>fs.c\u306eitrunc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Truncate inode (discard contents).\r\n\/\/ Only called after the last dirent referring\r\n\/\/ to this inode has been erased on disk.\r\nstatic void\r\nitrunc(struct inode *ip)\r\n{\r\n  int i, j;\r\n  struct buf *bp;\r\n  uint *a;\r\n\r\n  for(i = 0; i &lt; NDIRECT; i++){\r\n    if(ip-&gt;addrs&#x5B;i]){\r\n      bfree(ip-&gt;dev, ip-&gt;addrs&#x5B;i]);\r\n      ip-&gt;addrs&#x5B;i] = 0;\r\n    }\r\n  }\r\n  \r\n  if(ip-&gt;addrs&#x5B;NDIRECT]){\r\n    bp = bread(ip-&gt;dev, ip-&gt;addrs&#x5B;NDIRECT]);\r\n    a = (uint*)bp-&gt;data;\r\n    for(j = 0; j &lt; NINDIRECT; j++){\r\n      if(a&#x5B;j])\r\n        bfree(ip-&gt;dev, a&#x5B;j]);\r\n    }\r\n    brelse(bp);\r\n    bfree(ip-&gt;dev, ip-&gt;addrs&#x5B;NDIRECT]);\r\n    ip-&gt;addrs&#x5B;NDIRECT] = 0;\r\n  }\r\n\r\n  ip-&gt;size = 0;\r\n  iupdate(ip);\r\n}<\/pre>\n<p>bmap\u95a2\u6570\u306f\u3001readi\u95a2\u6570\u3084writei\u95a2\u6570\u306e\u3088\u3046\u306a\u3001inode\u306e\u30c7\u30fc\u30bf\u30b9\u30c8\u30ea\u30fc\u30e0\u3078\u30a2\u30af\u30bb\u30b9\u3059\u308b\u95a2\u6570\u3092\u66f8\u304d\u3084\u3059\u304f\u3059\u308b\u3002<br \/>\nreadi\u95a2\u6570\u306f\u3001inode\u304b\u3089\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u3002<br \/>\nreadi\u95a2\u6570\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u30aa\u30d5\u30bb\u30c3\u30c8\u3068\u30ab\u30a6\u30f3\u30c8\u304c\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u8d85\u3048\u306a\u3044\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u4e8b\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u8d85\u3048\u305f\u3068\u3053\u308d\u304b\u3089\u59cb\u307e\u308b\u3088\u3046\u306a\u8aad\u307f\u8fbc\u307f\u306b\u306f\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u307e\u305f\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u307e\u305f\u3050\u3088\u3046\u306a\u6307\u793a\u304c\u3042\u3063\u305f\u5834\u5408\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u3088\u308a\u3082\u5c11\u306a\u3044\u30c7\u30fc\u30bf\u3092\u8fd4\u3059\u3002<br \/>\n\u30e1\u30a4\u30f3\u306e\u30eb\u30fc\u30d7\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u5404\u30d6\u30ed\u30c3\u30af\u3092\u51e6\u7406\u3057\u3001\u30d0\u30c3\u30d5\u30a1\u304b\u3089\u30c7\u30fc\u30bf\u3092dst\u306b\u683c\u7d0d\u3059\u308b\u3002<br \/>\nwritei\u95a2\u6570\u306f\u30013\u3064\u306e\u4f8b\u5916\u3092\u9664\u3044\u3066readi\u95a2\u6570\u3068\u3068\u3066\u3082\u3088\u304f\u4f3c\u3066\u3044\u308b\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u304b\u3089\u3001\u3082\u3057\u304f\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u3092\u307e\u305f\u3050\u3088\u3046\u306a\u66f8\u304d\u8fbc\u307f\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u5927\u30b5\u30a4\u30ba\u3092\u4e0a\u9650\u3068\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u5927\u304d\u304f\u3059\u308b\u3002<br \/>\n\u30e1\u30a4\u30f3\u306e\u30eb\u30fc\u30d7\u3067\u306f\u3001\u30c7\u30fc\u30bf\u3092\u30d0\u30c3\u30d5\u30a1\u304b\u3089\u53d6\u308a\u51fa\u3059\u306e\u3067\u306f\u306a\u304f\u3001\u30d0\u30c3\u30d5\u30a1\u306b\u683c\u7d0d\u3059\u308b\u3002<br \/>\n\u305d\u3057\u3066\u3001\u66f8\u304d\u8fbc\u307f\u304c\u30d5\u30a1\u30a4\u30eb\u3092\u62e1\u5f35\u3057\u305f\u5834\u5408\u3001writei\u95a2\u6570\u306f\u305d\u306e\u30b5\u30a4\u30ba\u3092\u66f4\u65b0\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>fs.c\u306ereadi, writei\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Read data from inode.\r\nint\r\nreadi(struct inode *ip, char *dst, uint off, uint n)\r\n{\r\n  uint tot, m;\r\n  struct buf *bp;\r\n\r\n  if(ip-&gt;type == T_DEV){\r\n    if(ip-&gt;major &lt; 0 || ip-&gt;major &gt;= NDEV || !devsw&#x5B;ip-&gt;major].read)\r\n      return -1;\r\n    return devsw&#x5B;ip-&gt;major].read(ip, dst, n);\r\n  }\r\n\r\n  if(off &gt; ip-&gt;size || off + n &lt; off)\r\n    return -1;\r\n  if(off + n &gt; ip-&gt;size)\r\n    n = ip-&gt;size - off;\r\n\r\n  for(tot=0; tot&lt;n; tot+=m, off+=m, dst+=m){\r\n    bp = bread(ip-&gt;dev, bmap(ip, off\/BSIZE));\r\n    m = min(n - tot, BSIZE - off%BSIZE);\r\n    memmove(dst, bp-&gt;data + off%BSIZE, m);\r\n    brelse(bp);\r\n  }\r\n  return n;\r\n}\r\n\r\n\/\/ PAGEBREAK!\r\n\/\/ Write data to inode.\r\nint\r\nwritei(struct inode *ip, char *src, uint off, uint n)\r\n{\r\n  uint tot, m;\r\n  struct buf *bp;\r\n\r\n  if(ip-&gt;type == T_DEV){\r\n    if(ip-&gt;major &lt; 0 || ip-&gt;major &gt;= NDEV || !devsw&#x5B;ip-&gt;major].write)\r\n      return -1;\r\n    return devsw&#x5B;ip-&gt;major].write(ip, src, n);\r\n  }\r\n\r\n  if(off &gt; ip-&gt;size || off + n &lt; off)\r\n    return -1;\r\n  if(off + n &gt; MAXFILE*BSIZE)\r\n    return -1;\r\n\r\n  for(tot=0; tot&lt;n; tot+=m, off+=m, src+=m){\r\n    bp = bread(ip-&gt;dev, bmap(ip, off\/BSIZE));\r\n    m = min(n - tot, BSIZE - off%BSIZE);\r\n    memmove(bp-&gt;data + off%BSIZE, src, m);\r\n    log_write(bp);\r\n    brelse(bp);\r\n  }\r\n\r\n  if(n &gt; 0 &amp;&amp; off &gt; ip-&gt;size){\r\n    ip-&gt;size = off;\r\n    iupdate(ip);\r\n  }\r\n  return n;\r\n}<\/pre>\n<p>readi\u95a2\u6570\u3068writei\u95a2\u6570\u306e\u4e21\u65b9\u5171\u3001ip-&gt;type == T_DEV\u306e\u30c1\u30a7\u30c3\u30af\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u306b\u30c7\u30fc\u30bf\u304c\u306a\u3044\u3001\u7279\u6b8a\u306a\u30c7\u30d0\u30a4\u30b9\u3092\u5236\u5fa1\u3059\u308b\u305f\u3081\u306b\u3042\u308b\u3002<br \/>\n\u305d\u306e\u5834\u5408\u306b\u3064\u3044\u3066\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u306e\u5c64\u306e\u7bc0\u3067\u8aac\u660e\u3059\u308b\u3060\u308d\u3046\u3002<\/p>\n<p>stati\u95a2\u6570\u306f\u3001inode\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u3092stat\u69cb\u9020\u4f53\u306b\u30b3\u30d4\u30fc\u3059\u308b\u3002<br \/>\n\u3053\u306e\u69cb\u9020\u4f53\u306f\u3001stat\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u7d4c\u7531\u3067\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u305d\u306e\u307e\u307e\u63d0\u4f9b\u3055\u308c\u308b\u3002<\/p>\n<p>fs.c\u306estati\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Copy stat information from inode.\r\nvoid\r\nstati(struct inode *ip, struct stat *st)\r\n{\r\n  st-&gt;dev = ip-&gt;dev;\r\n  st-&gt;ino = ip-&gt;inum;\r\n  st-&gt;type = ip-&gt;type;\r\n  st-&gt;nlink = ip-&gt;nlink;\r\n  st-&gt;size = ip-&gt;size;\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30d5\u30a1\u30a4\u30eb\u304c\u30c7\u30a3\u30b9\u30af\u4e0a\u3067\u3069\u306e\u3088\u3046\u306b\u8868\u73fe\u3055\u308c\u3066\u3044\u308b\u304b\u3068\u3001\u305d\u306e\u5468\u8fba\u306e\uff08\u3068\u3044\u3063\u3066\u3082\u3069\u308c\u3082\u5fc5\u8981\u4e0d\u53ef\u6b20\uff09\u95a2\u6570\u306b\u3064\u3044\u3066\u306e\u8aac\u660e\u3067\u3059\u3002<\/p>\n<p>bmap\u306e\u5b9f\u88c5\u304b\u3089\u3057\u30661\u30d5\u30a1\u30a4\u30eb70kB\uff08NDIRECT * BSIZE + NINDIRECT * BSIZE\uff09\u307e\u3067\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\nwritei\u3092\u898b\u3066\u3082\u3001MAXFILE * BSIZE\u307e\u3067\u3068\u306a\u3063\u3066\u3044\u3066\u3001MAXFILE\u306fNDIRECT + NINDIRECT\u3068\u5b9a\u7fa9\u3055\u308c\u3066\u308b\u306e\u3067\u591a\u5206\u305d\u3046\u3067\u3057\u3087\u3046\u3002<br \/>\n\u3053\u308c\u3092\u62e1\u5f35\u3059\u308b\u306b\u306f\u3001\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u3067\u3082\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u3088\u3046\u306b\u3001addrs\u914d\u5217\u306e\u6700\u5f8c\u306b\u6b21\u306e\u30a4\u30f3\u30c0\u30a4\u30ec\u30af\u30c8\u30d6\u30ed\u30c3\u30af\u306e\u30d6\u30ed\u30c3\u30af\u756a\u53f7\u3092\u542b\u3081\u308b\u3088\u3046\u306b\u3059\u308c\u3070\u3044\u3044\u306e\u304b\u306a\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[405],"class_list":["post-1651","post","type-post","status-publish","format-standard","hentry","category-tech","tag-xv6"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}