diff --git a/raid/check.c b/raid/check.c index 26845d99..6b754345 100644 --- a/raid/check.c +++ b/raid/check.c @@ -36,11 +36,10 @@ * @ip[] Vector of @nv indexes of the valid parity blocks. * The indexes start from 0. They must be in order. * @nd Number of data blocks. - * @np Number of parity blocks. * @size Size of the blocks pointed by @v. It must be a multipler of 64. * @v Vector of pointers to the blocks of data and parity. - * It has (@nd + @np) elements. The starting elements are the blocks - * for data, following with the parity blocks. + * It has (@nd + @ip[@nv - 1] + 1) elements. The starting elements are the + * blocks for data, following with the parity blocks. * Each block has @size bytes. * @return 0 if the check is satisfied. -1 otherwise. */ @@ -122,7 +121,22 @@ int raid_check(int nr, int *ir, int nd, int np, size_t size, void **v) int rd; int i, j; - BUG_ON(nr >= np); + /* enforce limit on size */ + BUG_ON(size % 64 != 0); + + /* enforce limit on number of failures */ + BUG_ON(nr >= np); /* >= because we check with extra parity */ + BUG_ON(np > RAID_PARITY_MAX); + + /* enforce order in index vector */ + BUG_ON(nr >= 2 && ir[0] >= ir[1]); + BUG_ON(nr >= 3 && ir[1] >= ir[2]); + BUG_ON(nr >= 4 && ir[2] >= ir[3]); + BUG_ON(nr >= 5 && ir[3] >= ir[4]); + BUG_ON(nr >= 6 && ir[4] >= ir[5]); + + /* enforce limit on index vector */ + BUG_ON(nr > 0 && ir[nr-1] >= nd + np); /* count failed data disk */ rd = 0; diff --git a/raid/raid.c b/raid/raid.c index 9ecf703a..3052675f 100644 --- a/raid/raid.c +++ b/raid/raid.c @@ -222,9 +222,13 @@ void (*raid_genz_ptr)(int nd, size_t size, void **vv); void raid_gen(int nd, int np, size_t size, void **v) { - BUG_ON(np < 1 || np > RAID_PARITY_MAX); + /* enforce limit on size */ BUG_ON(size % 64 != 0); + /* enforce limit on number of failures */ + BUG_ON(np < 1); + BUG_ON(np > RAID_PARITY_MAX); + raid_gen_ptr[np - 1](nd, size, v); } @@ -493,17 +497,24 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) int nrd; /* number of data blocks to recover */ int nrp; /* number of parity blocks to recover */ - /* enforce limits on size */ + /* enforce limit on size */ BUG_ON(size % 64 != 0); - /* enforce the order in the index vector */ - BUG_ON(nr >= 2 && ir[0] > ir[1]); - BUG_ON(nr >= 3 && ir[1] > ir[2]); - BUG_ON(nr >= 4 && ir[2] > ir[3]); - BUG_ON(nr >= 5 && ir[3] > ir[4]); - BUG_ON(nr >= 6 && ir[4] > ir[5]); + /* enforce limit on number of failures */ + BUG_ON(nr > np); + BUG_ON(np > RAID_PARITY_MAX); - /* counts the number of data blocks to recover */ + /* enforce order in index vector */ + BUG_ON(nr >= 2 && ir[0] >= ir[1]); + BUG_ON(nr >= 3 && ir[1] >= ir[2]); + BUG_ON(nr >= 4 && ir[2] >= ir[3]); + BUG_ON(nr >= 5 && ir[3] >= ir[4]); + BUG_ON(nr >= 6 && ir[4] >= ir[5]); + + /* enforce limit on index vector */ + BUG_ON(nr > 0 && ir[nr-1] >= nd + np); + + /* count the number of data blocks to recover */ nrd = 0; while (nrd < nr && ir[nrd] < nd) ++nrd; @@ -511,13 +522,10 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) /* all the remaining are parity */ nrp = nr - nrd; - /* enforce basic sanity in arguments */ + /* enforce limit on number of failures */ BUG_ON(nrd > nd); BUG_ON(nrp > np); - /* ensure that we have enough parity to recover */ - BUG_ON(nrd + nrp > np); - /* if failed data is present */ if (nrd != 0) { int ip[RAID_PARITY_MAX]; @@ -547,9 +555,30 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) void raid_data(int nr, int *id, int *ip, int nd, size_t size, void **v) { - BUG_ON(nr > nd); + /* enforce limit on size */ BUG_ON(size % 64 != 0); + /* enforce limit on number of failures */ + BUG_ON(nr > nd); + BUG_ON(nr > RAID_PARITY_MAX); + + /* enforce order in index vector for data */ + BUG_ON(nr >= 2 && id[0] >= id[1]); + BUG_ON(nr >= 3 && id[1] >= id[2]); + BUG_ON(nr >= 4 && id[2] >= id[3]); + BUG_ON(nr >= 5 && id[3] >= id[4]); + BUG_ON(nr >= 6 && id[4] >= id[5]); + + /* enforce limit on index vector for data */ + BUG_ON(nr > 0 && id[nr-1] >= nd); + + /* enforce order in index vector for parity */ + BUG_ON(nr >= 2 && ip[0] >= ip[1]); + BUG_ON(nr >= 3 && ip[1] >= ip[2]); + BUG_ON(nr >= 4 && ip[2] >= ip[3]); + BUG_ON(nr >= 5 && ip[3] >= ip[4]); + BUG_ON(nr >= 6 && ip[4] >= ip[5]); + /* if failed data is present */ if (nr != 0) raid_rec_ptr[nr - 1](nr, id, ip, nd, size, v);