raid1.c 87 KB

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