raid1.c 72 KB

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