raid10.c 65 KB

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