上節我們談到條塊內讀失敗時,會在回呼函數 raid5_align_endio 中把請求加入陣列重試鏈表;在喚醒 raid5d 執行緒後,該請求會由 retry_aligned_read 函數進行重試讀:
4539static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio) 4540{ 4541 /* We may not be able to submit a whole bio at once as there 4542 * may not be enough stripe_heads available. 4543 * We cannot pre-allocate enough stripe_heads as we may need 4544 * more than exist in the cache (if we allow ever large chunks). 4545 * So we do one stripe head at a time and record in 4546 * ->bi_hw_segments how many have been done. 4547 * 4548 * We *know* that this entire raid_bio is in one chunk, so 4549 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. 4550 */
註解說明:若沒有足夠的 struct stripe_head,就無法一次把請求提交完;由於不能預先配置足夠的 stripe_head,因此一次只提交一個,並把已提交的數量記錄在 bio->bi_hw_segments。又因為是條塊內讀,整個 raid_bio 都在同一個條塊內,只需呼叫一次 raid5_compute_sector 算出對應磁碟下標 dd_idx。
看完註解可知這裡復用了 bio->bi_hw_segments 來記錄已下發的 stripe_head 數量。接著看具體用法:
4558 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); 4559 sector = raid5_compute_sector(conf, logical_sector, 4560 0, &dd_idx, NULL); 4561 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4558 行算出請求起始扇區對應的 stripe 扇區(讀的基本單位是 stripe 大小,即一頁);4559 行算出對應磁碟下標 dd_idx 與磁碟內偏移 sector;4561 行取得請求結束扇區。
4563 for (; logical_sector < last_sector; 4564 logical_sector += STRIPE_SECTORS, 4565 sector += STRIPE_SECTORS, 4566 scnt++) { 4567 4568 if (scnt < raid5_bi_processed_stripes(raid_bio)) 4569 /* already done this stripe */ 4570 continue; 4571 4572 sh = get_active_stripe(conf, sector, 0, 1, 0); 4573 4574 if (!sh) { 4575 /* failed to get a stripe - must wait */ 4576 raid5_set_bi_processed_stripes(raid_bio, scnt); 4577 conf->retry_read_aligned = raid_bio; 4578 return handled; 4579 } 4580 4581 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { 4582 release_stripe(sh); 4583 raid5_set_bi_processed_stripes(raid_bio, scnt); 4584 conf->retry_read_aligned = raid_bio; 4585 return handled; 4586 } 4587 4588 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); 4589 handle_stripe(sh); 4590 release_stripe(sh); 4591 handled++; 4592 }
4563 行對條塊內每個 stripe 操作(例如條塊 64KB、stripe 4KB、請求為整個條塊時需循環 16 次);4568 行若已下發過則跳過(藉 bio->bi_hw_segments 記錄已下發數量,下次再進來可續發後面的 stripe);4572 行取得 sector 對應的 stripe_head;4574 行若取不到 stripe_head,則保存已下發數量、把請求存入 retry_read_aligned,下次喚醒 raid5d 直接從該指針續發;4581 行把 bio 加入 stripe_head 請求鏈表;4582 行若加入失敗則釋放 stripe_head、記錄數量並保存重試請求;4588 行設定塊層不需合併標志;4589 行處理 stripe;4590 行遞減 stripe 計數;4591 行遞增已處理 stripe 數。
4593 remaining = raid5_dec_bi_active_stripes(raid_bio); 4594 if (remaining == 0) 4595 bio_endio(raid_bio, 0); 4596 if (atomic_dec_and_test(&conf->active_aligned_reads)) 4597 wake_up(&conf->wait_for_stripe); 4598 return handled;
4593 行遞減 stripe 數;4594 行判斷所有下發 stripe 是否已處理完;4595 行呼叫請求回呼;4596 行喚醒等待該條帶的行程;4598 行返回已下發 stripe 數。
已透過 handle_stripe 處理 stripe_head。對條塊內讀而言,handle_stripe 前兩個 if 不執行,直接來到 analyse_stripe。由於 retry_aligned_read 中已為 stripe_head 加上 bio,add_stripe_bio 會把 bio 掛到對應磁碟的 toread 隊列;條塊內讀只有一個資料盤掛有 bio,故 analyse_stripe 會執行到:
3245 if (test_bit(R5_Wantfill, &dev->flags)) 3246 s->to_fill++; 3247 else if (dev->toread) 3248 s->to_read++;
3247 行判斷讀隊列有請求,3248 行遞增需讀的裝置數。
由於先前條塊內讀失敗,可能是磁碟對應扇區出錯或磁碟異常(rdev 被設 Faulty),也可能是該扇區為壞塊(is_badblock 回傳 true)。第一種情況:
3271 if (rdev && test_bit(Faulty, &rdev->flags)) 3272 rdev = NULL; 3287 if (!rdev) 3288 /* Not in-sync */; ... 3304 else if (test_bit(R5_UPTODATE, &dev->flags) && 3305 test_bit(R5_Expanded, &dev->flags)) 3310 set_bit(R5_Insync, &dev->flags);
設了 Faulty 時 rdev 被設為 NULL,3287 行成立而不進入 3310 行,故 dev->flags 不會被設 R5_Insync。第二種情況(壞塊)則讀取後必然設 R5_ReadError:
3350 if (test_bit(R5_ReadError, &dev->flags)) 3351 clear_bit(R5_Insync, &dev->flags);
3350 行成立,3351 行清除 R5_Insync。因此兩種情況最終都會清掉 R5_Insync,接著:
3352 if (!test_bit(R5_Insync, &dev->flags)) { 3353 if (s->failed < 2) 3354 s->failed_num[s->failed] = i; 3355 s->failed++;
3352 行成立;3353 行成立(raid5 中 fail>2 即陣列已 fail);3354 行記錄 fail 磁碟下標;3355 行遞增 fail 計數。這一趟 analyse_stripe 下來得到 s->toread 與 s->failed(且 s->failed_num[0]=i),帶著它們回到 handle_stripe:
3468 /* Now we might consider reading some blocks, either to check/generate 3469 * parity, or to satisfy requests 3470 * or to load a block that is being partially written. 3471 */ 3472 if (s.to_read || s.non_overwrite 3473 || (conf->level == 6 && s.to_write && s.failed) 3474 || (s.syncing && (s.uptodate + s.compute < disks)) 3475 || s.replacing 3476 || s.expanding) 3477 handle_stripe_fill(sh, &s, disks);
3472 行 s.to_read 成立,果斷進入 handle_stripe_fill。
2707/** 2708 * handle_stripe_fill - read or compute data to satisfy pending requests. 2709 */ 2710static void handle_stripe_fill(struct stripe_head *sh, 2711 struct stripe_head_state *s, 2712 int disks) 2713{ 2714 int i; 2715 2716 /* look for blocks to read/compute, skip this if a compute 2717 * is already in flight, or if the stripe contents are in the 2718 * midst of changing due to a write 2719 */ 2720 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && 2721 !sh->reconstruct_state) 2722 for (i = disks; i--; ) 2723 if (fetch_block(sh, s, i, disks)) 2724 break; 2725 set_bit(STRIPE_HANDLE, &sh->state); 2726}
看註解是直接讀取或計算資料;由於要讀的磁碟已出錯,現在要讀其他盤的資料來計算。2722 行對條帶中每個磁碟呼叫 fetch_block。
2624static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, 2625 int disk_idx, int disks) 2626{ 2627 struct r5dev *dev = &sh->dev[disk_idx]; 2628 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], 2629 &sh->dev[s->failed_num[1]] }; 2630 2631 /* is the data in this block needed, and can we get it? */ 2632 if (!test_bit(R5_LOCKED, &dev->flags) && 2633 !test_bit(R5_UPTODATE, &dev->flags) && 2634 (dev->toread || 2635 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || 2636 s->syncing || s->expanding || 2637 (s->replacing && want_replace(sh, disk_idx)) || 2638 (s->failed >= 1 && fdev[0]->toread) || 2639 (s->failed >= 2 && fdev[1]->toread) || 2640 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && 2641 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || 2642 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
此時上下文為:條帶中 i 盤的 dev->toread 不為空,且 s->failed==1、s->failed_num[0]=i。當 disk_idx==i 時 2632、2633、2634 行都成立而進入 if;當 disk_idx!=i 時 2632、2633、2638 行成立也進入 if。
2648 if ((s->uptodate == disks - 1) && ... 2670 } else if (s->uptodate == disks-2 && s->failed >= 2) { ... 2695 } else if (test_bit(R5_Insync, &dev->flags)) { 2696 set_bit(R5_LOCKED, &dev->flags); 2697 set_bit(R5_Wantread, &dev->flags); 2698 s->locked++; 2699 pr_debug("Reading block %d (sync=%d)\n", 2700 disk_idx, s->syncing); 2701 }
因 s->uptodate==0,直接進入 2695 行,為非 fail 盤設 R5_LOCKED 與 R5_Wantread。
重試讀全流程歸納如下:1)發起條塊內讀;2)讀失敗,加入重試鏈表並喚醒 raid5d;3)raid5d 從重試鏈表移出請求,為每個 stripe 配置 stripe_head 並呼叫 handle_stripe;4)handle_stripe 經 analyse_stripe 設 s->toread 與 s->failed,再由 handle_stripe_fill 從其他冗餘盤讀資料,最後 ops_run_io 下發請求;5)所有子請求返回時 raid5_end_read_request 把 stripe_head 加入 handle_list;6)raid5d 取出 stripe_head 再呼叫 handle_stripe;7)此時 s->uptodate==disks-1,handle_stripe 呼叫 handle_stripe_fill 設 STRIPE_OP_COMPUTE_BLK,raid_run_ops 遂呼叫 ops_run_compute5 把需讀的塊算出;8)計算回呼 ops_complete_compute 把對應 dev->flags 設為 R5_UPTODATE 並重新加入 handle_list;9)再次進入 handle_stripe,analyse_stripe 設 R5_Wantfill 與 s->to_fill,handle_stripe 再設 STRIPE_OP_BIOFILL 與 STRIPE_BIOFILL_RUN,raid_run_ops 呼叫 ops_run_biofill 把資料拷貝到 bio 的頁;10)拷貝回呼 ops_complete_biofill 在所有 stripe 返回後,原始 bio 得到所需資料,透過 return_io 把原始請求 done 回去。下一小節繼續講 raid5 非條塊內的讀流程。
出處:https://blog.csdn.net/liumangxiong