Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux 核心

Linux 內核 md 原始碼導讀(五):RAID5 陣列的運行歷程

只要讀懂 RAID1 的 run 函數,再看 RAID5 的 run 就輕鬆多了,因為兩者要做的事大同小異。

RAID5 的 run 很長,但很大一部分與「建立運行環境」無關,尤其有一段處理 reshape 的程式碼,多數系統並不關心,可以直接跳過。刪減後大致如下:

 5307 static int run(struct mddev *mddev)
 5308 {
 5309   struct r5conf *conf;
 5310   int working_disks = 0;
 5311   int dirty_parity_disks = 0;
 5312   struct md_rdev *rdev;
 5313   sector_t reshape_offset = 0;
 5314   int i;
 5315   long long min_offset_diff = 0;
 5316   int first = 1;
 ...
 5426   if (mddev->private == NULL)
 5427     conf = setup_conf(mddev);
 5428   else
 5429     conf = mddev->private;
 5430
 5431   if (IS_ERR(conf))
 5432     return PTR_ERR(conf);
 5433
 5434   conf->min_offset_diff = min_offset_diff;
 5435   mddev->thread = conf->thread;
 5436   conf->thread = NULL;
 5437   mddev->private = conf;
 ...
 5491   /*
 5492    * 0 for a fully functional array, 1 or 2 for a degraded array.
 5493    */
 5494   mddev->degraded = calc_degraded(conf);
 ...
 5503   /* device size must be a multiple of chunk size */
 5504   mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
 5505   mddev->resync_max_sectors = mddev->dev_sectors;
 ...
 5556   md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
 5557
 5558   if (mddev->queue) {
 ...
 5628   }
 5629
 5630   return 0;

是不是簡單得令人意外?很多事情表面撲朔迷離,認真拆解後其實有規律可循。這個 run 做的事和 RAID1 一樣:建立讀寫的上下文環境。

5427 行:建立 struct r5conf,跟進看看:

 5131 static struct r5conf *setup_conf(struct mddev *mddev)
 5132 {
 5133   struct r5conf *conf;
 5134   int raid_disk, memory, max_disks;
 5135   struct md_rdev *rdev;
 5136   struct disk_info *disk;
 5137   char pers_name[6];
 5138
 5139   if (mddev->new_level != 5
 5140       && mddev->new_level != 4
 5141       && mddev->new_level != 6) {
 5142     printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
 5143            mdname(mddev), mddev->new_level);
 5144     return ERR_PTR(-EIO);
 5145   }
 5146   if ((mddev->new_level == 5
 5147        && !algorithm_valid_raid5(mddev->new_layout)) ||
 5148       (mddev->new_level == 6
 5149        && !algorithm_valid_raid6(mddev->new_layout))) {
 5150     printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
 5151            mdname(mddev), mddev->new_layout);
 5152     return ERR_PTR(-EIO);
 5153   }
 5154   if (mddev->new_level == 6 && mddev->raid_disks < 4) {
 5155     printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
 5156            mdname(mddev), mddev->raid_disks);
 5157     return ERR_PTR(-EINVAL);
 5158   }
 5159
 5160   if (!mddev->new_chunk_sectors ||
 5161       (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
 5162       !is_power_of_2(mddev->new_chunk_sectors)) {
 5163     printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
 5164            mdname(mddev), mddev->new_chunk_sectors << 9);
 5165     return ERR_PTR(-EINVAL);
 5166   }
 5167
 5168   conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
 5169   if (conf == NULL)
 5170     goto abort;
 5171   spin_lock_init(&conf->device_lock);
 5172   init_waitqueue_head(&conf->wait_for_stripe);
 5173   init_waitqueue_head(&conf->wait_for_overlap);
 5174   INIT_LIST_HEAD(&conf->handle_list);
 5175   INIT_LIST_HEAD(&conf->hold_list);
 5176   INIT_LIST_HEAD(&conf->delayed_list);
 5177   INIT_LIST_HEAD(&conf->bitmap_list);
 5178   INIT_LIST_HEAD(&conf->inactive_list);
 5179   atomic_set(&conf->active_stripes, 0);
 5180   atomic_set(&conf->preread_active_stripes, 0);
 5181   atomic_set(&conf->active_aligned_reads, 0);
 5182   conf->bypass_threshold = BYPASS_THRESHOLD;
 5183   conf->recovery_disabled = mddev->recovery_disabled - 1;
 5184
 5185   conf->raid_disks = mddev->raid_disks;
 5186   if (mddev->reshape_position == MaxSector)
 5187     conf->previous_raid_disks = mddev->raid_disks;
 5188   else
 5189     conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
 5190   max_disks = max(conf->raid_disks, conf->previous_raid_disks);
 5191   conf->scribble_len = scribble_len(max_disks);
 5192
 5193   conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
 5194              GFP_KERNEL);
 5195   if (!conf->disks)
 5196     goto abort;
 5197
 5198   conf->mddev = mddev;
 5199
 5200   if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
 5201     goto abort;
 5202
 5203   conf->level = mddev->new_level;
 5204   if (raid5_alloc_percpu(conf) != 0)
 5205     goto abort;
 5206
 5207   pr_debug("raid456: run(%s) called.\n", mdname(mddev));
 5208
 5209   rdev_for_each(rdev, mddev) {
 5210     raid_disk = rdev->raid_disk;
 5211     if (raid_disk >= max_disks
 5212         || raid_disk < 0)
 5213       continue;
 5214     disk = conf->disks + raid_disk;
 5215
 5216     if (test_bit(Replacement, &rdev->flags)) {
 5217       if (disk->replacement)
 5218         goto abort;
 5219       disk->replacement = rdev;
 5220     } else {
 5221       if (disk->rdev)
 5222         goto abort;
 5223       disk->rdev = rdev;
 5224     }
 5225
 5226     if (test_bit(In_sync, &rdev->flags)) {
 5227       char b[BDEVNAME_SIZE];
 5228       printk(KERN_INFO "md/raid:%s: device %s operational as raid"
 5229              " disk %d\n",
 5230              mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
 5231     } else if (rdev->saved_raid_disk != raid_disk)
 5232       /* Cannot rely on bitmap to complete recovery */
 5233       conf->fullsync = 1;
 5234   }
 5235
 5236   conf->chunk_sectors = mddev->new_chunk_sectors;
 5237   conf->level = mddev->new_level;
 5238   if (conf->level == 6)
 5239     conf->max_degraded = 2;
 5240   else
 5241     conf->max_degraded = 1;
 5242   conf->algorithm = mddev->new_layout;
 5243   conf->max_nr_stripes = NR_STRIPES;
 5244   conf->reshape_progress = mddev->reshape_position;
 5245   if (conf->reshape_progress != MaxSector) {
 5246     conf->prev_chunk_sectors = mddev->chunk_sectors;
 5247     conf->prev_algo = mddev->layout;
 5248   }
 5249
 5250   memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
 5251          max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
 5252   if (grow_stripes(conf, conf->max_nr_stripes)) {
 5253     printk(KERN_ERR
 5254            "md/raid:%s: couldn't allocate %dkB for buffers\n",
 5255            mdname(mddev), memory);
 5256     goto abort;
 5257   } else
 5258     printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
 5259            mdname(mddev), memory);
 5260
 5261   sprintf(pers_name, "raid%d", mddev->new_level);
 5262   conf->thread = md_register_thread(raid5d, mddev, pers_name);
 5263   if (!conf->thread) {
 5264     printk(KERN_ERR
 5265            "md/raid:%s: couldn't allocate thread.\n",
 5266            mdname(mddev));
 5267     goto abort;
 5268   }
 5269
 5270   return conf;

