raid10.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. /*
  2. * raid10.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Neil Brown
  5. *
  6. * RAID-10 support for md.
  7. *
  8. * Base on code in raid1.c. See raid1.c for further copyright information.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/seq_file.h>
  24. #include "md.h"
  25. #include "raid10.h"
  26. #include "raid0.h"
  27. #include "bitmap.h"
  28. /*
  29. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  30. * The layout of data is defined by
  31. * chunk_size
  32. * raid_disks
  33. * near_copies (stored in low byte of layout)
  34. * far_copies (stored in second byte of layout)
  35. * far_offset (stored in bit 16 of layout )
  36. *
  37. * The data to be stored is divided into chunks using chunksize.
  38. * Each device is divided into far_copies sections.
  39. * In each section, chunks are laid out in a style similar to raid0, but
  40. * near_copies copies of each chunk is stored (each on a different drive).
  41. * The starting device for each section is offset near_copies from the starting
  42. * device of the previous section.
  43. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
  44. * drive.
  45. * near_copies and far_copies must be at least one, and their product is at most
  46. * raid_disks.
  47. *
  48. * If far_offset is true, then the far_copies are handled a bit differently.
  49. * The copies are still in different stripes, but instead of be very far apart
  50. * on disk, there are adjacent stripes.
  51. */
  52. /*
  53. * Number of guaranteed r10bios in case of extreme VM load:
  54. */
  55. #define NR_RAID10_BIOS 256
  56. static void allow_barrier(conf_t *conf);
  57. static void lower_barrier(conf_t *conf);
  58. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  59. {
  60. conf_t *conf = data;
  61. int size = offsetof(struct r10bio_s, devs[conf->copies]);
  62. /* allocate a r10bio with room for raid_disks entries in the bios array */
  63. return kzalloc(size, gfp_flags);
  64. }
  65. static void r10bio_pool_free(void *r10_bio, void *data)
  66. {
  67. kfree(r10_bio);
  68. }
  69. /* Maximum size of each resync request */
  70. #define RESYNC_BLOCK_SIZE (64*1024)
  71. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  72. /* amount of memory to reserve for resync requests */
  73. #define RESYNC_WINDOW (1024*1024)
  74. /* maximum number of concurrent requests, memory permitting */
  75. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  76. /*
  77. * When performing a resync, we need to read and compare, so
  78. * we need as many pages are there are copies.
  79. * When performing a recovery, we need 2 bios, one for read,
  80. * one for write (we recover only one drive per r10buf)
  81. *
  82. */
  83. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  84. {
  85. conf_t *conf = data;
  86. struct page *page;
  87. r10bio_t *r10_bio;
  88. struct bio *bio;
  89. int i, j;
  90. int nalloc;
  91. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  92. if (!r10_bio)
  93. return NULL;
  94. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  95. nalloc = conf->copies; /* resync */
  96. else
  97. nalloc = 2; /* recovery */
  98. /*
  99. * Allocate bios.
  100. */
  101. for (j = nalloc ; j-- ; ) {
  102. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  103. if (!bio)
  104. goto out_free_bio;
  105. r10_bio->devs[j].bio = bio;
  106. }
  107. /*
  108. * Allocate RESYNC_PAGES data pages and attach them
  109. * where needed.
  110. */
  111. for (j = 0 ; j < nalloc; j++) {
  112. bio = r10_bio->devs[j].bio;
  113. for (i = 0; i < RESYNC_PAGES; i++) {
  114. page = alloc_page(gfp_flags);
  115. if (unlikely(!page))
  116. goto out_free_pages;
  117. bio->bi_io_vec[i].bv_page = page;
  118. }
  119. }
  120. return r10_bio;
  121. out_free_pages:
  122. for ( ; i > 0 ; i--)
  123. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  124. while (j--)
  125. for (i = 0; i < RESYNC_PAGES ; i++)
  126. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  127. j = -1;
  128. out_free_bio:
  129. while ( ++j < nalloc )
  130. bio_put(r10_bio->devs[j].bio);
  131. r10bio_pool_free(r10_bio, conf);
  132. return NULL;
  133. }
  134. static void r10buf_pool_free(void *__r10_bio, void *data)
  135. {
  136. int i;
  137. conf_t *conf = data;
  138. r10bio_t *r10bio = __r10_bio;
  139. int j;
  140. for (j=0; j < conf->copies; j++) {
  141. struct bio *bio = r10bio->devs[j].bio;
  142. if (bio) {
  143. for (i = 0; i < RESYNC_PAGES; i++) {
  144. safe_put_page(bio->bi_io_vec[i].bv_page);
  145. bio->bi_io_vec[i].bv_page = NULL;
  146. }
  147. bio_put(bio);
  148. }
  149. }
  150. r10bio_pool_free(r10bio, conf);
  151. }
  152. static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
  153. {
  154. int i;
  155. for (i = 0; i < conf->copies; i++) {
  156. struct bio **bio = & r10_bio->devs[i].bio;
  157. if (*bio && *bio != IO_BLOCKED)
  158. bio_put(*bio);
  159. *bio = NULL;
  160. }
  161. }
  162. static void free_r10bio(r10bio_t *r10_bio)
  163. {
  164. conf_t *conf = r10_bio->mddev->private;
  165. /*
  166. * Wake up any possible resync thread that waits for the device
  167. * to go idle.
  168. */
  169. allow_barrier(conf);
  170. put_all_bios(conf, r10_bio);
  171. mempool_free(r10_bio, conf->r10bio_pool);
  172. }
  173. static void put_buf(r10bio_t *r10_bio)
  174. {
  175. conf_t *conf = r10_bio->mddev->private;
  176. mempool_free(r10_bio, conf->r10buf_pool);
  177. lower_barrier(conf);
  178. }
  179. static void reschedule_retry(r10bio_t *r10_bio)
  180. {
  181. unsigned long flags;
  182. mddev_t *mddev = r10_bio->mddev;
  183. conf_t *conf = mddev->private;
  184. spin_lock_irqsave(&conf->device_lock, flags);
  185. list_add(&r10_bio->retry_list, &conf->retry_list);
  186. conf->nr_queued ++;
  187. spin_unlock_irqrestore(&conf->device_lock, flags);
  188. /* wake up frozen array... */
  189. wake_up(&conf->wait_barrier);
  190. md_wakeup_thread(mddev->thread);
  191. }
  192. /*
  193. * raid_end_bio_io() is called when we have finished servicing a mirrored
  194. * operation and are ready to return a success/failure code to the buffer
  195. * cache layer.
  196. */
  197. static void raid_end_bio_io(r10bio_t *r10_bio)
  198. {
  199. struct bio *bio = r10_bio->master_bio;
  200. bio_endio(bio,
  201. test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
  202. free_r10bio(r10_bio);
  203. }
  204. /*
  205. * Update disk head position estimator based on IRQ completion info.
  206. */
  207. static inline void update_head_pos(int slot, r10bio_t *r10_bio)
  208. {
  209. conf_t *conf = r10_bio->mddev->private;
  210. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  211. r10_bio->devs[slot].addr + (r10_bio->sectors);
  212. }
  213. static void raid10_end_read_request(struct bio *bio, int error)
  214. {
  215. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  216. r10bio_t *r10_bio = bio->bi_private;
  217. int slot, dev;
  218. conf_t *conf = r10_bio->mddev->private;
  219. slot = r10_bio->read_slot;
  220. dev = r10_bio->devs[slot].devnum;
  221. /*
  222. * this branch is our 'one mirror IO has finished' event handler:
  223. */
  224. update_head_pos(slot, r10_bio);
  225. if (uptodate) {
  226. /*
  227. * Set R10BIO_Uptodate in our master bio, so that
  228. * we will return a good error code to the higher
  229. * levels even if IO on some other mirrored buffer fails.
  230. *
  231. * The 'master' represents the composite IO operation to
  232. * user-side. So if something waits for IO, then it will
  233. * wait for the 'master' bio.
  234. */
  235. set_bit(R10BIO_Uptodate, &r10_bio->state);
  236. raid_end_bio_io(r10_bio);
  237. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  238. } else {
  239. /*
  240. * oops, read error - keep the refcount on the rdev
  241. */
  242. char b[BDEVNAME_SIZE];
  243. if (printk_ratelimit())
  244. printk(KERN_ERR "md/raid10:%s: %s: rescheduling sector %llu\n",
  245. mdname(conf->mddev),
  246. bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
  247. reschedule_retry(r10_bio);
  248. }
  249. }
  250. static void raid10_end_write_request(struct bio *bio, int error)
  251. {
  252. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  253. r10bio_t *r10_bio = bio->bi_private;
  254. int slot, dev;
  255. conf_t *conf = r10_bio->mddev->private;
  256. for (slot = 0; slot < conf->copies; slot++)
  257. if (r10_bio->devs[slot].bio == bio)
  258. break;
  259. dev = r10_bio->devs[slot].devnum;
  260. /*
  261. * this branch is our 'one mirror IO has finished' event handler:
  262. */
  263. if (!uptodate) {
  264. md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
  265. /* an I/O failed, we can't clear the bitmap */
  266. set_bit(R10BIO_Degraded, &r10_bio->state);
  267. } else
  268. /*
  269. * Set R10BIO_Uptodate in our master bio, so that
  270. * we will return a good error code for to the higher
  271. * levels even if IO on some other mirrored buffer fails.
  272. *
  273. * The 'master' represents the composite IO operation to
  274. * user-side. So if something waits for IO, then it will
  275. * wait for the 'master' bio.
  276. */
  277. set_bit(R10BIO_Uptodate, &r10_bio->state);
  278. update_head_pos(slot, r10_bio);
  279. /*
  280. *
  281. * Let's see if all mirrored write operations have finished
  282. * already.
  283. */
  284. if (atomic_dec_and_test(&r10_bio->remaining)) {
  285. /* clear the bitmap if all writes complete successfully */
  286. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  287. r10_bio->sectors,
  288. !test_bit(R10BIO_Degraded, &r10_bio->state),
  289. 0);
  290. md_write_end(r10_bio->mddev);
  291. raid_end_bio_io(r10_bio);
  292. }
  293. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  294. }
  295. /*
  296. * RAID10 layout manager
  297. * As well as the chunksize and raid_disks count, there are two
  298. * parameters: near_copies and far_copies.
  299. * near_copies * far_copies must be <= raid_disks.
  300. * Normally one of these will be 1.
  301. * If both are 1, we get raid0.
  302. * If near_copies == raid_disks, we get raid1.
  303. *
  304. * Chunks are laid out in raid0 style with near_copies copies of the
  305. * first chunk, followed by near_copies copies of the next chunk and
  306. * so on.
  307. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  308. * as described above, we start again with a device offset of near_copies.
  309. * So we effectively have another copy of the whole array further down all
  310. * the drives, but with blocks on different drives.
  311. * With this layout, and block is never stored twice on the one device.
  312. *
  313. * raid10_find_phys finds the sector offset of a given virtual sector
  314. * on each device that it is on.
  315. *
  316. * raid10_find_virt does the reverse mapping, from a device and a
  317. * sector offset to a virtual address
  318. */
  319. static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
  320. {
  321. int n,f;
  322. sector_t sector;
  323. sector_t chunk;
  324. sector_t stripe;
  325. int dev;
  326. int slot = 0;
  327. /* now calculate first sector/dev */
  328. chunk = r10bio->sector >> conf->chunk_shift;
  329. sector = r10bio->sector & conf->chunk_mask;
  330. chunk *= conf->near_copies;
  331. stripe = chunk;
  332. dev = sector_div(stripe, conf->raid_disks);
  333. if (conf->far_offset)
  334. stripe *= conf->far_copies;
  335. sector += stripe << conf->chunk_shift;
  336. /* and calculate all the others */
  337. for (n=0; n < conf->near_copies; n++) {
  338. int d = dev;
  339. sector_t s = sector;
  340. r10bio->devs[slot].addr = sector;
  341. r10bio->devs[slot].devnum = d;
  342. slot++;
  343. for (f = 1; f < conf->far_copies; f++) {
  344. d += conf->near_copies;
  345. if (d >= conf->raid_disks)
  346. d -= conf->raid_disks;
  347. s += conf->stride;
  348. r10bio->devs[slot].devnum = d;
  349. r10bio->devs[slot].addr = s;
  350. slot++;
  351. }
  352. dev++;
  353. if (dev >= conf->raid_disks) {
  354. dev = 0;
  355. sector += (conf->chunk_mask + 1);
  356. }
  357. }
  358. BUG_ON(slot != conf->copies);
  359. }
  360. static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
  361. {
  362. sector_t offset, chunk, vchunk;
  363. offset = sector & conf->chunk_mask;
  364. if (conf->far_offset) {
  365. int fc;
  366. chunk = sector >> conf->chunk_shift;
  367. fc = sector_div(chunk, conf->far_copies);
  368. dev -= fc * conf->near_copies;
  369. if (dev < 0)
  370. dev += conf->raid_disks;
  371. } else {
  372. while (sector >= conf->stride) {
  373. sector -= conf->stride;
  374. if (dev < conf->near_copies)
  375. dev += conf->raid_disks - conf->near_copies;
  376. else
  377. dev -= conf->near_copies;
  378. }
  379. chunk = sector >> conf->chunk_shift;
  380. }
  381. vchunk = chunk * conf->raid_disks + dev;
  382. sector_div(vchunk, conf->near_copies);
  383. return (vchunk << conf->chunk_shift) + offset;
  384. }
  385. /**
  386. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  387. * @q: request queue
  388. * @bvm: properties of new bio
  389. * @biovec: the request that could be merged to it.
  390. *
  391. * Return amount of bytes we can accept at this offset
  392. * If near_copies == raid_disk, there are no striping issues,
  393. * but in that case, the function isn't called at all.
  394. */
  395. static int raid10_mergeable_bvec(struct request_queue *q,
  396. struct bvec_merge_data *bvm,
  397. struct bio_vec *biovec)
  398. {
  399. mddev_t *mddev = q->queuedata;
  400. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  401. int max;
  402. unsigned int chunk_sectors = mddev->chunk_sectors;
  403. unsigned int bio_sectors = bvm->bi_size >> 9;
  404. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  405. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  406. if (max <= biovec->bv_len && bio_sectors == 0)
  407. return biovec->bv_len;
  408. else
  409. return max;
  410. }
  411. /*
  412. * This routine returns the disk from which the requested read should
  413. * be done. There is a per-array 'next expected sequential IO' sector
  414. * number - if this matches on the next IO then we use the last disk.
  415. * There is also a per-disk 'last know head position' sector that is
  416. * maintained from IRQ contexts, both the normal and the resync IO
  417. * completion handlers update this position correctly. If there is no
  418. * perfect sequential match then we pick the disk whose head is closest.
  419. *
  420. * If there are 2 mirrors in the same 2 devices, performance degrades
  421. * because position is mirror, not device based.
  422. *
  423. * The rdev for the device selected will have nr_pending incremented.
  424. */
  425. /*
  426. * FIXME: possibly should rethink readbalancing and do it differently
  427. * depending on near_copies / far_copies geometry.
  428. */
  429. static int read_balance(conf_t *conf, r10bio_t *r10_bio)
  430. {
  431. const sector_t this_sector = r10_bio->sector;
  432. int disk, slot;
  433. const int sectors = r10_bio->sectors;
  434. sector_t new_distance, best_dist;
  435. mdk_rdev_t *rdev;
  436. int do_balance;
  437. int best_slot;
  438. raid10_find_phys(conf, r10_bio);
  439. rcu_read_lock();
  440. retry:
  441. best_slot = -1;
  442. best_dist = MaxSector;
  443. do_balance = 1;
  444. /*
  445. * Check if we can balance. We can balance on the whole
  446. * device if no resync is going on (recovery is ok), or below
  447. * the resync window. We take the first readable disk when
  448. * above the resync window.
  449. */
  450. if (conf->mddev->recovery_cp < MaxSector
  451. && (this_sector + sectors >= conf->next_resync))
  452. do_balance = 0;
  453. for (slot = 0; slot < conf->copies ; slot++) {
  454. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  455. continue;
  456. disk = r10_bio->devs[slot].devnum;
  457. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  458. if (rdev == NULL)
  459. continue;
  460. if (!test_bit(In_sync, &rdev->flags))
  461. continue;
  462. if (!do_balance)
  463. break;
  464. /* This optimisation is debatable, and completely destroys
  465. * sequential read speed for 'far copies' arrays. So only
  466. * keep it for 'near' arrays, and review those later.
  467. */
  468. if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  469. break;
  470. /* for far > 1 always use the lowest address */
  471. if (conf->far_copies > 1)
  472. new_distance = r10_bio->devs[slot].addr;
  473. else
  474. new_distance = abs(r10_bio->devs[slot].addr -
  475. conf->mirrors[disk].head_position);
  476. if (new_distance < best_dist) {
  477. best_dist = new_distance;
  478. best_slot = slot;
  479. }
  480. }
  481. if (slot == conf->copies)
  482. slot = best_slot;
  483. if (slot >= 0) {
  484. disk = r10_bio->devs[slot].devnum;
  485. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  486. if (!rdev)
  487. goto retry;
  488. atomic_inc(&rdev->nr_pending);
  489. if (test_bit(Faulty, &rdev->flags)) {
  490. /* Cannot risk returning a device that failed
  491. * before we inc'ed nr_pending
  492. */
  493. rdev_dec_pending(rdev, conf->mddev);
  494. goto retry;
  495. }
  496. r10_bio->read_slot = slot;
  497. } else
  498. disk = -1;
  499. rcu_read_unlock();
  500. return disk;
  501. }
  502. static int raid10_congested(void *data, int bits)
  503. {
  504. mddev_t *mddev = data;
  505. conf_t *conf = mddev->private;
  506. int i, ret = 0;
  507. if (mddev_congested(mddev, bits))
  508. return 1;
  509. rcu_read_lock();
  510. for (i = 0; i < conf->raid_disks && ret == 0; i++) {
  511. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  512. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  513. struct request_queue *q = bdev_get_queue(rdev->bdev);
  514. ret |= bdi_congested(&q->backing_dev_info, bits);
  515. }
  516. }
  517. rcu_read_unlock();
  518. return ret;
  519. }
  520. static void flush_pending_writes(conf_t *conf)
  521. {
  522. /* Any writes that have been queued but are awaiting
  523. * bitmap updates get flushed here.
  524. */
  525. spin_lock_irq(&conf->device_lock);
  526. if (conf->pending_bio_list.head) {
  527. struct bio *bio;
  528. bio = bio_list_get(&conf->pending_bio_list);
  529. spin_unlock_irq(&conf->device_lock);
  530. /* flush any pending bitmap writes to disk
  531. * before proceeding w/ I/O */
  532. bitmap_unplug(conf->mddev->bitmap);
  533. while (bio) { /* submit pending writes */
  534. struct bio *next = bio->bi_next;
  535. bio->bi_next = NULL;
  536. generic_make_request(bio);
  537. bio = next;
  538. }
  539. } else
  540. spin_unlock_irq(&conf->device_lock);
  541. }
  542. /* Barriers....
  543. * Sometimes we need to suspend IO while we do something else,
  544. * either some resync/recovery, or reconfigure the array.
  545. * To do this we raise a 'barrier'.
  546. * The 'barrier' is a counter that can be raised multiple times
  547. * to count how many activities are happening which preclude
  548. * normal IO.
  549. * We can only raise the barrier if there is no pending IO.
  550. * i.e. if nr_pending == 0.
  551. * We choose only to raise the barrier if no-one is waiting for the
  552. * barrier to go down. This means that as soon as an IO request
  553. * is ready, no other operations which require a barrier will start
  554. * until the IO request has had a chance.
  555. *
  556. * So: regular IO calls 'wait_barrier'. When that returns there
  557. * is no backgroup IO happening, It must arrange to call
  558. * allow_barrier when it has finished its IO.
  559. * backgroup IO calls must call raise_barrier. Once that returns
  560. * there is no normal IO happeing. It must arrange to call
  561. * lower_barrier when the particular background IO completes.
  562. */
  563. static void raise_barrier(conf_t *conf, int force)
  564. {
  565. BUG_ON(force && !conf->barrier);
  566. spin_lock_irq(&conf->resync_lock);
  567. /* Wait until no block IO is waiting (unless 'force') */
  568. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  569. conf->resync_lock, );
  570. /* block any new IO from starting */
  571. conf->barrier++;
  572. /* Now wait for all pending IO to complete */
  573. wait_event_lock_irq(conf->wait_barrier,
  574. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  575. conf->resync_lock, );
  576. spin_unlock_irq(&conf->resync_lock);
  577. }
  578. static void lower_barrier(conf_t *conf)
  579. {
  580. unsigned long flags;
  581. spin_lock_irqsave(&conf->resync_lock, flags);
  582. conf->barrier--;
  583. spin_unlock_irqrestore(&conf->resync_lock, flags);
  584. wake_up(&conf->wait_barrier);
  585. }
  586. static void wait_barrier(conf_t *conf)
  587. {
  588. spin_lock_irq(&conf->resync_lock);
  589. if (conf->barrier) {
  590. conf->nr_waiting++;
  591. /* Wait for the barrier to drop.
  592. * However if there are already pending
  593. * requests (preventing the barrier from
  594. * rising completely), and the
  595. * pre-process bio queue isn't empty,
  596. * then don't wait, as we need to empty
  597. * that queue to get the nr_pending
  598. * count down.
  599. */
  600. wait_event_lock_irq(conf->wait_barrier,
  601. !conf->barrier ||
  602. (conf->nr_pending &&
  603. current->bio_list &&
  604. !bio_list_empty(current->bio_list)),
  605. conf->resync_lock,
  606. );
  607. conf->nr_waiting--;
  608. }
  609. conf->nr_pending++;
  610. spin_unlock_irq(&conf->resync_lock);
  611. }
  612. static void allow_barrier(conf_t *conf)
  613. {
  614. unsigned long flags;
  615. spin_lock_irqsave(&conf->resync_lock, flags);
  616. conf->nr_pending--;
  617. spin_unlock_irqrestore(&conf->resync_lock, flags);
  618. wake_up(&conf->wait_barrier);
  619. }
  620. static void freeze_array(conf_t *conf)
  621. {
  622. /* stop syncio and normal IO and wait for everything to
  623. * go quiet.
  624. * We increment barrier and nr_waiting, and then
  625. * wait until nr_pending match nr_queued+1
  626. * This is called in the context of one normal IO request
  627. * that has failed. Thus any sync request that might be pending
  628. * will be blocked by nr_pending, and we need to wait for
  629. * pending IO requests to complete or be queued for re-try.
  630. * Thus the number queued (nr_queued) plus this request (1)
  631. * must match the number of pending IOs (nr_pending) before
  632. * we continue.
  633. */
  634. spin_lock_irq(&conf->resync_lock);
  635. conf->barrier++;
  636. conf->nr_waiting++;
  637. wait_event_lock_irq(conf->wait_barrier,
  638. conf->nr_pending == conf->nr_queued+1,
  639. conf->resync_lock,
  640. flush_pending_writes(conf));
  641. spin_unlock_irq(&conf->resync_lock);
  642. }
  643. static void unfreeze_array(conf_t *conf)
  644. {
  645. /* reverse the effect of the freeze */
  646. spin_lock_irq(&conf->resync_lock);
  647. conf->barrier--;
  648. conf->nr_waiting--;
  649. wake_up(&conf->wait_barrier);
  650. spin_unlock_irq(&conf->resync_lock);
  651. }
  652. static int make_request(mddev_t *mddev, struct bio * bio)
  653. {
  654. conf_t *conf = mddev->private;
  655. mirror_info_t *mirror;
  656. r10bio_t *r10_bio;
  657. struct bio *read_bio;
  658. int i;
  659. int chunk_sects = conf->chunk_mask + 1;
  660. const int rw = bio_data_dir(bio);
  661. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  662. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  663. unsigned long flags;
  664. mdk_rdev_t *blocked_rdev;
  665. int plugged;
  666. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  667. md_flush_request(mddev, bio);
  668. return 0;
  669. }
  670. /* If this request crosses a chunk boundary, we need to
  671. * split it. This will only happen for 1 PAGE (or less) requests.
  672. */
  673. if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
  674. > chunk_sects &&
  675. conf->near_copies < conf->raid_disks)) {
  676. struct bio_pair *bp;
  677. /* Sanity check -- queue functions should prevent this happening */
  678. if (bio->bi_vcnt != 1 ||
  679. bio->bi_idx != 0)
  680. goto bad_map;
  681. /* This is a one page bio that upper layers
  682. * refuse to split for us, so we need to split it.
  683. */
  684. bp = bio_split(bio,
  685. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  686. /* Each of these 'make_request' calls will call 'wait_barrier'.
  687. * If the first succeeds but the second blocks due to the resync
  688. * thread raising the barrier, we will deadlock because the
  689. * IO to the underlying device will be queued in generic_make_request
  690. * and will never complete, so will never reduce nr_pending.
  691. * So increment nr_waiting here so no new raise_barriers will
  692. * succeed, and so the second wait_barrier cannot block.
  693. */
  694. spin_lock_irq(&conf->resync_lock);
  695. conf->nr_waiting++;
  696. spin_unlock_irq(&conf->resync_lock);
  697. if (make_request(mddev, &bp->bio1))
  698. generic_make_request(&bp->bio1);
  699. if (make_request(mddev, &bp->bio2))
  700. generic_make_request(&bp->bio2);
  701. spin_lock_irq(&conf->resync_lock);
  702. conf->nr_waiting--;
  703. wake_up(&conf->wait_barrier);
  704. spin_unlock_irq(&conf->resync_lock);
  705. bio_pair_release(bp);
  706. return 0;
  707. bad_map:
  708. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  709. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  710. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  711. bio_io_error(bio);
  712. return 0;
  713. }
  714. md_write_start(mddev, bio);
  715. /*
  716. * Register the new request and wait if the reconstruction
  717. * thread has put up a bar for new requests.
  718. * Continue immediately if no resync is active currently.
  719. */
  720. wait_barrier(conf);
  721. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  722. r10_bio->master_bio = bio;
  723. r10_bio->sectors = bio->bi_size >> 9;
  724. r10_bio->mddev = mddev;
  725. r10_bio->sector = bio->bi_sector;
  726. r10_bio->state = 0;
  727. if (rw == READ) {
  728. /*
  729. * read balancing logic:
  730. */
  731. int disk = read_balance(conf, r10_bio);
  732. int slot = r10_bio->read_slot;
  733. if (disk < 0) {
  734. raid_end_bio_io(r10_bio);
  735. return 0;
  736. }
  737. mirror = conf->mirrors + disk;
  738. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  739. r10_bio->devs[slot].bio = read_bio;
  740. read_bio->bi_sector = r10_bio->devs[slot].addr +
  741. mirror->rdev->data_offset;
  742. read_bio->bi_bdev = mirror->rdev->bdev;
  743. read_bio->bi_end_io = raid10_end_read_request;
  744. read_bio->bi_rw = READ | do_sync;
  745. read_bio->bi_private = r10_bio;
  746. generic_make_request(read_bio);
  747. return 0;
  748. }
  749. /*
  750. * WRITE:
  751. */
  752. /* first select target devices under rcu_lock and
  753. * inc refcount on their rdev. Record them by setting
  754. * bios[x] to bio
  755. */
  756. plugged = mddev_check_plugged(mddev);
  757. raid10_find_phys(conf, r10_bio);
  758. retry_write:
  759. blocked_rdev = NULL;
  760. rcu_read_lock();
  761. for (i = 0; i < conf->copies; i++) {
  762. int d = r10_bio->devs[i].devnum;
  763. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
  764. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  765. atomic_inc(&rdev->nr_pending);
  766. blocked_rdev = rdev;
  767. break;
  768. }
  769. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  770. atomic_inc(&rdev->nr_pending);
  771. r10_bio->devs[i].bio = bio;
  772. } else {
  773. r10_bio->devs[i].bio = NULL;
  774. set_bit(R10BIO_Degraded, &r10_bio->state);
  775. }
  776. }
  777. rcu_read_unlock();
  778. if (unlikely(blocked_rdev)) {
  779. /* Have to wait for this device to get unblocked, then retry */
  780. int j;
  781. int d;
  782. for (j = 0; j < i; j++)
  783. if (r10_bio->devs[j].bio) {
  784. d = r10_bio->devs[j].devnum;
  785. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  786. }
  787. allow_barrier(conf);
  788. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  789. wait_barrier(conf);
  790. goto retry_write;
  791. }
  792. atomic_set(&r10_bio->remaining, 1);
  793. bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
  794. for (i = 0; i < conf->copies; i++) {
  795. struct bio *mbio;
  796. int d = r10_bio->devs[i].devnum;
  797. if (!r10_bio->devs[i].bio)
  798. continue;
  799. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  800. r10_bio->devs[i].bio = mbio;
  801. mbio->bi_sector = r10_bio->devs[i].addr+
  802. conf->mirrors[d].rdev->data_offset;
  803. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  804. mbio->bi_end_io = raid10_end_write_request;
  805. mbio->bi_rw = WRITE | do_sync | do_fua;
  806. mbio->bi_private = r10_bio;
  807. atomic_inc(&r10_bio->remaining);
  808. spin_lock_irqsave(&conf->device_lock, flags);
  809. bio_list_add(&conf->pending_bio_list, mbio);
  810. spin_unlock_irqrestore(&conf->device_lock, flags);
  811. }
  812. if (atomic_dec_and_test(&r10_bio->remaining)) {
  813. /* This matches the end of raid10_end_write_request() */
  814. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  815. r10_bio->sectors,
  816. !test_bit(R10BIO_Degraded, &r10_bio->state),
  817. 0);
  818. md_write_end(mddev);
  819. raid_end_bio_io(r10_bio);
  820. }
  821. /* In case raid10d snuck in to freeze_array */
  822. wake_up(&conf->wait_barrier);
  823. if (do_sync || !mddev->bitmap || !plugged)
  824. md_wakeup_thread(mddev->thread);
  825. return 0;
  826. }
  827. static void status(struct seq_file *seq, mddev_t *mddev)
  828. {
  829. conf_t *conf = mddev->private;
  830. int i;
  831. if (conf->near_copies < conf->raid_disks)
  832. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  833. if (conf->near_copies > 1)
  834. seq_printf(seq, " %d near-copies", conf->near_copies);
  835. if (conf->far_copies > 1) {
  836. if (conf->far_offset)
  837. seq_printf(seq, " %d offset-copies", conf->far_copies);
  838. else
  839. seq_printf(seq, " %d far-copies", conf->far_copies);
  840. }
  841. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  842. conf->raid_disks - mddev->degraded);
  843. for (i = 0; i < conf->raid_disks; i++)
  844. seq_printf(seq, "%s",
  845. conf->mirrors[i].rdev &&
  846. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  847. seq_printf(seq, "]");
  848. }
  849. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  850. {
  851. char b[BDEVNAME_SIZE];
  852. conf_t *conf = mddev->private;
  853. /*
  854. * If it is not operational, then we have already marked it as dead
  855. * else if it is the last working disks, ignore the error, let the
  856. * next level up know.
  857. * else mark the drive as failed
  858. */
  859. if (test_bit(In_sync, &rdev->flags)
  860. && conf->raid_disks-mddev->degraded == 1)
  861. /*
  862. * Don't fail the drive, just return an IO error.
  863. * The test should really be more sophisticated than
  864. * "working_disks == 1", but it isn't critical, and
  865. * can wait until we do more sophisticated "is the drive
  866. * really dead" tests...
  867. */
  868. return;
  869. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  870. unsigned long flags;
  871. spin_lock_irqsave(&conf->device_lock, flags);
  872. mddev->degraded++;
  873. spin_unlock_irqrestore(&conf->device_lock, flags);
  874. /*
  875. * if recovery is running, make sure it aborts.
  876. */
  877. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  878. }
  879. set_bit(Faulty, &rdev->flags);
  880. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  881. printk(KERN_ALERT
  882. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  883. "md/raid10:%s: Operation continuing on %d devices.\n",
  884. mdname(mddev), bdevname(rdev->bdev, b),
  885. mdname(mddev), conf->raid_disks - mddev->degraded);
  886. }
  887. static void print_conf(conf_t *conf)
  888. {
  889. int i;
  890. mirror_info_t *tmp;
  891. printk(KERN_DEBUG "RAID10 conf printout:\n");
  892. if (!conf) {
  893. printk(KERN_DEBUG "(!conf)\n");
  894. return;
  895. }
  896. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  897. conf->raid_disks);
  898. for (i = 0; i < conf->raid_disks; i++) {
  899. char b[BDEVNAME_SIZE];
  900. tmp = conf->mirrors + i;
  901. if (tmp->rdev)
  902. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  903. i, !test_bit(In_sync, &tmp->rdev->flags),
  904. !test_bit(Faulty, &tmp->rdev->flags),
  905. bdevname(tmp->rdev->bdev,b));
  906. }
  907. }
  908. static void close_sync(conf_t *conf)
  909. {
  910. wait_barrier(conf);
  911. allow_barrier(conf);
  912. mempool_destroy(conf->r10buf_pool);
  913. conf->r10buf_pool = NULL;
  914. }
  915. /* check if there are enough drives for
  916. * every block to appear on atleast one
  917. */
  918. static int enough(conf_t *conf)
  919. {
  920. int first = 0;
  921. do {
  922. int n = conf->copies;
  923. int cnt = 0;
  924. while (n--) {
  925. if (conf->mirrors[first].rdev)
  926. cnt++;
  927. first = (first+1) % conf->raid_disks;
  928. }
  929. if (cnt == 0)
  930. return 0;
  931. } while (first != 0);
  932. return 1;
  933. }
  934. static int raid10_spare_active(mddev_t *mddev)
  935. {
  936. int i;
  937. conf_t *conf = mddev->private;
  938. mirror_info_t *tmp;
  939. int count = 0;
  940. unsigned long flags;
  941. /*
  942. * Find all non-in_sync disks within the RAID10 configuration
  943. * and mark them in_sync
  944. */
  945. for (i = 0; i < conf->raid_disks; i++) {
  946. tmp = conf->mirrors + i;
  947. if (tmp->rdev
  948. && !test_bit(Faulty, &tmp->rdev->flags)
  949. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  950. count++;
  951. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  952. }
  953. }
  954. spin_lock_irqsave(&conf->device_lock, flags);
  955. mddev->degraded -= count;
  956. spin_unlock_irqrestore(&conf->device_lock, flags);
  957. print_conf(conf);
  958. return count;
  959. }
  960. static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  961. {
  962. conf_t *conf = mddev->private;
  963. int err = -EEXIST;
  964. int mirror;
  965. mirror_info_t *p;
  966. int first = 0;
  967. int last = conf->raid_disks - 1;
  968. if (mddev->recovery_cp < MaxSector)
  969. /* only hot-add to in-sync arrays, as recovery is
  970. * very different from resync
  971. */
  972. return -EBUSY;
  973. if (!enough(conf))
  974. return -EINVAL;
  975. if (rdev->raid_disk >= 0)
  976. first = last = rdev->raid_disk;
  977. if (rdev->saved_raid_disk >= 0 &&
  978. rdev->saved_raid_disk >= first &&
  979. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  980. mirror = rdev->saved_raid_disk;
  981. else
  982. mirror = first;
  983. for ( ; mirror <= last ; mirror++)
  984. if ( !(p=conf->mirrors+mirror)->rdev) {
  985. disk_stack_limits(mddev->gendisk, rdev->bdev,
  986. rdev->data_offset << 9);
  987. /* as we don't honour merge_bvec_fn, we must
  988. * never risk violating it, so limit
  989. * ->max_segments to one lying with a single
  990. * page, as a one page request is never in
  991. * violation.
  992. */
  993. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  994. blk_queue_max_segments(mddev->queue, 1);
  995. blk_queue_segment_boundary(mddev->queue,
  996. PAGE_CACHE_SIZE - 1);
  997. }
  998. p->head_position = 0;
  999. rdev->raid_disk = mirror;
  1000. err = 0;
  1001. if (rdev->saved_raid_disk != mirror)
  1002. conf->fullsync = 1;
  1003. rcu_assign_pointer(p->rdev, rdev);
  1004. break;
  1005. }
  1006. md_integrity_add_rdev(rdev, mddev);
  1007. print_conf(conf);
  1008. return err;
  1009. }
  1010. static int raid10_remove_disk(mddev_t *mddev, int number)
  1011. {
  1012. conf_t *conf = mddev->private;
  1013. int err = 0;
  1014. mdk_rdev_t *rdev;
  1015. mirror_info_t *p = conf->mirrors+ number;
  1016. print_conf(conf);
  1017. rdev = p->rdev;
  1018. if (rdev) {
  1019. if (test_bit(In_sync, &rdev->flags) ||
  1020. atomic_read(&rdev->nr_pending)) {
  1021. err = -EBUSY;
  1022. goto abort;
  1023. }
  1024. /* Only remove faulty devices in recovery
  1025. * is not possible.
  1026. */
  1027. if (!test_bit(Faulty, &rdev->flags) &&
  1028. enough(conf)) {
  1029. err = -EBUSY;
  1030. goto abort;
  1031. }
  1032. p->rdev = NULL;
  1033. synchronize_rcu();
  1034. if (atomic_read(&rdev->nr_pending)) {
  1035. /* lost the race, try later */
  1036. err = -EBUSY;
  1037. p->rdev = rdev;
  1038. goto abort;
  1039. }
  1040. err = md_integrity_register(mddev);
  1041. }
  1042. abort:
  1043. print_conf(conf);
  1044. return err;
  1045. }
  1046. static void end_sync_read(struct bio *bio, int error)
  1047. {
  1048. r10bio_t *r10_bio = bio->bi_private;
  1049. conf_t *conf = r10_bio->mddev->private;
  1050. int i,d;
  1051. for (i=0; i<conf->copies; i++)
  1052. if (r10_bio->devs[i].bio == bio)
  1053. break;
  1054. BUG_ON(i == conf->copies);
  1055. update_head_pos(i, r10_bio);
  1056. d = r10_bio->devs[i].devnum;
  1057. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1058. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1059. else {
  1060. atomic_add(r10_bio->sectors,
  1061. &conf->mirrors[d].rdev->corrected_errors);
  1062. if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  1063. md_error(r10_bio->mddev,
  1064. conf->mirrors[d].rdev);
  1065. }
  1066. /* for reconstruct, we always reschedule after a read.
  1067. * for resync, only after all reads
  1068. */
  1069. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1070. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1071. atomic_dec_and_test(&r10_bio->remaining)) {
  1072. /* we have read all the blocks,
  1073. * do the comparison in process context in raid10d
  1074. */
  1075. reschedule_retry(r10_bio);
  1076. }
  1077. }
  1078. static void end_sync_write(struct bio *bio, int error)
  1079. {
  1080. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1081. r10bio_t *r10_bio = bio->bi_private;
  1082. mddev_t *mddev = r10_bio->mddev;
  1083. conf_t *conf = mddev->private;
  1084. int i,d;
  1085. for (i = 0; i < conf->copies; i++)
  1086. if (r10_bio->devs[i].bio == bio)
  1087. break;
  1088. d = r10_bio->devs[i].devnum;
  1089. if (!uptodate)
  1090. md_error(mddev, conf->mirrors[d].rdev);
  1091. update_head_pos(i, r10_bio);
  1092. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1093. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1094. if (r10_bio->master_bio == NULL) {
  1095. /* the primary of several recovery bios */
  1096. sector_t s = r10_bio->sectors;
  1097. put_buf(r10_bio);
  1098. md_done_sync(mddev, s, 1);
  1099. break;
  1100. } else {
  1101. r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
  1102. put_buf(r10_bio);
  1103. r10_bio = r10_bio2;
  1104. }
  1105. }
  1106. }
  1107. /*
  1108. * Note: sync and recover and handled very differently for raid10
  1109. * This code is for resync.
  1110. * For resync, we read through virtual addresses and read all blocks.
  1111. * If there is any error, we schedule a write. The lowest numbered
  1112. * drive is authoritative.
  1113. * However requests come for physical address, so we need to map.
  1114. * For every physical address there are raid_disks/copies virtual addresses,
  1115. * which is always are least one, but is not necessarly an integer.
  1116. * This means that a physical address can span multiple chunks, so we may
  1117. * have to submit multiple io requests for a single sync request.
  1118. */
  1119. /*
  1120. * We check if all blocks are in-sync and only write to blocks that
  1121. * aren't in sync
  1122. */
  1123. static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1124. {
  1125. conf_t *conf = mddev->private;
  1126. int i, first;
  1127. struct bio *tbio, *fbio;
  1128. atomic_set(&r10_bio->remaining, 1);
  1129. /* find the first device with a block */
  1130. for (i=0; i<conf->copies; i++)
  1131. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1132. break;
  1133. if (i == conf->copies)
  1134. goto done;
  1135. first = i;
  1136. fbio = r10_bio->devs[i].bio;
  1137. /* now find blocks with errors */
  1138. for (i=0 ; i < conf->copies ; i++) {
  1139. int j, d;
  1140. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1141. tbio = r10_bio->devs[i].bio;
  1142. if (tbio->bi_end_io != end_sync_read)
  1143. continue;
  1144. if (i == first)
  1145. continue;
  1146. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1147. /* We know that the bi_io_vec layout is the same for
  1148. * both 'first' and 'i', so we just compare them.
  1149. * All vec entries are PAGE_SIZE;
  1150. */
  1151. for (j = 0; j < vcnt; j++)
  1152. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1153. page_address(tbio->bi_io_vec[j].bv_page),
  1154. PAGE_SIZE))
  1155. break;
  1156. if (j == vcnt)
  1157. continue;
  1158. mddev->resync_mismatches += r10_bio->sectors;
  1159. }
  1160. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1161. /* Don't fix anything. */
  1162. continue;
  1163. /* Ok, we need to write this bio
  1164. * First we need to fixup bv_offset, bv_len and
  1165. * bi_vecs, as the read request might have corrupted these
  1166. */
  1167. tbio->bi_vcnt = vcnt;
  1168. tbio->bi_size = r10_bio->sectors << 9;
  1169. tbio->bi_idx = 0;
  1170. tbio->bi_phys_segments = 0;
  1171. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1172. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1173. tbio->bi_next = NULL;
  1174. tbio->bi_rw = WRITE;
  1175. tbio->bi_private = r10_bio;
  1176. tbio->bi_sector = r10_bio->devs[i].addr;
  1177. for (j=0; j < vcnt ; j++) {
  1178. tbio->bi_io_vec[j].bv_offset = 0;
  1179. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1180. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1181. page_address(fbio->bi_io_vec[j].bv_page),
  1182. PAGE_SIZE);
  1183. }
  1184. tbio->bi_end_io = end_sync_write;
  1185. d = r10_bio->devs[i].devnum;
  1186. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1187. atomic_inc(&r10_bio->remaining);
  1188. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1189. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1190. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1191. generic_make_request(tbio);
  1192. }
  1193. done:
  1194. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1195. md_done_sync(mddev, r10_bio->sectors, 1);
  1196. put_buf(r10_bio);
  1197. }
  1198. }
  1199. /*
  1200. * Now for the recovery code.
  1201. * Recovery happens across physical sectors.
  1202. * We recover all non-is_sync drives by finding the virtual address of
  1203. * each, and then choose a working drive that also has that virt address.
  1204. * There is a separate r10_bio for each non-in_sync drive.
  1205. * Only the first two slots are in use. The first for reading,
  1206. * The second for writing.
  1207. *
  1208. */
  1209. static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1210. {
  1211. conf_t *conf = mddev->private;
  1212. int i, d;
  1213. struct bio *bio, *wbio;
  1214. /* move the pages across to the second bio
  1215. * and submit the write request
  1216. */
  1217. bio = r10_bio->devs[0].bio;
  1218. wbio = r10_bio->devs[1].bio;
  1219. for (i=0; i < wbio->bi_vcnt; i++) {
  1220. struct page *p = bio->bi_io_vec[i].bv_page;
  1221. bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
  1222. wbio->bi_io_vec[i].bv_page = p;
  1223. }
  1224. d = r10_bio->devs[1].devnum;
  1225. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1226. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1227. if (test_bit(R10BIO_Uptodate, &r10_bio->state))
  1228. generic_make_request(wbio);
  1229. else
  1230. bio_endio(wbio, -EIO);
  1231. }
  1232. /*
  1233. * Used by fix_read_error() to decay the per rdev read_errors.
  1234. * We halve the read error count for every hour that has elapsed
  1235. * since the last recorded read error.
  1236. *
  1237. */
  1238. static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
  1239. {
  1240. struct timespec cur_time_mon;
  1241. unsigned long hours_since_last;
  1242. unsigned int read_errors = atomic_read(&rdev->read_errors);
  1243. ktime_get_ts(&cur_time_mon);
  1244. if (rdev->last_read_error.tv_sec == 0 &&
  1245. rdev->last_read_error.tv_nsec == 0) {
  1246. /* first time we've seen a read error */
  1247. rdev->last_read_error = cur_time_mon;
  1248. return;
  1249. }
  1250. hours_since_last = (cur_time_mon.tv_sec -
  1251. rdev->last_read_error.tv_sec) / 3600;
  1252. rdev->last_read_error = cur_time_mon;
  1253. /*
  1254. * if hours_since_last is > the number of bits in read_errors
  1255. * just set read errors to 0. We do this to avoid
  1256. * overflowing the shift of read_errors by hours_since_last.
  1257. */
  1258. if (hours_since_last >= 8 * sizeof(read_errors))
  1259. atomic_set(&rdev->read_errors, 0);
  1260. else
  1261. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  1262. }
  1263. /*
  1264. * This is a kernel thread which:
  1265. *
  1266. * 1. Retries failed read operations on working mirrors.
  1267. * 2. Updates the raid superblock when problems encounter.
  1268. * 3. Performs writes following reads for array synchronising.
  1269. */
  1270. static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
  1271. {
  1272. int sect = 0; /* Offset from r10_bio->sector */
  1273. int sectors = r10_bio->sectors;
  1274. mdk_rdev_t*rdev;
  1275. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  1276. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  1277. /* still own a reference to this rdev, so it cannot
  1278. * have been cleared recently.
  1279. */
  1280. rdev = conf->mirrors[d].rdev;
  1281. if (test_bit(Faulty, &rdev->flags))
  1282. /* drive has already been failed, just ignore any
  1283. more fix_read_error() attempts */
  1284. return;
  1285. check_decay_read_errors(mddev, rdev);
  1286. atomic_inc(&rdev->read_errors);
  1287. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  1288. char b[BDEVNAME_SIZE];
  1289. bdevname(rdev->bdev, b);
  1290. printk(KERN_NOTICE
  1291. "md/raid10:%s: %s: Raid device exceeded "
  1292. "read_error threshold [cur %d:max %d]\n",
  1293. mdname(mddev), b,
  1294. atomic_read(&rdev->read_errors), max_read_errors);
  1295. printk(KERN_NOTICE
  1296. "md/raid10:%s: %s: Failing raid device\n",
  1297. mdname(mddev), b);
  1298. md_error(mddev, conf->mirrors[d].rdev);
  1299. return;
  1300. }
  1301. while(sectors) {
  1302. int s = sectors;
  1303. int sl = r10_bio->read_slot;
  1304. int success = 0;
  1305. int start;
  1306. if (s > (PAGE_SIZE>>9))
  1307. s = PAGE_SIZE >> 9;
  1308. rcu_read_lock();
  1309. do {
  1310. d = r10_bio->devs[sl].devnum;
  1311. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1312. if (rdev &&
  1313. test_bit(In_sync, &rdev->flags)) {
  1314. atomic_inc(&rdev->nr_pending);
  1315. rcu_read_unlock();
  1316. success = sync_page_io(rdev,
  1317. r10_bio->devs[sl].addr +
  1318. sect,
  1319. s<<9,
  1320. conf->tmppage, READ, false);
  1321. rdev_dec_pending(rdev, mddev);
  1322. rcu_read_lock();
  1323. if (success)
  1324. break;
  1325. }
  1326. sl++;
  1327. if (sl == conf->copies)
  1328. sl = 0;
  1329. } while (!success && sl != r10_bio->read_slot);
  1330. rcu_read_unlock();
  1331. if (!success) {
  1332. /* Cannot read from anywhere -- bye bye array */
  1333. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  1334. md_error(mddev, conf->mirrors[dn].rdev);
  1335. break;
  1336. }
  1337. start = sl;
  1338. /* write it back and re-read */
  1339. rcu_read_lock();
  1340. while (sl != r10_bio->read_slot) {
  1341. char b[BDEVNAME_SIZE];
  1342. if (sl==0)
  1343. sl = conf->copies;
  1344. sl--;
  1345. d = r10_bio->devs[sl].devnum;
  1346. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1347. if (rdev &&
  1348. test_bit(In_sync, &rdev->flags)) {
  1349. atomic_inc(&rdev->nr_pending);
  1350. rcu_read_unlock();
  1351. atomic_add(s, &rdev->corrected_errors);
  1352. if (sync_page_io(rdev,
  1353. r10_bio->devs[sl].addr +
  1354. sect,
  1355. s<<9, conf->tmppage, WRITE, false)
  1356. == 0) {
  1357. /* Well, this device is dead */
  1358. printk(KERN_NOTICE
  1359. "md/raid10:%s: read correction "
  1360. "write failed"
  1361. " (%d sectors at %llu on %s)\n",
  1362. mdname(mddev), s,
  1363. (unsigned long long)(
  1364. sect + rdev->data_offset),
  1365. bdevname(rdev->bdev, b));
  1366. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  1367. "drive\n",
  1368. mdname(mddev),
  1369. bdevname(rdev->bdev, b));
  1370. md_error(mddev, rdev);
  1371. }
  1372. rdev_dec_pending(rdev, mddev);
  1373. rcu_read_lock();
  1374. }
  1375. }
  1376. sl = start;
  1377. while (sl != r10_bio->read_slot) {
  1378. if (sl==0)
  1379. sl = conf->copies;
  1380. sl--;
  1381. d = r10_bio->devs[sl].devnum;
  1382. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1383. if (rdev &&
  1384. test_bit(In_sync, &rdev->flags)) {
  1385. char b[BDEVNAME_SIZE];
  1386. atomic_inc(&rdev->nr_pending);
  1387. rcu_read_unlock();
  1388. if (sync_page_io(rdev,
  1389. r10_bio->devs[sl].addr +
  1390. sect,
  1391. s<<9, conf->tmppage,
  1392. READ, false) == 0) {
  1393. /* Well, this device is dead */
  1394. printk(KERN_NOTICE
  1395. "md/raid10:%s: unable to read back "
  1396. "corrected sectors"
  1397. " (%d sectors at %llu on %s)\n",
  1398. mdname(mddev), s,
  1399. (unsigned long long)(
  1400. sect + rdev->data_offset),
  1401. bdevname(rdev->bdev, b));
  1402. printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
  1403. mdname(mddev),
  1404. bdevname(rdev->bdev, b));
  1405. md_error(mddev, rdev);
  1406. } else {
  1407. printk(KERN_INFO
  1408. "md/raid10:%s: read error corrected"
  1409. " (%d sectors at %llu on %s)\n",
  1410. mdname(mddev), s,
  1411. (unsigned long long)(
  1412. sect + rdev->data_offset),
  1413. bdevname(rdev->bdev, b));
  1414. }
  1415. rdev_dec_pending(rdev, mddev);
  1416. rcu_read_lock();
  1417. }
  1418. }
  1419. rcu_read_unlock();
  1420. sectors -= s;
  1421. sect += s;
  1422. }
  1423. }
  1424. static void raid10d(mddev_t *mddev)
  1425. {
  1426. r10bio_t *r10_bio;
  1427. struct bio *bio;
  1428. unsigned long flags;
  1429. conf_t *conf = mddev->private;
  1430. struct list_head *head = &conf->retry_list;
  1431. mdk_rdev_t *rdev;
  1432. struct blk_plug plug;
  1433. md_check_recovery(mddev);
  1434. blk_start_plug(&plug);
  1435. for (;;) {
  1436. char b[BDEVNAME_SIZE];
  1437. flush_pending_writes(conf);
  1438. spin_lock_irqsave(&conf->device_lock, flags);
  1439. if (list_empty(head)) {
  1440. spin_unlock_irqrestore(&conf->device_lock, flags);
  1441. break;
  1442. }
  1443. r10_bio = list_entry(head->prev, r10bio_t, retry_list);
  1444. list_del(head->prev);
  1445. conf->nr_queued--;
  1446. spin_unlock_irqrestore(&conf->device_lock, flags);
  1447. mddev = r10_bio->mddev;
  1448. conf = mddev->private;
  1449. if (test_bit(R10BIO_IsSync, &r10_bio->state))
  1450. sync_request_write(mddev, r10_bio);
  1451. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  1452. recovery_request_write(mddev, r10_bio);
  1453. else {
  1454. int slot = r10_bio->read_slot;
  1455. int mirror = r10_bio->devs[slot].devnum;
  1456. /* we got a read error. Maybe the drive is bad. Maybe just
  1457. * the block and we can fix it.
  1458. * We freeze all other IO, and try reading the block from
  1459. * other devices. When we find one, we re-write
  1460. * and check it that fixes the read error.
  1461. * This is all done synchronously while the array is
  1462. * frozen.
  1463. */
  1464. if (mddev->ro == 0) {
  1465. freeze_array(conf);
  1466. fix_read_error(conf, mddev, r10_bio);
  1467. unfreeze_array(conf);
  1468. }
  1469. rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
  1470. bio = r10_bio->devs[slot].bio;
  1471. r10_bio->devs[slot].bio =
  1472. mddev->ro ? IO_BLOCKED : NULL;
  1473. mirror = read_balance(conf, r10_bio);
  1474. if (mirror == -1) {
  1475. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  1476. " read error for block %llu\n",
  1477. mdname(mddev),
  1478. bdevname(bio->bi_bdev,b),
  1479. (unsigned long long)r10_bio->sector);
  1480. raid_end_bio_io(r10_bio);
  1481. bio_put(bio);
  1482. } else {
  1483. const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  1484. bio_put(bio);
  1485. slot = r10_bio->read_slot;
  1486. rdev = conf->mirrors[mirror].rdev;
  1487. if (printk_ratelimit())
  1488. printk(KERN_ERR "md/raid10:%s: %s: redirecting sector %llu to"
  1489. " another mirror\n",
  1490. mdname(mddev),
  1491. bdevname(rdev->bdev,b),
  1492. (unsigned long long)r10_bio->sector);
  1493. bio = bio_clone_mddev(r10_bio->master_bio,
  1494. GFP_NOIO, mddev);
  1495. r10_bio->devs[slot].bio = bio;
  1496. bio->bi_sector = r10_bio->devs[slot].addr
  1497. + rdev->data_offset;
  1498. bio->bi_bdev = rdev->bdev;
  1499. bio->bi_rw = READ | do_sync;
  1500. bio->bi_private = r10_bio;
  1501. bio->bi_end_io = raid10_end_read_request;
  1502. generic_make_request(bio);
  1503. }
  1504. }
  1505. cond_resched();
  1506. }
  1507. blk_finish_plug(&plug);
  1508. }
  1509. static int init_resync(conf_t *conf)
  1510. {
  1511. int buffs;
  1512. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1513. BUG_ON(conf->r10buf_pool);
  1514. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  1515. if (!conf->r10buf_pool)
  1516. return -ENOMEM;
  1517. conf->next_resync = 0;
  1518. return 0;
  1519. }
  1520. /*
  1521. * perform a "sync" on one "block"
  1522. *
  1523. * We need to make sure that no normal I/O request - particularly write
  1524. * requests - conflict with active sync requests.
  1525. *
  1526. * This is achieved by tracking pending requests and a 'barrier' concept
  1527. * that can be installed to exclude normal IO requests.
  1528. *
  1529. * Resync and recovery are handled very differently.
  1530. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  1531. *
  1532. * For resync, we iterate over virtual addresses, read all copies,
  1533. * and update if there are differences. If only one copy is live,
  1534. * skip it.
  1535. * For recovery, we iterate over physical addresses, read a good
  1536. * value for each non-in_sync drive, and over-write.
  1537. *
  1538. * So, for recovery we may have several outstanding complex requests for a
  1539. * given address, one for each out-of-sync device. We model this by allocating
  1540. * a number of r10_bio structures, one for each out-of-sync device.
  1541. * As we setup these structures, we collect all bio's together into a list
  1542. * which we then process collectively to add pages, and then process again
  1543. * to pass to generic_make_request.
  1544. *
  1545. * The r10_bio structures are linked using a borrowed master_bio pointer.
  1546. * This link is counted in ->remaining. When the r10_bio that points to NULL
  1547. * has its remaining count decremented to 0, the whole complex operation
  1548. * is complete.
  1549. *
  1550. */
  1551. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
  1552. int *skipped, int go_faster)
  1553. {
  1554. conf_t *conf = mddev->private;
  1555. r10bio_t *r10_bio;
  1556. struct bio *biolist = NULL, *bio;
  1557. sector_t max_sector, nr_sectors;
  1558. int i;
  1559. int max_sync;
  1560. sector_t sync_blocks;
  1561. sector_t sectors_skipped = 0;
  1562. int chunks_skipped = 0;
  1563. if (!conf->r10buf_pool)
  1564. if (init_resync(conf))
  1565. return 0;
  1566. skipped:
  1567. max_sector = mddev->dev_sectors;
  1568. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1569. max_sector = mddev->resync_max_sectors;
  1570. if (sector_nr >= max_sector) {
  1571. /* If we aborted, we need to abort the
  1572. * sync on the 'current' bitmap chucks (there can
  1573. * be several when recovering multiple devices).
  1574. * as we may have started syncing it but not finished.
  1575. * We can find the current address in
  1576. * mddev->curr_resync, but for recovery,
  1577. * we need to convert that to several
  1578. * virtual addresses.
  1579. */
  1580. if (mddev->curr_resync < max_sector) { /* aborted */
  1581. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1582. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1583. &sync_blocks, 1);
  1584. else for (i=0; i<conf->raid_disks; i++) {
  1585. sector_t sect =
  1586. raid10_find_virt(conf, mddev->curr_resync, i);
  1587. bitmap_end_sync(mddev->bitmap, sect,
  1588. &sync_blocks, 1);
  1589. }
  1590. } else /* completed sync */
  1591. conf->fullsync = 0;
  1592. bitmap_close_sync(mddev->bitmap);
  1593. close_sync(conf);
  1594. *skipped = 1;
  1595. return sectors_skipped;
  1596. }
  1597. if (chunks_skipped >= conf->raid_disks) {
  1598. /* if there has been nothing to do on any drive,
  1599. * then there is nothing to do at all..
  1600. */
  1601. *skipped = 1;
  1602. return (max_sector - sector_nr) + sectors_skipped;
  1603. }
  1604. if (max_sector > mddev->resync_max)
  1605. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  1606. /* make sure whole request will fit in a chunk - if chunks
  1607. * are meaningful
  1608. */
  1609. if (conf->near_copies < conf->raid_disks &&
  1610. max_sector > (sector_nr | conf->chunk_mask))
  1611. max_sector = (sector_nr | conf->chunk_mask) + 1;
  1612. /*
  1613. * If there is non-resync activity waiting for us then
  1614. * put in a delay to throttle resync.
  1615. */
  1616. if (!go_faster && conf->nr_waiting)
  1617. msleep_interruptible(1000);
  1618. /* Again, very different code for resync and recovery.
  1619. * Both must result in an r10bio with a list of bios that
  1620. * have bi_end_io, bi_sector, bi_bdev set,
  1621. * and bi_private set to the r10bio.
  1622. * For recovery, we may actually create several r10bios
  1623. * with 2 bios in each, that correspond to the bios in the main one.
  1624. * In this case, the subordinate r10bios link back through a
  1625. * borrowed master_bio pointer, and the counter in the master
  1626. * includes a ref from each subordinate.
  1627. */
  1628. /* First, we decide what to do and set ->bi_end_io
  1629. * To end_sync_read if we want to read, and
  1630. * end_sync_write if we will want to write.
  1631. */
  1632. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  1633. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1634. /* recovery... the complicated one */
  1635. int j, k;
  1636. r10_bio = NULL;
  1637. for (i=0 ; i<conf->raid_disks; i++) {
  1638. int still_degraded;
  1639. r10bio_t *rb2;
  1640. sector_t sect;
  1641. int must_sync;
  1642. if (conf->mirrors[i].rdev == NULL ||
  1643. test_bit(In_sync, &conf->mirrors[i].rdev->flags))
  1644. continue;
  1645. still_degraded = 0;
  1646. /* want to reconstruct this device */
  1647. rb2 = r10_bio;
  1648. sect = raid10_find_virt(conf, sector_nr, i);
  1649. /* Unless we are doing a full sync, we only need
  1650. * to recover the block if it is set in the bitmap
  1651. */
  1652. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1653. &sync_blocks, 1);
  1654. if (sync_blocks < max_sync)
  1655. max_sync = sync_blocks;
  1656. if (!must_sync &&
  1657. !conf->fullsync) {
  1658. /* yep, skip the sync_blocks here, but don't assume
  1659. * that there will never be anything to do here
  1660. */
  1661. chunks_skipped = -1;
  1662. continue;
  1663. }
  1664. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1665. raise_barrier(conf, rb2 != NULL);
  1666. atomic_set(&r10_bio->remaining, 0);
  1667. r10_bio->master_bio = (struct bio*)rb2;
  1668. if (rb2)
  1669. atomic_inc(&rb2->remaining);
  1670. r10_bio->mddev = mddev;
  1671. set_bit(R10BIO_IsRecover, &r10_bio->state);
  1672. r10_bio->sector = sect;
  1673. raid10_find_phys(conf, r10_bio);
  1674. /* Need to check if the array will still be
  1675. * degraded
  1676. */
  1677. for (j=0; j<conf->raid_disks; j++)
  1678. if (conf->mirrors[j].rdev == NULL ||
  1679. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  1680. still_degraded = 1;
  1681. break;
  1682. }
  1683. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1684. &sync_blocks, still_degraded);
  1685. for (j=0; j<conf->copies;j++) {
  1686. int d = r10_bio->devs[j].devnum;
  1687. if (!conf->mirrors[d].rdev ||
  1688. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  1689. continue;
  1690. /* This is where we read from */
  1691. bio = r10_bio->devs[0].bio;
  1692. bio->bi_next = biolist;
  1693. biolist = bio;
  1694. bio->bi_private = r10_bio;
  1695. bio->bi_end_io = end_sync_read;
  1696. bio->bi_rw = READ;
  1697. bio->bi_sector = r10_bio->devs[j].addr +
  1698. conf->mirrors[d].rdev->data_offset;
  1699. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1700. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1701. atomic_inc(&r10_bio->remaining);
  1702. /* and we write to 'i' */
  1703. for (k=0; k<conf->copies; k++)
  1704. if (r10_bio->devs[k].devnum == i)
  1705. break;
  1706. BUG_ON(k == conf->copies);
  1707. bio = r10_bio->devs[1].bio;
  1708. bio->bi_next = biolist;
  1709. biolist = bio;
  1710. bio->bi_private = r10_bio;
  1711. bio->bi_end_io = end_sync_write;
  1712. bio->bi_rw = WRITE;
  1713. bio->bi_sector = r10_bio->devs[k].addr +
  1714. conf->mirrors[i].rdev->data_offset;
  1715. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1716. r10_bio->devs[0].devnum = d;
  1717. r10_bio->devs[1].devnum = i;
  1718. break;
  1719. }
  1720. if (j == conf->copies) {
  1721. /* Cannot recover, so abort the recovery */
  1722. put_buf(r10_bio);
  1723. if (rb2)
  1724. atomic_dec(&rb2->remaining);
  1725. r10_bio = rb2;
  1726. if (!test_and_set_bit(MD_RECOVERY_INTR,
  1727. &mddev->recovery))
  1728. printk(KERN_INFO "md/raid10:%s: insufficient "
  1729. "working devices for recovery.\n",
  1730. mdname(mddev));
  1731. break;
  1732. }
  1733. }
  1734. if (biolist == NULL) {
  1735. while (r10_bio) {
  1736. r10bio_t *rb2 = r10_bio;
  1737. r10_bio = (r10bio_t*) rb2->master_bio;
  1738. rb2->master_bio = NULL;
  1739. put_buf(rb2);
  1740. }
  1741. goto giveup;
  1742. }
  1743. } else {
  1744. /* resync. Schedule a read for every block at this virt offset */
  1745. int count = 0;
  1746. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  1747. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1748. &sync_blocks, mddev->degraded) &&
  1749. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  1750. &mddev->recovery)) {
  1751. /* We can skip this block */
  1752. *skipped = 1;
  1753. return sync_blocks + sectors_skipped;
  1754. }
  1755. if (sync_blocks < max_sync)
  1756. max_sync = sync_blocks;
  1757. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1758. r10_bio->mddev = mddev;
  1759. atomic_set(&r10_bio->remaining, 0);
  1760. raise_barrier(conf, 0);
  1761. conf->next_resync = sector_nr;
  1762. r10_bio->master_bio = NULL;
  1763. r10_bio->sector = sector_nr;
  1764. set_bit(R10BIO_IsSync, &r10_bio->state);
  1765. raid10_find_phys(conf, r10_bio);
  1766. r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
  1767. for (i=0; i<conf->copies; i++) {
  1768. int d = r10_bio->devs[i].devnum;
  1769. bio = r10_bio->devs[i].bio;
  1770. bio->bi_end_io = NULL;
  1771. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1772. if (conf->mirrors[d].rdev == NULL ||
  1773. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  1774. continue;
  1775. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1776. atomic_inc(&r10_bio->remaining);
  1777. bio->bi_next = biolist;
  1778. biolist = bio;
  1779. bio->bi_private = r10_bio;
  1780. bio->bi_end_io = end_sync_read;
  1781. bio->bi_rw = READ;
  1782. bio->bi_sector = r10_bio->devs[i].addr +
  1783. conf->mirrors[d].rdev->data_offset;
  1784. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1785. count++;
  1786. }
  1787. if (count < 2) {
  1788. for (i=0; i<conf->copies; i++) {
  1789. int d = r10_bio->devs[i].devnum;
  1790. if (r10_bio->devs[i].bio->bi_end_io)
  1791. rdev_dec_pending(conf->mirrors[d].rdev,
  1792. mddev);
  1793. }
  1794. put_buf(r10_bio);
  1795. biolist = NULL;
  1796. goto giveup;
  1797. }
  1798. }
  1799. for (bio = biolist; bio ; bio=bio->bi_next) {
  1800. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1801. if (bio->bi_end_io)
  1802. bio->bi_flags |= 1 << BIO_UPTODATE;
  1803. bio->bi_vcnt = 0;
  1804. bio->bi_idx = 0;
  1805. bio->bi_phys_segments = 0;
  1806. bio->bi_size = 0;
  1807. }
  1808. nr_sectors = 0;
  1809. if (sector_nr + max_sync < max_sector)
  1810. max_sector = sector_nr + max_sync;
  1811. do {
  1812. struct page *page;
  1813. int len = PAGE_SIZE;
  1814. if (sector_nr + (len>>9) > max_sector)
  1815. len = (max_sector - sector_nr) << 9;
  1816. if (len == 0)
  1817. break;
  1818. for (bio= biolist ; bio ; bio=bio->bi_next) {
  1819. struct bio *bio2;
  1820. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  1821. if (bio_add_page(bio, page, len, 0))
  1822. continue;
  1823. /* stop here */
  1824. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1825. for (bio2 = biolist;
  1826. bio2 && bio2 != bio;
  1827. bio2 = bio2->bi_next) {
  1828. /* remove last page from this bio */
  1829. bio2->bi_vcnt--;
  1830. bio2->bi_size -= len;
  1831. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  1832. }
  1833. goto bio_full;
  1834. }
  1835. nr_sectors += len>>9;
  1836. sector_nr += len>>9;
  1837. } while (biolist->bi_vcnt < RESYNC_PAGES);
  1838. bio_full:
  1839. r10_bio->sectors = nr_sectors;
  1840. while (biolist) {
  1841. bio = biolist;
  1842. biolist = biolist->bi_next;
  1843. bio->bi_next = NULL;
  1844. r10_bio = bio->bi_private;
  1845. r10_bio->sectors = nr_sectors;
  1846. if (bio->bi_end_io == end_sync_read) {
  1847. md_sync_acct(bio->bi_bdev, nr_sectors);
  1848. generic_make_request(bio);
  1849. }
  1850. }
  1851. if (sectors_skipped)
  1852. /* pretend they weren't skipped, it makes
  1853. * no important difference in this case
  1854. */
  1855. md_done_sync(mddev, sectors_skipped, 1);
  1856. return sectors_skipped + nr_sectors;
  1857. giveup:
  1858. /* There is nowhere to write, so all non-sync
  1859. * drives must be failed, so try the next chunk...
  1860. */
  1861. if (sector_nr + max_sync < max_sector)
  1862. max_sector = sector_nr + max_sync;
  1863. sectors_skipped += (max_sector - sector_nr);
  1864. chunks_skipped ++;
  1865. sector_nr = max_sector;
  1866. goto skipped;
  1867. }
  1868. static sector_t
  1869. raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  1870. {
  1871. sector_t size;
  1872. conf_t *conf = mddev->private;
  1873. if (!raid_disks)
  1874. raid_disks = conf->raid_disks;
  1875. if (!sectors)
  1876. sectors = conf->dev_sectors;
  1877. size = sectors >> conf->chunk_shift;
  1878. sector_div(size, conf->far_copies);
  1879. size = size * raid_disks;
  1880. sector_div(size, conf->near_copies);
  1881. return size << conf->chunk_shift;
  1882. }
  1883. static conf_t *setup_conf(mddev_t *mddev)
  1884. {
  1885. conf_t *conf = NULL;
  1886. int nc, fc, fo;
  1887. sector_t stride, size;
  1888. int err = -EINVAL;
  1889. if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
  1890. !is_power_of_2(mddev->new_chunk_sectors)) {
  1891. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  1892. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  1893. mdname(mddev), PAGE_SIZE);
  1894. goto out;
  1895. }
  1896. nc = mddev->new_layout & 255;
  1897. fc = (mddev->new_layout >> 8) & 255;
  1898. fo = mddev->new_layout & (1<<16);
  1899. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  1900. (mddev->new_layout >> 17)) {
  1901. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  1902. mdname(mddev), mddev->new_layout);
  1903. goto out;
  1904. }
  1905. err = -ENOMEM;
  1906. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  1907. if (!conf)
  1908. goto out;
  1909. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1910. GFP_KERNEL);
  1911. if (!conf->mirrors)
  1912. goto out;
  1913. conf->tmppage = alloc_page(GFP_KERNEL);
  1914. if (!conf->tmppage)
  1915. goto out;
  1916. conf->raid_disks = mddev->raid_disks;
  1917. conf->near_copies = nc;
  1918. conf->far_copies = fc;
  1919. conf->copies = nc*fc;
  1920. conf->far_offset = fo;
  1921. conf->chunk_mask = mddev->new_chunk_sectors - 1;
  1922. conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
  1923. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  1924. r10bio_pool_free, conf);
  1925. if (!conf->r10bio_pool)
  1926. goto out;
  1927. size = mddev->dev_sectors >> conf->chunk_shift;
  1928. sector_div(size, fc);
  1929. size = size * conf->raid_disks;
  1930. sector_div(size, nc);
  1931. /* 'size' is now the number of chunks in the array */
  1932. /* calculate "used chunks per device" in 'stride' */
  1933. stride = size * conf->copies;
  1934. /* We need to round up when dividing by raid_disks to
  1935. * get the stride size.
  1936. */
  1937. stride += conf->raid_disks - 1;
  1938. sector_div(stride, conf->raid_disks);
  1939. conf->dev_sectors = stride << conf->chunk_shift;
  1940. if (fo)
  1941. stride = 1;
  1942. else
  1943. sector_div(stride, fc);
  1944. conf->stride = stride << conf->chunk_shift;
  1945. spin_lock_init(&conf->device_lock);
  1946. INIT_LIST_HEAD(&conf->retry_list);
  1947. spin_lock_init(&conf->resync_lock);
  1948. init_waitqueue_head(&conf->wait_barrier);
  1949. conf->thread = md_register_thread(raid10d, mddev, NULL);
  1950. if (!conf->thread)
  1951. goto out;
  1952. conf->mddev = mddev;
  1953. return conf;
  1954. out:
  1955. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  1956. mdname(mddev));
  1957. if (conf) {
  1958. if (conf->r10bio_pool)
  1959. mempool_destroy(conf->r10bio_pool);
  1960. kfree(conf->mirrors);
  1961. safe_put_page(conf->tmppage);
  1962. kfree(conf);
  1963. }
  1964. return ERR_PTR(err);
  1965. }
  1966. static int run(mddev_t *mddev)
  1967. {
  1968. conf_t *conf;
  1969. int i, disk_idx, chunk_size;
  1970. mirror_info_t *disk;
  1971. mdk_rdev_t *rdev;
  1972. sector_t size;
  1973. /*
  1974. * copy the already verified devices into our private RAID10
  1975. * bookkeeping area. [whatever we allocate in run(),
  1976. * should be freed in stop()]
  1977. */
  1978. if (mddev->private == NULL) {
  1979. conf = setup_conf(mddev);
  1980. if (IS_ERR(conf))
  1981. return PTR_ERR(conf);
  1982. mddev->private = conf;
  1983. }
  1984. conf = mddev->private;
  1985. if (!conf)
  1986. goto out;
  1987. mddev->thread = conf->thread;
  1988. conf->thread = NULL;
  1989. chunk_size = mddev->chunk_sectors << 9;
  1990. blk_queue_io_min(mddev->queue, chunk_size);
  1991. if (conf->raid_disks % conf->near_copies)
  1992. blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
  1993. else
  1994. blk_queue_io_opt(mddev->queue, chunk_size *
  1995. (conf->raid_disks / conf->near_copies));
  1996. list_for_each_entry(rdev, &mddev->disks, same_set) {
  1997. disk_idx = rdev->raid_disk;
  1998. if (disk_idx >= conf->raid_disks
  1999. || disk_idx < 0)
  2000. continue;
  2001. disk = conf->mirrors + disk_idx;
  2002. disk->rdev = rdev;
  2003. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2004. rdev->data_offset << 9);
  2005. /* as we don't honour merge_bvec_fn, we must never risk
  2006. * violating it, so limit max_segments to 1 lying
  2007. * within a single page.
  2008. */
  2009. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  2010. blk_queue_max_segments(mddev->queue, 1);
  2011. blk_queue_segment_boundary(mddev->queue,
  2012. PAGE_CACHE_SIZE - 1);
  2013. }
  2014. disk->head_position = 0;
  2015. }
  2016. /* need to check that every block has at least one working mirror */
  2017. if (!enough(conf)) {
  2018. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  2019. mdname(mddev));
  2020. goto out_free_conf;
  2021. }
  2022. mddev->degraded = 0;
  2023. for (i = 0; i < conf->raid_disks; i++) {
  2024. disk = conf->mirrors + i;
  2025. if (!disk->rdev ||
  2026. !test_bit(In_sync, &disk->rdev->flags)) {
  2027. disk->head_position = 0;
  2028. mddev->degraded++;
  2029. if (disk->rdev)
  2030. conf->fullsync = 1;
  2031. }
  2032. }
  2033. if (mddev->recovery_cp != MaxSector)
  2034. printk(KERN_NOTICE "md/raid10:%s: not clean"
  2035. " -- starting background reconstruction\n",
  2036. mdname(mddev));
  2037. printk(KERN_INFO
  2038. "md/raid10:%s: active with %d out of %d devices\n",
  2039. mdname(mddev), conf->raid_disks - mddev->degraded,
  2040. conf->raid_disks);
  2041. /*
  2042. * Ok, everything is just fine now
  2043. */
  2044. mddev->dev_sectors = conf->dev_sectors;
  2045. size = raid10_size(mddev, 0, 0);
  2046. md_set_array_sectors(mddev, size);
  2047. mddev->resync_max_sectors = size;
  2048. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  2049. mddev->queue->backing_dev_info.congested_data = mddev;
  2050. /* Calculate max read-ahead size.
  2051. * We need to readahead at least twice a whole stripe....
  2052. * maybe...
  2053. */
  2054. {
  2055. int stripe = conf->raid_disks *
  2056. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  2057. stripe /= conf->near_copies;
  2058. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  2059. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  2060. }
  2061. if (conf->near_copies < conf->raid_disks)
  2062. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  2063. if (md_integrity_register(mddev))
  2064. goto out_free_conf;
  2065. return 0;
  2066. out_free_conf:
  2067. md_unregister_thread(&mddev->thread);
  2068. if (conf->r10bio_pool)
  2069. mempool_destroy(conf->r10bio_pool);
  2070. safe_put_page(conf->tmppage);
  2071. kfree(conf->mirrors);
  2072. kfree(conf);
  2073. mddev->private = NULL;
  2074. out:
  2075. return -EIO;
  2076. }
  2077. static int stop(mddev_t *mddev)
  2078. {
  2079. conf_t *conf = mddev->private;
  2080. raise_barrier(conf, 0);
  2081. lower_barrier(conf);
  2082. md_unregister_thread(&mddev->thread);
  2083. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  2084. if (conf->r10bio_pool)
  2085. mempool_destroy(conf->r10bio_pool);
  2086. kfree(conf->mirrors);
  2087. kfree(conf);
  2088. mddev->private = NULL;
  2089. return 0;
  2090. }
  2091. static void raid10_quiesce(mddev_t *mddev, int state)
  2092. {
  2093. conf_t *conf = mddev->private;
  2094. switch(state) {
  2095. case 1:
  2096. raise_barrier(conf, 0);
  2097. break;
  2098. case 0:
  2099. lower_barrier(conf);
  2100. break;
  2101. }
  2102. }
  2103. static void *raid10_takeover_raid0(mddev_t *mddev)
  2104. {
  2105. mdk_rdev_t *rdev;
  2106. conf_t *conf;
  2107. if (mddev->degraded > 0) {
  2108. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  2109. mdname(mddev));
  2110. return ERR_PTR(-EINVAL);
  2111. }
  2112. /* Set new parameters */
  2113. mddev->new_level = 10;
  2114. /* new layout: far_copies = 1, near_copies = 2 */
  2115. mddev->new_layout = (1<<8) + 2;
  2116. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2117. mddev->delta_disks = mddev->raid_disks;
  2118. mddev->raid_disks *= 2;
  2119. /* make sure it will be not marked as dirty */
  2120. mddev->recovery_cp = MaxSector;
  2121. conf = setup_conf(mddev);
  2122. if (!IS_ERR(conf)) {
  2123. list_for_each_entry(rdev, &mddev->disks, same_set)
  2124. if (rdev->raid_disk >= 0)
  2125. rdev->new_raid_disk = rdev->raid_disk * 2;
  2126. conf->barrier = 1;
  2127. }
  2128. return conf;
  2129. }
  2130. static void *raid10_takeover(mddev_t *mddev)
  2131. {
  2132. struct raid0_private_data *raid0_priv;
  2133. /* raid10 can take over:
  2134. * raid0 - providing it has only two drives
  2135. */
  2136. if (mddev->level == 0) {
  2137. /* for raid0 takeover only one zone is supported */
  2138. raid0_priv = mddev->private;
  2139. if (raid0_priv->nr_strip_zones > 1) {
  2140. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  2141. " with more than one zone.\n",
  2142. mdname(mddev));
  2143. return ERR_PTR(-EINVAL);
  2144. }
  2145. return raid10_takeover_raid0(mddev);
  2146. }
  2147. return ERR_PTR(-EINVAL);
  2148. }
  2149. static struct mdk_personality raid10_personality =
  2150. {
  2151. .name = "raid10",
  2152. .level = 10,
  2153. .owner = THIS_MODULE,
  2154. .make_request = make_request,
  2155. .run = run,
  2156. .stop = stop,
  2157. .status = status,
  2158. .error_handler = error,
  2159. .hot_add_disk = raid10_add_disk,
  2160. .hot_remove_disk= raid10_remove_disk,
  2161. .spare_active = raid10_spare_active,
  2162. .sync_request = sync_request,
  2163. .quiesce = raid10_quiesce,
  2164. .size = raid10_size,
  2165. .takeover = raid10_takeover,
  2166. };
  2167. static int __init raid_init(void)
  2168. {
  2169. return register_md_personality(&raid10_personality);
  2170. }
  2171. static void raid_exit(void)
  2172. {
  2173. unregister_md_personality(&raid10_personality);
  2174. }
  2175. module_init(raid_init);
  2176. module_exit(raid_exit);
  2177. MODULE_LICENSE("GPL");
  2178. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  2179. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  2180. MODULE_ALIAS("md-raid10");
  2181. MODULE_ALIAS("md-level-10");