raid1.c 60 KB

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