raid1.c 59 KB

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