同樣地,這個函數與 RAID1 的 setup_conf 頗為神似。

5139 行:檢查陣列級別,支援 raid4、5、6。5147 行:檢查 raid5 的 layout 是否合法。5160 行:檢查 chunk 大小,必須是 page 的整數倍且為 2 的 n 次方。5168 行:配置 struct r5conf 並初始化。5185 行:設定資料盤數。5193 行:配置 struct disk_info,用來保存與磁碟的關聯。5200 行:配置保存 struct stripe_head 的哈希表,以便快速查找指定扇區對應的 stripe_head。

5209 至 5234 行:最關鍵的是 5223 行,把 struct disk_info 與 struct md_rdev 掛上關係。5236 行:設定條塊大小。5237 行:設定級別。5241 行:設定最大降級盤數。5252 行:配置 struct stripe_head 的 slab,跟進 grow_stripes:

 1501 static int grow_stripes(struct r5conf *conf, int num)
 1502 {
 1503   struct kmem_cache *sc;
 1504   int devs = max(conf->raid_disks, conf->previous_raid_disks);
 1505
 1506   if (conf->mddev->gendisk)
 1507     sprintf(conf->cache_name[0],
 1508         "raid%d-%s", conf->level, mdname(conf->mddev));
 1509   else
 1510     sprintf(conf->cache_name[0],
 1511         "raid%d-%p", conf->level, conf->mddev);
 1512   sprintf(conf->cache_name[1],
 1513       "%s-alt", conf->cache_name[0]);
 1514
 1515   conf->active_name = 0;
 1516   sc = kmem_cache_create(conf->cache_name[conf->active_name],
 1517       sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
 1518       0, 0, NULL);
 1519   if (!sc)
 1520     return 1;
 1521   conf->slab_cache = sc;
 1522   conf->pool_size = devs;
 1523   while (num--)
 1524     if (!grow_one_stripe(conf))
 1525       return 1;
 1526   return 0;
 1527 }

