raid10.c 66 KB

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