raid1.c 60 KB

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