raid1.c 86 KB

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