raid1.c 58 KB

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