raid1.c 64 KB

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