1504 行:計算資料盤數目。1506 行:設定 slab 名稱。1515 行:建立 slab,單一物件大小為 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),因為 stripe_head 尾部內建了 devs 個 struct r5dev。1523 行:建立空閒的 stripe_head。

看似只是單純建立,本不必深究,但這個函數裡藏著一個被呼叫到爆的函數 release_stripe,所以還是得跟進:

 1477 static int grow_one_stripe(struct r5conf *conf)
 1478 {
 1479   struct stripe_head *sh;
 1480   sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
 1481   if (!sh)
 1482     return 0;
 1483
 1484   sh->raid_conf = conf;
 1485
 1486   spin_lock_init(&sh->stripe_lock);
 1487
 1488   if (grow_buffers(sh)) {
 1489     shrink_buffers(sh);
 1490     kmem_cache_free(conf->slab_cache, sh);
 1491     return 0;
 1492   }
 1493   /* we just created an active stripe so... */
 1494   atomic_set(&sh->count, 1);
 1495   atomic_inc(&conf->active_stripes);
 1496   INIT_LIST_HEAD(&sh->lru);
 1497   release_stripe(sh);
 1498   return 1;
 1499 }

1480 行:新配置一個 stripe_head。1484 行:關聯 r5conf。1488 行:grow_buffers 為每個 r5dev 配置一頁 page,供 stripe_head 做頁拷貝與校驗計算,頁指標保存在 sh->dev[].page。1494 行:設定 stripe_head 計數器,1497 行的 release_stripe 會把它遞減。1495 行:遞增活躍條帶數。1496 行:初始化 lru 鏈表。1497 行:釋放 stripe_head,掛進空閒條帶鏈表。release_stripe 最終會走到 do_release_stripe,其中這幾行特別重要:

 228   list_add_tail(&sh->lru, &conf->inactive_list);
 229   wake_up(&conf->wait_for_stripe);
 230   if (conf->retry_read_aligned)
 231     md_wakeup_thread(conf->mddev->thread);

228 行:把 stripe_head 掛到 inactive_list,也就是條帶空閒鏈表。229 行:喚醒等待空閒條帶的請求——每個陣列的 stripe_head 資源有限,配置不到時就在等待佇列排隊。231 行:喚醒條塊讀請求。

回到 setup_conf,此處已透過 grow_stripes 為陣列配置了 NR_STRIPES 個 stripe_head。5262 行:建立 raid5 主線程。setup_conf 至此結束,回到 run:5434 至 5437 行完成 conf 與 mddev 的關聯與賦值;5494 至 5556 行處理 mddev 相關欄位;5558 行初始化 mddev 的請求佇列與 queue_limits。

小結:RAID5 的 run 與 RAID1 作用相同,都是向上虛擬一個區塊裝置、向下包裝磁碟、建立讀寫請求通道。差別在於 RAID5 的讀寫以 struct stripe_head 為基礎,整個流程都圍繞它展開。下一小節談 RAID10 陣列的運行。

出處:https://blog.csdn.net/liumangxiong

Copyright © Linux教程網 All Rights Reserved