{"id":1680,"date":"2012-04-29T06:40:09","date_gmt":"2012-04-28T21:40:09","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1680"},"modified":"2012-04-29T07:12:14","modified_gmt":"2012-04-28T22:12:14","slug":"xv6-69-chapter-5-file-system-exercises","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1680","title":{"rendered":"[xv6 #69] Chapter 5 &#8211; File system &#8211; Exercises"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e77\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>1. \u3069\u3046\u3044\u3046\u5834\u5408\u306bballoc\u3067panic\u304c\u8d77\u304d\u308b\u304b\uff1f \u305d\u308c\u3092\u56de\u5fa9\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u308b\u304b\uff1f<\/p>\n<p>2. \u3069\u3046\u3044\u3046\u5834\u5408\u306bialloc\u3067panic\u304c\u8d77\u304d\u308b\u304b\uff1f \u305d\u308c\u3092\u56de\u5fa9\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u308b\u304b\uff1f<\/p>\n<p>3. inode\u4e16\u4ee3\u756a\u53f7\u3002<\/p>\n<p>4. file\u304c\u306a\u3044\u5834\u5408\u306b\u306a\u305cfilealloc\u306fpanic\u3092\u8d77\u3053\u3055\u306a\u3044\u306e\u304b\uff1f \u306a\u305c\u3053\u308c\u304c\u3088\u308a\u4e00\u822c\u7684\u3067\u305d\u308c\u3086\u3048\u826f\u3044\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u306e\u65b9\u6cd5\u306a\u306e\u304b\uff1f<\/p>\n<p>5. sys_link\u304ciunlock(ip)\u3068dirlink\u3092\u547c\u3073\u51fa\u3059\u9593\u306b\u3001\u305d\u306eip\u304c\u3001\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u3088\u3063\u3066\u30a2\u30f3\u30ea\u30f3\u30af\u3055\u308c\u305fip\u3068\u4e00\u81f4\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3002<br \/>\n\u30ea\u30f3\u30af\u306f\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u308b\u3060\u308d\u3046\u304b\uff1f<br \/>\n\u306a\u305c\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u308b\u306e\u304b\uff1f\u3082\u3057\u304f\u306f\u306a\u305c\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u306a\u3044\u306e\u304b\uff1f<\/p>\n<p>6. create\u306f\u6b63\u5e38\u306b\u5b8c\u4e86\u3059\u308b\u305f\u3081\u306b4\u3064\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057\uff08\u4e00\u3064\u306fialloc\u3001\u4ed6\u306e\u4e09\u3064\u306fdirlink\uff09\u3092\u5fc5\u8981\u3068\u3059\u308b\u3002<br \/>\n\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u3001create\u306fpanic\u3092\u547c\u3076\u3002<br \/>\n\u306a\u305c\u3053\u308c\u306f\u8a31\u5bb9\u3067\u304d\u308b\u306e\u304b\uff1f<br \/>\n\u306a\u305c\u3001\u305d\u308c\u3089\u306e4\u3064\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057\u306f\u5931\u6557\u3057\u5f97\u306a\u3044\u306e\u3060\u308d\u3046\u304b\uff1f<\/p>\n<p>7. sys_chdir\u306fiput(cp-&gt;cwd)\u306e\u524d\u306biunlock(ip)\u3092\u547c\u3073\u3001iput(cp-&gt;cwd)\u306fcp-&gt;cwd\u3092\u30ed\u30c3\u30af\u3057\u3088\u3046\u3068\u3059\u308b\u3002<br \/>\niput\u306e\u5f8c\u306eiunlock(ip)\u306f\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3092\u5f15\u304d\u8d77\u3053\u3055\u306a\u3044\u304c\u3001\u305d\u308c\u306f\u4f55\u6545\u304b\uff1f<\/p>\n<h3>\u4f5c\u696d<\/h3>\n<h3>1. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u898b\u3066\u306e\u901a\u308a\u3001balloc\u306f\u7a7a\u3044\u3066\u308b\u30d6\u30ed\u30c3\u30af\u3092\u63a2\u3057\u3066\u8fd4\u3059\u306e\u3067\u3001\u7a7a\u304d\u30d6\u30ed\u30c3\u30af\u304c\u306a\u3051\u308c\u3070panic\u307e\u3067\u5230\u9054\u3057\u307e\u3059\u3002<br \/>\npanic\u3058\u3083\u306a\u304f\u3066\u547c\u3073\u51fa\u3057\u5143\u306b\u4f8b\u5916\u3092\u901a\u77e5\u3059\u308b\u3088\u3046\u306a\u4ed5\u7d44\u307f\u306b\u3059\u308b\u3068panic\u306f\u56de\u907f\u51fa\u6765\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3069\u3061\u3089\u306b\u3057\u308d\u7121\u3044\u3082\u306e\u3092\u9b54\u6cd5\u306e\u529b\u3067\u637b\u51fa\u3059\u308b\u3053\u3068\u306f\u51fa\u6765\u306a\u3044\u306e\u3067\u56de\u5fa9\u306f\u7121\u7406\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>fs.c\u306eballoc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Allocate a zeroed disk block.\r\nstatic uint\r\nballoc(uint dev)\r\n{\r\n  int b, bi, m;\r\n  struct buf *bp;\r\n  struct superblock sb;\r\n\r\n  bp = 0;\r\n  readsb(dev, &amp;sb);\r\n  for(b = 0; b &lt; sb.size; b += BPB){\r\n    bp = bread(dev, BBLOCK(b, sb.ninodes));\r\n    for(bi = 0; bi &lt; BPB &amp;&amp; bi &lt; (sb.size - b); bi++){\r\n      m = 1 &lt;&lt; (bi % 8);\r\n      if((bp-&gt;data&#x5B;bi\/8] &amp; m) == 0){  \/\/ Is block free?\r\n        bp-&gt;data&#x5B;bi\/8] |= m;  \/\/ Mark block in use on disk.\r\n        log_write(bp);\r\n        brelse(bp);\r\n        bzero(dev, b + bi);\r\n        return b + bi;\r\n      }\r\n    }\r\n    brelse(bp);\r\n  }\r\n  panic(&quot;balloc: out of blocks&quot;);\r\n}<\/pre>\n<h3>2. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u3053\u308c\u30821\u3068\u540c\u3058\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>fs.c\u306eialloc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Allocate a new inode with the given type on device dev.\r\nstruct inode*\r\nialloc(uint dev, short type)\r\n{\r\n  int inum;\r\n  struct buf *bp;\r\n  struct dinode *dip;\r\n  struct superblock sb;\r\n\r\n  readsb(dev, &amp;sb);\r\n  for(inum = 1; inum &lt; sb.ninodes; inum++){  \/\/ loop over inode blocks\r\n    bp = bread(dev, IBLOCK(inum));\r\n    dip = (struct dinode*)bp-&gt;data + inum%IPB;\r\n    if(dip-&gt;type == 0){  \/\/ a free inode\r\n      memset(dip, 0, sizeof(*dip));\r\n      dip-&gt;type = type;\r\n      log_write(bp);   \/\/ mark it allocated on the disk\r\n      brelse(bp);\r\n      return iget(dev, inum);\r\n    }\r\n    brelse(bp);\r\n  }\r\n  panic(&quot;ialloc: no inodes&quot;);\r\n}<\/pre>\n<h3>3. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u5143\u306e\u6587\u3082inode generation numbers.\u3060\u3051\u306a\u306e\u3067\u3061\u3087\u3063\u3068\u8ab2\u984c\u306e\u610f\u56f3\u304c\u5206\u304b\u308a\u307e\u305b\u3093\u304c\u3001inode\u4e16\u4ee3\u756a\u53f7\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3069\u3046\u3057\u305f\u3089\u826f\u3044\u304b\uff1f\u3068\u3044\u3046\u4e8b\u3060\u3068\u89e3\u91c8\u3057\u3066\u8003\u3048\u3066\u307f\u307e\u3059\u3002<br \/>\n\u307e\u305ainode\u4e16\u4ee3\u756a\u53f7\u3068\u3044\u3046\u306e\u306f\u3001\u3042\u308binode\u304c\u6307\u3057\u793a\u3059\u30c7\u30fc\u30bf\u304c\u5909\u66f4\u3055\u308c\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3055\u308c\u308b\u5024\u3067\u3059\u3002\uff08\u591a\u5206\u2026\uff09<br \/>\n\u3053\u308c\u306finode\u306e\u5909\u66f4\u72b6\u6cc1\u3092\u8ffd\u8de1\u3059\u308b\u306e\u306b\u4f7f\u3048\u3066\u3001\u305d\u306e\u5024\u3092\u5143\u306b\u5272\u308a\u5f53\u3066\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3093\u304b\u3082\u6539\u826f\u51fa\u6765\u305f\u308a\u3059\u308b\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u306a\u3093\u3066\u601d\u3044\u307e\u3059\u3002\uff08\u60f3\u50cf\uff09<br \/>\ninode\u4e16\u4ee3\u756a\u53f7\u306a\u3093\u3066\u8a00\u8449\u6b63\u76f4\u4eca\u65e5\u77e5\u308a\u307e\u3057\u305f\u3002<br \/>\n\u3067\u3001inode\u4e16\u4ee3\u756a\u53f7\u306e\u5b9f\u88c5\u65b9\u6cd5\u3067\u3059\u304c\u3001\u307e\u305ainode\u69cb\u9020\u4f53\u3068dinode\u69cb\u9020\u4f53\u306b\u4e16\u4ee3\u756a\u53f7\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3067\u3057\u3087\u3046\u3002<br \/>\ndinode\u306e\u307b\u3046\u306b\u306f\u8ffd\u52a0\u305b\u305a\u306b\u3001\u4f8b\u3048\u3070\u30b9\u30fc\u30d1\u30fc\u30d6\u30ed\u30c3\u30af\u3068\u304b\u306b\u307e\u3068\u3081\u3066\u8a18\u9332\u3059\u308b\u306e\u3082\u3042\u308a\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001fs.c\u306ewritei\u95a2\u6570\u3067\u305d\u306e\u4e16\u4ee3\u756a\u53f7\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3066\u3042\u3052\u308c\u3070\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30b8\u30e3\u30fc\u30ca\u30ea\u30f3\u30b0\u306e\u305f\u3081\u306b\u5b9f\u969b\u306fwritei\u3067\u306f\u306a\u304f\u305d\u3053\u304b\u3089\u547c\u3070\u308c\u308blog_write\u3067\u66f8\u304d\u8fbc\u307f\u304c\u884c\u308f\u308c\u308b\u306e\u3067\u3001\u305d\u3061\u3089\u3067\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u5b9f\u969b\u306e\u5b9f\u88c5\u30ec\u30d9\u30eb\u3067\u3042\u307e\u308a\u7d30\u304b\u3044\u3068\u3053\u308d\u307e\u3067\u8003\u3048\u308b\u3068\u30ed\u30c3\u30af\u3068\u304b\u3067\u30c9\u30c4\u30dc\u306b\u30cf\u30de\u30ea\u305d\u3046\u306a\u306e\u3067\u3053\u306e\u7a0b\u5ea6\u3067\u52d8\u5f01\u3057\u3066\u3044\u305f\u3060\u304d\u305f\u304f\u2026<\/p>\n<h3>4. \u306b\u3064\u3044\u3066<\/h3>\n<p>balloc\u3084ialloc\u306f\u3001\u30d6\u30ed\u30c3\u30af\u3084inode\u5358\u4f4d\u306e\u5272\u308a\u5f53\u3066\u3067\u3042\u308a\u3001\u305d\u306e\u30ec\u30d9\u30eb\u3067\u5931\u6557\u3057\u3066\u3082\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b63\u76f4\u4f55\u3082\u51fa\u6765\u306a\u3044\u306f\u305a\uff08\u4f55\u304b\u51fa\u6765\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u30ab\u30fc\u30cd\u30eb\u304c\u3084\u3063\u3066\u308b\u3053\u3068\u3088\u308a\u96e3\u3057\u3044\u3053\u3068\u3092\u3057\u306a\u304d\u3083\u306a\u3089\u306a\u3044\u308f\u3051\u3067\u2026\uff09\u306a\u306e\u3067\u3001panic\u305b\u305a\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u4e8b\u5f8c\u51e6\u7406\u306e\u8cac\u4efb\u3092\u8ca0\u308f\u305b\u308b\u306e\u306f\u73fe\u5b9f\u7684\u3058\u3083\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u4e00\u65b9\u3001filealloc\u304c\u5931\u6557\u3059\u308b\u306e\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u5168\u4f53\u3067\u540c\u6642\u306b\u958b\u3051\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u5927\u6570\uff08xv6\u306e\u5834\u5408\u306f\u305f\u3063\u305f\u306e100\uff09\u306b\u9054\u3057\u305f\u3068\u304d\u3067\u3042\u3063\u3066\u3001\u524d\u8005\u306e\u5931\u6557\u306e\u6df1\u523b\u5ea6\u3068\u6bd4\u3079\u305f\u3089\u3001\u304b\u306a\u308a\u30cc\u30eb\u3044\u5931\u6557\u3068\u8a00\u3048\u307e\u3059\u3002<br \/>\n\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u3082\u3001filealloc\u304c\u5931\u6557\u3057\u305f\u3089\u3001\u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb\u304c\u958b\u3051\u306a\u304b\u3063\u305f\u3068\u3057\u3066\u5358\u306b\u7d42\u4e86\u3059\u308b\u3068\u304b\u3001\u958b\u3051\u308b\u3088\u3046\u306b\u306a\u308b\u307e\u3067\u3001\u305d\u308c\u307e\u3067\u958b\u3044\u3066\u3066\u5fc5\u8981\u306e\u306a\u3044\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3068\u304b\u306e\u4e8b\u5f8c\u51e6\u7406\u304c\u884c\u3048\u3001\u305d\u306e\u5b9f\u88c5\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5185\u5bb9\u306b\u3082\u3088\u308a\u307e\u3059\u304c\u57fa\u672c\u7684\u306b\u306f\u305d\u3046\u96e3\u3057\u304f\u306a\u3044\u306f\u305a\u3067\u3059\u3002<br \/>\n\u306a\u306e\u3067\u3001\u3053\u306e\u30ec\u30d9\u30eb\u306e\u5931\u6557\u3067\u3044\u3061\u3044\u3061panic\u3059\u308b\u3088\u308a\u306f\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u305d\u306e\u5f8c\u306e\u51e6\u7406\u3092\u4efb\u305b\u305f\u65b9\u304c\u73fe\u5b9f\u7684\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>file.c\u306efilealloc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Allocate a file structure.\r\nstruct file*\r\nfilealloc(void)\r\n{\r\n  struct file *f;\r\n\r\n  acquire(&amp;ftable.lock);\r\n  for(f = ftable.file; f &lt; ftable.file + NFILE; f++){\r\n    if(f-&gt;ref == 0){\r\n      f-&gt;ref = 1;\r\n      release(&amp;ftable.lock);\r\n      return f;\r\n    }\r\n  }\r\n  release(&amp;ftable.lock);\r\n  return 0;\r\n}<\/pre>\n<h3>5. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u8cea\u554f\u306e\u7b54\u3048\u3068\u3057\u3066\u306f\u3001\u300c\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u306a\u3044\u3002\u306a\u305c\u306a\u3089dirlink\u304c\u5931\u6557\u3059\u308b\u306e\u3067\u3001bad\u30e9\u30d9\u30eb\u4ee5\u4e0b\u306e\u884c\u304c\u5b9f\u884c\u3055\u308c\u5dfb\u304d\u623b\u3055\u308c\u308b\u306e\u3067\u3002\u300d\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u308b\u306e\u3067\u3001\u4e2d\u9014\u534a\u7aef\u306a\u72b6\u614b\u3067\u7d42\u308f\u308b\u3053\u3068\u306f\u306a\u3044\u306e\u3067\u554f\u984c\u306a\u3044\u306f\u305a\u3067\u3059\u3002<\/p>\n<p>sysfile.c\u306esys_link\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Create the path new as a link to the same inode as old.\r\nint\r\nsys_link(void)\r\n{\r\n  char name&#x5B;DIRSIZ], *new, *old;\r\n  struct inode *dp, *ip;\r\n\r\n  if(argstr(0, &amp;old) &lt; 0 || argstr(1, &amp;new) &lt; 0)\r\n    return -1;\r\n  if((ip = namei(old)) == 0)\r\n    return -1;\r\n\r\n  begin_trans();\r\n\r\n  ilock(ip);\r\n  if(ip-&gt;type == T_DIR){\r\n    iunlockput(ip);\r\n    commit_trans();\r\n    return -1;\r\n  }\r\n\r\n  ip-&gt;nlink++;\r\n  iupdate(ip);\r\n  iunlock(ip);\r\n\r\n  if((dp = nameiparent(new, name)) == 0)\r\n    goto bad;\r\n  ilock(dp);\r\n  if(dp-&gt;dev != ip-&gt;dev || dirlink(dp, name, ip-&gt;inum) &lt; 0){\r\n    iunlockput(dp);\r\n    goto bad;\r\n  }\r\n  iunlockput(dp);\r\n  iput(ip);\r\n\r\n  commit_trans();\r\n\r\n  return 0;\r\n\r\nbad:\r\n  ilock(ip);\r\n  ip-&gt;nlink--;\r\n  iupdate(ip);\r\n  iunlockput(ip);\r\n  commit_trans();\r\n  return -1;\r\n}<\/pre>\n<h3>6. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u307e\u305a\u3001dirlink\u304c\u5931\u6557\uff08panic\u3067\u306f\u306a\u304f\uff09\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u3068\u3044\u3046\u306e\u306f\u3001\u5bfe\u8c61\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3059\u3067\u306b\u540c\u3058\u540d\u524d\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u3068\u304d\u3060\u3051\u3067\u3059\u3002<br \/>\n3\u3064\u306edirlink\u306e\u3046\u30612\u3064\u306f\u3001\u65b0\u3057\u304f\u4f5c\u6210\u3055\u308c\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u5bfe\u3057\u3066&#8221;.&#8221;\u3068&#8221;..&#8221;\u3092\u4f5c\u6210\u3059\u308b\u51e6\u7406\u3067\u3042\u308a\u3001\u4e0a\u8a18\u306e\u7406\u7531\u304b\u3089\u5931\u6557\u3057\u3088\u3046\u304c\u306a\u3044\u306f\u305a\u3067\u3059\u3002<br \/>\n\u307e\u305f3\u3064\u3081\u306edirlink\u306f\u3001\u65b0\u3057\u304f\u4f5c\u6210\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3001\u3082\u3057\u304f\u306f\u30d5\u30a1\u30a4\u30eb\u3092\u89aa\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u767b\u9332\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u304c\u3001\u3059\u3067\u306b\u89aa\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u540c\u3058\u540d\u524d\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u524d\u534a\u306eif((ip = dirlookup(dp, name, &amp;off)) != 0)\u306e\u90e8\u5206\u306b\u3088\u3063\u3066\u9664\u5916\u3055\u308c\u308b\u306e\u3067\u30013\u3064\u3081\u306edirlink\u306f\u5931\u6557\u3057\u306a\u3044\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u5f53\u7136\u3001\u4e00\u9023\u306e\u51e6\u7406\u4e2d\u306fdp\uff08\u89aa\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306einode\uff09\u304c\u30ed\u30c3\u30af\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u9014\u4e2d\u3067\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u3082\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>sysfile.c\u306ecreate\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static struct inode*\r\ncreate(char *path, short type, short major, short minor)\r\n{\r\n  uint off;\r\n  struct inode *ip, *dp;\r\n  char name&#x5B;DIRSIZ];\r\n\r\n  if((dp = nameiparent(path, name)) == 0)\r\n    return 0;\r\n  ilock(dp);\r\n\r\n  if((ip = dirlookup(dp, name, &amp;off)) != 0){\r\n    iunlockput(dp);\r\n    ilock(ip);\r\n    if(type == T_FILE &amp;&amp; ip-&gt;type == T_FILE)\r\n      return ip;\r\n    iunlockput(ip);\r\n    return 0;\r\n  }\r\n\r\n  if((ip = ialloc(dp-&gt;dev, type)) == 0)\r\n    panic(&quot;create: ialloc&quot;);\r\n\r\n  ilock(ip);\r\n  ip-&gt;major = major;\r\n  ip-&gt;minor = minor;\r\n  ip-&gt;nlink = 1;\r\n  iupdate(ip);\r\n\r\n  if(type == T_DIR){  \/\/ Create . and .. entries.\r\n    dp-&gt;nlink++;  \/\/ for &quot;..&quot;\r\n    iupdate(dp);\r\n    \/\/ No ip-&gt;nlink++ for &quot;.&quot;: avoid cyclic ref count.\r\n    if(dirlink(ip, &quot;.&quot;, ip-&gt;inum) &lt; 0 || dirlink(ip, &quot;..&quot;, dp-&gt;inum) &lt; 0)\r\n      panic(&quot;create dots&quot;);\r\n  }\r\n\r\n  if(dirlink(dp, name, ip-&gt;inum) &lt; 0)\r\n    panic(&quot;create: dirlink&quot;);\r\n\r\n  iunlockput(dp);\r\n\r\n  return ip;\r\n}<\/pre>\n<h3>7. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u672c\u6587\u3067\u306f\u306fiput(cp-&gt;cwd)\u3068\u306a\u3063\u3066\u307e\u3059\u304c\u3001\u30bd\u30fc\u30b9\u3067\u306fiput(proc-&gt;cwd)\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001\u30bd\u30fc\u30b9\u3067\u306fiput\u3067iunlock\u306f\u547c\u3070\u308c\u307e\u305b\u3093\u3002<br \/>\n\u30bd\u30fc\u30b9\u306e\u30ed\u30b0\u3092\u8fbf\u3063\u305f\u3068\u3053\u308d\u3001\u3053\u306e\u8ab2\u984c\u306f\u4eca\u306e\u30bd\u30fc\u30b9\u3067\u306f\u306a\u304f\u53e4\u3044\u30bd\u30fc\u30b9\u306b\u5bfe\u3059\u308b\u3082\u306e\u306e\u3088\u3046\u3067\u3059\u3002<br \/>\n\u53e4\u3044\u30bd\u30fc\u30b9\u3067\u306f\u3001iput\u306f\u30ed\u30c3\u30af\u4e2d\u306einode\u3092\u8981\u6c42\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u307e\u3057\u305f\u3002\uff08\u73fe\u5728\u306f\u672a\u30ed\u30c3\u30af\u306einode\u3092\u8981\u6c42\u3059\u308b\u3002\uff09<br \/>\niput\u3060\u3051\u3067\u306a\u304f\u95a2\u9023\u306e\u95a2\u6570\u306e\u5b9f\u88c5\u3082\u4eca\u3068\u5927\u304d\u304f\u9055\u3046\u307f\u305f\u3044\u306a\u306e\u3067\u3001\u3053\u306e\u8ab2\u984c\u306f\u30d1\u30b9\u3057\u307e\u3059\u3002<\/p>\n<p>sysfile.c\u306esys_chdir\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">int\r\nsys_chdir(void)\r\n{\r\n  char *path;\r\n  struct inode *ip;\r\n\r\n  if(argstr(0, &amp;path) &lt; 0 || (ip = namei(path)) == 0)\r\n    return -1;\r\n  ilock(ip);\r\n  if(ip-&gt;type != T_DIR){\r\n    iunlockput(ip);\r\n    return -1;\r\n  }\r\n  iunlock(ip);\r\n  iput(proc-&gt;cwd);\r\n  proc-&gt;cwd = ip;\r\n  return 0;\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u4eca\u307e\u3067\u306eExercises\u306e\u56de\u306e\u4e2d\u3067\u306f\u4e00\u756a\u307e\u3068\u3082\u306b\u5bfe\u5fdc\u3067\u304d\u305f\u3093\u3058\u3083\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u307e\u3041\u601d\u3044\u8fbc\u307f\u3067\u5618\u66f8\u3044\u3066\u308b\u53ef\u80fd\u6027\u3082\u5927\u3067\u3059\u304c\u2026<\/p>\n<p>\u4e00\u5fdc\u4eca\u56de\u3067\u672c\u6587\u306f\u7d42\u308f\u308a\u3067\u3059\u3002<br \/>\n\u9577\u3044\u3088\u3046\u3067\u77ed\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u8208\u5473\u6df1\u304f\u8aad\u3081\u305f\u306e\u3067\u3001\u3053\u3053\u307e\u3067\u7d9a\u3051\u308c\u305f\u3093\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3042\u3068\u306f\u3001\u4ed8\u9332A, B\u304c\u6b8b\u3063\u3066\u307e\u3059\u306e\u3067\u307c\u3061\u307c\u3061\u305d\u3061\u3089\u3082\u8aad\u3093\u3067\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e77\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>1. \u3069\u3046\u3044\u3046\u5834\u5408\u306bballoc\u3067panic\u304c\u8d77\u304d\u308b\u304b\uff1f \u305d\u308c\u3092\u56de\u5fa9\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u308b\u304b\uff1f<\/p>\n<p>2. \u3069\u3046\u3044\u3046\u5834\u5408\u306bialloc\u3067panic\u304c\u8d77\u304d\u308b\u304b\uff1f \u305d\u308c\u3092\u56de\u5fa9\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u308b\u304b\uff1f<\/p>\n<p>3. inode\u4e16\u4ee3\u756a\u53f7\u3002<\/p>\n<p>4. file\u304c\u306a\u3044\u5834\u5408\u306b\u306a\u305cfilealloc\u306fpanic\u3092\u8d77\u3053\u3055\u306a\u3044\u306e\u304b\uff1f \u306a\u305c\u3053\u308c\u304c\u3088\u308a\u4e00\u822c\u7684\u3067\u305d\u308c\u3086\u3048\u826f\u3044\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u306e\u65b9\u6cd5\u306a\u306e\u304b\uff1f<\/p>\n<p>5. sys_link\u304ciunlock(ip)\u3068dirlink\u3092\u547c\u3073\u51fa\u3059\u9593\u306b\u3001\u305d\u306eip\u304c\u3001\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u3088\u3063\u3066\u30a2\u30f3\u30ea\u30f3\u30af\u3055\u308c\u305fip\u3068\u4e00\u81f4\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3002<br \/>\n\u30ea\u30f3\u30af\u306f\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u308b\u3060\u308d\u3046\u304b\uff1f<br \/>\n\u306a\u305c\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u308b\u306e\u304b\uff1f\u3082\u3057\u304f\u306f\u306a\u305c\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u306a\u3044\u306e\u304b\uff1f<\/p>\n<p>6. create\u306f\u6b63\u5e38\u306b\u5b8c\u4e86\u3059\u308b\u305f\u3081\u306b4\u3064\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057\uff08\u4e00\u3064\u306fialloc\u3001\u4ed6\u306e\u4e09\u3064\u306fdirlink\uff09\u3092\u5fc5\u8981\u3068\u3059\u308b\u3002<br \/>\n\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u3001create\u306fpanic\u3092\u547c\u3076\u3002<br \/>\n\u306a\u305c\u3053\u308c\u306f\u8a31\u5bb9\u3067\u304d\u308b\u306e\u304b\uff1f<br \/>\n\u306a\u305c\u3001\u305d\u308c\u3089\u306e4\u3064\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057\u306f\u5931\u6557\u3057\u5f97\u306a\u3044\u306e\u3060\u308d\u3046\u304b\uff1f<\/p>\n<p>7. sys_chdir\u306fiput(cp-&gt;cwd)\u306e\u524d\u306biunlock(ip)\u3092\u547c\u3073\u3001iput(cp-&gt;cwd)\u306fcp-&gt;cwd\u3092\u30ed\u30c3\u30af\u3057\u3088\u3046\u3068\u3059\u308b\u3002<br \/>\niput\u306e\u5f8c\u306eiunlock(ip)\u306f\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3092\u5f15\u304d\u8d77\u3053\u3055\u306a\u3044\u304c\u3001\u305d\u308c\u306f\u4f55\u6545\u304b\uff1f<\/p>\n<h3>\u4f5c\u696d<\/h3>\n<h3>1. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u898b\u3066\u306e\u901a\u308a\u3001balloc\u306f\u7a7a\u3044\u3066\u308b\u30d6\u30ed\u30c3\u30af\u3092\u63a2\u3057\u3066\u8fd4\u3059\u306e\u3067\u3001\u7a7a\u304d\u30d6\u30ed\u30c3\u30af\u304c\u306a\u3051\u308c\u3070panic\u307e\u3067\u5230\u9054\u3057\u307e\u3059\u3002<br \/>\npanic\u3058\u3083\u306a\u304f\u3066\u547c\u3073\u51fa\u3057\u5143\u306b\u4f8b\u5916\u3092\u901a\u77e5\u3059\u308b\u3088\u3046\u306a\u4ed5\u7d44\u307f\u306b\u3059\u308b\u3068panic\u306f\u56de\u907f\u51fa\u6765\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3069\u3061\u3089\u306b\u3057\u308d\u7121\u3044\u3082\u306e\u3092\u9b54\u6cd5\u306e\u529b\u3067\u637b\u51fa\u3059\u308b\u3053\u3068\u306f\u51fa\u6765\u306a\u3044\u306e\u3067\u56de\u5fa9\u306f\u7121\u7406\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>fs.c\u306eballoc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Allocate a zeroed disk block.\r\nstatic uint\r\nballoc(uint dev)\r\n{\r\n  int b, bi, m;\r\n  struct buf *bp;\r\n  struct superblock sb;\r\n\r\n  bp = 0;\r\n  readsb(dev, &amp;sb);\r\n  for(b = 0; b &lt; sb.size; b += BPB){\r\n    bp = bread(dev, BBLOCK(b, sb.ninodes));\r\n    for(bi = 0; bi &lt; BPB &amp;&amp; bi &lt; (sb.size - b); bi++){\r\n      m = 1 &lt;&lt; (bi % 8);\r\n      if((bp-&gt;data&#x5B;bi\/8] &amp; m) == 0){  \/\/ Is block free?\r\n        bp-&gt;data&#x5B;bi\/8] |= m;  \/\/ Mark block in use on disk.\r\n        log_write(bp);\r\n        brelse(bp);\r\n        bzero(dev, b + bi);\r\n        return b + bi;\r\n      }\r\n    }\r\n    brelse(bp);\r\n  }\r\n  panic(&quot;balloc: out of blocks&quot;);\r\n}<\/pre>\n<h3>2. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u3053\u308c\u30821\u3068\u540c\u3058\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>fs.c\u306eialloc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Allocate a new inode with the given type on device dev.\r\nstruct inode*\r\nialloc(uint dev, short type)\r\n{\r\n  int inum;\r\n  struct buf *bp;\r\n  struct dinode *dip;\r\n  struct superblock sb;\r\n\r\n  readsb(dev, &amp;sb);\r\n  for(inum = 1; inum &lt; sb.ninodes; inum++){  \/\/ loop over inode blocks\r\n    bp = bread(dev, IBLOCK(inum));\r\n    dip = (struct dinode*)bp-&gt;data + inum%IPB;\r\n    if(dip-&gt;type == 0){  \/\/ a free inode\r\n      memset(dip, 0, sizeof(*dip));\r\n      dip-&gt;type = type;\r\n      log_write(bp);   \/\/ mark it allocated on the disk\r\n      brelse(bp);\r\n      return iget(dev, inum);\r\n    }\r\n    brelse(bp);\r\n  }\r\n  panic(&quot;ialloc: no inodes&quot;);\r\n}<\/pre>\n<h3>3. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u5143\u306e\u6587\u3082inode generation numbers.\u3060\u3051\u306a\u306e\u3067\u3061\u3087\u3063\u3068\u8ab2\u984c\u306e\u610f\u56f3\u304c\u5206\u304b\u308a\u307e\u305b\u3093\u304c\u3001inode\u4e16\u4ee3\u756a\u53f7\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3069\u3046\u3057\u305f\u3089\u826f\u3044\u304b\uff1f\u3068\u3044\u3046\u4e8b\u3060\u3068\u89e3\u91c8\u3057\u3066\u8003\u3048\u3066\u307f\u307e\u3059\u3002<br \/>\n\u307e\u305ainode\u4e16\u4ee3\u756a\u53f7\u3068\u3044\u3046\u306e\u306f\u3001\u3042\u308binode\u304c\u6307\u3057\u793a\u3059\u30c7\u30fc\u30bf\u304c\u5909\u66f4\u3055\u308c\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3055\u308c\u308b\u5024\u3067\u3059\u3002\uff08\u591a\u5206\u2026\uff09<br \/>\n\u3053\u308c\u306finode\u306e\u5909\u66f4\u72b6\u6cc1\u3092\u8ffd\u8de1\u3059\u308b\u306e\u306b\u4f7f\u3048\u3066\u3001\u305d\u306e\u5024\u3092\u5143\u306b\u5272\u308a\u5f53\u3066\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u3093\u304b\u3082\u6539\u826f\u51fa\u6765\u305f\u308a\u3059\u308b\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u306a\u3093\u3066\u601d\u3044\u307e\u3059\u3002\uff08\u60f3\u50cf\uff09<br \/>\ninode\u4e16\u4ee3\u756a\u53f7\u306a\u3093\u3066\u8a00\u8449\u6b63\u76f4\u4eca\u65e5\u77e5\u308a\u307e\u3057\u305f\u3002<br \/>\n\u3067\u3001inode\u4e16\u4ee3\u756a\u53f7\u306e\u5b9f\u88c5\u65b9\u6cd5\u3067\u3059\u304c\u3001\u307e\u305ainode\u69cb\u9020\u4f53\u3068dinode\u69cb\u9020\u4f53\u306b\u4e16\u4ee3\u756a\u53f7\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3067\u3057\u3087\u3046\u3002<br \/>\ndinode\u306e\u307b\u3046\u306b\u306f\u8ffd\u52a0\u305b\u305a\u306b\u3001\u4f8b\u3048\u3070\u30b9\u30fc\u30d1\u30fc\u30d6\u30ed\u30c3\u30af\u3068\u304b\u306b\u307e\u3068\u3081\u3066\u8a18\u9332\u3059\u308b\u306e\u3082\u3042\u308a\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001fs.c\u306ewritei\u95a2\u6570\u3067\u305d\u306e\u4e16\u4ee3\u756a\u53f7\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3066\u3042\u3052\u308c\u3070\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30b8\u30e3\u30fc\u30ca\u30ea\u30f3\u30b0\u306e\u305f\u3081\u306b\u5b9f\u969b\u306fwritei\u3067\u306f\u306a\u304f\u305d\u3053\u304b\u3089\u547c\u3070\u308c\u308blog_write\u3067\u66f8\u304d\u8fbc\u307f\u304c\u884c\u308f\u308c\u308b\u306e\u3067\u3001\u305d\u3061\u3089\u3067\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u5b9f\u969b\u306e\u5b9f\u88c5\u30ec\u30d9\u30eb\u3067\u3042\u307e\u308a\u7d30\u304b\u3044\u3068\u3053\u308d\u307e\u3067\u8003\u3048\u308b\u3068\u30ed\u30c3\u30af\u3068\u304b\u3067\u30c9\u30c4\u30dc\u306b\u30cf\u30de\u30ea\u305d\u3046\u306a\u306e\u3067\u3053\u306e\u7a0b\u5ea6\u3067\u52d8\u5f01\u3057\u3066\u3044\u305f\u3060\u304d\u305f\u304f\u2026<\/p>\n<h3>4. \u306b\u3064\u3044\u3066<\/h3>\n<p>balloc\u3084ialloc\u306f\u3001\u30d6\u30ed\u30c3\u30af\u3084inode\u5358\u4f4d\u306e\u5272\u308a\u5f53\u3066\u3067\u3042\u308a\u3001\u305d\u306e\u30ec\u30d9\u30eb\u3067\u5931\u6557\u3057\u3066\u3082\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b63\u76f4\u4f55\u3082\u51fa\u6765\u306a\u3044\u306f\u305a\uff08\u4f55\u304b\u51fa\u6765\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u30ab\u30fc\u30cd\u30eb\u304c\u3084\u3063\u3066\u308b\u3053\u3068\u3088\u308a\u96e3\u3057\u3044\u3053\u3068\u3092\u3057\u306a\u304d\u3083\u306a\u3089\u306a\u3044\u308f\u3051\u3067\u2026\uff09\u306a\u306e\u3067\u3001panic\u305b\u305a\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u4e8b\u5f8c\u51e6\u7406\u306e\u8cac\u4efb\u3092\u8ca0\u308f\u305b\u308b\u306e\u306f\u73fe\u5b9f\u7684\u3058\u3083\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u4e00\u65b9\u3001filealloc\u304c\u5931\u6557\u3059\u308b\u306e\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u5168\u4f53\u3067\u540c\u6642\u306b\u958b\u3051\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u5927\u6570\uff08xv6\u306e\u5834\u5408\u306f\u305f\u3063\u305f\u306e100\uff09\u306b\u9054\u3057\u305f\u3068\u304d\u3067\u3042\u3063\u3066\u3001\u524d\u8005\u306e\u5931\u6557\u306e\u6df1\u523b\u5ea6\u3068\u6bd4\u3079\u305f\u3089\u3001\u304b\u306a\u308a\u30cc\u30eb\u3044\u5931\u6557\u3068\u8a00\u3048\u307e\u3059\u3002<br \/>\n\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u3082\u3001filealloc\u304c\u5931\u6557\u3057\u305f\u3089\u3001\u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb\u304c\u958b\u3051\u306a\u304b\u3063\u305f\u3068\u3057\u3066\u5358\u306b\u7d42\u4e86\u3059\u308b\u3068\u304b\u3001\u958b\u3051\u308b\u3088\u3046\u306b\u306a\u308b\u307e\u3067\u3001\u305d\u308c\u307e\u3067\u958b\u3044\u3066\u3066\u5fc5\u8981\u306e\u306a\u3044\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3068\u304b\u306e\u4e8b\u5f8c\u51e6\u7406\u304c\u884c\u3048\u3001\u305d\u306e\u5b9f\u88c5\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5185\u5bb9\u306b\u3082\u3088\u308a\u307e\u3059\u304c\u57fa\u672c\u7684\u306b\u306f\u305d\u3046\u96e3\u3057\u304f\u306a\u3044\u306f\u305a\u3067\u3059\u3002<br \/>\n\u306a\u306e\u3067\u3001\u3053\u306e\u30ec\u30d9\u30eb\u306e\u5931\u6557\u3067\u3044\u3061\u3044\u3061panic\u3059\u308b\u3088\u308a\u306f\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u305d\u306e\u5f8c\u306e\u51e6\u7406\u3092\u4efb\u305b\u305f\u65b9\u304c\u73fe\u5b9f\u7684\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>file.c\u306efilealloc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Allocate a file structure.\r\nstruct file*\r\nfilealloc(void)\r\n{\r\n  struct file *f;\r\n\r\n  acquire(&amp;ftable.lock);\r\n  for(f = ftable.file; f &lt; ftable.file + NFILE; f++){\r\n    if(f-&gt;ref == 0){\r\n      f-&gt;ref = 1;\r\n      release(&amp;ftable.lock);\r\n      return f;\r\n    }\r\n  }\r\n  release(&amp;ftable.lock);\r\n  return 0;\r\n}<\/pre>\n<h3>5. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u8cea\u554f\u306e\u7b54\u3048\u3068\u3057\u3066\u306f\u3001\u300c\u6b63\u3057\u304f\u4f5c\u6210\u3055\u308c\u306a\u3044\u3002\u306a\u305c\u306a\u3089dirlink\u304c\u5931\u6557\u3059\u308b\u306e\u3067\u3001bad\u30e9\u30d9\u30eb\u4ee5\u4e0b\u306e\u884c\u304c\u5b9f\u884c\u3055\u308c\u5dfb\u304d\u623b\u3055\u308c\u308b\u306e\u3067\u3002\u300d\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u308b\u306e\u3067\u3001\u4e2d\u9014\u534a\u7aef\u306a\u72b6\u614b\u3067\u7d42\u308f\u308b\u3053\u3068\u306f\u306a\u3044\u306e\u3067\u554f\u984c\u306a\u3044\u306f\u305a\u3067\u3059\u3002<\/p>\n<p>sysfile.c\u306esys_link\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Create the path new as a link to the same inode as old.\r\nint\r\nsys_link(void)\r\n{\r\n  char name&#x5B;DIRSIZ], *new, *old;\r\n  struct inode *dp, *ip;\r\n\r\n  if(argstr(0, &amp;old) &lt; 0 || argstr(1, &amp;new) &lt; 0)\r\n    return -1;\r\n  if((ip = namei(old)) == 0)\r\n    return -1;\r\n\r\n  begin_trans();\r\n\r\n  ilock(ip);\r\n  if(ip-&gt;type == T_DIR){\r\n    iunlockput(ip);\r\n    commit_trans();\r\n    return -1;\r\n  }\r\n\r\n  ip-&gt;nlink++;\r\n  iupdate(ip);\r\n  iunlock(ip);\r\n\r\n  if((dp = nameiparent(new, name)) == 0)\r\n    goto bad;\r\n  ilock(dp);\r\n  if(dp-&gt;dev != ip-&gt;dev || dirlink(dp, name, ip-&gt;inum) &lt; 0){\r\n    iunlockput(dp);\r\n    goto bad;\r\n  }\r\n  iunlockput(dp);\r\n  iput(ip);\r\n\r\n  commit_trans();\r\n\r\n  return 0;\r\n\r\nbad:\r\n  ilock(ip);\r\n  ip-&gt;nlink--;\r\n  iupdate(ip);\r\n  iunlockput(ip);\r\n  commit_trans();\r\n  return -1;\r\n}<\/pre>\n<h3>6. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u307e\u305a\u3001dirlink\u304c\u5931\u6557\uff08panic\u3067\u306f\u306a\u304f\uff09\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u3068\u3044\u3046\u306e\u306f\u3001\u5bfe\u8c61\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3059\u3067\u306b\u540c\u3058\u540d\u524d\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u3068\u304d\u3060\u3051\u3067\u3059\u3002<br \/>\n3\u3064\u306edirlink\u306e\u3046\u30612\u3064\u306f\u3001\u65b0\u3057\u304f\u4f5c\u6210\u3055\u308c\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u5bfe\u3057\u3066&#8221;.&#8221;\u3068&#8221;..&#8221;\u3092\u4f5c\u6210\u3059\u308b\u51e6\u7406\u3067\u3042\u308a\u3001\u4e0a\u8a18\u306e\u7406\u7531\u304b\u3089\u5931\u6557\u3057\u3088\u3046\u304c\u306a\u3044\u306f\u305a\u3067\u3059\u3002<br \/>\n\u307e\u305f3\u3064\u3081\u306edirlink\u306f\u3001\u65b0\u3057\u304f\u4f5c\u6210\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3001\u3082\u3057\u304f\u306f\u30d5\u30a1\u30a4\u30eb\u3092\u89aa\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u767b\u9332\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u304c\u3001\u3059\u3067\u306b\u89aa\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u540c\u3058\u540d\u524d\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u524d\u534a\u306eif((ip = dirlookup(dp, name, &amp;off)) != 0)\u306e\u90e8\u5206\u306b\u3088\u3063\u3066\u9664\u5916\u3055\u308c\u308b\u306e\u3067\u30013\u3064\u3081\u306edirlink\u306f\u5931\u6557\u3057\u306a\u3044\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u5f53\u7136\u3001\u4e00\u9023\u306e\u51e6\u7406\u4e2d\u306fdp\uff08\u89aa\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306einode\uff09\u304c\u30ed\u30c3\u30af\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u9014\u4e2d\u3067\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u3092\u8003\u616e\u3059\u308b\u5fc5\u8981\u3082\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>sysfile.c\u306ecreate\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static struct inode*\r\ncreate(char *path, short type, short major, short minor)\r\n{\r\n  uint off;\r\n  struct inode *ip, *dp;\r\n  char name&#x5B;DIRSIZ];\r\n\r\n  if((dp = nameiparent(path, name)) == 0)\r\n    return 0;\r\n  ilock(dp);\r\n\r\n  if((ip = dirlookup(dp, name, &amp;off)) != 0){\r\n    iunlockput(dp);\r\n    ilock(ip);\r\n    if(type == T_FILE &amp;&amp; ip-&gt;type == T_FILE)\r\n      return ip;\r\n    iunlockput(ip);\r\n    return 0;\r\n  }\r\n\r\n  if((ip = ialloc(dp-&gt;dev, type)) == 0)\r\n    panic(&quot;create: ialloc&quot;);\r\n\r\n  ilock(ip);\r\n  ip-&gt;major = major;\r\n  ip-&gt;minor = minor;\r\n  ip-&gt;nlink = 1;\r\n  iupdate(ip);\r\n\r\n  if(type == T_DIR){  \/\/ Create . and .. entries.\r\n    dp-&gt;nlink++;  \/\/ for &quot;..&quot;\r\n    iupdate(dp);\r\n    \/\/ No ip-&gt;nlink++ for &quot;.&quot;: avoid cyclic ref count.\r\n    if(dirlink(ip, &quot;.&quot;, ip-&gt;inum) &lt; 0 || dirlink(ip, &quot;..&quot;, dp-&gt;inum) &lt; 0)\r\n      panic(&quot;create dots&quot;);\r\n  }\r\n\r\n  if(dirlink(dp, name, ip-&gt;inum) &lt; 0)\r\n    panic(&quot;create: dirlink&quot;);\r\n\r\n  iunlockput(dp);\r\n\r\n  return ip;\r\n}<\/pre>\n<h3>7. \u306b\u3064\u3044\u3066<\/h3>\n<p>\u672c\u6587\u3067\u306f\u306fiput(cp-&gt;cwd)\u3068\u306a\u3063\u3066\u307e\u3059\u304c\u3001\u30bd\u30fc\u30b9\u3067\u306fiput(proc-&gt;cwd)\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001\u30bd\u30fc\u30b9\u3067\u306fiput\u3067iunlock\u306f\u547c\u3070\u308c\u307e\u305b\u3093\u3002<br \/>\n\u30bd\u30fc\u30b9\u306e\u30ed\u30b0\u3092\u8fbf\u3063\u305f\u3068\u3053\u308d\u3001\u3053\u306e\u8ab2\u984c\u306f\u4eca\u306e\u30bd\u30fc\u30b9\u3067\u306f\u306a\u304f\u53e4\u3044\u30bd\u30fc\u30b9\u306b\u5bfe\u3059\u308b\u3082\u306e\u306e\u3088\u3046\u3067\u3059\u3002<br \/>\n\u53e4\u3044\u30bd\u30fc\u30b9\u3067\u306f\u3001iput\u306f\u30ed\u30c3\u30af\u4e2d\u306einode\u3092\u8981\u6c42\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u307e\u3057\u305f\u3002\uff08\u73fe\u5728\u306f\u672a\u30ed\u30c3\u30af\u306einode\u3092\u8981\u6c42\u3059\u308b\u3002\uff09<br \/>\niput\u3060\u3051\u3067\u306a\u304f\u95a2\u9023\u306e\u95a2\u6570\u306e\u5b9f\u88c5\u3082\u4eca\u3068\u5927\u304d\u304f\u9055\u3046\u307f\u305f\u3044\u306a\u306e\u3067\u3001\u3053\u306e\u8ab2\u984c\u306f\u30d1\u30b9\u3057\u307e\u3059\u3002<\/p>\n<p>sysfile.c\u306esys_chdir\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">int\r\nsys_chdir(void)\r\n{\r\n  char *path;\r\n  struct inode *ip;\r\n\r\n  if(argstr(0, &amp;path) &lt; 0 || (ip = namei(path)) == 0)\r\n    return -1;\r\n  ilock(ip);\r\n  if(ip-&gt;type != T_DIR){\r\n    iunlockput(ip);\r\n    return -1;\r\n  }\r\n  iunlock(ip);\r\n  iput(proc-&gt;cwd);\r\n  proc-&gt;cwd = ip;\r\n  return 0;\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u4eca\u307e\u3067\u306eExercises\u306e\u56de\u306e\u4e2d\u3067\u306f\u4e00\u756a\u307e\u3068\u3082\u306b\u5bfe\u5fdc\u3067\u304d\u305f\u3093\u3058\u3083\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u307e\u3041\u601d\u3044\u8fbc\u307f\u3067\u5618\u66f8\u3044\u3066\u308b\u53ef\u80fd\u6027\u3082\u5927\u3067\u3059\u304c\u2026<\/p>\n<p>\u4e00\u5fdc\u4eca\u56de\u3067\u672c\u6587\u306f\u7d42\u308f\u308a\u3067\u3059\u3002<br \/>\n\u9577\u3044\u3088\u3046\u3067\u77ed\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u8208\u5473\u6df1\u304f\u8aad\u3081\u305f\u306e\u3067\u3001\u3053\u3053\u307e\u3067\u7d9a\u3051\u308c\u305f\u3093\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3042\u3068\u306f\u3001\u4ed8\u9332A, B\u304c\u6b8b\u3063\u3066\u307e\u3059\u306e\u3067\u307c\u3061\u307c\u3061\u305d\u3061\u3089\u3082\u8aad\u3093\u3067\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\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-1680","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\/1680","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=1680"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1680\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}