raid1.c 60 KB

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