raid1.c 68 KB

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