raid1.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200
  1. /*
  2. * raid1.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
  5. *
  6. * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  7. *
  8. * RAID-1 management functions.
  9. *
  10. * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
  11. *
  12. * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
  13. * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
  14. *
  15. * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
  16. * bitmapped intelligence in resync:
  17. *
  18. * - bitmap marked during normal i/o
  19. * - bitmap used to skip nondirty blocks during sync
  20. *
  21. * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
  22. * - persistent bitmap code
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2, or (at your option)
  27. * any later version.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * (for example /usr/src/linux/COPYING); if not, write to the Free
  31. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/slab.h>
  34. #include <linux/delay.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/module.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/ratelimit.h>
  39. #include "md.h"
  40. #include "raid1.h"
  41. #include "bitmap.h"
  42. /*
  43. * Number of guaranteed r1bios in case of extreme VM load:
  44. */
  45. #define NR_RAID1_BIOS 256
  46. /* when we get a read error on a read-only array, we redirect to another
  47. * device without failing the first device, or trying to over-write to
  48. * correct the read error. To keep track of bad blocks on a per-bio
  49. * level, we store IO_BLOCKED in the appropriate 'bios' pointer
  50. */
  51. #define IO_BLOCKED ((struct bio *)1)
  52. /* When we successfully write to a known bad-block, we need to remove the
  53. * bad-block marking which must be done from process context. So we record
  54. * the success by setting devs[n].bio to IO_MADE_GOOD
  55. */
  56. #define IO_MADE_GOOD ((struct bio *)2)
  57. #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
  58. /* When there are this many requests queue to be written by
  59. * the raid1 thread, we become 'congested' to provide back-pressure
  60. * for writeback.
  61. */
  62. static int max_queued_requests = 1024;
  63. static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
  64. sector_t bi_sector);
  65. static void lower_barrier(struct r1conf *conf);
  66. static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
  67. {
  68. struct pool_info *pi = data;
  69. int size = offsetof(struct r1bio, bios[pi->raid_disks]);
  70. /* allocate a r1bio with room for raid_disks entries in the bios array */
  71. return kzalloc(size, gfp_flags);
  72. }
  73. static void r1bio_pool_free(void *r1_bio, void *data)
  74. {
  75. kfree(r1_bio);
  76. }
  77. #define RESYNC_BLOCK_SIZE (64*1024)
  78. #define RESYNC_DEPTH 32
  79. #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
  80. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  81. #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
  82. #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
  83. #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS)
  84. static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
  85. {
  86. struct pool_info *pi = data;
  87. struct r1bio *r1_bio;
  88. struct bio *bio;
  89. int need_pages;
  90. int i, j;
  91. r1_bio = r1bio_pool_alloc(gfp_flags, pi);
  92. if (!r1_bio)
  93. return NULL;
  94. /*
  95. * Allocate bios : 1 for reading, n-1 for writing
  96. */
  97. for (j = pi->raid_disks ; j-- ; ) {
  98. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  99. if (!bio)
  100. goto out_free_bio;
  101. r1_bio->bios[j] = bio;
  102. }
  103. /*
  104. * Allocate RESYNC_PAGES data pages and attach them to
  105. * the first bio.
  106. * If this is a user-requested check/repair, allocate
  107. * RESYNC_PAGES for each bio.
  108. */
  109. if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
  110. need_pages = pi->raid_disks;
  111. else
  112. need_pages = 1;
  113. for (j = 0; j < need_pages; j++) {
  114. bio = r1_bio->bios[j];
  115. bio->bi_vcnt = RESYNC_PAGES;
  116. if (bio_alloc_pages(bio, gfp_flags))
  117. goto out_free_pages;
  118. }
  119. /* If not user-requests, copy the page pointers to all bios */
  120. if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
  121. for (i=0; i<RESYNC_PAGES ; i++)
  122. for (j=1; j<pi->raid_disks; j++)
  123. r1_bio->bios[j]->bi_io_vec[i].bv_page =
  124. r1_bio->bios[0]->bi_io_vec[i].bv_page;
  125. }
  126. r1_bio->master_bio = NULL;
  127. return r1_bio;
  128. out_free_pages:
  129. while (--j >= 0) {
  130. struct bio_vec *bv;
  131. bio_for_each_segment_all(bv, r1_bio->bios[j], i)
  132. __free_page(bv->bv_page);
  133. }
  134. out_free_bio:
  135. while (++j < pi->raid_disks)
  136. bio_put(r1_bio->bios[j]);
  137. r1bio_pool_free(r1_bio, data);
  138. return NULL;
  139. }
  140. static void r1buf_pool_free(void *__r1_bio, void *data)
  141. {
  142. struct pool_info *pi = data;
  143. int i,j;
  144. struct r1bio *r1bio = __r1_bio;
  145. for (i = 0; i < RESYNC_PAGES; i++)
  146. for (j = pi->raid_disks; j-- ;) {
  147. if (j == 0 ||
  148. r1bio->bios[j]->bi_io_vec[i].bv_page !=
  149. r1bio->bios[0]->bi_io_vec[i].bv_page)
  150. safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
  151. }
  152. for (i=0 ; i < pi->raid_disks; i++)
  153. bio_put(r1bio->bios[i]);
  154. r1bio_pool_free(r1bio, data);
  155. }
  156. static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
  157. {
  158. int i;
  159. for (i = 0; i < conf->raid_disks * 2; i++) {
  160. struct bio **bio = r1_bio->bios + i;
  161. if (!BIO_SPECIAL(*bio))
  162. bio_put(*bio);
  163. *bio = NULL;
  164. }
  165. }
  166. static void free_r1bio(struct r1bio *r1_bio)
  167. {
  168. struct r1conf *conf = r1_bio->mddev->private;
  169. put_all_bios(conf, r1_bio);
  170. mempool_free(r1_bio, conf->r1bio_pool);
  171. }
  172. static void put_buf(struct r1bio *r1_bio)
  173. {
  174. struct r1conf *conf = r1_bio->mddev->private;
  175. int i;
  176. for (i = 0; i < conf->raid_disks * 2; i++) {
  177. struct bio *bio = r1_bio->bios[i];
  178. if (bio->bi_end_io)
  179. rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
  180. }
  181. mempool_free(r1_bio, conf->r1buf_pool);
  182. lower_barrier(conf);
  183. }
  184. static void reschedule_retry(struct r1bio *r1_bio)
  185. {
  186. unsigned long flags;
  187. struct mddev *mddev = r1_bio->mddev;
  188. struct r1conf *conf = mddev->private;
  189. spin_lock_irqsave(&conf->device_lock, flags);
  190. list_add(&r1_bio->retry_list, &conf->retry_list);
  191. conf->nr_queued ++;
  192. spin_unlock_irqrestore(&conf->device_lock, flags);
  193. wake_up(&conf->wait_barrier);
  194. md_wakeup_thread(mddev->thread);
  195. }
  196. /*
  197. * raid_end_bio_io() is called when we have finished servicing a mirrored
  198. * operation and are ready to return a success/failure code to the buffer
  199. * cache layer.
  200. */
  201. static void call_bio_endio(struct r1bio *r1_bio)
  202. {
  203. struct bio *bio = r1_bio->master_bio;
  204. int done;
  205. struct r1conf *conf = r1_bio->mddev->private;
  206. sector_t start_next_window = r1_bio->start_next_window;
  207. sector_t bi_sector = bio->bi_iter.bi_sector;
  208. if (bio->bi_phys_segments) {
  209. unsigned long flags;
  210. spin_lock_irqsave(&conf->device_lock, flags);
  211. bio->bi_phys_segments--;
  212. done = (bio->bi_phys_segments == 0);
  213. spin_unlock_irqrestore(&conf->device_lock, flags);
  214. /*
  215. * make_request() might be waiting for
  216. * bi_phys_segments to decrease
  217. */
  218. wake_up(&conf->wait_barrier);
  219. } else
  220. done = 1;
  221. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  222. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  223. if (done) {
  224. bio_endio(bio, 0);
  225. /*
  226. * Wake up any possible resync thread that waits for the device
  227. * to go idle.
  228. */
  229. allow_barrier(conf, start_next_window, bi_sector);
  230. }
  231. }
  232. static void raid_end_bio_io(struct r1bio *r1_bio)
  233. {
  234. struct bio *bio = r1_bio->master_bio;
  235. /* if nobody has done the final endio yet, do it now */
  236. if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
  237. pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
  238. (bio_data_dir(bio) == WRITE) ? "write" : "read",
  239. (unsigned long long) bio->bi_iter.bi_sector,
  240. (unsigned long long) bio_end_sector(bio) - 1);
  241. call_bio_endio(r1_bio);
  242. }
  243. free_r1bio(r1_bio);
  244. }
  245. /*
  246. * Update disk head position estimator based on IRQ completion info.
  247. */
  248. static inline void update_head_pos(int disk, struct r1bio *r1_bio)
  249. {
  250. struct r1conf *conf = r1_bio->mddev->private;
  251. conf->mirrors[disk].head_position =
  252. r1_bio->sector + (r1_bio->sectors);
  253. }
  254. /*
  255. * Find the disk number which triggered given bio
  256. */
  257. static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
  258. {
  259. int mirror;
  260. struct r1conf *conf = r1_bio->mddev->private;
  261. int raid_disks = conf->raid_disks;
  262. for (mirror = 0; mirror < raid_disks * 2; mirror++)
  263. if (r1_bio->bios[mirror] == bio)
  264. break;
  265. BUG_ON(mirror == raid_disks * 2);
  266. update_head_pos(mirror, r1_bio);
  267. return mirror;
  268. }
  269. static void raid1_end_read_request(struct bio *bio, int error)
  270. {
  271. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  272. struct r1bio *r1_bio = bio->bi_private;
  273. int mirror;
  274. struct r1conf *conf = r1_bio->mddev->private;
  275. mirror = r1_bio->read_disk;
  276. /*
  277. * this branch is our 'one mirror IO has finished' event handler:
  278. */
  279. update_head_pos(mirror, r1_bio);
  280. if (uptodate)
  281. set_bit(R1BIO_Uptodate, &r1_bio->state);
  282. else {
  283. /* If all other devices have failed, we want to return
  284. * the error upwards rather than fail the last device.
  285. * Here we redefine "uptodate" to mean "Don't want to retry"
  286. */
  287. unsigned long flags;
  288. spin_lock_irqsave(&conf->device_lock, flags);
  289. if (r1_bio->mddev->degraded == conf->raid_disks ||
  290. (r1_bio->mddev->degraded == conf->raid_disks-1 &&
  291. test_bit(In_sync, &conf->mirrors[mirror].rdev->flags)))
  292. uptodate = 1;
  293. spin_unlock_irqrestore(&conf->device_lock, flags);
  294. }
  295. if (uptodate) {
  296. raid_end_bio_io(r1_bio);
  297. rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
  298. } else {
  299. /*
  300. * oops, read error:
  301. */
  302. char b[BDEVNAME_SIZE];
  303. printk_ratelimited(
  304. KERN_ERR "md/raid1:%s: %s: "
  305. "rescheduling sector %llu\n",
  306. mdname(conf->mddev),
  307. bdevname(conf->mirrors[mirror].rdev->bdev,
  308. b),
  309. (unsigned long long)r1_bio->sector);
  310. set_bit(R1BIO_ReadError, &r1_bio->state);
  311. reschedule_retry(r1_bio);
  312. /* don't drop the reference on read_disk yet */
  313. }
  314. }
  315. static void close_write(struct r1bio *r1_bio)
  316. {
  317. /* it really is the end of this request */
  318. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  319. /* free extra copy of the data pages */
  320. int i = r1_bio->behind_page_count;
  321. while (i--)
  322. safe_put_page(r1_bio->behind_bvecs[i].bv_page);
  323. kfree(r1_bio->behind_bvecs);
  324. r1_bio->behind_bvecs = NULL;
  325. }
  326. /* clear the bitmap if all writes complete successfully */
  327. bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
  328. r1_bio->sectors,
  329. !test_bit(R1BIO_Degraded, &r1_bio->state),
  330. test_bit(R1BIO_BehindIO, &r1_bio->state));
  331. md_write_end(r1_bio->mddev);
  332. }
  333. static void r1_bio_write_done(struct r1bio *r1_bio)
  334. {
  335. if (!atomic_dec_and_test(&r1_bio->remaining))
  336. return;
  337. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  338. reschedule_retry(r1_bio);
  339. else {
  340. close_write(r1_bio);
  341. if (test_bit(R1BIO_MadeGood, &r1_bio->state))
  342. reschedule_retry(r1_bio);
  343. else
  344. raid_end_bio_io(r1_bio);
  345. }
  346. }
  347. static void raid1_end_write_request(struct bio *bio, int error)
  348. {
  349. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  350. struct r1bio *r1_bio = bio->bi_private;
  351. int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
  352. struct r1conf *conf = r1_bio->mddev->private;
  353. struct bio *to_put = NULL;
  354. mirror = find_bio_disk(r1_bio, bio);
  355. /*
  356. * 'one mirror IO has finished' event handler:
  357. */
  358. if (!uptodate) {
  359. set_bit(WriteErrorSeen,
  360. &conf->mirrors[mirror].rdev->flags);
  361. if (!test_and_set_bit(WantReplacement,
  362. &conf->mirrors[mirror].rdev->flags))
  363. set_bit(MD_RECOVERY_NEEDED, &
  364. conf->mddev->recovery);
  365. set_bit(R1BIO_WriteError, &r1_bio->state);
  366. } else {
  367. /*
  368. * Set R1BIO_Uptodate in our master bio, so that we
  369. * will return a good error code for to the higher
  370. * levels even if IO on some other mirrored buffer
  371. * fails.
  372. *
  373. * The 'master' represents the composite IO operation
  374. * to user-side. So if something waits for IO, then it
  375. * will wait for the 'master' bio.
  376. */
  377. sector_t first_bad;
  378. int bad_sectors;
  379. r1_bio->bios[mirror] = NULL;
  380. to_put = bio;
  381. /*
  382. * Do not set R1BIO_Uptodate if the current device is
  383. * rebuilding or Faulty. This is because we cannot use
  384. * such device for properly reading the data back (we could
  385. * potentially use it, if the current write would have felt
  386. * before rdev->recovery_offset, but for simplicity we don't
  387. * check this here.
  388. */
  389. if (test_bit(In_sync, &conf->mirrors[mirror].rdev->flags) &&
  390. !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags))
  391. set_bit(R1BIO_Uptodate, &r1_bio->state);
  392. /* Maybe we can clear some bad blocks. */
  393. if (is_badblock(conf->mirrors[mirror].rdev,
  394. r1_bio->sector, r1_bio->sectors,
  395. &first_bad, &bad_sectors)) {
  396. r1_bio->bios[mirror] = IO_MADE_GOOD;
  397. set_bit(R1BIO_MadeGood, &r1_bio->state);
  398. }
  399. }
  400. if (behind) {
  401. if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
  402. atomic_dec(&r1_bio->behind_remaining);
  403. /*
  404. * In behind mode, we ACK the master bio once the I/O
  405. * has safely reached all non-writemostly
  406. * disks. Setting the Returned bit ensures that this
  407. * gets done only once -- we don't ever want to return
  408. * -EIO here, instead we'll wait
  409. */
  410. if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
  411. test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  412. /* Maybe we can return now */
  413. if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
  414. struct bio *mbio = r1_bio->master_bio;
  415. pr_debug("raid1: behind end write sectors"
  416. " %llu-%llu\n",
  417. (unsigned long long) mbio->bi_iter.bi_sector,
  418. (unsigned long long) bio_end_sector(mbio) - 1);
  419. call_bio_endio(r1_bio);
  420. }
  421. }
  422. }
  423. if (r1_bio->bios[mirror] == NULL)
  424. rdev_dec_pending(conf->mirrors[mirror].rdev,
  425. conf->mddev);
  426. /*
  427. * Let's see if all mirrored write operations have finished
  428. * already.
  429. */
  430. r1_bio_write_done(r1_bio);
  431. if (to_put)
  432. bio_put(to_put);
  433. }
  434. /*
  435. * This routine returns the disk from which the requested read should
  436. * be done. There is a per-array 'next expected sequential IO' sector
  437. * number - if this matches on the next IO then we use the last disk.
  438. * There is also a per-disk 'last know head position' sector that is
  439. * maintained from IRQ contexts, both the normal and the resync IO
  440. * completion handlers update this position correctly. If there is no
  441. * perfect sequential match then we pick the disk whose head is closest.
  442. *
  443. * If there are 2 mirrors in the same 2 devices, performance degrades
  444. * because position is mirror, not device based.
  445. *
  446. * The rdev for the device selected will have nr_pending incremented.
  447. */
  448. static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
  449. {
  450. const sector_t this_sector = r1_bio->sector;
  451. int sectors;
  452. int best_good_sectors;
  453. int best_disk, best_dist_disk, best_pending_disk;
  454. int has_nonrot_disk;
  455. int disk;
  456. sector_t best_dist;
  457. unsigned int min_pending;
  458. struct md_rdev *rdev;
  459. int choose_first;
  460. int choose_next_idle;
  461. rcu_read_lock();
  462. /*
  463. * Check if we can balance. We can balance on the whole
  464. * device if no resync is going on, or below the resync window.
  465. * We take the first readable disk when above the resync window.
  466. */
  467. retry:
  468. sectors = r1_bio->sectors;
  469. best_disk = -1;
  470. best_dist_disk = -1;
  471. best_dist = MaxSector;
  472. best_pending_disk = -1;
  473. min_pending = UINT_MAX;
  474. best_good_sectors = 0;
  475. has_nonrot_disk = 0;
  476. choose_next_idle = 0;
  477. if ((conf->mddev->recovery_cp < this_sector + sectors) ||
  478. (mddev_is_clustered(conf->mddev) &&
  479. md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
  480. this_sector + sectors)))
  481. choose_first = 1;
  482. else
  483. choose_first = 0;
  484. for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
  485. sector_t dist;
  486. sector_t first_bad;
  487. int bad_sectors;
  488. unsigned int pending;
  489. bool nonrot;
  490. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  491. if (r1_bio->bios[disk] == IO_BLOCKED
  492. || rdev == NULL
  493. || test_bit(Unmerged, &rdev->flags)
  494. || test_bit(Faulty, &rdev->flags))
  495. continue;
  496. if (!test_bit(In_sync, &rdev->flags) &&
  497. rdev->recovery_offset < this_sector + sectors)
  498. continue;
  499. if (test_bit(WriteMostly, &rdev->flags)) {
  500. /* Don't balance among write-mostly, just
  501. * use the first as a last resort */
  502. if (best_dist_disk < 0) {
  503. if (is_badblock(rdev, this_sector, sectors,
  504. &first_bad, &bad_sectors)) {
  505. if (first_bad < this_sector)
  506. /* Cannot use this */
  507. continue;
  508. best_good_sectors = first_bad - this_sector;
  509. } else
  510. best_good_sectors = sectors;
  511. best_dist_disk = disk;
  512. best_pending_disk = disk;
  513. }
  514. continue;
  515. }
  516. /* This is a reasonable device to use. It might
  517. * even be best.
  518. */
  519. if (is_badblock(rdev, this_sector, sectors,
  520. &first_bad, &bad_sectors)) {
  521. if (best_dist < MaxSector)
  522. /* already have a better device */
  523. continue;
  524. if (first_bad <= this_sector) {
  525. /* cannot read here. If this is the 'primary'
  526. * device, then we must not read beyond
  527. * bad_sectors from another device..
  528. */
  529. bad_sectors -= (this_sector - first_bad);
  530. if (choose_first && sectors > bad_sectors)
  531. sectors = bad_sectors;
  532. if (best_good_sectors > sectors)
  533. best_good_sectors = sectors;
  534. } else {
  535. sector_t good_sectors = first_bad - this_sector;
  536. if (good_sectors > best_good_sectors) {
  537. best_good_sectors = good_sectors;
  538. best_disk = disk;
  539. }
  540. if (choose_first)
  541. break;
  542. }
  543. continue;
  544. } else
  545. best_good_sectors = sectors;
  546. nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
  547. has_nonrot_disk |= nonrot;
  548. pending = atomic_read(&rdev->nr_pending);
  549. dist = abs(this_sector - conf->mirrors[disk].head_position);
  550. if (choose_first) {
  551. best_disk = disk;
  552. break;
  553. }
  554. /* Don't change to another disk for sequential reads */
  555. if (conf->mirrors[disk].next_seq_sect == this_sector
  556. || dist == 0) {
  557. int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
  558. struct raid1_info *mirror = &conf->mirrors[disk];
  559. best_disk = disk;
  560. /*
  561. * If buffered sequential IO size exceeds optimal
  562. * iosize, check if there is idle disk. If yes, choose
  563. * the idle disk. read_balance could already choose an
  564. * idle disk before noticing it's a sequential IO in
  565. * this disk. This doesn't matter because this disk
  566. * will idle, next time it will be utilized after the
  567. * first disk has IO size exceeds optimal iosize. In
  568. * this way, iosize of the first disk will be optimal
  569. * iosize at least. iosize of the second disk might be
  570. * small, but not a big deal since when the second disk
  571. * starts IO, the first disk is likely still busy.
  572. */
  573. if (nonrot && opt_iosize > 0 &&
  574. mirror->seq_start != MaxSector &&
  575. mirror->next_seq_sect > opt_iosize &&
  576. mirror->next_seq_sect - opt_iosize >=
  577. mirror->seq_start) {
  578. choose_next_idle = 1;
  579. continue;
  580. }
  581. break;
  582. }
  583. /* If device is idle, use it */
  584. if (pending == 0) {
  585. best_disk = disk;
  586. break;
  587. }
  588. if (choose_next_idle)
  589. continue;
  590. if (min_pending > pending) {
  591. min_pending = pending;
  592. best_pending_disk = disk;
  593. }
  594. if (dist < best_dist) {
  595. best_dist = dist;
  596. best_dist_disk = disk;
  597. }
  598. }
  599. /*
  600. * If all disks are rotational, choose the closest disk. If any disk is
  601. * non-rotational, choose the disk with less pending request even the
  602. * disk is rotational, which might/might not be optimal for raids with
  603. * mixed ratation/non-rotational disks depending on workload.
  604. */
  605. if (best_disk == -1) {
  606. if (has_nonrot_disk)
  607. best_disk = best_pending_disk;
  608. else
  609. best_disk = best_dist_disk;
  610. }
  611. if (best_disk >= 0) {
  612. rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
  613. if (!rdev)
  614. goto retry;
  615. atomic_inc(&rdev->nr_pending);
  616. if (test_bit(Faulty, &rdev->flags)) {
  617. /* cannot risk returning a device that failed
  618. * before we inc'ed nr_pending
  619. */
  620. rdev_dec_pending(rdev, conf->mddev);
  621. goto retry;
  622. }
  623. sectors = best_good_sectors;
  624. if (conf->mirrors[best_disk].next_seq_sect != this_sector)
  625. conf->mirrors[best_disk].seq_start = this_sector;
  626. conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
  627. }
  628. rcu_read_unlock();
  629. *max_sectors = sectors;
  630. return best_disk;
  631. }
  632. static int raid1_mergeable_bvec(struct mddev *mddev,
  633. struct bvec_merge_data *bvm,
  634. struct bio_vec *biovec)
  635. {
  636. struct r1conf *conf = mddev->private;
  637. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  638. int max = biovec->bv_len;
  639. if (mddev->merge_check_needed) {
  640. int disk;
  641. rcu_read_lock();
  642. for (disk = 0; disk < conf->raid_disks * 2; disk++) {
  643. struct md_rdev *rdev = rcu_dereference(
  644. conf->mirrors[disk].rdev);
  645. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  646. struct request_queue *q =
  647. bdev_get_queue(rdev->bdev);
  648. if (q->merge_bvec_fn) {
  649. bvm->bi_sector = sector +
  650. rdev->data_offset;
  651. bvm->bi_bdev = rdev->bdev;
  652. max = min(max, q->merge_bvec_fn(
  653. q, bvm, biovec));
  654. }
  655. }
  656. }
  657. rcu_read_unlock();
  658. }
  659. return max;
  660. }
  661. static int raid1_congested(struct mddev *mddev, int bits)
  662. {
  663. struct r1conf *conf = mddev->private;
  664. int i, ret = 0;
  665. if ((bits & (1 << WB_async_congested)) &&
  666. conf->pending_count >= max_queued_requests)
  667. return 1;
  668. rcu_read_lock();
  669. for (i = 0; i < conf->raid_disks * 2; i++) {
  670. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  671. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  672. struct request_queue *q = bdev_get_queue(rdev->bdev);
  673. BUG_ON(!q);
  674. /* Note the '|| 1' - when read_balance prefers
  675. * non-congested targets, it can be removed
  676. */
  677. if ((bits & (1 << WB_async_congested)) || 1)
  678. ret |= bdi_congested(&q->backing_dev_info, bits);
  679. else
  680. ret &= bdi_congested(&q->backing_dev_info, bits);
  681. }
  682. }
  683. rcu_read_unlock();
  684. return ret;
  685. }
  686. static void flush_pending_writes(struct r1conf *conf)
  687. {
  688. /* Any writes that have been queued but are awaiting
  689. * bitmap updates get flushed here.
  690. */
  691. spin_lock_irq(&conf->device_lock);
  692. if (conf->pending_bio_list.head) {
  693. struct bio *bio;
  694. bio = bio_list_get(&conf->pending_bio_list);
  695. conf->pending_count = 0;
  696. spin_unlock_irq(&conf->device_lock);
  697. /* flush any pending bitmap writes to
  698. * disk before proceeding w/ I/O */
  699. bitmap_unplug(conf->mddev->bitmap);
  700. wake_up(&conf->wait_barrier);
  701. while (bio) { /* submit pending writes */
  702. struct bio *next = bio->bi_next;
  703. bio->bi_next = NULL;
  704. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  705. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  706. /* Just ignore it */
  707. bio_endio(bio, 0);
  708. else
  709. generic_make_request(bio);
  710. bio = next;
  711. }
  712. } else
  713. spin_unlock_irq(&conf->device_lock);
  714. }
  715. /* Barriers....
  716. * Sometimes we need to suspend IO while we do something else,
  717. * either some resync/recovery, or reconfigure the array.
  718. * To do this we raise a 'barrier'.
  719. * The 'barrier' is a counter that can be raised multiple times
  720. * to count how many activities are happening which preclude
  721. * normal IO.
  722. * We can only raise the barrier if there is no pending IO.
  723. * i.e. if nr_pending == 0.
  724. * We choose only to raise the barrier if no-one is waiting for the
  725. * barrier to go down. This means that as soon as an IO request
  726. * is ready, no other operations which require a barrier will start
  727. * until the IO request has had a chance.
  728. *
  729. * So: regular IO calls 'wait_barrier'. When that returns there
  730. * is no backgroup IO happening, It must arrange to call
  731. * allow_barrier when it has finished its IO.
  732. * backgroup IO calls must call raise_barrier. Once that returns
  733. * there is no normal IO happeing. It must arrange to call
  734. * lower_barrier when the particular background IO completes.
  735. */
  736. static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
  737. {
  738. spin_lock_irq(&conf->resync_lock);
  739. /* Wait until no block IO is waiting */
  740. wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
  741. conf->resync_lock);
  742. /* block any new IO from starting */
  743. conf->barrier++;
  744. conf->next_resync = sector_nr;
  745. /* For these conditions we must wait:
  746. * A: while the array is in frozen state
  747. * B: while barrier >= RESYNC_DEPTH, meaning resync reach
  748. * the max count which allowed.
  749. * C: next_resync + RESYNC_SECTORS > start_next_window, meaning
  750. * next resync will reach to the window which normal bios are
  751. * handling.
  752. * D: while there are any active requests in the current window.
  753. */
  754. wait_event_lock_irq(conf->wait_barrier,
  755. !conf->array_frozen &&
  756. conf->barrier < RESYNC_DEPTH &&
  757. conf->current_window_requests == 0 &&
  758. (conf->start_next_window >=
  759. conf->next_resync + RESYNC_SECTORS),
  760. conf->resync_lock);
  761. conf->nr_pending++;
  762. spin_unlock_irq(&conf->resync_lock);
  763. }
  764. static void lower_barrier(struct r1conf *conf)
  765. {
  766. unsigned long flags;
  767. BUG_ON(conf->barrier <= 0);
  768. spin_lock_irqsave(&conf->resync_lock, flags);
  769. conf->barrier--;
  770. conf->nr_pending--;
  771. spin_unlock_irqrestore(&conf->resync_lock, flags);
  772. wake_up(&conf->wait_barrier);
  773. }
  774. static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio)
  775. {
  776. bool wait = false;
  777. if (conf->array_frozen || !bio)
  778. wait = true;
  779. else if (conf->barrier && bio_data_dir(bio) == WRITE) {
  780. if ((conf->mddev->curr_resync_completed
  781. >= bio_end_sector(bio)) ||
  782. (conf->next_resync + NEXT_NORMALIO_DISTANCE
  783. <= bio->bi_iter.bi_sector))
  784. wait = false;
  785. else
  786. wait = true;
  787. }
  788. return wait;
  789. }
  790. static sector_t wait_barrier(struct r1conf *conf, struct bio *bio)
  791. {
  792. sector_t sector = 0;
  793. spin_lock_irq(&conf->resync_lock);
  794. if (need_to_wait_for_sync(conf, bio)) {
  795. conf->nr_waiting++;
  796. /* Wait for the barrier to drop.
  797. * However if there are already pending
  798. * requests (preventing the barrier from
  799. * rising completely), and the
  800. * per-process bio queue isn't empty,
  801. * then don't wait, as we need to empty
  802. * that queue to allow conf->start_next_window
  803. * to increase.
  804. */
  805. wait_event_lock_irq(conf->wait_barrier,
  806. !conf->array_frozen &&
  807. (!conf->barrier ||
  808. ((conf->start_next_window <
  809. conf->next_resync + RESYNC_SECTORS) &&
  810. current->bio_list &&
  811. !bio_list_empty(current->bio_list))),
  812. conf->resync_lock);
  813. conf->nr_waiting--;
  814. }
  815. if (bio && bio_data_dir(bio) == WRITE) {
  816. if (bio->bi_iter.bi_sector >=
  817. conf->mddev->curr_resync_completed) {
  818. if (conf->start_next_window == MaxSector)
  819. conf->start_next_window =
  820. conf->next_resync +
  821. NEXT_NORMALIO_DISTANCE;
  822. if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE)
  823. <= bio->bi_iter.bi_sector)
  824. conf->next_window_requests++;
  825. else
  826. conf->current_window_requests++;
  827. sector = conf->start_next_window;
  828. }
  829. }
  830. conf->nr_pending++;
  831. spin_unlock_irq(&conf->resync_lock);
  832. return sector;
  833. }
  834. static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
  835. sector_t bi_sector)
  836. {
  837. unsigned long flags;
  838. spin_lock_irqsave(&conf->resync_lock, flags);
  839. conf->nr_pending--;
  840. if (start_next_window) {
  841. if (start_next_window == conf->start_next_window) {
  842. if (conf->start_next_window + NEXT_NORMALIO_DISTANCE
  843. <= bi_sector)
  844. conf->next_window_requests--;
  845. else
  846. conf->current_window_requests--;
  847. } else
  848. conf->current_window_requests--;
  849. if (!conf->current_window_requests) {
  850. if (conf->next_window_requests) {
  851. conf->current_window_requests =
  852. conf->next_window_requests;
  853. conf->next_window_requests = 0;
  854. conf->start_next_window +=
  855. NEXT_NORMALIO_DISTANCE;
  856. } else
  857. conf->start_next_window = MaxSector;
  858. }
  859. }
  860. spin_unlock_irqrestore(&conf->resync_lock, flags);
  861. wake_up(&conf->wait_barrier);
  862. }
  863. static void freeze_array(struct r1conf *conf, int extra)
  864. {
  865. /* stop syncio and normal IO and wait for everything to
  866. * go quite.
  867. * We wait until nr_pending match nr_queued+extra
  868. * This is called in the context of one normal IO request
  869. * that has failed. Thus any sync request that might be pending
  870. * will be blocked by nr_pending, and we need to wait for
  871. * pending IO requests to complete or be queued for re-try.
  872. * Thus the number queued (nr_queued) plus this request (extra)
  873. * must match the number of pending IOs (nr_pending) before
  874. * we continue.
  875. */
  876. spin_lock_irq(&conf->resync_lock);
  877. conf->array_frozen = 1;
  878. wait_event_lock_irq_cmd(conf->wait_barrier,
  879. conf->nr_pending == conf->nr_queued+extra,
  880. conf->resync_lock,
  881. flush_pending_writes(conf));
  882. spin_unlock_irq(&conf->resync_lock);
  883. }
  884. static void unfreeze_array(struct r1conf *conf)
  885. {
  886. /* reverse the effect of the freeze */
  887. spin_lock_irq(&conf->resync_lock);
  888. conf->array_frozen = 0;
  889. wake_up(&conf->wait_barrier);
  890. spin_unlock_irq(&conf->resync_lock);
  891. }
  892. /* duplicate the data pages for behind I/O
  893. */
  894. static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
  895. {
  896. int i;
  897. struct bio_vec *bvec;
  898. struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
  899. GFP_NOIO);
  900. if (unlikely(!bvecs))
  901. return;
  902. bio_for_each_segment_all(bvec, bio, i) {
  903. bvecs[i] = *bvec;
  904. bvecs[i].bv_page = alloc_page(GFP_NOIO);
  905. if (unlikely(!bvecs[i].bv_page))
  906. goto do_sync_io;
  907. memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
  908. kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
  909. kunmap(bvecs[i].bv_page);
  910. kunmap(bvec->bv_page);
  911. }
  912. r1_bio->behind_bvecs = bvecs;
  913. r1_bio->behind_page_count = bio->bi_vcnt;
  914. set_bit(R1BIO_BehindIO, &r1_bio->state);
  915. return;
  916. do_sync_io:
  917. for (i = 0; i < bio->bi_vcnt; i++)
  918. if (bvecs[i].bv_page)
  919. put_page(bvecs[i].bv_page);
  920. kfree(bvecs);
  921. pr_debug("%dB behind alloc failed, doing sync I/O\n",
  922. bio->bi_iter.bi_size);
  923. }
  924. struct raid1_plug_cb {
  925. struct blk_plug_cb cb;
  926. struct bio_list pending;
  927. int pending_cnt;
  928. };
  929. static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
  930. {
  931. struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
  932. cb);
  933. struct mddev *mddev = plug->cb.data;
  934. struct r1conf *conf = mddev->private;
  935. struct bio *bio;
  936. if (from_schedule || current->bio_list) {
  937. spin_lock_irq(&conf->device_lock);
  938. bio_list_merge(&conf->pending_bio_list, &plug->pending);
  939. conf->pending_count += plug->pending_cnt;
  940. spin_unlock_irq(&conf->device_lock);
  941. wake_up(&conf->wait_barrier);
  942. md_wakeup_thread(mddev->thread);
  943. kfree(plug);
  944. return;
  945. }
  946. /* we aren't scheduling, so we can do the write-out directly. */
  947. bio = bio_list_get(&plug->pending);
  948. bitmap_unplug(mddev->bitmap);
  949. wake_up(&conf->wait_barrier);
  950. while (bio) { /* submit pending writes */
  951. struct bio *next = bio->bi_next;
  952. bio->bi_next = NULL;
  953. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  954. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  955. /* Just ignore it */
  956. bio_endio(bio, 0);
  957. else
  958. generic_make_request(bio);
  959. bio = next;
  960. }
  961. kfree(plug);
  962. }
  963. static void make_request(struct mddev *mddev, struct bio * bio)
  964. {
  965. struct r1conf *conf = mddev->private;
  966. struct raid1_info *mirror;
  967. struct r1bio *r1_bio;
  968. struct bio *read_bio;
  969. int i, disks;
  970. struct bitmap *bitmap;
  971. unsigned long flags;
  972. const int rw = bio_data_dir(bio);
  973. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  974. const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
  975. const unsigned long do_discard = (bio->bi_rw
  976. & (REQ_DISCARD | REQ_SECURE));
  977. const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
  978. struct md_rdev *blocked_rdev;
  979. struct blk_plug_cb *cb;
  980. struct raid1_plug_cb *plug = NULL;
  981. int first_clone;
  982. int sectors_handled;
  983. int max_sectors;
  984. sector_t start_next_window;
  985. /*
  986. * Register the new request and wait if the reconstruction
  987. * thread has put up a bar for new requests.
  988. * Continue immediately if no resync is active currently.
  989. */
  990. md_write_start(mddev, bio); /* wait on superblock update early */
  991. if (bio_data_dir(bio) == WRITE &&
  992. ((bio_end_sector(bio) > mddev->suspend_lo &&
  993. bio->bi_iter.bi_sector < mddev->suspend_hi) ||
  994. (mddev_is_clustered(mddev) &&
  995. md_cluster_ops->area_resyncing(mddev, WRITE,
  996. bio->bi_iter.bi_sector, bio_end_sector(bio))))) {
  997. /* As the suspend_* range is controlled by
  998. * userspace, we want an interruptible
  999. * wait.
  1000. */
  1001. DEFINE_WAIT(w);
  1002. for (;;) {
  1003. flush_signals(current);
  1004. prepare_to_wait(&conf->wait_barrier,
  1005. &w, TASK_INTERRUPTIBLE);
  1006. if (bio_end_sector(bio) <= mddev->suspend_lo ||
  1007. bio->bi_iter.bi_sector >= mddev->suspend_hi ||
  1008. (mddev_is_clustered(mddev) &&
  1009. !md_cluster_ops->area_resyncing(mddev, WRITE,
  1010. bio->bi_iter.bi_sector, bio_end_sector(bio))))
  1011. break;
  1012. schedule();
  1013. }
  1014. finish_wait(&conf->wait_barrier, &w);
  1015. }
  1016. start_next_window = wait_barrier(conf, bio);
  1017. bitmap = mddev->bitmap;
  1018. /*
  1019. * make_request() can abort the operation when READA is being
  1020. * used and no empty request is available.
  1021. *
  1022. */
  1023. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1024. r1_bio->master_bio = bio;
  1025. r1_bio->sectors = bio_sectors(bio);
  1026. r1_bio->state = 0;
  1027. r1_bio->mddev = mddev;
  1028. r1_bio->sector = bio->bi_iter.bi_sector;
  1029. /* We might need to issue multiple reads to different
  1030. * devices if there are bad blocks around, so we keep
  1031. * track of the number of reads in bio->bi_phys_segments.
  1032. * If this is 0, there is only one r1_bio and no locking
  1033. * will be needed when requests complete. If it is
  1034. * non-zero, then it is the number of not-completed requests.
  1035. */
  1036. bio->bi_phys_segments = 0;
  1037. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1038. if (rw == READ) {
  1039. /*
  1040. * read balancing logic:
  1041. */
  1042. int rdisk;
  1043. read_again:
  1044. rdisk = read_balance(conf, r1_bio, &max_sectors);
  1045. if (rdisk < 0) {
  1046. /* couldn't find anywhere to read from */
  1047. raid_end_bio_io(r1_bio);
  1048. return;
  1049. }
  1050. mirror = conf->mirrors + rdisk;
  1051. if (test_bit(WriteMostly, &mirror->rdev->flags) &&
  1052. bitmap) {
  1053. /* Reading from a write-mostly device must
  1054. * take care not to over-take any writes
  1055. * that are 'behind'
  1056. */
  1057. wait_event(bitmap->behind_wait,
  1058. atomic_read(&bitmap->behind_writes) == 0);
  1059. }
  1060. r1_bio->read_disk = rdisk;
  1061. r1_bio->start_next_window = 0;
  1062. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1063. bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
  1064. max_sectors);
  1065. r1_bio->bios[rdisk] = read_bio;
  1066. read_bio->bi_iter.bi_sector = r1_bio->sector +
  1067. mirror->rdev->data_offset;
  1068. read_bio->bi_bdev = mirror->rdev->bdev;
  1069. read_bio->bi_end_io = raid1_end_read_request;
  1070. read_bio->bi_rw = READ | do_sync;
  1071. read_bio->bi_private = r1_bio;
  1072. if (max_sectors < r1_bio->sectors) {
  1073. /* could not read all from this device, so we will
  1074. * need another r1_bio.
  1075. */
  1076. sectors_handled = (r1_bio->sector + max_sectors
  1077. - bio->bi_iter.bi_sector);
  1078. r1_bio->sectors = max_sectors;
  1079. spin_lock_irq(&conf->device_lock);
  1080. if (bio->bi_phys_segments == 0)
  1081. bio->bi_phys_segments = 2;
  1082. else
  1083. bio->bi_phys_segments++;
  1084. spin_unlock_irq(&conf->device_lock);
  1085. /* Cannot call generic_make_request directly
  1086. * as that will be queued in __make_request
  1087. * and subsequent mempool_alloc might block waiting
  1088. * for it. So hand bio over to raid1d.
  1089. */
  1090. reschedule_retry(r1_bio);
  1091. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1092. r1_bio->master_bio = bio;
  1093. r1_bio->sectors = bio_sectors(bio) - sectors_handled;
  1094. r1_bio->state = 0;
  1095. r1_bio->mddev = mddev;
  1096. r1_bio->sector = bio->bi_iter.bi_sector +
  1097. sectors_handled;
  1098. goto read_again;
  1099. } else
  1100. generic_make_request(read_bio);
  1101. return;
  1102. }
  1103. /*
  1104. * WRITE:
  1105. */
  1106. if (conf->pending_count >= max_queued_requests) {
  1107. md_wakeup_thread(mddev->thread);
  1108. wait_event(conf->wait_barrier,
  1109. conf->pending_count < max_queued_requests);
  1110. }
  1111. /* first select target devices under rcu_lock and
  1112. * inc refcount on their rdev. Record them by setting
  1113. * bios[x] to bio
  1114. * If there are known/acknowledged bad blocks on any device on
  1115. * which we have seen a write error, we want to avoid writing those
  1116. * blocks.
  1117. * This potentially requires several writes to write around
  1118. * the bad blocks. Each set of writes gets it's own r1bio
  1119. * with a set of bios attached.
  1120. */
  1121. disks = conf->raid_disks * 2;
  1122. retry_write:
  1123. r1_bio->start_next_window = start_next_window;
  1124. blocked_rdev = NULL;
  1125. rcu_read_lock();
  1126. max_sectors = r1_bio->sectors;
  1127. for (i = 0; i < disks; i++) {
  1128. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1129. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1130. atomic_inc(&rdev->nr_pending);
  1131. blocked_rdev = rdev;
  1132. break;
  1133. }
  1134. r1_bio->bios[i] = NULL;
  1135. if (!rdev || test_bit(Faulty, &rdev->flags)
  1136. || test_bit(Unmerged, &rdev->flags)) {
  1137. if (i < conf->raid_disks)
  1138. set_bit(R1BIO_Degraded, &r1_bio->state);
  1139. continue;
  1140. }
  1141. atomic_inc(&rdev->nr_pending);
  1142. if (test_bit(WriteErrorSeen, &rdev->flags)) {
  1143. sector_t first_bad;
  1144. int bad_sectors;
  1145. int is_bad;
  1146. is_bad = is_badblock(rdev, r1_bio->sector,
  1147. max_sectors,
  1148. &first_bad, &bad_sectors);
  1149. if (is_bad < 0) {
  1150. /* mustn't write here until the bad block is
  1151. * acknowledged*/
  1152. set_bit(BlockedBadBlocks, &rdev->flags);
  1153. blocked_rdev = rdev;
  1154. break;
  1155. }
  1156. if (is_bad && first_bad <= r1_bio->sector) {
  1157. /* Cannot write here at all */
  1158. bad_sectors -= (r1_bio->sector - first_bad);
  1159. if (bad_sectors < max_sectors)
  1160. /* mustn't write more than bad_sectors
  1161. * to other devices yet
  1162. */
  1163. max_sectors = bad_sectors;
  1164. rdev_dec_pending(rdev, mddev);
  1165. /* We don't set R1BIO_Degraded as that
  1166. * only applies if the disk is
  1167. * missing, so it might be re-added,
  1168. * and we want to know to recover this
  1169. * chunk.
  1170. * In this case the device is here,
  1171. * and the fact that this chunk is not
  1172. * in-sync is recorded in the bad
  1173. * block log
  1174. */
  1175. continue;
  1176. }
  1177. if (is_bad) {
  1178. int good_sectors = first_bad - r1_bio->sector;
  1179. if (good_sectors < max_sectors)
  1180. max_sectors = good_sectors;
  1181. }
  1182. }
  1183. r1_bio->bios[i] = bio;
  1184. }
  1185. rcu_read_unlock();
  1186. if (unlikely(blocked_rdev)) {
  1187. /* Wait for this device to become unblocked */
  1188. int j;
  1189. sector_t old = start_next_window;
  1190. for (j = 0; j < i; j++)
  1191. if (r1_bio->bios[j])
  1192. rdev_dec_pending(conf->mirrors[j].rdev, mddev);
  1193. r1_bio->state = 0;
  1194. allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector);
  1195. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1196. start_next_window = wait_barrier(conf, bio);
  1197. /*
  1198. * We must make sure the multi r1bios of bio have
  1199. * the same value of bi_phys_segments
  1200. */
  1201. if (bio->bi_phys_segments && old &&
  1202. old != start_next_window)
  1203. /* Wait for the former r1bio(s) to complete */
  1204. wait_event(conf->wait_barrier,
  1205. bio->bi_phys_segments == 1);
  1206. goto retry_write;
  1207. }
  1208. if (max_sectors < r1_bio->sectors) {
  1209. /* We are splitting this write into multiple parts, so
  1210. * we need to prepare for allocating another r1_bio.
  1211. */
  1212. r1_bio->sectors = max_sectors;
  1213. spin_lock_irq(&conf->device_lock);
  1214. if (bio->bi_phys_segments == 0)
  1215. bio->bi_phys_segments = 2;
  1216. else
  1217. bio->bi_phys_segments++;
  1218. spin_unlock_irq(&conf->device_lock);
  1219. }
  1220. sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
  1221. atomic_set(&r1_bio->remaining, 1);
  1222. atomic_set(&r1_bio->behind_remaining, 0);
  1223. first_clone = 1;
  1224. for (i = 0; i < disks; i++) {
  1225. struct bio *mbio;
  1226. if (!r1_bio->bios[i])
  1227. continue;
  1228. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1229. bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors);
  1230. if (first_clone) {
  1231. /* do behind I/O ?
  1232. * Not if there are too many, or cannot
  1233. * allocate memory, or a reader on WriteMostly
  1234. * is waiting for behind writes to flush */
  1235. if (bitmap &&
  1236. (atomic_read(&bitmap->behind_writes)
  1237. < mddev->bitmap_info.max_write_behind) &&
  1238. !waitqueue_active(&bitmap->behind_wait))
  1239. alloc_behind_pages(mbio, r1_bio);
  1240. bitmap_startwrite(bitmap, r1_bio->sector,
  1241. r1_bio->sectors,
  1242. test_bit(R1BIO_BehindIO,
  1243. &r1_bio->state));
  1244. first_clone = 0;
  1245. }
  1246. if (r1_bio->behind_bvecs) {
  1247. struct bio_vec *bvec;
  1248. int j;
  1249. /*
  1250. * We trimmed the bio, so _all is legit
  1251. */
  1252. bio_for_each_segment_all(bvec, mbio, j)
  1253. bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
  1254. if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
  1255. atomic_inc(&r1_bio->behind_remaining);
  1256. }
  1257. r1_bio->bios[i] = mbio;
  1258. mbio->bi_iter.bi_sector = (r1_bio->sector +
  1259. conf->mirrors[i].rdev->data_offset);
  1260. mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1261. mbio->bi_end_io = raid1_end_write_request;
  1262. mbio->bi_rw =
  1263. WRITE | do_flush_fua | do_sync | do_discard | do_same;
  1264. mbio->bi_private = r1_bio;
  1265. atomic_inc(&r1_bio->remaining);
  1266. cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
  1267. if (cb)
  1268. plug = container_of(cb, struct raid1_plug_cb, cb);
  1269. else
  1270. plug = NULL;
  1271. spin_lock_irqsave(&conf->device_lock, flags);
  1272. if (plug) {
  1273. bio_list_add(&plug->pending, mbio);
  1274. plug->pending_cnt++;
  1275. } else {
  1276. bio_list_add(&conf->pending_bio_list, mbio);
  1277. conf->pending_count++;
  1278. }
  1279. spin_unlock_irqrestore(&conf->device_lock, flags);
  1280. if (!plug)
  1281. md_wakeup_thread(mddev->thread);
  1282. }
  1283. /* Mustn't call r1_bio_write_done before this next test,
  1284. * as it could result in the bio being freed.
  1285. */
  1286. if (sectors_handled < bio_sectors(bio)) {
  1287. r1_bio_write_done(r1_bio);
  1288. /* We need another r1_bio. It has already been counted
  1289. * in bio->bi_phys_segments
  1290. */
  1291. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1292. r1_bio->master_bio = bio;
  1293. r1_bio->sectors = bio_sectors(bio) - sectors_handled;
  1294. r1_bio->state = 0;
  1295. r1_bio->mddev = mddev;
  1296. r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
  1297. goto retry_write;
  1298. }
  1299. r1_bio_write_done(r1_bio);
  1300. /* In case raid1d snuck in to freeze_array */
  1301. wake_up(&conf->wait_barrier);
  1302. }
  1303. static void status(struct seq_file *seq, struct mddev *mddev)
  1304. {
  1305. struct r1conf *conf = mddev->private;
  1306. int i;
  1307. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  1308. conf->raid_disks - mddev->degraded);
  1309. rcu_read_lock();
  1310. for (i = 0; i < conf->raid_disks; i++) {
  1311. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1312. seq_printf(seq, "%s",
  1313. rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  1314. }
  1315. rcu_read_unlock();
  1316. seq_printf(seq, "]");
  1317. }
  1318. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1319. {
  1320. char b[BDEVNAME_SIZE];
  1321. struct r1conf *conf = mddev->private;
  1322. unsigned long flags;
  1323. /*
  1324. * If it is not operational, then we have already marked it as dead
  1325. * else if it is the last working disks, ignore the error, let the
  1326. * next level up know.
  1327. * else mark the drive as failed
  1328. */
  1329. if (test_bit(In_sync, &rdev->flags)
  1330. && (conf->raid_disks - mddev->degraded) == 1) {
  1331. /*
  1332. * Don't fail the drive, act as though we were just a
  1333. * normal single drive.
  1334. * However don't try a recovery from this drive as
  1335. * it is very likely to fail.
  1336. */
  1337. conf->recovery_disabled = mddev->recovery_disabled;
  1338. return;
  1339. }
  1340. set_bit(Blocked, &rdev->flags);
  1341. spin_lock_irqsave(&conf->device_lock, flags);
  1342. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1343. mddev->degraded++;
  1344. set_bit(Faulty, &rdev->flags);
  1345. } else
  1346. set_bit(Faulty, &rdev->flags);
  1347. spin_unlock_irqrestore(&conf->device_lock, flags);
  1348. /*
  1349. * if recovery is running, make sure it aborts.
  1350. */
  1351. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1352. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1353. printk(KERN_ALERT
  1354. "md/raid1:%s: Disk failure on %s, disabling device.\n"
  1355. "md/raid1:%s: Operation continuing on %d devices.\n",
  1356. mdname(mddev), bdevname(rdev->bdev, b),
  1357. mdname(mddev), conf->raid_disks - mddev->degraded);
  1358. }
  1359. static void print_conf(struct r1conf *conf)
  1360. {
  1361. int i;
  1362. printk(KERN_DEBUG "RAID1 conf printout:\n");
  1363. if (!conf) {
  1364. printk(KERN_DEBUG "(!conf)\n");
  1365. return;
  1366. }
  1367. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  1368. conf->raid_disks);
  1369. rcu_read_lock();
  1370. for (i = 0; i < conf->raid_disks; i++) {
  1371. char b[BDEVNAME_SIZE];
  1372. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1373. if (rdev)
  1374. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1375. i, !test_bit(In_sync, &rdev->flags),
  1376. !test_bit(Faulty, &rdev->flags),
  1377. bdevname(rdev->bdev,b));
  1378. }
  1379. rcu_read_unlock();
  1380. }
  1381. static void close_sync(struct r1conf *conf)
  1382. {
  1383. wait_barrier(conf, NULL);
  1384. allow_barrier(conf, 0, 0);
  1385. mempool_destroy(conf->r1buf_pool);
  1386. conf->r1buf_pool = NULL;
  1387. spin_lock_irq(&conf->resync_lock);
  1388. conf->next_resync = 0;
  1389. conf->start_next_window = MaxSector;
  1390. conf->current_window_requests +=
  1391. conf->next_window_requests;
  1392. conf->next_window_requests = 0;
  1393. spin_unlock_irq(&conf->resync_lock);
  1394. }
  1395. static int raid1_spare_active(struct mddev *mddev)
  1396. {
  1397. int i;
  1398. struct r1conf *conf = mddev->private;
  1399. int count = 0;
  1400. unsigned long flags;
  1401. /*
  1402. * Find all failed disks within the RAID1 configuration
  1403. * and mark them readable.
  1404. * Called under mddev lock, so rcu protection not needed.
  1405. * device_lock used to avoid races with raid1_end_read_request
  1406. * which expects 'In_sync' flags and ->degraded to be consistent.
  1407. */
  1408. spin_lock_irqsave(&conf->device_lock, flags);
  1409. for (i = 0; i < conf->raid_disks; i++) {
  1410. struct md_rdev *rdev = conf->mirrors[i].rdev;
  1411. struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
  1412. if (repl
  1413. && !test_bit(Candidate, &repl->flags)
  1414. && repl->recovery_offset == MaxSector
  1415. && !test_bit(Faulty, &repl->flags)
  1416. && !test_and_set_bit(In_sync, &repl->flags)) {
  1417. /* replacement has just become active */
  1418. if (!rdev ||
  1419. !test_and_clear_bit(In_sync, &rdev->flags))
  1420. count++;
  1421. if (rdev) {
  1422. /* Replaced device not technically
  1423. * faulty, but we need to be sure
  1424. * it gets removed and never re-added
  1425. */
  1426. set_bit(Faulty, &rdev->flags);
  1427. sysfs_notify_dirent_safe(
  1428. rdev->sysfs_state);
  1429. }
  1430. }
  1431. if (rdev
  1432. && rdev->recovery_offset == MaxSector
  1433. && !test_bit(Faulty, &rdev->flags)
  1434. && !test_and_set_bit(In_sync, &rdev->flags)) {
  1435. count++;
  1436. sysfs_notify_dirent_safe(rdev->sysfs_state);
  1437. }
  1438. }
  1439. mddev->degraded -= count;
  1440. spin_unlock_irqrestore(&conf->device_lock, flags);
  1441. print_conf(conf);
  1442. return count;
  1443. }
  1444. static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1445. {
  1446. struct r1conf *conf = mddev->private;
  1447. int err = -EEXIST;
  1448. int mirror = 0;
  1449. struct raid1_info *p;
  1450. int first = 0;
  1451. int last = conf->raid_disks - 1;
  1452. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1453. if (mddev->recovery_disabled == conf->recovery_disabled)
  1454. return -EBUSY;
  1455. if (rdev->raid_disk >= 0)
  1456. first = last = rdev->raid_disk;
  1457. if (q->merge_bvec_fn) {
  1458. set_bit(Unmerged, &rdev->flags);
  1459. mddev->merge_check_needed = 1;
  1460. }
  1461. for (mirror = first; mirror <= last; mirror++) {
  1462. p = conf->mirrors+mirror;
  1463. if (!p->rdev) {
  1464. if (mddev->gendisk)
  1465. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1466. rdev->data_offset << 9);
  1467. p->head_position = 0;
  1468. rdev->raid_disk = mirror;
  1469. err = 0;
  1470. /* As all devices are equivalent, we don't need a full recovery
  1471. * if this was recently any drive of the array
  1472. */
  1473. if (rdev->saved_raid_disk < 0)
  1474. conf->fullsync = 1;
  1475. rcu_assign_pointer(p->rdev, rdev);
  1476. break;
  1477. }
  1478. if (test_bit(WantReplacement, &p->rdev->flags) &&
  1479. p[conf->raid_disks].rdev == NULL) {
  1480. /* Add this device as a replacement */
  1481. clear_bit(In_sync, &rdev->flags);
  1482. set_bit(Replacement, &rdev->flags);
  1483. rdev->raid_disk = mirror;
  1484. err = 0;
  1485. conf->fullsync = 1;
  1486. rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
  1487. break;
  1488. }
  1489. }
  1490. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1491. /* Some requests might not have seen this new
  1492. * merge_bvec_fn. We must wait for them to complete
  1493. * before merging the device fully.
  1494. * First we make sure any code which has tested
  1495. * our function has submitted the request, then
  1496. * we wait for all outstanding requests to complete.
  1497. */
  1498. synchronize_sched();
  1499. freeze_array(conf, 0);
  1500. unfreeze_array(conf);
  1501. clear_bit(Unmerged, &rdev->flags);
  1502. }
  1503. md_integrity_add_rdev(rdev, mddev);
  1504. if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1505. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  1506. print_conf(conf);
  1507. return err;
  1508. }
  1509. static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1510. {
  1511. struct r1conf *conf = mddev->private;
  1512. int err = 0;
  1513. int number = rdev->raid_disk;
  1514. struct raid1_info *p = conf->mirrors + number;
  1515. if (rdev != p->rdev)
  1516. p = conf->mirrors + conf->raid_disks + number;
  1517. print_conf(conf);
  1518. if (rdev == p->rdev) {
  1519. if (test_bit(In_sync, &rdev->flags) ||
  1520. atomic_read(&rdev->nr_pending)) {
  1521. err = -EBUSY;
  1522. goto abort;
  1523. }
  1524. /* Only remove non-faulty devices if recovery
  1525. * is not possible.
  1526. */
  1527. if (!test_bit(Faulty, &rdev->flags) &&
  1528. mddev->recovery_disabled != conf->recovery_disabled &&
  1529. mddev->degraded < conf->raid_disks) {
  1530. err = -EBUSY;
  1531. goto abort;
  1532. }
  1533. p->rdev = NULL;
  1534. synchronize_rcu();
  1535. if (atomic_read(&rdev->nr_pending)) {
  1536. /* lost the race, try later */
  1537. err = -EBUSY;
  1538. p->rdev = rdev;
  1539. goto abort;
  1540. } else if (conf->mirrors[conf->raid_disks + number].rdev) {
  1541. /* We just removed a device that is being replaced.
  1542. * Move down the replacement. We drain all IO before
  1543. * doing this to avoid confusion.
  1544. */
  1545. struct md_rdev *repl =
  1546. conf->mirrors[conf->raid_disks + number].rdev;
  1547. freeze_array(conf, 0);
  1548. clear_bit(Replacement, &repl->flags);
  1549. p->rdev = repl;
  1550. conf->mirrors[conf->raid_disks + number].rdev = NULL;
  1551. unfreeze_array(conf);
  1552. clear_bit(WantReplacement, &rdev->flags);
  1553. } else
  1554. clear_bit(WantReplacement, &rdev->flags);
  1555. err = md_integrity_register(mddev);
  1556. }
  1557. abort:
  1558. print_conf(conf);
  1559. return err;
  1560. }
  1561. static void end_sync_read(struct bio *bio, int error)
  1562. {
  1563. struct r1bio *r1_bio = bio->bi_private;
  1564. update_head_pos(r1_bio->read_disk, r1_bio);
  1565. /*
  1566. * we have read a block, now it needs to be re-written,
  1567. * or re-read if the read failed.
  1568. * We don't do much here, just schedule handling by raid1d
  1569. */
  1570. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1571. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1572. if (atomic_dec_and_test(&r1_bio->remaining))
  1573. reschedule_retry(r1_bio);
  1574. }
  1575. static void end_sync_write(struct bio *bio, int error)
  1576. {
  1577. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1578. struct r1bio *r1_bio = bio->bi_private;
  1579. struct mddev *mddev = r1_bio->mddev;
  1580. struct r1conf *conf = mddev->private;
  1581. int mirror=0;
  1582. sector_t first_bad;
  1583. int bad_sectors;
  1584. mirror = find_bio_disk(r1_bio, bio);
  1585. if (!uptodate) {
  1586. sector_t sync_blocks = 0;
  1587. sector_t s = r1_bio->sector;
  1588. long sectors_to_go = r1_bio->sectors;
  1589. /* make sure these bits doesn't get cleared. */
  1590. do {
  1591. bitmap_end_sync(mddev->bitmap, s,
  1592. &sync_blocks, 1);
  1593. s += sync_blocks;
  1594. sectors_to_go -= sync_blocks;
  1595. } while (sectors_to_go > 0);
  1596. set_bit(WriteErrorSeen,
  1597. &conf->mirrors[mirror].rdev->flags);
  1598. if (!test_and_set_bit(WantReplacement,
  1599. &conf->mirrors[mirror].rdev->flags))
  1600. set_bit(MD_RECOVERY_NEEDED, &
  1601. mddev->recovery);
  1602. set_bit(R1BIO_WriteError, &r1_bio->state);
  1603. } else if (is_badblock(conf->mirrors[mirror].rdev,
  1604. r1_bio->sector,
  1605. r1_bio->sectors,
  1606. &first_bad, &bad_sectors) &&
  1607. !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
  1608. r1_bio->sector,
  1609. r1_bio->sectors,
  1610. &first_bad, &bad_sectors)
  1611. )
  1612. set_bit(R1BIO_MadeGood, &r1_bio->state);
  1613. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1614. int s = r1_bio->sectors;
  1615. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1616. test_bit(R1BIO_WriteError, &r1_bio->state))
  1617. reschedule_retry(r1_bio);
  1618. else {
  1619. put_buf(r1_bio);
  1620. md_done_sync(mddev, s, uptodate);
  1621. }
  1622. }
  1623. }
  1624. static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1625. int sectors, struct page *page, int rw)
  1626. {
  1627. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1628. /* success */
  1629. return 1;
  1630. if (rw == WRITE) {
  1631. set_bit(WriteErrorSeen, &rdev->flags);
  1632. if (!test_and_set_bit(WantReplacement,
  1633. &rdev->flags))
  1634. set_bit(MD_RECOVERY_NEEDED, &
  1635. rdev->mddev->recovery);
  1636. }
  1637. /* need to record an error - either for the block or the device */
  1638. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1639. md_error(rdev->mddev, rdev);
  1640. return 0;
  1641. }
  1642. static int fix_sync_read_error(struct r1bio *r1_bio)
  1643. {
  1644. /* Try some synchronous reads of other devices to get
  1645. * good data, much like with normal read errors. Only
  1646. * read into the pages we already have so we don't
  1647. * need to re-issue the read request.
  1648. * We don't need to freeze the array, because being in an
  1649. * active sync request, there is no normal IO, and
  1650. * no overlapping syncs.
  1651. * We don't need to check is_badblock() again as we
  1652. * made sure that anything with a bad block in range
  1653. * will have bi_end_io clear.
  1654. */
  1655. struct mddev *mddev = r1_bio->mddev;
  1656. struct r1conf *conf = mddev->private;
  1657. struct bio *bio = r1_bio->bios[r1_bio->read_disk];
  1658. sector_t sect = r1_bio->sector;
  1659. int sectors = r1_bio->sectors;
  1660. int idx = 0;
  1661. while(sectors) {
  1662. int s = sectors;
  1663. int d = r1_bio->read_disk;
  1664. int success = 0;
  1665. struct md_rdev *rdev;
  1666. int start;
  1667. if (s > (PAGE_SIZE>>9))
  1668. s = PAGE_SIZE >> 9;
  1669. do {
  1670. if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
  1671. /* No rcu protection needed here devices
  1672. * can only be removed when no resync is
  1673. * active, and resync is currently active
  1674. */
  1675. rdev = conf->mirrors[d].rdev;
  1676. if (sync_page_io(rdev, sect, s<<9,
  1677. bio->bi_io_vec[idx].bv_page,
  1678. READ, false)) {
  1679. success = 1;
  1680. break;
  1681. }
  1682. }
  1683. d++;
  1684. if (d == conf->raid_disks * 2)
  1685. d = 0;
  1686. } while (!success && d != r1_bio->read_disk);
  1687. if (!success) {
  1688. char b[BDEVNAME_SIZE];
  1689. int abort = 0;
  1690. /* Cannot read from anywhere, this block is lost.
  1691. * Record a bad block on each device. If that doesn't
  1692. * work just disable and interrupt the recovery.
  1693. * Don't fail devices as that won't really help.
  1694. */
  1695. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
  1696. " for block %llu\n",
  1697. mdname(mddev),
  1698. bdevname(bio->bi_bdev, b),
  1699. (unsigned long long)r1_bio->sector);
  1700. for (d = 0; d < conf->raid_disks * 2; d++) {
  1701. rdev = conf->mirrors[d].rdev;
  1702. if (!rdev || test_bit(Faulty, &rdev->flags))
  1703. continue;
  1704. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1705. abort = 1;
  1706. }
  1707. if (abort) {
  1708. conf->recovery_disabled =
  1709. mddev->recovery_disabled;
  1710. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1711. md_done_sync(mddev, r1_bio->sectors, 0);
  1712. put_buf(r1_bio);
  1713. return 0;
  1714. }
  1715. /* Try next page */
  1716. sectors -= s;
  1717. sect += s;
  1718. idx++;
  1719. continue;
  1720. }
  1721. start = d;
  1722. /* write it back and re-read */
  1723. while (d != r1_bio->read_disk) {
  1724. if (d == 0)
  1725. d = conf->raid_disks * 2;
  1726. d--;
  1727. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1728. continue;
  1729. rdev = conf->mirrors[d].rdev;
  1730. if (r1_sync_page_io(rdev, sect, s,
  1731. bio->bi_io_vec[idx].bv_page,
  1732. WRITE) == 0) {
  1733. r1_bio->bios[d]->bi_end_io = NULL;
  1734. rdev_dec_pending(rdev, mddev);
  1735. }
  1736. }
  1737. d = start;
  1738. while (d != r1_bio->read_disk) {
  1739. if (d == 0)
  1740. d = conf->raid_disks * 2;
  1741. d--;
  1742. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1743. continue;
  1744. rdev = conf->mirrors[d].rdev;
  1745. if (r1_sync_page_io(rdev, sect, s,
  1746. bio->bi_io_vec[idx].bv_page,
  1747. READ) != 0)
  1748. atomic_add(s, &rdev->corrected_errors);
  1749. }
  1750. sectors -= s;
  1751. sect += s;
  1752. idx ++;
  1753. }
  1754. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1755. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1756. return 1;
  1757. }
  1758. static void process_checks(struct r1bio *r1_bio)
  1759. {
  1760. /* We have read all readable devices. If we haven't
  1761. * got the block, then there is no hope left.
  1762. * If we have, then we want to do a comparison
  1763. * and skip the write if everything is the same.
  1764. * If any blocks failed to read, then we need to
  1765. * attempt an over-write
  1766. */
  1767. struct mddev *mddev = r1_bio->mddev;
  1768. struct r1conf *conf = mddev->private;
  1769. int primary;
  1770. int i;
  1771. int vcnt;
  1772. /* Fix variable parts of all bios */
  1773. vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
  1774. for (i = 0; i < conf->raid_disks * 2; i++) {
  1775. int j;
  1776. int size;
  1777. int uptodate;
  1778. struct bio *b = r1_bio->bios[i];
  1779. if (b->bi_end_io != end_sync_read)
  1780. continue;
  1781. /* fixup the bio for reuse, but preserve BIO_UPTODATE */
  1782. uptodate = test_bit(BIO_UPTODATE, &b->bi_flags);
  1783. bio_reset(b);
  1784. if (!uptodate)
  1785. clear_bit(BIO_UPTODATE, &b->bi_flags);
  1786. b->bi_vcnt = vcnt;
  1787. b->bi_iter.bi_size = r1_bio->sectors << 9;
  1788. b->bi_iter.bi_sector = r1_bio->sector +
  1789. conf->mirrors[i].rdev->data_offset;
  1790. b->bi_bdev = conf->mirrors[i].rdev->bdev;
  1791. b->bi_end_io = end_sync_read;
  1792. b->bi_private = r1_bio;
  1793. size = b->bi_iter.bi_size;
  1794. for (j = 0; j < vcnt ; j++) {
  1795. struct bio_vec *bi;
  1796. bi = &b->bi_io_vec[j];
  1797. bi->bv_offset = 0;
  1798. if (size > PAGE_SIZE)
  1799. bi->bv_len = PAGE_SIZE;
  1800. else
  1801. bi->bv_len = size;
  1802. size -= PAGE_SIZE;
  1803. }
  1804. }
  1805. for (primary = 0; primary < conf->raid_disks * 2; primary++)
  1806. if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
  1807. test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
  1808. r1_bio->bios[primary]->bi_end_io = NULL;
  1809. rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
  1810. break;
  1811. }
  1812. r1_bio->read_disk = primary;
  1813. for (i = 0; i < conf->raid_disks * 2; i++) {
  1814. int j;
  1815. struct bio *pbio = r1_bio->bios[primary];
  1816. struct bio *sbio = r1_bio->bios[i];
  1817. int uptodate = test_bit(BIO_UPTODATE, &sbio->bi_flags);
  1818. if (sbio->bi_end_io != end_sync_read)
  1819. continue;
  1820. /* Now we can 'fixup' the BIO_UPTODATE flag */
  1821. set_bit(BIO_UPTODATE, &sbio->bi_flags);
  1822. if (uptodate) {
  1823. for (j = vcnt; j-- ; ) {
  1824. struct page *p, *s;
  1825. p = pbio->bi_io_vec[j].bv_page;
  1826. s = sbio->bi_io_vec[j].bv_page;
  1827. if (memcmp(page_address(p),
  1828. page_address(s),
  1829. sbio->bi_io_vec[j].bv_len))
  1830. break;
  1831. }
  1832. } else
  1833. j = 0;
  1834. if (j >= 0)
  1835. atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
  1836. if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
  1837. && uptodate)) {
  1838. /* No need to write to this device. */
  1839. sbio->bi_end_io = NULL;
  1840. rdev_dec_pending(conf->mirrors[i].rdev, mddev);
  1841. continue;
  1842. }
  1843. bio_copy_data(sbio, pbio);
  1844. }
  1845. }
  1846. static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
  1847. {
  1848. struct r1conf *conf = mddev->private;
  1849. int i;
  1850. int disks = conf->raid_disks * 2;
  1851. struct bio *bio, *wbio;
  1852. bio = r1_bio->bios[r1_bio->read_disk];
  1853. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  1854. /* ouch - failed to read all of that. */
  1855. if (!fix_sync_read_error(r1_bio))
  1856. return;
  1857. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1858. process_checks(r1_bio);
  1859. /*
  1860. * schedule writes
  1861. */
  1862. atomic_set(&r1_bio->remaining, 1);
  1863. for (i = 0; i < disks ; i++) {
  1864. wbio = r1_bio->bios[i];
  1865. if (wbio->bi_end_io == NULL ||
  1866. (wbio->bi_end_io == end_sync_read &&
  1867. (i == r1_bio->read_disk ||
  1868. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  1869. continue;
  1870. wbio->bi_rw = WRITE;
  1871. wbio->bi_end_io = end_sync_write;
  1872. atomic_inc(&r1_bio->remaining);
  1873. md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
  1874. generic_make_request(wbio);
  1875. }
  1876. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1877. /* if we're here, all write(s) have completed, so clean up */
  1878. int s = r1_bio->sectors;
  1879. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1880. test_bit(R1BIO_WriteError, &r1_bio->state))
  1881. reschedule_retry(r1_bio);
  1882. else {
  1883. put_buf(r1_bio);
  1884. md_done_sync(mddev, s, 1);
  1885. }
  1886. }
  1887. }
  1888. /*
  1889. * This is a kernel thread which:
  1890. *
  1891. * 1. Retries failed read operations on working mirrors.
  1892. * 2. Updates the raid superblock when problems encounter.
  1893. * 3. Performs writes following reads for array synchronising.
  1894. */
  1895. static void fix_read_error(struct r1conf *conf, int read_disk,
  1896. sector_t sect, int sectors)
  1897. {
  1898. struct mddev *mddev = conf->mddev;
  1899. while(sectors) {
  1900. int s = sectors;
  1901. int d = read_disk;
  1902. int success = 0;
  1903. int start;
  1904. struct md_rdev *rdev;
  1905. if (s > (PAGE_SIZE>>9))
  1906. s = PAGE_SIZE >> 9;
  1907. do {
  1908. /* Note: no rcu protection needed here
  1909. * as this is synchronous in the raid1d thread
  1910. * which is the thread that might remove
  1911. * a device. If raid1d ever becomes multi-threaded....
  1912. */
  1913. sector_t first_bad;
  1914. int bad_sectors;
  1915. rdev = conf->mirrors[d].rdev;
  1916. if (rdev &&
  1917. (test_bit(In_sync, &rdev->flags) ||
  1918. (!test_bit(Faulty, &rdev->flags) &&
  1919. rdev->recovery_offset >= sect + s)) &&
  1920. is_badblock(rdev, sect, s,
  1921. &first_bad, &bad_sectors) == 0 &&
  1922. sync_page_io(rdev, sect, s<<9,
  1923. conf->tmppage, READ, false))
  1924. success = 1;
  1925. else {
  1926. d++;
  1927. if (d == conf->raid_disks * 2)
  1928. d = 0;
  1929. }
  1930. } while (!success && d != read_disk);
  1931. if (!success) {
  1932. /* Cannot read from anywhere - mark it bad */
  1933. struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
  1934. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1935. md_error(mddev, rdev);
  1936. break;
  1937. }
  1938. /* write it back and re-read */
  1939. start = d;
  1940. while (d != read_disk) {
  1941. if (d==0)
  1942. d = conf->raid_disks * 2;
  1943. d--;
  1944. rdev = conf->mirrors[d].rdev;
  1945. if (rdev &&
  1946. !test_bit(Faulty, &rdev->flags))
  1947. r1_sync_page_io(rdev, sect, s,
  1948. conf->tmppage, WRITE);
  1949. }
  1950. d = start;
  1951. while (d != read_disk) {
  1952. char b[BDEVNAME_SIZE];
  1953. if (d==0)
  1954. d = conf->raid_disks * 2;
  1955. d--;
  1956. rdev = conf->mirrors[d].rdev;
  1957. if (rdev &&
  1958. !test_bit(Faulty, &rdev->flags)) {
  1959. if (r1_sync_page_io(rdev, sect, s,
  1960. conf->tmppage, READ)) {
  1961. atomic_add(s, &rdev->corrected_errors);
  1962. printk(KERN_INFO
  1963. "md/raid1:%s: read error corrected "
  1964. "(%d sectors at %llu on %s)\n",
  1965. mdname(mddev), s,
  1966. (unsigned long long)(sect +
  1967. rdev->data_offset),
  1968. bdevname(rdev->bdev, b));
  1969. }
  1970. }
  1971. }
  1972. sectors -= s;
  1973. sect += s;
  1974. }
  1975. }
  1976. static int narrow_write_error(struct r1bio *r1_bio, int i)
  1977. {
  1978. struct mddev *mddev = r1_bio->mddev;
  1979. struct r1conf *conf = mddev->private;
  1980. struct md_rdev *rdev = conf->mirrors[i].rdev;
  1981. /* bio has the data to be written to device 'i' where
  1982. * we just recently had a write error.
  1983. * We repeatedly clone the bio and trim down to one block,
  1984. * then try the write. Where the write fails we record
  1985. * a bad block.
  1986. * It is conceivable that the bio doesn't exactly align with
  1987. * blocks. We must handle this somehow.
  1988. *
  1989. * We currently own a reference on the rdev.
  1990. */
  1991. int block_sectors;
  1992. sector_t sector;
  1993. int sectors;
  1994. int sect_to_write = r1_bio->sectors;
  1995. int ok = 1;
  1996. if (rdev->badblocks.shift < 0)
  1997. return 0;
  1998. block_sectors = roundup(1 << rdev->badblocks.shift,
  1999. bdev_logical_block_size(rdev->bdev) >> 9);
  2000. sector = r1_bio->sector;
  2001. sectors = ((sector + block_sectors)
  2002. & ~(sector_t)(block_sectors - 1))
  2003. - sector;
  2004. while (sect_to_write) {
  2005. struct bio *wbio;
  2006. if (sectors > sect_to_write)
  2007. sectors = sect_to_write;
  2008. /* Write at 'sector' for 'sectors'*/
  2009. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  2010. unsigned vcnt = r1_bio->behind_page_count;
  2011. struct bio_vec *vec = r1_bio->behind_bvecs;
  2012. while (!vec->bv_page) {
  2013. vec++;
  2014. vcnt--;
  2015. }
  2016. wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
  2017. memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
  2018. wbio->bi_vcnt = vcnt;
  2019. } else {
  2020. wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
  2021. }
  2022. wbio->bi_rw = WRITE;
  2023. wbio->bi_iter.bi_sector = r1_bio->sector;
  2024. wbio->bi_iter.bi_size = r1_bio->sectors << 9;
  2025. bio_trim(wbio, sector - r1_bio->sector, sectors);
  2026. wbio->bi_iter.bi_sector += rdev->data_offset;
  2027. wbio->bi_bdev = rdev->bdev;
  2028. if (submit_bio_wait(WRITE, wbio) == 0)
  2029. /* failure! */
  2030. ok = rdev_set_badblocks(rdev, sector,
  2031. sectors, 0)
  2032. && ok;
  2033. bio_put(wbio);
  2034. sect_to_write -= sectors;
  2035. sector += sectors;
  2036. sectors = block_sectors;
  2037. }
  2038. return ok;
  2039. }
  2040. static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
  2041. {
  2042. int m;
  2043. int s = r1_bio->sectors;
  2044. for (m = 0; m < conf->raid_disks * 2 ; m++) {
  2045. struct md_rdev *rdev = conf->mirrors[m].rdev;
  2046. struct bio *bio = r1_bio->bios[m];
  2047. if (bio->bi_end_io == NULL)
  2048. continue;
  2049. if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
  2050. test_bit(R1BIO_MadeGood, &r1_bio->state)) {
  2051. rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
  2052. }
  2053. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
  2054. test_bit(R1BIO_WriteError, &r1_bio->state)) {
  2055. if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
  2056. md_error(conf->mddev, rdev);
  2057. }
  2058. }
  2059. put_buf(r1_bio);
  2060. md_done_sync(conf->mddev, s, 1);
  2061. }
  2062. static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
  2063. {
  2064. int m;
  2065. for (m = 0; m < conf->raid_disks * 2 ; m++)
  2066. if (r1_bio->bios[m] == IO_MADE_GOOD) {
  2067. struct md_rdev *rdev = conf->mirrors[m].rdev;
  2068. rdev_clear_badblocks(rdev,
  2069. r1_bio->sector,
  2070. r1_bio->sectors, 0);
  2071. rdev_dec_pending(rdev, conf->mddev);
  2072. } else if (r1_bio->bios[m] != NULL) {
  2073. /* This drive got a write error. We need to
  2074. * narrow down and record precise write
  2075. * errors.
  2076. */
  2077. if (!narrow_write_error(r1_bio, m)) {
  2078. md_error(conf->mddev,
  2079. conf->mirrors[m].rdev);
  2080. /* an I/O failed, we can't clear the bitmap */
  2081. set_bit(R1BIO_Degraded, &r1_bio->state);
  2082. }
  2083. rdev_dec_pending(conf->mirrors[m].rdev,
  2084. conf->mddev);
  2085. }
  2086. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  2087. close_write(r1_bio);
  2088. raid_end_bio_io(r1_bio);
  2089. }
  2090. static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
  2091. {
  2092. int disk;
  2093. int max_sectors;
  2094. struct mddev *mddev = conf->mddev;
  2095. struct bio *bio;
  2096. char b[BDEVNAME_SIZE];
  2097. struct md_rdev *rdev;
  2098. clear_bit(R1BIO_ReadError, &r1_bio->state);
  2099. /* we got a read error. Maybe the drive is bad. Maybe just
  2100. * the block and we can fix it.
  2101. * We freeze all other IO, and try reading the block from
  2102. * other devices. When we find one, we re-write
  2103. * and check it that fixes the read error.
  2104. * This is all done synchronously while the array is
  2105. * frozen
  2106. */
  2107. if (mddev->ro == 0) {
  2108. freeze_array(conf, 1);
  2109. fix_read_error(conf, r1_bio->read_disk,
  2110. r1_bio->sector, r1_bio->sectors);
  2111. unfreeze_array(conf);
  2112. } else
  2113. md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
  2114. rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
  2115. bio = r1_bio->bios[r1_bio->read_disk];
  2116. bdevname(bio->bi_bdev, b);
  2117. read_more:
  2118. disk = read_balance(conf, r1_bio, &max_sectors);
  2119. if (disk == -1) {
  2120. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
  2121. " read error for block %llu\n",
  2122. mdname(mddev), b, (unsigned long long)r1_bio->sector);
  2123. raid_end_bio_io(r1_bio);
  2124. } else {
  2125. const unsigned long do_sync
  2126. = r1_bio->master_bio->bi_rw & REQ_SYNC;
  2127. if (bio) {
  2128. r1_bio->bios[r1_bio->read_disk] =
  2129. mddev->ro ? IO_BLOCKED : NULL;
  2130. bio_put(bio);
  2131. }
  2132. r1_bio->read_disk = disk;
  2133. bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
  2134. bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
  2135. max_sectors);
  2136. r1_bio->bios[r1_bio->read_disk] = bio;
  2137. rdev = conf->mirrors[disk].rdev;
  2138. printk_ratelimited(KERN_ERR
  2139. "md/raid1:%s: redirecting sector %llu"
  2140. " to other mirror: %s\n",
  2141. mdname(mddev),
  2142. (unsigned long long)r1_bio->sector,
  2143. bdevname(rdev->bdev, b));
  2144. bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
  2145. bio->bi_bdev = rdev->bdev;
  2146. bio->bi_end_io = raid1_end_read_request;
  2147. bio->bi_rw = READ | do_sync;
  2148. bio->bi_private = r1_bio;
  2149. if (max_sectors < r1_bio->sectors) {
  2150. /* Drat - have to split this up more */
  2151. struct bio *mbio = r1_bio->master_bio;
  2152. int sectors_handled = (r1_bio->sector + max_sectors
  2153. - mbio->bi_iter.bi_sector);
  2154. r1_bio->sectors = max_sectors;
  2155. spin_lock_irq(&conf->device_lock);
  2156. if (mbio->bi_phys_segments == 0)
  2157. mbio->bi_phys_segments = 2;
  2158. else
  2159. mbio->bi_phys_segments++;
  2160. spin_unlock_irq(&conf->device_lock);
  2161. generic_make_request(bio);
  2162. bio = NULL;
  2163. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  2164. r1_bio->master_bio = mbio;
  2165. r1_bio->sectors = bio_sectors(mbio) - sectors_handled;
  2166. r1_bio->state = 0;
  2167. set_bit(R1BIO_ReadError, &r1_bio->state);
  2168. r1_bio->mddev = mddev;
  2169. r1_bio->sector = mbio->bi_iter.bi_sector +
  2170. sectors_handled;
  2171. goto read_more;
  2172. } else
  2173. generic_make_request(bio);
  2174. }
  2175. }
  2176. static void raid1d(struct md_thread *thread)
  2177. {
  2178. struct mddev *mddev = thread->mddev;
  2179. struct r1bio *r1_bio;
  2180. unsigned long flags;
  2181. struct r1conf *conf = mddev->private;
  2182. struct list_head *head = &conf->retry_list;
  2183. struct blk_plug plug;
  2184. md_check_recovery(mddev);
  2185. blk_start_plug(&plug);
  2186. for (;;) {
  2187. flush_pending_writes(conf);
  2188. spin_lock_irqsave(&conf->device_lock, flags);
  2189. if (list_empty(head)) {
  2190. spin_unlock_irqrestore(&conf->device_lock, flags);
  2191. break;
  2192. }
  2193. r1_bio = list_entry(head->prev, struct r1bio, retry_list);
  2194. list_del(head->prev);
  2195. conf->nr_queued--;
  2196. spin_unlock_irqrestore(&conf->device_lock, flags);
  2197. mddev = r1_bio->mddev;
  2198. conf = mddev->private;
  2199. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  2200. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  2201. test_bit(R1BIO_WriteError, &r1_bio->state))
  2202. handle_sync_write_finished(conf, r1_bio);
  2203. else
  2204. sync_request_write(mddev, r1_bio);
  2205. } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  2206. test_bit(R1BIO_WriteError, &r1_bio->state))
  2207. handle_write_finished(conf, r1_bio);
  2208. else if (test_bit(R1BIO_ReadError, &r1_bio->state))
  2209. handle_read_error(conf, r1_bio);
  2210. else
  2211. /* just a partial read to be scheduled from separate
  2212. * context
  2213. */
  2214. generic_make_request(r1_bio->bios[r1_bio->read_disk]);
  2215. cond_resched();
  2216. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2217. md_check_recovery(mddev);
  2218. }
  2219. blk_finish_plug(&plug);
  2220. }
  2221. static int init_resync(struct r1conf *conf)
  2222. {
  2223. int buffs;
  2224. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2225. BUG_ON(conf->r1buf_pool);
  2226. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  2227. conf->poolinfo);
  2228. if (!conf->r1buf_pool)
  2229. return -ENOMEM;
  2230. conf->next_resync = 0;
  2231. return 0;
  2232. }
  2233. /*
  2234. * perform a "sync" on one "block"
  2235. *
  2236. * We need to make sure that no normal I/O request - particularly write
  2237. * requests - conflict with active sync requests.
  2238. *
  2239. * This is achieved by tracking pending requests and a 'barrier' concept
  2240. * that can be installed to exclude normal IO requests.
  2241. */
  2242. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
  2243. {
  2244. struct r1conf *conf = mddev->private;
  2245. struct r1bio *r1_bio;
  2246. struct bio *bio;
  2247. sector_t max_sector, nr_sectors;
  2248. int disk = -1;
  2249. int i;
  2250. int wonly = -1;
  2251. int write_targets = 0, read_targets = 0;
  2252. sector_t sync_blocks;
  2253. int still_degraded = 0;
  2254. int good_sectors = RESYNC_SECTORS;
  2255. int min_bad = 0; /* number of sectors that are bad in all devices */
  2256. if (!conf->r1buf_pool)
  2257. if (init_resync(conf))
  2258. return 0;
  2259. max_sector = mddev->dev_sectors;
  2260. if (sector_nr >= max_sector) {
  2261. /* If we aborted, we need to abort the
  2262. * sync on the 'current' bitmap chunk (there will
  2263. * only be one in raid1 resync.
  2264. * We can find the current addess in mddev->curr_resync
  2265. */
  2266. if (mddev->curr_resync < max_sector) /* aborted */
  2267. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2268. &sync_blocks, 1);
  2269. else /* completed sync */
  2270. conf->fullsync = 0;
  2271. bitmap_close_sync(mddev->bitmap);
  2272. close_sync(conf);
  2273. return 0;
  2274. }
  2275. if (mddev->bitmap == NULL &&
  2276. mddev->recovery_cp == MaxSector &&
  2277. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2278. conf->fullsync == 0) {
  2279. *skipped = 1;
  2280. return max_sector - sector_nr;
  2281. }
  2282. /* before building a request, check if we can skip these blocks..
  2283. * This call the bitmap_start_sync doesn't actually record anything
  2284. */
  2285. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2286. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2287. /* We can skip this block, and probably several more */
  2288. *skipped = 1;
  2289. return sync_blocks;
  2290. }
  2291. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2292. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  2293. raise_barrier(conf, sector_nr);
  2294. rcu_read_lock();
  2295. /*
  2296. * If we get a correctably read error during resync or recovery,
  2297. * we might want to read from a different device. So we
  2298. * flag all drives that could conceivably be read from for READ,
  2299. * and any others (which will be non-In_sync devices) for WRITE.
  2300. * If a read fails, we try reading from something else for which READ
  2301. * is OK.
  2302. */
  2303. r1_bio->mddev = mddev;
  2304. r1_bio->sector = sector_nr;
  2305. r1_bio->state = 0;
  2306. set_bit(R1BIO_IsSync, &r1_bio->state);
  2307. for (i = 0; i < conf->raid_disks * 2; i++) {
  2308. struct md_rdev *rdev;
  2309. bio = r1_bio->bios[i];
  2310. bio_reset(bio);
  2311. rdev = rcu_dereference(conf->mirrors[i].rdev);
  2312. if (rdev == NULL ||
  2313. test_bit(Faulty, &rdev->flags)) {
  2314. if (i < conf->raid_disks)
  2315. still_degraded = 1;
  2316. } else if (!test_bit(In_sync, &rdev->flags)) {
  2317. bio->bi_rw = WRITE;
  2318. bio->bi_end_io = end_sync_write;
  2319. write_targets ++;
  2320. } else {
  2321. /* may need to read from here */
  2322. sector_t first_bad = MaxSector;
  2323. int bad_sectors;
  2324. if (is_badblock(rdev, sector_nr, good_sectors,
  2325. &first_bad, &bad_sectors)) {
  2326. if (first_bad > sector_nr)
  2327. good_sectors = first_bad - sector_nr;
  2328. else {
  2329. bad_sectors -= (sector_nr - first_bad);
  2330. if (min_bad == 0 ||
  2331. min_bad > bad_sectors)
  2332. min_bad = bad_sectors;
  2333. }
  2334. }
  2335. if (sector_nr < first_bad) {
  2336. if (test_bit(WriteMostly, &rdev->flags)) {
  2337. if (wonly < 0)
  2338. wonly = i;
  2339. } else {
  2340. if (disk < 0)
  2341. disk = i;
  2342. }
  2343. bio->bi_rw = READ;
  2344. bio->bi_end_io = end_sync_read;
  2345. read_targets++;
  2346. } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
  2347. test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
  2348. !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
  2349. /*
  2350. * The device is suitable for reading (InSync),
  2351. * but has bad block(s) here. Let's try to correct them,
  2352. * if we are doing resync or repair. Otherwise, leave
  2353. * this device alone for this sync request.
  2354. */
  2355. bio->bi_rw = WRITE;
  2356. bio->bi_end_io = end_sync_write;
  2357. write_targets++;
  2358. }
  2359. }
  2360. if (bio->bi_end_io) {
  2361. atomic_inc(&rdev->nr_pending);
  2362. bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
  2363. bio->bi_bdev = rdev->bdev;
  2364. bio->bi_private = r1_bio;
  2365. }
  2366. }
  2367. rcu_read_unlock();
  2368. if (disk < 0)
  2369. disk = wonly;
  2370. r1_bio->read_disk = disk;
  2371. if (read_targets == 0 && min_bad > 0) {
  2372. /* These sectors are bad on all InSync devices, so we
  2373. * need to mark them bad on all write targets
  2374. */
  2375. int ok = 1;
  2376. for (i = 0 ; i < conf->raid_disks * 2 ; i++)
  2377. if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
  2378. struct md_rdev *rdev = conf->mirrors[i].rdev;
  2379. ok = rdev_set_badblocks(rdev, sector_nr,
  2380. min_bad, 0
  2381. ) && ok;
  2382. }
  2383. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2384. *skipped = 1;
  2385. put_buf(r1_bio);
  2386. if (!ok) {
  2387. /* Cannot record the badblocks, so need to
  2388. * abort the resync.
  2389. * If there are multiple read targets, could just
  2390. * fail the really bad ones ???
  2391. */
  2392. conf->recovery_disabled = mddev->recovery_disabled;
  2393. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  2394. return 0;
  2395. } else
  2396. return min_bad;
  2397. }
  2398. if (min_bad > 0 && min_bad < good_sectors) {
  2399. /* only resync enough to reach the next bad->good
  2400. * transition */
  2401. good_sectors = min_bad;
  2402. }
  2403. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  2404. /* extra read targets are also write targets */
  2405. write_targets += read_targets-1;
  2406. if (write_targets == 0 || read_targets == 0) {
  2407. /* There is nowhere to write, so all non-sync
  2408. * drives must be failed - so we are finished
  2409. */
  2410. sector_t rv;
  2411. if (min_bad > 0)
  2412. max_sector = sector_nr + min_bad;
  2413. rv = max_sector - sector_nr;
  2414. *skipped = 1;
  2415. put_buf(r1_bio);
  2416. return rv;
  2417. }
  2418. if (max_sector > mddev->resync_max)
  2419. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2420. if (max_sector > sector_nr + good_sectors)
  2421. max_sector = sector_nr + good_sectors;
  2422. nr_sectors = 0;
  2423. sync_blocks = 0;
  2424. do {
  2425. struct page *page;
  2426. int len = PAGE_SIZE;
  2427. if (sector_nr + (len>>9) > max_sector)
  2428. len = (max_sector - sector_nr) << 9;
  2429. if (len == 0)
  2430. break;
  2431. if (sync_blocks == 0) {
  2432. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2433. &sync_blocks, still_degraded) &&
  2434. !conf->fullsync &&
  2435. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  2436. break;
  2437. BUG_ON(sync_blocks < (PAGE_SIZE>>9));
  2438. if ((len >> 9) > sync_blocks)
  2439. len = sync_blocks<<9;
  2440. }
  2441. for (i = 0 ; i < conf->raid_disks * 2; i++) {
  2442. bio = r1_bio->bios[i];
  2443. if (bio->bi_end_io) {
  2444. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2445. if (bio_add_page(bio, page, len, 0) == 0) {
  2446. /* stop here */
  2447. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2448. while (i > 0) {
  2449. i--;
  2450. bio = r1_bio->bios[i];
  2451. if (bio->bi_end_io==NULL)
  2452. continue;
  2453. /* remove last page from this bio */
  2454. bio->bi_vcnt--;
  2455. bio->bi_iter.bi_size -= len;
  2456. __clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  2457. }
  2458. goto bio_full;
  2459. }
  2460. }
  2461. }
  2462. nr_sectors += len>>9;
  2463. sector_nr += len>>9;
  2464. sync_blocks -= (len>>9);
  2465. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  2466. bio_full:
  2467. r1_bio->sectors = nr_sectors;
  2468. /* For a user-requested sync, we read all readable devices and do a
  2469. * compare
  2470. */
  2471. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2472. atomic_set(&r1_bio->remaining, read_targets);
  2473. for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
  2474. bio = r1_bio->bios[i];
  2475. if (bio->bi_end_io == end_sync_read) {
  2476. read_targets--;
  2477. md_sync_acct(bio->bi_bdev, nr_sectors);
  2478. generic_make_request(bio);
  2479. }
  2480. }
  2481. } else {
  2482. atomic_set(&r1_bio->remaining, 1);
  2483. bio = r1_bio->bios[r1_bio->read_disk];
  2484. md_sync_acct(bio->bi_bdev, nr_sectors);
  2485. generic_make_request(bio);
  2486. }
  2487. return nr_sectors;
  2488. }
  2489. static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2490. {
  2491. if (sectors)
  2492. return sectors;
  2493. return mddev->dev_sectors;
  2494. }
  2495. static struct r1conf *setup_conf(struct mddev *mddev)
  2496. {
  2497. struct r1conf *conf;
  2498. int i;
  2499. struct raid1_info *disk;
  2500. struct md_rdev *rdev;
  2501. int err = -ENOMEM;
  2502. conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
  2503. if (!conf)
  2504. goto abort;
  2505. conf->mirrors = kzalloc(sizeof(struct raid1_info)
  2506. * mddev->raid_disks * 2,
  2507. GFP_KERNEL);
  2508. if (!conf->mirrors)
  2509. goto abort;
  2510. conf->tmppage = alloc_page(GFP_KERNEL);
  2511. if (!conf->tmppage)
  2512. goto abort;
  2513. conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  2514. if (!conf->poolinfo)
  2515. goto abort;
  2516. conf->poolinfo->raid_disks = mddev->raid_disks * 2;
  2517. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2518. r1bio_pool_free,
  2519. conf->poolinfo);
  2520. if (!conf->r1bio_pool)
  2521. goto abort;
  2522. conf->poolinfo->mddev = mddev;
  2523. err = -EINVAL;
  2524. spin_lock_init(&conf->device_lock);
  2525. rdev_for_each(rdev, mddev) {
  2526. struct request_queue *q;
  2527. int disk_idx = rdev->raid_disk;
  2528. if (disk_idx >= mddev->raid_disks
  2529. || disk_idx < 0)
  2530. continue;
  2531. if (test_bit(Replacement, &rdev->flags))
  2532. disk = conf->mirrors + mddev->raid_disks + disk_idx;
  2533. else
  2534. disk = conf->mirrors + disk_idx;
  2535. if (disk->rdev)
  2536. goto abort;
  2537. disk->rdev = rdev;
  2538. q = bdev_get_queue(rdev->bdev);
  2539. if (q->merge_bvec_fn)
  2540. mddev->merge_check_needed = 1;
  2541. disk->head_position = 0;
  2542. disk->seq_start = MaxSector;
  2543. }
  2544. conf->raid_disks = mddev->raid_disks;
  2545. conf->mddev = mddev;
  2546. INIT_LIST_HEAD(&conf->retry_list);
  2547. spin_lock_init(&conf->resync_lock);
  2548. init_waitqueue_head(&conf->wait_barrier);
  2549. bio_list_init(&conf->pending_bio_list);
  2550. conf->pending_count = 0;
  2551. conf->recovery_disabled = mddev->recovery_disabled - 1;
  2552. conf->start_next_window = MaxSector;
  2553. conf->current_window_requests = conf->next_window_requests = 0;
  2554. err = -EIO;
  2555. for (i = 0; i < conf->raid_disks * 2; i++) {
  2556. disk = conf->mirrors + i;
  2557. if (i < conf->raid_disks &&
  2558. disk[conf->raid_disks].rdev) {
  2559. /* This slot has a replacement. */
  2560. if (!disk->rdev) {
  2561. /* No original, just make the replacement
  2562. * a recovering spare
  2563. */
  2564. disk->rdev =
  2565. disk[conf->raid_disks].rdev;
  2566. disk[conf->raid_disks].rdev = NULL;
  2567. } else if (!test_bit(In_sync, &disk->rdev->flags))
  2568. /* Original is not in_sync - bad */
  2569. goto abort;
  2570. }
  2571. if (!disk->rdev ||
  2572. !test_bit(In_sync, &disk->rdev->flags)) {
  2573. disk->head_position = 0;
  2574. if (disk->rdev &&
  2575. (disk->rdev->saved_raid_disk < 0))
  2576. conf->fullsync = 1;
  2577. }
  2578. }
  2579. err = -ENOMEM;
  2580. conf->thread = md_register_thread(raid1d, mddev, "raid1");
  2581. if (!conf->thread) {
  2582. printk(KERN_ERR
  2583. "md/raid1:%s: couldn't allocate thread\n",
  2584. mdname(mddev));
  2585. goto abort;
  2586. }
  2587. return conf;
  2588. abort:
  2589. if (conf) {
  2590. if (conf->r1bio_pool)
  2591. mempool_destroy(conf->r1bio_pool);
  2592. kfree(conf->mirrors);
  2593. safe_put_page(conf->tmppage);
  2594. kfree(conf->poolinfo);
  2595. kfree(conf);
  2596. }
  2597. return ERR_PTR(err);
  2598. }
  2599. static void raid1_free(struct mddev *mddev, void *priv);
  2600. static int run(struct mddev *mddev)
  2601. {
  2602. struct r1conf *conf;
  2603. int i;
  2604. struct md_rdev *rdev;
  2605. int ret;
  2606. bool discard_supported = false;
  2607. if (mddev->level != 1) {
  2608. printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
  2609. mdname(mddev), mddev->level);
  2610. return -EIO;
  2611. }
  2612. if (mddev->reshape_position != MaxSector) {
  2613. printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
  2614. mdname(mddev));
  2615. return -EIO;
  2616. }
  2617. /*
  2618. * copy the already verified devices into our private RAID1
  2619. * bookkeeping area. [whatever we allocate in run(),
  2620. * should be freed in raid1_free()]
  2621. */
  2622. if (mddev->private == NULL)
  2623. conf = setup_conf(mddev);
  2624. else
  2625. conf = mddev->private;
  2626. if (IS_ERR(conf))
  2627. return PTR_ERR(conf);
  2628. if (mddev->queue)
  2629. blk_queue_max_write_same_sectors(mddev->queue, 0);
  2630. rdev_for_each(rdev, mddev) {
  2631. if (!mddev->gendisk)
  2632. continue;
  2633. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2634. rdev->data_offset << 9);
  2635. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  2636. discard_supported = true;
  2637. }
  2638. mddev->degraded = 0;
  2639. for (i=0; i < conf->raid_disks; i++)
  2640. if (conf->mirrors[i].rdev == NULL ||
  2641. !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
  2642. test_bit(Faulty, &conf->mirrors[i].rdev->flags))
  2643. mddev->degraded++;
  2644. if (conf->raid_disks - mddev->degraded == 1)
  2645. mddev->recovery_cp = MaxSector;
  2646. if (mddev->recovery_cp != MaxSector)
  2647. printk(KERN_NOTICE "md/raid1:%s: not clean"
  2648. " -- starting background reconstruction\n",
  2649. mdname(mddev));
  2650. printk(KERN_INFO
  2651. "md/raid1:%s: active with %d out of %d mirrors\n",
  2652. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2653. mddev->raid_disks);
  2654. /*
  2655. * Ok, everything is just fine now
  2656. */
  2657. mddev->thread = conf->thread;
  2658. conf->thread = NULL;
  2659. mddev->private = conf;
  2660. md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
  2661. if (mddev->queue) {
  2662. if (discard_supported)
  2663. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  2664. mddev->queue);
  2665. else
  2666. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  2667. mddev->queue);
  2668. }
  2669. ret = md_integrity_register(mddev);
  2670. if (ret) {
  2671. md_unregister_thread(&mddev->thread);
  2672. raid1_free(mddev, conf);
  2673. }
  2674. return ret;
  2675. }
  2676. static void raid1_free(struct mddev *mddev, void *priv)
  2677. {
  2678. struct r1conf *conf = priv;
  2679. if (conf->r1bio_pool)
  2680. mempool_destroy(conf->r1bio_pool);
  2681. kfree(conf->mirrors);
  2682. safe_put_page(conf->tmppage);
  2683. kfree(conf->poolinfo);
  2684. kfree(conf);
  2685. }
  2686. static int raid1_resize(struct mddev *mddev, sector_t sectors)
  2687. {
  2688. /* no resync is happening, and there is enough space
  2689. * on all devices, so we can resize.
  2690. * We need to make sure resync covers any new space.
  2691. * If the array is shrinking we should possibly wait until
  2692. * any io in the removed space completes, but it hardly seems
  2693. * worth it.
  2694. */
  2695. sector_t newsize = raid1_size(mddev, sectors, 0);
  2696. if (mddev->external_size &&
  2697. mddev->array_sectors > newsize)
  2698. return -EINVAL;
  2699. if (mddev->bitmap) {
  2700. int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
  2701. if (ret)
  2702. return ret;
  2703. }
  2704. md_set_array_sectors(mddev, newsize);
  2705. set_capacity(mddev->gendisk, mddev->array_sectors);
  2706. revalidate_disk(mddev->gendisk);
  2707. if (sectors > mddev->dev_sectors &&
  2708. mddev->recovery_cp > mddev->dev_sectors) {
  2709. mddev->recovery_cp = mddev->dev_sectors;
  2710. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2711. }
  2712. mddev->dev_sectors = sectors;
  2713. mddev->resync_max_sectors = sectors;
  2714. return 0;
  2715. }
  2716. static int raid1_reshape(struct mddev *mddev)
  2717. {
  2718. /* We need to:
  2719. * 1/ resize the r1bio_pool
  2720. * 2/ resize conf->mirrors
  2721. *
  2722. * We allocate a new r1bio_pool if we can.
  2723. * Then raise a device barrier and wait until all IO stops.
  2724. * Then resize conf->mirrors and swap in the new r1bio pool.
  2725. *
  2726. * At the same time, we "pack" the devices so that all the missing
  2727. * devices have the higher raid_disk numbers.
  2728. */
  2729. mempool_t *newpool, *oldpool;
  2730. struct pool_info *newpoolinfo;
  2731. struct raid1_info *newmirrors;
  2732. struct r1conf *conf = mddev->private;
  2733. int cnt, raid_disks;
  2734. unsigned long flags;
  2735. int d, d2, err;
  2736. /* Cannot change chunk_size, layout, or level */
  2737. if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
  2738. mddev->layout != mddev->new_layout ||
  2739. mddev->level != mddev->new_level) {
  2740. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2741. mddev->new_layout = mddev->layout;
  2742. mddev->new_level = mddev->level;
  2743. return -EINVAL;
  2744. }
  2745. err = md_allow_write(mddev);
  2746. if (err)
  2747. return err;
  2748. raid_disks = mddev->raid_disks + mddev->delta_disks;
  2749. if (raid_disks < conf->raid_disks) {
  2750. cnt=0;
  2751. for (d= 0; d < conf->raid_disks; d++)
  2752. if (conf->mirrors[d].rdev)
  2753. cnt++;
  2754. if (cnt > raid_disks)
  2755. return -EBUSY;
  2756. }
  2757. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  2758. if (!newpoolinfo)
  2759. return -ENOMEM;
  2760. newpoolinfo->mddev = mddev;
  2761. newpoolinfo->raid_disks = raid_disks * 2;
  2762. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2763. r1bio_pool_free, newpoolinfo);
  2764. if (!newpool) {
  2765. kfree(newpoolinfo);
  2766. return -ENOMEM;
  2767. }
  2768. newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
  2769. GFP_KERNEL);
  2770. if (!newmirrors) {
  2771. kfree(newpoolinfo);
  2772. mempool_destroy(newpool);
  2773. return -ENOMEM;
  2774. }
  2775. freeze_array(conf, 0);
  2776. /* ok, everything is stopped */
  2777. oldpool = conf->r1bio_pool;
  2778. conf->r1bio_pool = newpool;
  2779. for (d = d2 = 0; d < conf->raid_disks; d++) {
  2780. struct md_rdev *rdev = conf->mirrors[d].rdev;
  2781. if (rdev && rdev->raid_disk != d2) {
  2782. sysfs_unlink_rdev(mddev, rdev);
  2783. rdev->raid_disk = d2;
  2784. sysfs_unlink_rdev(mddev, rdev);
  2785. if (sysfs_link_rdev(mddev, rdev))
  2786. printk(KERN_WARNING
  2787. "md/raid1:%s: cannot register rd%d\n",
  2788. mdname(mddev), rdev->raid_disk);
  2789. }
  2790. if (rdev)
  2791. newmirrors[d2++].rdev = rdev;
  2792. }
  2793. kfree(conf->mirrors);
  2794. conf->mirrors = newmirrors;
  2795. kfree(conf->poolinfo);
  2796. conf->poolinfo = newpoolinfo;
  2797. spin_lock_irqsave(&conf->device_lock, flags);
  2798. mddev->degraded += (raid_disks - conf->raid_disks);
  2799. spin_unlock_irqrestore(&conf->device_lock, flags);
  2800. conf->raid_disks = mddev->raid_disks = raid_disks;
  2801. mddev->delta_disks = 0;
  2802. unfreeze_array(conf);
  2803. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2804. md_wakeup_thread(mddev->thread);
  2805. mempool_destroy(oldpool);
  2806. return 0;
  2807. }
  2808. static void raid1_quiesce(struct mddev *mddev, int state)
  2809. {
  2810. struct r1conf *conf = mddev->private;
  2811. switch(state) {
  2812. case 2: /* wake for suspend */
  2813. wake_up(&conf->wait_barrier);
  2814. break;
  2815. case 1:
  2816. freeze_array(conf, 0);
  2817. break;
  2818. case 0:
  2819. unfreeze_array(conf);
  2820. break;
  2821. }
  2822. }
  2823. static void *raid1_takeover(struct mddev *mddev)
  2824. {
  2825. /* raid1 can take over:
  2826. * raid5 with 2 devices, any layout or chunk size
  2827. */
  2828. if (mddev->level == 5 && mddev->raid_disks == 2) {
  2829. struct r1conf *conf;
  2830. mddev->new_level = 1;
  2831. mddev->new_layout = 0;
  2832. mddev->new_chunk_sectors = 0;
  2833. conf = setup_conf(mddev);
  2834. if (!IS_ERR(conf))
  2835. /* Array must appear to be quiesced */
  2836. conf->array_frozen = 1;
  2837. return conf;
  2838. }
  2839. return ERR_PTR(-EINVAL);
  2840. }
  2841. static struct md_personality raid1_personality =
  2842. {
  2843. .name = "raid1",
  2844. .level = 1,
  2845. .owner = THIS_MODULE,
  2846. .make_request = make_request,
  2847. .run = run,
  2848. .free = raid1_free,
  2849. .status = status,
  2850. .error_handler = error,
  2851. .hot_add_disk = raid1_add_disk,
  2852. .hot_remove_disk= raid1_remove_disk,
  2853. .spare_active = raid1_spare_active,
  2854. .sync_request = sync_request,
  2855. .resize = raid1_resize,
  2856. .size = raid1_size,
  2857. .check_reshape = raid1_reshape,
  2858. .quiesce = raid1_quiesce,
  2859. .takeover = raid1_takeover,
  2860. .congested = raid1_congested,
  2861. .mergeable_bvec = raid1_mergeable_bvec,
  2862. };
  2863. static int __init raid_init(void)
  2864. {
  2865. return register_md_personality(&raid1_personality);
  2866. }
  2867. static void raid_exit(void)
  2868. {
  2869. unregister_md_personality(&raid1_personality);
  2870. }
  2871. module_init(raid_init);
  2872. module_exit(raid_exit);
  2873. MODULE_LICENSE("GPL");
  2874. MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
  2875. MODULE_ALIAS("md-personality-3"); /* RAID1 */
  2876. MODULE_ALIAS("md-raid1");
  2877. MODULE_ALIAS("md-level-1");
  2878. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);