raid1.c 59 KB

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