raid10.c 67 KB

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