raid5.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->bm_write is the number of the last batch successfully written.
  30. * conf->bm_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is bm_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/highmem.h>
  47. #include <linux/bitops.h>
  48. #include <linux/kthread.h>
  49. #include <asm/atomic.h>
  50. #include "raid6.h"
  51. #include <linux/raid/bitmap.h>
  52. /*
  53. * Stripe cache
  54. */
  55. #define NR_STRIPES 256
  56. #define STRIPE_SIZE PAGE_SIZE
  57. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  58. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  59. #define IO_THRESHOLD 1
  60. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  61. #define HASH_MASK (NR_HASH - 1)
  62. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  63. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  64. * order without overlap. There may be several bio's per stripe+device, and
  65. * a bio could span several devices.
  66. * When walking this list for a particular stripe+device, we must never proceed
  67. * beyond a bio that extends past this device, as the next bio might no longer
  68. * be valid.
  69. * This macro is used to determine the 'next' bio in the list, given the sector
  70. * of the current stripe+device
  71. */
  72. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  73. /*
  74. * The following can be used to debug the driver
  75. */
  76. #define RAID5_PARANOIA 1
  77. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  78. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  79. #else
  80. # define CHECK_DEVLOCK()
  81. #endif
  82. #ifdef DEBUG
  83. #define inline
  84. #define __inline__
  85. #endif
  86. #if !RAID6_USE_EMPTY_ZERO_PAGE
  87. /* In .bss so it's zeroed */
  88. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  89. #endif
  90. static inline int raid6_next_disk(int disk, int raid_disks)
  91. {
  92. disk++;
  93. return (disk < raid_disks) ? disk : 0;
  94. }
  95. static void return_io(struct bio *return_bi)
  96. {
  97. struct bio *bi = return_bi;
  98. while (bi) {
  99. int bytes = bi->bi_size;
  100. return_bi = bi->bi_next;
  101. bi->bi_next = NULL;
  102. bi->bi_size = 0;
  103. bi->bi_end_io(bi, bytes,
  104. test_bit(BIO_UPTODATE, &bi->bi_flags)
  105. ? 0 : -EIO);
  106. bi = return_bi;
  107. }
  108. }
  109. static void print_raid5_conf (raid5_conf_t *conf);
  110. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  111. {
  112. if (atomic_dec_and_test(&sh->count)) {
  113. BUG_ON(!list_empty(&sh->lru));
  114. BUG_ON(atomic_read(&conf->active_stripes)==0);
  115. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  116. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  117. list_add_tail(&sh->lru, &conf->delayed_list);
  118. blk_plug_device(conf->mddev->queue);
  119. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  120. sh->bm_seq - conf->seq_write > 0) {
  121. list_add_tail(&sh->lru, &conf->bitmap_list);
  122. blk_plug_device(conf->mddev->queue);
  123. } else {
  124. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  125. list_add_tail(&sh->lru, &conf->handle_list);
  126. }
  127. md_wakeup_thread(conf->mddev->thread);
  128. } else {
  129. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  130. atomic_dec(&conf->preread_active_stripes);
  131. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  132. md_wakeup_thread(conf->mddev->thread);
  133. }
  134. atomic_dec(&conf->active_stripes);
  135. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  136. list_add_tail(&sh->lru, &conf->inactive_list);
  137. wake_up(&conf->wait_for_stripe);
  138. if (conf->retry_read_aligned)
  139. md_wakeup_thread(conf->mddev->thread);
  140. }
  141. }
  142. }
  143. }
  144. static void release_stripe(struct stripe_head *sh)
  145. {
  146. raid5_conf_t *conf = sh->raid_conf;
  147. unsigned long flags;
  148. spin_lock_irqsave(&conf->device_lock, flags);
  149. __release_stripe(conf, sh);
  150. spin_unlock_irqrestore(&conf->device_lock, flags);
  151. }
  152. static inline void remove_hash(struct stripe_head *sh)
  153. {
  154. pr_debug("remove_hash(), stripe %llu\n",
  155. (unsigned long long)sh->sector);
  156. hlist_del_init(&sh->hash);
  157. }
  158. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  159. {
  160. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  161. pr_debug("insert_hash(), stripe %llu\n",
  162. (unsigned long long)sh->sector);
  163. CHECK_DEVLOCK();
  164. hlist_add_head(&sh->hash, hp);
  165. }
  166. /* find an idle stripe, make sure it is unhashed, and return it. */
  167. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  168. {
  169. struct stripe_head *sh = NULL;
  170. struct list_head *first;
  171. CHECK_DEVLOCK();
  172. if (list_empty(&conf->inactive_list))
  173. goto out;
  174. first = conf->inactive_list.next;
  175. sh = list_entry(first, struct stripe_head, lru);
  176. list_del_init(first);
  177. remove_hash(sh);
  178. atomic_inc(&conf->active_stripes);
  179. out:
  180. return sh;
  181. }
  182. static void shrink_buffers(struct stripe_head *sh, int num)
  183. {
  184. struct page *p;
  185. int i;
  186. for (i=0; i<num ; i++) {
  187. p = sh->dev[i].page;
  188. if (!p)
  189. continue;
  190. sh->dev[i].page = NULL;
  191. put_page(p);
  192. }
  193. }
  194. static int grow_buffers(struct stripe_head *sh, int num)
  195. {
  196. int i;
  197. for (i=0; i<num; i++) {
  198. struct page *page;
  199. if (!(page = alloc_page(GFP_KERNEL))) {
  200. return 1;
  201. }
  202. sh->dev[i].page = page;
  203. }
  204. return 0;
  205. }
  206. static void raid5_build_block (struct stripe_head *sh, int i);
  207. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  208. {
  209. raid5_conf_t *conf = sh->raid_conf;
  210. int i;
  211. BUG_ON(atomic_read(&sh->count) != 0);
  212. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  213. CHECK_DEVLOCK();
  214. pr_debug("init_stripe called, stripe %llu\n",
  215. (unsigned long long)sh->sector);
  216. remove_hash(sh);
  217. sh->sector = sector;
  218. sh->pd_idx = pd_idx;
  219. sh->state = 0;
  220. sh->disks = disks;
  221. for (i = sh->disks; i--; ) {
  222. struct r5dev *dev = &sh->dev[i];
  223. if (dev->toread || dev->towrite || dev->written ||
  224. test_bit(R5_LOCKED, &dev->flags)) {
  225. printk("sector=%llx i=%d %p %p %p %d\n",
  226. (unsigned long long)sh->sector, i, dev->toread,
  227. dev->towrite, dev->written,
  228. test_bit(R5_LOCKED, &dev->flags));
  229. BUG();
  230. }
  231. dev->flags = 0;
  232. raid5_build_block(sh, i);
  233. }
  234. insert_hash(conf, sh);
  235. }
  236. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  237. {
  238. struct stripe_head *sh;
  239. struct hlist_node *hn;
  240. CHECK_DEVLOCK();
  241. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  242. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  243. if (sh->sector == sector && sh->disks == disks)
  244. return sh;
  245. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  246. return NULL;
  247. }
  248. static void unplug_slaves(mddev_t *mddev);
  249. static void raid5_unplug_device(request_queue_t *q);
  250. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  251. int pd_idx, int noblock)
  252. {
  253. struct stripe_head *sh;
  254. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  255. spin_lock_irq(&conf->device_lock);
  256. do {
  257. wait_event_lock_irq(conf->wait_for_stripe,
  258. conf->quiesce == 0,
  259. conf->device_lock, /* nothing */);
  260. sh = __find_stripe(conf, sector, disks);
  261. if (!sh) {
  262. if (!conf->inactive_blocked)
  263. sh = get_free_stripe(conf);
  264. if (noblock && sh == NULL)
  265. break;
  266. if (!sh) {
  267. conf->inactive_blocked = 1;
  268. wait_event_lock_irq(conf->wait_for_stripe,
  269. !list_empty(&conf->inactive_list) &&
  270. (atomic_read(&conf->active_stripes)
  271. < (conf->max_nr_stripes *3/4)
  272. || !conf->inactive_blocked),
  273. conf->device_lock,
  274. raid5_unplug_device(conf->mddev->queue)
  275. );
  276. conf->inactive_blocked = 0;
  277. } else
  278. init_stripe(sh, sector, pd_idx, disks);
  279. } else {
  280. if (atomic_read(&sh->count)) {
  281. BUG_ON(!list_empty(&sh->lru));
  282. } else {
  283. if (!test_bit(STRIPE_HANDLE, &sh->state))
  284. atomic_inc(&conf->active_stripes);
  285. if (list_empty(&sh->lru) &&
  286. !test_bit(STRIPE_EXPANDING, &sh->state))
  287. BUG();
  288. list_del_init(&sh->lru);
  289. }
  290. }
  291. } while (sh == NULL);
  292. if (sh)
  293. atomic_inc(&sh->count);
  294. spin_unlock_irq(&conf->device_lock);
  295. return sh;
  296. }
  297. static int grow_one_stripe(raid5_conf_t *conf)
  298. {
  299. struct stripe_head *sh;
  300. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  301. if (!sh)
  302. return 0;
  303. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  304. sh->raid_conf = conf;
  305. spin_lock_init(&sh->lock);
  306. if (grow_buffers(sh, conf->raid_disks)) {
  307. shrink_buffers(sh, conf->raid_disks);
  308. kmem_cache_free(conf->slab_cache, sh);
  309. return 0;
  310. }
  311. sh->disks = conf->raid_disks;
  312. /* we just created an active stripe so... */
  313. atomic_set(&sh->count, 1);
  314. atomic_inc(&conf->active_stripes);
  315. INIT_LIST_HEAD(&sh->lru);
  316. release_stripe(sh);
  317. return 1;
  318. }
  319. static int grow_stripes(raid5_conf_t *conf, int num)
  320. {
  321. struct kmem_cache *sc;
  322. int devs = conf->raid_disks;
  323. sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
  324. sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
  325. conf->active_name = 0;
  326. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  327. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  328. 0, 0, NULL, NULL);
  329. if (!sc)
  330. return 1;
  331. conf->slab_cache = sc;
  332. conf->pool_size = devs;
  333. while (num--)
  334. if (!grow_one_stripe(conf))
  335. return 1;
  336. return 0;
  337. }
  338. #ifdef CONFIG_MD_RAID5_RESHAPE
  339. static int resize_stripes(raid5_conf_t *conf, int newsize)
  340. {
  341. /* Make all the stripes able to hold 'newsize' devices.
  342. * New slots in each stripe get 'page' set to a new page.
  343. *
  344. * This happens in stages:
  345. * 1/ create a new kmem_cache and allocate the required number of
  346. * stripe_heads.
  347. * 2/ gather all the old stripe_heads and tranfer the pages across
  348. * to the new stripe_heads. This will have the side effect of
  349. * freezing the array as once all stripe_heads have been collected,
  350. * no IO will be possible. Old stripe heads are freed once their
  351. * pages have been transferred over, and the old kmem_cache is
  352. * freed when all stripes are done.
  353. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  354. * we simple return a failre status - no need to clean anything up.
  355. * 4/ allocate new pages for the new slots in the new stripe_heads.
  356. * If this fails, we don't bother trying the shrink the
  357. * stripe_heads down again, we just leave them as they are.
  358. * As each stripe_head is processed the new one is released into
  359. * active service.
  360. *
  361. * Once step2 is started, we cannot afford to wait for a write,
  362. * so we use GFP_NOIO allocations.
  363. */
  364. struct stripe_head *osh, *nsh;
  365. LIST_HEAD(newstripes);
  366. struct disk_info *ndisks;
  367. int err = 0;
  368. struct kmem_cache *sc;
  369. int i;
  370. if (newsize <= conf->pool_size)
  371. return 0; /* never bother to shrink */
  372. md_allow_write(conf->mddev);
  373. /* Step 1 */
  374. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  375. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  376. 0, 0, NULL, NULL);
  377. if (!sc)
  378. return -ENOMEM;
  379. for (i = conf->max_nr_stripes; i; i--) {
  380. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  381. if (!nsh)
  382. break;
  383. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  384. nsh->raid_conf = conf;
  385. spin_lock_init(&nsh->lock);
  386. list_add(&nsh->lru, &newstripes);
  387. }
  388. if (i) {
  389. /* didn't get enough, give up */
  390. while (!list_empty(&newstripes)) {
  391. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  392. list_del(&nsh->lru);
  393. kmem_cache_free(sc, nsh);
  394. }
  395. kmem_cache_destroy(sc);
  396. return -ENOMEM;
  397. }
  398. /* Step 2 - Must use GFP_NOIO now.
  399. * OK, we have enough stripes, start collecting inactive
  400. * stripes and copying them over
  401. */
  402. list_for_each_entry(nsh, &newstripes, lru) {
  403. spin_lock_irq(&conf->device_lock);
  404. wait_event_lock_irq(conf->wait_for_stripe,
  405. !list_empty(&conf->inactive_list),
  406. conf->device_lock,
  407. unplug_slaves(conf->mddev)
  408. );
  409. osh = get_free_stripe(conf);
  410. spin_unlock_irq(&conf->device_lock);
  411. atomic_set(&nsh->count, 1);
  412. for(i=0; i<conf->pool_size; i++)
  413. nsh->dev[i].page = osh->dev[i].page;
  414. for( ; i<newsize; i++)
  415. nsh->dev[i].page = NULL;
  416. kmem_cache_free(conf->slab_cache, osh);
  417. }
  418. kmem_cache_destroy(conf->slab_cache);
  419. /* Step 3.
  420. * At this point, we are holding all the stripes so the array
  421. * is completely stalled, so now is a good time to resize
  422. * conf->disks.
  423. */
  424. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  425. if (ndisks) {
  426. for (i=0; i<conf->raid_disks; i++)
  427. ndisks[i] = conf->disks[i];
  428. kfree(conf->disks);
  429. conf->disks = ndisks;
  430. } else
  431. err = -ENOMEM;
  432. /* Step 4, return new stripes to service */
  433. while(!list_empty(&newstripes)) {
  434. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  435. list_del_init(&nsh->lru);
  436. for (i=conf->raid_disks; i < newsize; i++)
  437. if (nsh->dev[i].page == NULL) {
  438. struct page *p = alloc_page(GFP_NOIO);
  439. nsh->dev[i].page = p;
  440. if (!p)
  441. err = -ENOMEM;
  442. }
  443. release_stripe(nsh);
  444. }
  445. /* critical section pass, GFP_NOIO no longer needed */
  446. conf->slab_cache = sc;
  447. conf->active_name = 1-conf->active_name;
  448. conf->pool_size = newsize;
  449. return err;
  450. }
  451. #endif
  452. static int drop_one_stripe(raid5_conf_t *conf)
  453. {
  454. struct stripe_head *sh;
  455. spin_lock_irq(&conf->device_lock);
  456. sh = get_free_stripe(conf);
  457. spin_unlock_irq(&conf->device_lock);
  458. if (!sh)
  459. return 0;
  460. BUG_ON(atomic_read(&sh->count));
  461. shrink_buffers(sh, conf->pool_size);
  462. kmem_cache_free(conf->slab_cache, sh);
  463. atomic_dec(&conf->active_stripes);
  464. return 1;
  465. }
  466. static void shrink_stripes(raid5_conf_t *conf)
  467. {
  468. while (drop_one_stripe(conf))
  469. ;
  470. if (conf->slab_cache)
  471. kmem_cache_destroy(conf->slab_cache);
  472. conf->slab_cache = NULL;
  473. }
  474. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  475. int error)
  476. {
  477. struct stripe_head *sh = bi->bi_private;
  478. raid5_conf_t *conf = sh->raid_conf;
  479. int disks = sh->disks, i;
  480. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  481. char b[BDEVNAME_SIZE];
  482. mdk_rdev_t *rdev;
  483. if (bi->bi_size)
  484. return 1;
  485. for (i=0 ; i<disks; i++)
  486. if (bi == &sh->dev[i].req)
  487. break;
  488. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  489. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  490. uptodate);
  491. if (i == disks) {
  492. BUG();
  493. return 0;
  494. }
  495. if (uptodate) {
  496. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  497. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  498. rdev = conf->disks[i].rdev;
  499. printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
  500. mdname(conf->mddev), STRIPE_SECTORS,
  501. (unsigned long long)sh->sector + rdev->data_offset,
  502. bdevname(rdev->bdev, b));
  503. clear_bit(R5_ReadError, &sh->dev[i].flags);
  504. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  505. }
  506. if (atomic_read(&conf->disks[i].rdev->read_errors))
  507. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  508. } else {
  509. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  510. int retry = 0;
  511. rdev = conf->disks[i].rdev;
  512. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  513. atomic_inc(&rdev->read_errors);
  514. if (conf->mddev->degraded)
  515. printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
  516. mdname(conf->mddev),
  517. (unsigned long long)sh->sector + rdev->data_offset,
  518. bdn);
  519. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  520. /* Oh, no!!! */
  521. printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
  522. mdname(conf->mddev),
  523. (unsigned long long)sh->sector + rdev->data_offset,
  524. bdn);
  525. else if (atomic_read(&rdev->read_errors)
  526. > conf->max_nr_stripes)
  527. printk(KERN_WARNING
  528. "raid5:%s: Too many read errors, failing device %s.\n",
  529. mdname(conf->mddev), bdn);
  530. else
  531. retry = 1;
  532. if (retry)
  533. set_bit(R5_ReadError, &sh->dev[i].flags);
  534. else {
  535. clear_bit(R5_ReadError, &sh->dev[i].flags);
  536. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  537. md_error(conf->mddev, rdev);
  538. }
  539. }
  540. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  541. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  542. set_bit(STRIPE_HANDLE, &sh->state);
  543. release_stripe(sh);
  544. return 0;
  545. }
  546. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  547. int error)
  548. {
  549. struct stripe_head *sh = bi->bi_private;
  550. raid5_conf_t *conf = sh->raid_conf;
  551. int disks = sh->disks, i;
  552. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  553. if (bi->bi_size)
  554. return 1;
  555. for (i=0 ; i<disks; i++)
  556. if (bi == &sh->dev[i].req)
  557. break;
  558. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  559. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  560. uptodate);
  561. if (i == disks) {
  562. BUG();
  563. return 0;
  564. }
  565. if (!uptodate)
  566. md_error(conf->mddev, conf->disks[i].rdev);
  567. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  568. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  569. set_bit(STRIPE_HANDLE, &sh->state);
  570. release_stripe(sh);
  571. return 0;
  572. }
  573. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  574. static void raid5_build_block (struct stripe_head *sh, int i)
  575. {
  576. struct r5dev *dev = &sh->dev[i];
  577. bio_init(&dev->req);
  578. dev->req.bi_io_vec = &dev->vec;
  579. dev->req.bi_vcnt++;
  580. dev->req.bi_max_vecs++;
  581. dev->vec.bv_page = dev->page;
  582. dev->vec.bv_len = STRIPE_SIZE;
  583. dev->vec.bv_offset = 0;
  584. dev->req.bi_sector = sh->sector;
  585. dev->req.bi_private = sh;
  586. dev->flags = 0;
  587. dev->sector = compute_blocknr(sh, i);
  588. }
  589. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  590. {
  591. char b[BDEVNAME_SIZE];
  592. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  593. pr_debug("raid5: error called\n");
  594. if (!test_bit(Faulty, &rdev->flags)) {
  595. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  596. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  597. unsigned long flags;
  598. spin_lock_irqsave(&conf->device_lock, flags);
  599. mddev->degraded++;
  600. spin_unlock_irqrestore(&conf->device_lock, flags);
  601. /*
  602. * if recovery was running, make sure it aborts.
  603. */
  604. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  605. }
  606. set_bit(Faulty, &rdev->flags);
  607. printk (KERN_ALERT
  608. "raid5: Disk failure on %s, disabling device."
  609. " Operation continuing on %d devices\n",
  610. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  611. }
  612. }
  613. /*
  614. * Input: a 'big' sector number,
  615. * Output: index of the data and parity disk, and the sector # in them.
  616. */
  617. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  618. unsigned int data_disks, unsigned int * dd_idx,
  619. unsigned int * pd_idx, raid5_conf_t *conf)
  620. {
  621. long stripe;
  622. unsigned long chunk_number;
  623. unsigned int chunk_offset;
  624. sector_t new_sector;
  625. int sectors_per_chunk = conf->chunk_size >> 9;
  626. /* First compute the information on this sector */
  627. /*
  628. * Compute the chunk number and the sector offset inside the chunk
  629. */
  630. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  631. chunk_number = r_sector;
  632. BUG_ON(r_sector != chunk_number);
  633. /*
  634. * Compute the stripe number
  635. */
  636. stripe = chunk_number / data_disks;
  637. /*
  638. * Compute the data disk and parity disk indexes inside the stripe
  639. */
  640. *dd_idx = chunk_number % data_disks;
  641. /*
  642. * Select the parity disk based on the user selected algorithm.
  643. */
  644. switch(conf->level) {
  645. case 4:
  646. *pd_idx = data_disks;
  647. break;
  648. case 5:
  649. switch (conf->algorithm) {
  650. case ALGORITHM_LEFT_ASYMMETRIC:
  651. *pd_idx = data_disks - stripe % raid_disks;
  652. if (*dd_idx >= *pd_idx)
  653. (*dd_idx)++;
  654. break;
  655. case ALGORITHM_RIGHT_ASYMMETRIC:
  656. *pd_idx = stripe % raid_disks;
  657. if (*dd_idx >= *pd_idx)
  658. (*dd_idx)++;
  659. break;
  660. case ALGORITHM_LEFT_SYMMETRIC:
  661. *pd_idx = data_disks - stripe % raid_disks;
  662. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  663. break;
  664. case ALGORITHM_RIGHT_SYMMETRIC:
  665. *pd_idx = stripe % raid_disks;
  666. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  667. break;
  668. default:
  669. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  670. conf->algorithm);
  671. }
  672. break;
  673. case 6:
  674. /**** FIX THIS ****/
  675. switch (conf->algorithm) {
  676. case ALGORITHM_LEFT_ASYMMETRIC:
  677. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  678. if (*pd_idx == raid_disks-1)
  679. (*dd_idx)++; /* Q D D D P */
  680. else if (*dd_idx >= *pd_idx)
  681. (*dd_idx) += 2; /* D D P Q D */
  682. break;
  683. case ALGORITHM_RIGHT_ASYMMETRIC:
  684. *pd_idx = stripe % raid_disks;
  685. if (*pd_idx == raid_disks-1)
  686. (*dd_idx)++; /* Q D D D P */
  687. else if (*dd_idx >= *pd_idx)
  688. (*dd_idx) += 2; /* D D P Q D */
  689. break;
  690. case ALGORITHM_LEFT_SYMMETRIC:
  691. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  692. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  693. break;
  694. case ALGORITHM_RIGHT_SYMMETRIC:
  695. *pd_idx = stripe % raid_disks;
  696. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  697. break;
  698. default:
  699. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  700. conf->algorithm);
  701. }
  702. break;
  703. }
  704. /*
  705. * Finally, compute the new sector number
  706. */
  707. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  708. return new_sector;
  709. }
  710. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  711. {
  712. raid5_conf_t *conf = sh->raid_conf;
  713. int raid_disks = sh->disks;
  714. int data_disks = raid_disks - conf->max_degraded;
  715. sector_t new_sector = sh->sector, check;
  716. int sectors_per_chunk = conf->chunk_size >> 9;
  717. sector_t stripe;
  718. int chunk_offset;
  719. int chunk_number, dummy1, dummy2, dd_idx = i;
  720. sector_t r_sector;
  721. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  722. stripe = new_sector;
  723. BUG_ON(new_sector != stripe);
  724. if (i == sh->pd_idx)
  725. return 0;
  726. switch(conf->level) {
  727. case 4: break;
  728. case 5:
  729. switch (conf->algorithm) {
  730. case ALGORITHM_LEFT_ASYMMETRIC:
  731. case ALGORITHM_RIGHT_ASYMMETRIC:
  732. if (i > sh->pd_idx)
  733. i--;
  734. break;
  735. case ALGORITHM_LEFT_SYMMETRIC:
  736. case ALGORITHM_RIGHT_SYMMETRIC:
  737. if (i < sh->pd_idx)
  738. i += raid_disks;
  739. i -= (sh->pd_idx + 1);
  740. break;
  741. default:
  742. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  743. conf->algorithm);
  744. }
  745. break;
  746. case 6:
  747. if (i == raid6_next_disk(sh->pd_idx, raid_disks))
  748. return 0; /* It is the Q disk */
  749. switch (conf->algorithm) {
  750. case ALGORITHM_LEFT_ASYMMETRIC:
  751. case ALGORITHM_RIGHT_ASYMMETRIC:
  752. if (sh->pd_idx == raid_disks-1)
  753. i--; /* Q D D D P */
  754. else if (i > sh->pd_idx)
  755. i -= 2; /* D D P Q D */
  756. break;
  757. case ALGORITHM_LEFT_SYMMETRIC:
  758. case ALGORITHM_RIGHT_SYMMETRIC:
  759. if (sh->pd_idx == raid_disks-1)
  760. i--; /* Q D D D P */
  761. else {
  762. /* D D P Q D */
  763. if (i < sh->pd_idx)
  764. i += raid_disks;
  765. i -= (sh->pd_idx + 2);
  766. }
  767. break;
  768. default:
  769. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  770. conf->algorithm);
  771. }
  772. break;
  773. }
  774. chunk_number = stripe * data_disks + i;
  775. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  776. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  777. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  778. printk(KERN_ERR "compute_blocknr: map not correct\n");
  779. return 0;
  780. }
  781. return r_sector;
  782. }
  783. /*
  784. * Copy data between a page in the stripe cache, and one or more bion
  785. * The page could align with the middle of the bio, or there could be
  786. * several bion, each with several bio_vecs, which cover part of the page
  787. * Multiple bion are linked together on bi_next. There may be extras
  788. * at the end of this list. We ignore them.
  789. */
  790. static void copy_data(int frombio, struct bio *bio,
  791. struct page *page,
  792. sector_t sector)
  793. {
  794. char *pa = page_address(page);
  795. struct bio_vec *bvl;
  796. int i;
  797. int page_offset;
  798. if (bio->bi_sector >= sector)
  799. page_offset = (signed)(bio->bi_sector - sector) * 512;
  800. else
  801. page_offset = (signed)(sector - bio->bi_sector) * -512;
  802. bio_for_each_segment(bvl, bio, i) {
  803. int len = bio_iovec_idx(bio,i)->bv_len;
  804. int clen;
  805. int b_offset = 0;
  806. if (page_offset < 0) {
  807. b_offset = -page_offset;
  808. page_offset += b_offset;
  809. len -= b_offset;
  810. }
  811. if (len > 0 && page_offset + len > STRIPE_SIZE)
  812. clen = STRIPE_SIZE - page_offset;
  813. else clen = len;
  814. if (clen > 0) {
  815. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  816. if (frombio)
  817. memcpy(pa+page_offset, ba+b_offset, clen);
  818. else
  819. memcpy(ba+b_offset, pa+page_offset, clen);
  820. __bio_kunmap_atomic(ba, KM_USER0);
  821. }
  822. if (clen < len) /* hit end of page */
  823. break;
  824. page_offset += len;
  825. }
  826. }
  827. #define check_xor() do { \
  828. if (count == MAX_XOR_BLOCKS) { \
  829. xor_blocks(count, STRIPE_SIZE, dest, ptr);\
  830. count = 0; \
  831. } \
  832. } while(0)
  833. static void compute_block(struct stripe_head *sh, int dd_idx)
  834. {
  835. int i, count, disks = sh->disks;
  836. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  837. pr_debug("compute_block, stripe %llu, idx %d\n",
  838. (unsigned long long)sh->sector, dd_idx);
  839. dest = page_address(sh->dev[dd_idx].page);
  840. memset(dest, 0, STRIPE_SIZE);
  841. count = 0;
  842. for (i = disks ; i--; ) {
  843. if (i == dd_idx)
  844. continue;
  845. p = page_address(sh->dev[i].page);
  846. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  847. ptr[count++] = p;
  848. else
  849. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  850. " not present\n", dd_idx,
  851. (unsigned long long)sh->sector, i);
  852. check_xor();
  853. }
  854. if (count)
  855. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  856. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  857. }
  858. static void compute_parity5(struct stripe_head *sh, int method)
  859. {
  860. raid5_conf_t *conf = sh->raid_conf;
  861. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  862. void *ptr[MAX_XOR_BLOCKS], *dest;
  863. struct bio *chosen;
  864. pr_debug("compute_parity5, stripe %llu, method %d\n",
  865. (unsigned long long)sh->sector, method);
  866. count = 0;
  867. dest = page_address(sh->dev[pd_idx].page);
  868. switch(method) {
  869. case READ_MODIFY_WRITE:
  870. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
  871. for (i=disks ; i-- ;) {
  872. if (i==pd_idx)
  873. continue;
  874. if (sh->dev[i].towrite &&
  875. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  876. ptr[count++] = page_address(sh->dev[i].page);
  877. chosen = sh->dev[i].towrite;
  878. sh->dev[i].towrite = NULL;
  879. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  880. wake_up(&conf->wait_for_overlap);
  881. BUG_ON(sh->dev[i].written);
  882. sh->dev[i].written = chosen;
  883. check_xor();
  884. }
  885. }
  886. break;
  887. case RECONSTRUCT_WRITE:
  888. memset(dest, 0, STRIPE_SIZE);
  889. for (i= disks; i-- ;)
  890. if (i!=pd_idx && sh->dev[i].towrite) {
  891. chosen = sh->dev[i].towrite;
  892. sh->dev[i].towrite = NULL;
  893. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  894. wake_up(&conf->wait_for_overlap);
  895. BUG_ON(sh->dev[i].written);
  896. sh->dev[i].written = chosen;
  897. }
  898. break;
  899. case CHECK_PARITY:
  900. break;
  901. }
  902. if (count) {
  903. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  904. count = 0;
  905. }
  906. for (i = disks; i--;)
  907. if (sh->dev[i].written) {
  908. sector_t sector = sh->dev[i].sector;
  909. struct bio *wbi = sh->dev[i].written;
  910. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  911. copy_data(1, wbi, sh->dev[i].page, sector);
  912. wbi = r5_next_bio(wbi, sector);
  913. }
  914. set_bit(R5_LOCKED, &sh->dev[i].flags);
  915. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  916. }
  917. switch(method) {
  918. case RECONSTRUCT_WRITE:
  919. case CHECK_PARITY:
  920. for (i=disks; i--;)
  921. if (i != pd_idx) {
  922. ptr[count++] = page_address(sh->dev[i].page);
  923. check_xor();
  924. }
  925. break;
  926. case READ_MODIFY_WRITE:
  927. for (i = disks; i--;)
  928. if (sh->dev[i].written) {
  929. ptr[count++] = page_address(sh->dev[i].page);
  930. check_xor();
  931. }
  932. }
  933. if (count)
  934. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  935. if (method != CHECK_PARITY) {
  936. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  937. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  938. } else
  939. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  940. }
  941. static void compute_parity6(struct stripe_head *sh, int method)
  942. {
  943. raid6_conf_t *conf = sh->raid_conf;
  944. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  945. struct bio *chosen;
  946. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  947. void *ptrs[disks];
  948. qd_idx = raid6_next_disk(pd_idx, disks);
  949. d0_idx = raid6_next_disk(qd_idx, disks);
  950. pr_debug("compute_parity, stripe %llu, method %d\n",
  951. (unsigned long long)sh->sector, method);
  952. switch(method) {
  953. case READ_MODIFY_WRITE:
  954. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  955. case RECONSTRUCT_WRITE:
  956. for (i= disks; i-- ;)
  957. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  958. chosen = sh->dev[i].towrite;
  959. sh->dev[i].towrite = NULL;
  960. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  961. wake_up(&conf->wait_for_overlap);
  962. BUG_ON(sh->dev[i].written);
  963. sh->dev[i].written = chosen;
  964. }
  965. break;
  966. case CHECK_PARITY:
  967. BUG(); /* Not implemented yet */
  968. }
  969. for (i = disks; i--;)
  970. if (sh->dev[i].written) {
  971. sector_t sector = sh->dev[i].sector;
  972. struct bio *wbi = sh->dev[i].written;
  973. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  974. copy_data(1, wbi, sh->dev[i].page, sector);
  975. wbi = r5_next_bio(wbi, sector);
  976. }
  977. set_bit(R5_LOCKED, &sh->dev[i].flags);
  978. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  979. }
  980. // switch(method) {
  981. // case RECONSTRUCT_WRITE:
  982. // case CHECK_PARITY:
  983. // case UPDATE_PARITY:
  984. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  985. /* FIX: Is this ordering of drives even remotely optimal? */
  986. count = 0;
  987. i = d0_idx;
  988. do {
  989. ptrs[count++] = page_address(sh->dev[i].page);
  990. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  991. printk("block %d/%d not uptodate on parity calc\n", i,count);
  992. i = raid6_next_disk(i, disks);
  993. } while ( i != d0_idx );
  994. // break;
  995. // }
  996. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  997. switch(method) {
  998. case RECONSTRUCT_WRITE:
  999. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1000. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1001. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1002. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1003. break;
  1004. case UPDATE_PARITY:
  1005. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1006. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1007. break;
  1008. }
  1009. }
  1010. /* Compute one missing block */
  1011. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1012. {
  1013. int i, count, disks = sh->disks;
  1014. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  1015. int pd_idx = sh->pd_idx;
  1016. int qd_idx = raid6_next_disk(pd_idx, disks);
  1017. pr_debug("compute_block_1, stripe %llu, idx %d\n",
  1018. (unsigned long long)sh->sector, dd_idx);
  1019. if ( dd_idx == qd_idx ) {
  1020. /* We're actually computing the Q drive */
  1021. compute_parity6(sh, UPDATE_PARITY);
  1022. } else {
  1023. dest = page_address(sh->dev[dd_idx].page);
  1024. if (!nozero) memset(dest, 0, STRIPE_SIZE);
  1025. count = 0;
  1026. for (i = disks ; i--; ) {
  1027. if (i == dd_idx || i == qd_idx)
  1028. continue;
  1029. p = page_address(sh->dev[i].page);
  1030. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1031. ptr[count++] = p;
  1032. else
  1033. printk("compute_block() %d, stripe %llu, %d"
  1034. " not present\n", dd_idx,
  1035. (unsigned long long)sh->sector, i);
  1036. check_xor();
  1037. }
  1038. if (count)
  1039. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  1040. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1041. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1042. }
  1043. }
  1044. /* Compute two missing blocks */
  1045. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1046. {
  1047. int i, count, disks = sh->disks;
  1048. int pd_idx = sh->pd_idx;
  1049. int qd_idx = raid6_next_disk(pd_idx, disks);
  1050. int d0_idx = raid6_next_disk(qd_idx, disks);
  1051. int faila, failb;
  1052. /* faila and failb are disk numbers relative to d0_idx */
  1053. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1054. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1055. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1056. BUG_ON(faila == failb);
  1057. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1058. pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1059. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1060. if ( failb == disks-1 ) {
  1061. /* Q disk is one of the missing disks */
  1062. if ( faila == disks-2 ) {
  1063. /* Missing P+Q, just recompute */
  1064. compute_parity6(sh, UPDATE_PARITY);
  1065. return;
  1066. } else {
  1067. /* We're missing D+Q; recompute D from P */
  1068. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1069. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1070. return;
  1071. }
  1072. }
  1073. /* We're missing D+P or D+D; build pointer table */
  1074. {
  1075. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1076. void *ptrs[disks];
  1077. count = 0;
  1078. i = d0_idx;
  1079. do {
  1080. ptrs[count++] = page_address(sh->dev[i].page);
  1081. i = raid6_next_disk(i, disks);
  1082. if (i != dd_idx1 && i != dd_idx2 &&
  1083. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1084. printk("compute_2 with missing block %d/%d\n", count, i);
  1085. } while ( i != d0_idx );
  1086. if ( failb == disks-2 ) {
  1087. /* We're missing D+P. */
  1088. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1089. } else {
  1090. /* We're missing D+D. */
  1091. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1092. }
  1093. /* Both the above update both missing blocks */
  1094. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1095. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1096. }
  1097. }
  1098. /*
  1099. * Each stripe/dev can have one or more bion attached.
  1100. * toread/towrite point to the first in a chain.
  1101. * The bi_next chain must be in order.
  1102. */
  1103. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1104. {
  1105. struct bio **bip;
  1106. raid5_conf_t *conf = sh->raid_conf;
  1107. int firstwrite=0;
  1108. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1109. (unsigned long long)bi->bi_sector,
  1110. (unsigned long long)sh->sector);
  1111. spin_lock(&sh->lock);
  1112. spin_lock_irq(&conf->device_lock);
  1113. if (forwrite) {
  1114. bip = &sh->dev[dd_idx].towrite;
  1115. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1116. firstwrite = 1;
  1117. } else
  1118. bip = &sh->dev[dd_idx].toread;
  1119. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1120. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1121. goto overlap;
  1122. bip = & (*bip)->bi_next;
  1123. }
  1124. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1125. goto overlap;
  1126. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1127. if (*bip)
  1128. bi->bi_next = *bip;
  1129. *bip = bi;
  1130. bi->bi_phys_segments ++;
  1131. spin_unlock_irq(&conf->device_lock);
  1132. spin_unlock(&sh->lock);
  1133. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1134. (unsigned long long)bi->bi_sector,
  1135. (unsigned long long)sh->sector, dd_idx);
  1136. if (conf->mddev->bitmap && firstwrite) {
  1137. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1138. STRIPE_SECTORS, 0);
  1139. sh->bm_seq = conf->seq_flush+1;
  1140. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1141. }
  1142. if (forwrite) {
  1143. /* check if page is covered */
  1144. sector_t sector = sh->dev[dd_idx].sector;
  1145. for (bi=sh->dev[dd_idx].towrite;
  1146. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1147. bi && bi->bi_sector <= sector;
  1148. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1149. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1150. sector = bi->bi_sector + (bi->bi_size>>9);
  1151. }
  1152. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1153. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1154. }
  1155. return 1;
  1156. overlap:
  1157. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1158. spin_unlock_irq(&conf->device_lock);
  1159. spin_unlock(&sh->lock);
  1160. return 0;
  1161. }
  1162. static void end_reshape(raid5_conf_t *conf);
  1163. static int page_is_zero(struct page *p)
  1164. {
  1165. char *a = page_address(p);
  1166. return ((*(u32*)a) == 0 &&
  1167. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1168. }
  1169. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1170. {
  1171. int sectors_per_chunk = conf->chunk_size >> 9;
  1172. int pd_idx, dd_idx;
  1173. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1174. raid5_compute_sector(stripe * (disks - conf->max_degraded)
  1175. *sectors_per_chunk + chunk_offset,
  1176. disks, disks - conf->max_degraded,
  1177. &dd_idx, &pd_idx, conf);
  1178. return pd_idx;
  1179. }
  1180. static void
  1181. handle_requests_to_failed_array(raid5_conf_t *conf, struct stripe_head *sh,
  1182. struct stripe_head_state *s, int disks,
  1183. struct bio **return_bi)
  1184. {
  1185. int i;
  1186. for (i = disks; i--; ) {
  1187. struct bio *bi;
  1188. int bitmap_end = 0;
  1189. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1190. mdk_rdev_t *rdev;
  1191. rcu_read_lock();
  1192. rdev = rcu_dereference(conf->disks[i].rdev);
  1193. if (rdev && test_bit(In_sync, &rdev->flags))
  1194. /* multiple read failures in one stripe */
  1195. md_error(conf->mddev, rdev);
  1196. rcu_read_unlock();
  1197. }
  1198. spin_lock_irq(&conf->device_lock);
  1199. /* fail all writes first */
  1200. bi = sh->dev[i].towrite;
  1201. sh->dev[i].towrite = NULL;
  1202. if (bi) {
  1203. s->to_write--;
  1204. bitmap_end = 1;
  1205. }
  1206. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1207. wake_up(&conf->wait_for_overlap);
  1208. while (bi && bi->bi_sector <
  1209. sh->dev[i].sector + STRIPE_SECTORS) {
  1210. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1211. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1212. if (--bi->bi_phys_segments == 0) {
  1213. md_write_end(conf->mddev);
  1214. bi->bi_next = *return_bi;
  1215. *return_bi = bi;
  1216. }
  1217. bi = nextbi;
  1218. }
  1219. /* and fail all 'written' */
  1220. bi = sh->dev[i].written;
  1221. sh->dev[i].written = NULL;
  1222. if (bi) bitmap_end = 1;
  1223. while (bi && bi->bi_sector <
  1224. sh->dev[i].sector + STRIPE_SECTORS) {
  1225. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1226. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1227. if (--bi->bi_phys_segments == 0) {
  1228. md_write_end(conf->mddev);
  1229. bi->bi_next = *return_bi;
  1230. *return_bi = bi;
  1231. }
  1232. bi = bi2;
  1233. }
  1234. /* fail any reads if this device is non-operational */
  1235. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1236. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1237. bi = sh->dev[i].toread;
  1238. sh->dev[i].toread = NULL;
  1239. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1240. wake_up(&conf->wait_for_overlap);
  1241. if (bi) s->to_read--;
  1242. while (bi && bi->bi_sector <
  1243. sh->dev[i].sector + STRIPE_SECTORS) {
  1244. struct bio *nextbi =
  1245. r5_next_bio(bi, sh->dev[i].sector);
  1246. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1247. if (--bi->bi_phys_segments == 0) {
  1248. bi->bi_next = *return_bi;
  1249. *return_bi = bi;
  1250. }
  1251. bi = nextbi;
  1252. }
  1253. }
  1254. spin_unlock_irq(&conf->device_lock);
  1255. if (bitmap_end)
  1256. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1257. STRIPE_SECTORS, 0, 0);
  1258. }
  1259. }
  1260. static void handle_issuing_new_read_requests5(struct stripe_head *sh,
  1261. struct stripe_head_state *s, int disks)
  1262. {
  1263. int i;
  1264. for (i = disks; i--; ) {
  1265. struct r5dev *dev = &sh->dev[i];
  1266. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1267. !test_bit(R5_UPTODATE, &dev->flags) &&
  1268. (dev->toread ||
  1269. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1270. s->syncing || s->expanding ||
  1271. (s->failed && (sh->dev[s->failed_num].toread ||
  1272. (sh->dev[s->failed_num].towrite &&
  1273. !test_bit(R5_OVERWRITE, &sh->dev[s->failed_num].flags))
  1274. )))) {
  1275. /* we would like to get this block, possibly
  1276. * by computing it, but we might not be able to
  1277. */
  1278. if (s->uptodate == disks-1) {
  1279. pr_debug("Computing block %d\n", i);
  1280. compute_block(sh, i);
  1281. s->uptodate++;
  1282. } else if (test_bit(R5_Insync, &dev->flags)) {
  1283. set_bit(R5_LOCKED, &dev->flags);
  1284. set_bit(R5_Wantread, &dev->flags);
  1285. s->locked++;
  1286. pr_debug("Reading block %d (sync=%d)\n",
  1287. i, s->syncing);
  1288. }
  1289. }
  1290. }
  1291. set_bit(STRIPE_HANDLE, &sh->state);
  1292. }
  1293. static void handle_issuing_new_read_requests6(struct stripe_head *sh,
  1294. struct stripe_head_state *s, struct r6_state *r6s,
  1295. int disks)
  1296. {
  1297. int i;
  1298. for (i = disks; i--; ) {
  1299. struct r5dev *dev = &sh->dev[i];
  1300. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1301. !test_bit(R5_UPTODATE, &dev->flags) &&
  1302. (dev->toread || (dev->towrite &&
  1303. !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1304. s->syncing || s->expanding ||
  1305. (s->failed >= 1 &&
  1306. (sh->dev[r6s->failed_num[0]].toread ||
  1307. s->to_write)) ||
  1308. (s->failed >= 2 &&
  1309. (sh->dev[r6s->failed_num[1]].toread ||
  1310. s->to_write)))) {
  1311. /* we would like to get this block, possibly
  1312. * by computing it, but we might not be able to
  1313. */
  1314. if (s->uptodate == disks-1) {
  1315. pr_debug("Computing stripe %llu block %d\n",
  1316. (unsigned long long)sh->sector, i);
  1317. compute_block_1(sh, i, 0);
  1318. s->uptodate++;
  1319. } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
  1320. /* Computing 2-failure is *very* expensive; only
  1321. * do it if failed >= 2
  1322. */
  1323. int other;
  1324. for (other = disks; other--; ) {
  1325. if (other == i)
  1326. continue;
  1327. if (!test_bit(R5_UPTODATE,
  1328. &sh->dev[other].flags))
  1329. break;
  1330. }
  1331. BUG_ON(other < 0);
  1332. pr_debug("Computing stripe %llu blocks %d,%d\n",
  1333. (unsigned long long)sh->sector,
  1334. i, other);
  1335. compute_block_2(sh, i, other);
  1336. s->uptodate += 2;
  1337. } else if (test_bit(R5_Insync, &dev->flags)) {
  1338. set_bit(R5_LOCKED, &dev->flags);
  1339. set_bit(R5_Wantread, &dev->flags);
  1340. s->locked++;
  1341. pr_debug("Reading block %d (sync=%d)\n",
  1342. i, s->syncing);
  1343. }
  1344. }
  1345. }
  1346. set_bit(STRIPE_HANDLE, &sh->state);
  1347. }
  1348. /* handle_completed_write_requests
  1349. * any written block on an uptodate or failed drive can be returned.
  1350. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1351. * never LOCKED, so we don't need to test 'failed' directly.
  1352. */
  1353. static void handle_completed_write_requests(raid5_conf_t *conf,
  1354. struct stripe_head *sh, int disks, struct bio **return_bi)
  1355. {
  1356. int i;
  1357. struct r5dev *dev;
  1358. for (i = disks; i--; )
  1359. if (sh->dev[i].written) {
  1360. dev = &sh->dev[i];
  1361. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1362. test_bit(R5_UPTODATE, &dev->flags)) {
  1363. /* We can return any write requests */
  1364. struct bio *wbi, *wbi2;
  1365. int bitmap_end = 0;
  1366. pr_debug("Return write for disc %d\n", i);
  1367. spin_lock_irq(&conf->device_lock);
  1368. wbi = dev->written;
  1369. dev->written = NULL;
  1370. while (wbi && wbi->bi_sector <
  1371. dev->sector + STRIPE_SECTORS) {
  1372. wbi2 = r5_next_bio(wbi, dev->sector);
  1373. if (--wbi->bi_phys_segments == 0) {
  1374. md_write_end(conf->mddev);
  1375. wbi->bi_next = *return_bi;
  1376. *return_bi = wbi;
  1377. }
  1378. wbi = wbi2;
  1379. }
  1380. if (dev->towrite == NULL)
  1381. bitmap_end = 1;
  1382. spin_unlock_irq(&conf->device_lock);
  1383. if (bitmap_end)
  1384. bitmap_endwrite(conf->mddev->bitmap,
  1385. sh->sector,
  1386. STRIPE_SECTORS,
  1387. !test_bit(STRIPE_DEGRADED, &sh->state),
  1388. 0);
  1389. }
  1390. }
  1391. }
  1392. static void handle_issuing_new_write_requests5(raid5_conf_t *conf,
  1393. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  1394. {
  1395. int rmw = 0, rcw = 0, i;
  1396. for (i = disks; i--; ) {
  1397. /* would I have to read this buffer for read_modify_write */
  1398. struct r5dev *dev = &sh->dev[i];
  1399. if ((dev->towrite || i == sh->pd_idx) &&
  1400. !test_bit(R5_LOCKED, &dev->flags) &&
  1401. !test_bit(R5_UPTODATE, &dev->flags)) {
  1402. if (test_bit(R5_Insync, &dev->flags))
  1403. rmw++;
  1404. else
  1405. rmw += 2*disks; /* cannot read it */
  1406. }
  1407. /* Would I have to read this buffer for reconstruct_write */
  1408. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1409. !test_bit(R5_LOCKED, &dev->flags) &&
  1410. !test_bit(R5_UPTODATE, &dev->flags)) {
  1411. if (test_bit(R5_Insync, &dev->flags))
  1412. rcw++;
  1413. else
  1414. rcw += 2*disks;
  1415. }
  1416. }
  1417. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  1418. (unsigned long long)sh->sector, rmw, rcw);
  1419. set_bit(STRIPE_HANDLE, &sh->state);
  1420. if (rmw < rcw && rmw > 0)
  1421. /* prefer read-modify-write, but need to get some data */
  1422. for (i = disks; i--; ) {
  1423. struct r5dev *dev = &sh->dev[i];
  1424. if ((dev->towrite || i == sh->pd_idx) &&
  1425. !test_bit(R5_LOCKED, &dev->flags) &&
  1426. !test_bit(R5_UPTODATE, &dev->flags) &&
  1427. test_bit(R5_Insync, &dev->flags)) {
  1428. if (
  1429. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1430. pr_debug("Read_old block "
  1431. "%d for r-m-w\n", i);
  1432. set_bit(R5_LOCKED, &dev->flags);
  1433. set_bit(R5_Wantread, &dev->flags);
  1434. s->locked++;
  1435. } else {
  1436. set_bit(STRIPE_DELAYED, &sh->state);
  1437. set_bit(STRIPE_HANDLE, &sh->state);
  1438. }
  1439. }
  1440. }
  1441. if (rcw <= rmw && rcw > 0)
  1442. /* want reconstruct write, but need to get some data */
  1443. for (i = disks; i--; ) {
  1444. struct r5dev *dev = &sh->dev[i];
  1445. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  1446. i != sh->pd_idx &&
  1447. !test_bit(R5_LOCKED, &dev->flags) &&
  1448. !test_bit(R5_UPTODATE, &dev->flags) &&
  1449. test_bit(R5_Insync, &dev->flags)) {
  1450. if (
  1451. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1452. pr_debug("Read_old block "
  1453. "%d for Reconstruct\n", i);
  1454. set_bit(R5_LOCKED, &dev->flags);
  1455. set_bit(R5_Wantread, &dev->flags);
  1456. s->locked++;
  1457. } else {
  1458. set_bit(STRIPE_DELAYED, &sh->state);
  1459. set_bit(STRIPE_HANDLE, &sh->state);
  1460. }
  1461. }
  1462. }
  1463. /* now if nothing is locked, and if we have enough data,
  1464. * we can start a write request
  1465. */
  1466. if (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  1467. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1468. pr_debug("Computing parity...\n");
  1469. compute_parity5(sh, rcw == 0 ?
  1470. RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1471. /* now every locked buffer is ready to be written */
  1472. for (i = disks; i--; )
  1473. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1474. pr_debug("Writing block %d\n", i);
  1475. s->locked++;
  1476. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1477. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1478. || (i == sh->pd_idx && s->failed == 0))
  1479. set_bit(STRIPE_INSYNC, &sh->state);
  1480. }
  1481. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1482. atomic_dec(&conf->preread_active_stripes);
  1483. if (atomic_read(&conf->preread_active_stripes) <
  1484. IO_THRESHOLD)
  1485. md_wakeup_thread(conf->mddev->thread);
  1486. }
  1487. }
  1488. }
  1489. static void handle_issuing_new_write_requests6(raid5_conf_t *conf,
  1490. struct stripe_head *sh, struct stripe_head_state *s,
  1491. struct r6_state *r6s, int disks)
  1492. {
  1493. int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
  1494. int qd_idx = r6s->qd_idx;
  1495. for (i = disks; i--; ) {
  1496. struct r5dev *dev = &sh->dev[i];
  1497. /* Would I have to read this buffer for reconstruct_write */
  1498. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1499. && i != pd_idx && i != qd_idx
  1500. && (!test_bit(R5_LOCKED, &dev->flags)
  1501. ) &&
  1502. !test_bit(R5_UPTODATE, &dev->flags)) {
  1503. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1504. else {
  1505. pr_debug("raid6: must_compute: "
  1506. "disk %d flags=%#lx\n", i, dev->flags);
  1507. must_compute++;
  1508. }
  1509. }
  1510. }
  1511. pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
  1512. (unsigned long long)sh->sector, rcw, must_compute);
  1513. set_bit(STRIPE_HANDLE, &sh->state);
  1514. if (rcw > 0)
  1515. /* want reconstruct write, but need to get some data */
  1516. for (i = disks; i--; ) {
  1517. struct r5dev *dev = &sh->dev[i];
  1518. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1519. && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
  1520. && !test_bit(R5_LOCKED, &dev->flags) &&
  1521. !test_bit(R5_UPTODATE, &dev->flags) &&
  1522. test_bit(R5_Insync, &dev->flags)) {
  1523. if (
  1524. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1525. pr_debug("Read_old stripe %llu "
  1526. "block %d for Reconstruct\n",
  1527. (unsigned long long)sh->sector, i);
  1528. set_bit(R5_LOCKED, &dev->flags);
  1529. set_bit(R5_Wantread, &dev->flags);
  1530. s->locked++;
  1531. } else {
  1532. pr_debug("Request delayed stripe %llu "
  1533. "block %d for Reconstruct\n",
  1534. (unsigned long long)sh->sector, i);
  1535. set_bit(STRIPE_DELAYED, &sh->state);
  1536. set_bit(STRIPE_HANDLE, &sh->state);
  1537. }
  1538. }
  1539. }
  1540. /* now if nothing is locked, and if we have enough data, we can start a
  1541. * write request
  1542. */
  1543. if (s->locked == 0 && rcw == 0 &&
  1544. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1545. if (must_compute > 0) {
  1546. /* We have failed blocks and need to compute them */
  1547. switch (s->failed) {
  1548. case 0:
  1549. BUG();
  1550. case 1:
  1551. compute_block_1(sh, r6s->failed_num[0], 0);
  1552. break;
  1553. case 2:
  1554. compute_block_2(sh, r6s->failed_num[0],
  1555. r6s->failed_num[1]);
  1556. break;
  1557. default: /* This request should have been failed? */
  1558. BUG();
  1559. }
  1560. }
  1561. pr_debug("Computing parity for stripe %llu\n",
  1562. (unsigned long long)sh->sector);
  1563. compute_parity6(sh, RECONSTRUCT_WRITE);
  1564. /* now every locked buffer is ready to be written */
  1565. for (i = disks; i--; )
  1566. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1567. pr_debug("Writing stripe %llu block %d\n",
  1568. (unsigned long long)sh->sector, i);
  1569. s->locked++;
  1570. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1571. }
  1572. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  1573. set_bit(STRIPE_INSYNC, &sh->state);
  1574. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1575. atomic_dec(&conf->preread_active_stripes);
  1576. if (atomic_read(&conf->preread_active_stripes) <
  1577. IO_THRESHOLD)
  1578. md_wakeup_thread(conf->mddev->thread);
  1579. }
  1580. }
  1581. }
  1582. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  1583. struct stripe_head_state *s, int disks)
  1584. {
  1585. set_bit(STRIPE_HANDLE, &sh->state);
  1586. if (s->failed == 0) {
  1587. BUG_ON(s->uptodate != disks);
  1588. compute_parity5(sh, CHECK_PARITY);
  1589. s->uptodate--;
  1590. if (page_is_zero(sh->dev[sh->pd_idx].page)) {
  1591. /* parity is correct (on disc, not in buffer any more)
  1592. */
  1593. set_bit(STRIPE_INSYNC, &sh->state);
  1594. } else {
  1595. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1596. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1597. /* don't try to repair!! */
  1598. set_bit(STRIPE_INSYNC, &sh->state);
  1599. else {
  1600. compute_block(sh, sh->pd_idx);
  1601. s->uptodate++;
  1602. }
  1603. }
  1604. }
  1605. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1606. struct r5dev *dev;
  1607. /* either failed parity check, or recovery is happening */
  1608. if (s->failed == 0)
  1609. s->failed_num = sh->pd_idx;
  1610. dev = &sh->dev[s->failed_num];
  1611. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1612. BUG_ON(s->uptodate != disks);
  1613. set_bit(R5_LOCKED, &dev->flags);
  1614. set_bit(R5_Wantwrite, &dev->flags);
  1615. clear_bit(STRIPE_DEGRADED, &sh->state);
  1616. s->locked++;
  1617. set_bit(STRIPE_INSYNC, &sh->state);
  1618. }
  1619. }
  1620. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  1621. struct stripe_head_state *s,
  1622. struct r6_state *r6s, struct page *tmp_page,
  1623. int disks)
  1624. {
  1625. int update_p = 0, update_q = 0;
  1626. struct r5dev *dev;
  1627. int pd_idx = sh->pd_idx;
  1628. int qd_idx = r6s->qd_idx;
  1629. set_bit(STRIPE_HANDLE, &sh->state);
  1630. BUG_ON(s->failed > 2);
  1631. BUG_ON(s->uptodate < disks);
  1632. /* Want to check and possibly repair P and Q.
  1633. * However there could be one 'failed' device, in which
  1634. * case we can only check one of them, possibly using the
  1635. * other to generate missing data
  1636. */
  1637. /* If !tmp_page, we cannot do the calculations,
  1638. * but as we have set STRIPE_HANDLE, we will soon be called
  1639. * by stripe_handle with a tmp_page - just wait until then.
  1640. */
  1641. if (tmp_page) {
  1642. if (s->failed == r6s->q_failed) {
  1643. /* The only possible failed device holds 'Q', so it
  1644. * makes sense to check P (If anything else were failed,
  1645. * we would have used P to recreate it).
  1646. */
  1647. compute_block_1(sh, pd_idx, 1);
  1648. if (!page_is_zero(sh->dev[pd_idx].page)) {
  1649. compute_block_1(sh, pd_idx, 0);
  1650. update_p = 1;
  1651. }
  1652. }
  1653. if (!r6s->q_failed && s->failed < 2) {
  1654. /* q is not failed, and we didn't use it to generate
  1655. * anything, so it makes sense to check it
  1656. */
  1657. memcpy(page_address(tmp_page),
  1658. page_address(sh->dev[qd_idx].page),
  1659. STRIPE_SIZE);
  1660. compute_parity6(sh, UPDATE_PARITY);
  1661. if (memcmp(page_address(tmp_page),
  1662. page_address(sh->dev[qd_idx].page),
  1663. STRIPE_SIZE) != 0) {
  1664. clear_bit(STRIPE_INSYNC, &sh->state);
  1665. update_q = 1;
  1666. }
  1667. }
  1668. if (update_p || update_q) {
  1669. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1670. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1671. /* don't try to repair!! */
  1672. update_p = update_q = 0;
  1673. }
  1674. /* now write out any block on a failed drive,
  1675. * or P or Q if they need it
  1676. */
  1677. if (s->failed == 2) {
  1678. dev = &sh->dev[r6s->failed_num[1]];
  1679. s->locked++;
  1680. set_bit(R5_LOCKED, &dev->flags);
  1681. set_bit(R5_Wantwrite, &dev->flags);
  1682. }
  1683. if (s->failed >= 1) {
  1684. dev = &sh->dev[r6s->failed_num[0]];
  1685. s->locked++;
  1686. set_bit(R5_LOCKED, &dev->flags);
  1687. set_bit(R5_Wantwrite, &dev->flags);
  1688. }
  1689. if (update_p) {
  1690. dev = &sh->dev[pd_idx];
  1691. s->locked++;
  1692. set_bit(R5_LOCKED, &dev->flags);
  1693. set_bit(R5_Wantwrite, &dev->flags);
  1694. }
  1695. if (update_q) {
  1696. dev = &sh->dev[qd_idx];
  1697. s->locked++;
  1698. set_bit(R5_LOCKED, &dev->flags);
  1699. set_bit(R5_Wantwrite, &dev->flags);
  1700. }
  1701. clear_bit(STRIPE_DEGRADED, &sh->state);
  1702. set_bit(STRIPE_INSYNC, &sh->state);
  1703. }
  1704. }
  1705. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  1706. struct r6_state *r6s)
  1707. {
  1708. int i;
  1709. /* We have read all the blocks in this stripe and now we need to
  1710. * copy some of them into a target stripe for expand.
  1711. */
  1712. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1713. for (i = 0; i < sh->disks; i++)
  1714. if (i != sh->pd_idx && (r6s && i != r6s->qd_idx)) {
  1715. int dd_idx, pd_idx, j;
  1716. struct stripe_head *sh2;
  1717. sector_t bn = compute_blocknr(sh, i);
  1718. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1719. conf->raid_disks -
  1720. conf->max_degraded, &dd_idx,
  1721. &pd_idx, conf);
  1722. sh2 = get_active_stripe(conf, s, conf->raid_disks,
  1723. pd_idx, 1);
  1724. if (sh2 == NULL)
  1725. /* so far only the early blocks of this stripe
  1726. * have been requested. When later blocks
  1727. * get requested, we will try again
  1728. */
  1729. continue;
  1730. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1731. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1732. /* must have already done this block */
  1733. release_stripe(sh2);
  1734. continue;
  1735. }
  1736. memcpy(page_address(sh2->dev[dd_idx].page),
  1737. page_address(sh->dev[i].page),
  1738. STRIPE_SIZE);
  1739. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1740. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1741. for (j = 0; j < conf->raid_disks; j++)
  1742. if (j != sh2->pd_idx &&
  1743. (r6s && j != r6s->qd_idx) &&
  1744. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1745. break;
  1746. if (j == conf->raid_disks) {
  1747. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1748. set_bit(STRIPE_HANDLE, &sh2->state);
  1749. }
  1750. release_stripe(sh2);
  1751. }
  1752. }
  1753. /*
  1754. * handle_stripe - do things to a stripe.
  1755. *
  1756. * We lock the stripe and then examine the state of various bits
  1757. * to see what needs to be done.
  1758. * Possible results:
  1759. * return some read request which now have data
  1760. * return some write requests which are safely on disc
  1761. * schedule a read on some buffers
  1762. * schedule a write of some buffers
  1763. * return confirmation of parity correctness
  1764. *
  1765. * Parity calculations are done inside the stripe lock
  1766. * buffers are taken off read_list or write_list, and bh_cache buffers
  1767. * get BH_Lock set before the stripe lock is released.
  1768. *
  1769. */
  1770. static void handle_stripe5(struct stripe_head *sh)
  1771. {
  1772. raid5_conf_t *conf = sh->raid_conf;
  1773. int disks = sh->disks, i;
  1774. struct bio *return_bi = NULL;
  1775. struct stripe_head_state s;
  1776. struct r5dev *dev;
  1777. memset(&s, 0, sizeof(s));
  1778. pr_debug("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  1779. (unsigned long long)sh->sector, atomic_read(&sh->count),
  1780. sh->pd_idx);
  1781. spin_lock(&sh->lock);
  1782. clear_bit(STRIPE_HANDLE, &sh->state);
  1783. clear_bit(STRIPE_DELAYED, &sh->state);
  1784. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1785. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1786. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  1787. /* Now to look around and see what can be done */
  1788. rcu_read_lock();
  1789. for (i=disks; i--; ) {
  1790. mdk_rdev_t *rdev;
  1791. struct r5dev *dev = &sh->dev[i];
  1792. clear_bit(R5_Insync, &dev->flags);
  1793. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  1794. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1795. /* maybe we can reply to a read */
  1796. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1797. struct bio *rbi, *rbi2;
  1798. pr_debug("Return read for disc %d\n", i);
  1799. spin_lock_irq(&conf->device_lock);
  1800. rbi = dev->toread;
  1801. dev->toread = NULL;
  1802. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1803. wake_up(&conf->wait_for_overlap);
  1804. spin_unlock_irq(&conf->device_lock);
  1805. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1806. copy_data(0, rbi, dev->page, dev->sector);
  1807. rbi2 = r5_next_bio(rbi, dev->sector);
  1808. spin_lock_irq(&conf->device_lock);
  1809. if (--rbi->bi_phys_segments == 0) {
  1810. rbi->bi_next = return_bi;
  1811. return_bi = rbi;
  1812. }
  1813. spin_unlock_irq(&conf->device_lock);
  1814. rbi = rbi2;
  1815. }
  1816. }
  1817. /* now count some things */
  1818. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  1819. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  1820. if (dev->toread)
  1821. s.to_read++;
  1822. if (dev->towrite) {
  1823. s.to_write++;
  1824. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1825. s.non_overwrite++;
  1826. }
  1827. if (dev->written)
  1828. s.written++;
  1829. rdev = rcu_dereference(conf->disks[i].rdev);
  1830. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1831. /* The ReadError flag will just be confusing now */
  1832. clear_bit(R5_ReadError, &dev->flags);
  1833. clear_bit(R5_ReWrite, &dev->flags);
  1834. }
  1835. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1836. || test_bit(R5_ReadError, &dev->flags)) {
  1837. s.failed++;
  1838. s.failed_num = i;
  1839. } else
  1840. set_bit(R5_Insync, &dev->flags);
  1841. }
  1842. rcu_read_unlock();
  1843. pr_debug("locked=%d uptodate=%d to_read=%d"
  1844. " to_write=%d failed=%d failed_num=%d\n",
  1845. s.locked, s.uptodate, s.to_read, s.to_write,
  1846. s.failed, s.failed_num);
  1847. /* check if the array has lost two devices and, if so, some requests might
  1848. * need to be failed
  1849. */
  1850. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  1851. handle_requests_to_failed_array(conf, sh, &s, disks,
  1852. &return_bi);
  1853. if (s.failed > 1 && s.syncing) {
  1854. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1855. clear_bit(STRIPE_SYNCING, &sh->state);
  1856. s.syncing = 0;
  1857. }
  1858. /* might be able to return some write requests if the parity block
  1859. * is safe, or on a failed drive
  1860. */
  1861. dev = &sh->dev[sh->pd_idx];
  1862. if ( s.written &&
  1863. ((test_bit(R5_Insync, &dev->flags) &&
  1864. !test_bit(R5_LOCKED, &dev->flags) &&
  1865. test_bit(R5_UPTODATE, &dev->flags)) ||
  1866. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  1867. handle_completed_write_requests(conf, sh, disks, &return_bi);
  1868. /* Now we might consider reading some blocks, either to check/generate
  1869. * parity, or to satisfy requests
  1870. * or to load a block that is being partially written.
  1871. */
  1872. if (s.to_read || s.non_overwrite ||
  1873. (s.syncing && (s.uptodate < disks)) || s.expanding)
  1874. handle_issuing_new_read_requests5(sh, &s, disks);
  1875. /* now to consider writing and what else, if anything should be read */
  1876. if (s.to_write)
  1877. handle_issuing_new_write_requests5(conf, sh, &s, disks);
  1878. /* maybe we need to check and possibly fix the parity for this stripe
  1879. * Any reads will already have been scheduled, so we just see if enough data
  1880. * is available
  1881. */
  1882. if (s.syncing && s.locked == 0 &&
  1883. !test_bit(STRIPE_INSYNC, &sh->state))
  1884. handle_parity_checks5(conf, sh, &s, disks);
  1885. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1886. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1887. clear_bit(STRIPE_SYNCING, &sh->state);
  1888. }
  1889. /* If the failed drive is just a ReadError, then we might need to progress
  1890. * the repair/check process
  1891. */
  1892. if (s.failed == 1 && !conf->mddev->ro &&
  1893. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  1894. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  1895. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  1896. ) {
  1897. dev = &sh->dev[s.failed_num];
  1898. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1899. set_bit(R5_Wantwrite, &dev->flags);
  1900. set_bit(R5_ReWrite, &dev->flags);
  1901. set_bit(R5_LOCKED, &dev->flags);
  1902. s.locked++;
  1903. } else {
  1904. /* let's read it back */
  1905. set_bit(R5_Wantread, &dev->flags);
  1906. set_bit(R5_LOCKED, &dev->flags);
  1907. s.locked++;
  1908. }
  1909. }
  1910. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1911. /* Need to write out all blocks after computing parity */
  1912. sh->disks = conf->raid_disks;
  1913. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1914. compute_parity5(sh, RECONSTRUCT_WRITE);
  1915. for (i = conf->raid_disks; i--; ) {
  1916. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1917. s.locked++;
  1918. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1919. }
  1920. clear_bit(STRIPE_EXPANDING, &sh->state);
  1921. } else if (s.expanded) {
  1922. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1923. atomic_dec(&conf->reshape_stripes);
  1924. wake_up(&conf->wait_for_overlap);
  1925. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1926. }
  1927. if (s.expanding && s.locked == 0)
  1928. handle_stripe_expansion(conf, sh, NULL);
  1929. spin_unlock(&sh->lock);
  1930. return_io(return_bi);
  1931. for (i=disks; i-- ;) {
  1932. int rw;
  1933. struct bio *bi;
  1934. mdk_rdev_t *rdev;
  1935. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1936. rw = WRITE;
  1937. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1938. rw = READ;
  1939. else
  1940. continue;
  1941. bi = &sh->dev[i].req;
  1942. bi->bi_rw = rw;
  1943. if (rw == WRITE)
  1944. bi->bi_end_io = raid5_end_write_request;
  1945. else
  1946. bi->bi_end_io = raid5_end_read_request;
  1947. rcu_read_lock();
  1948. rdev = rcu_dereference(conf->disks[i].rdev);
  1949. if (rdev && test_bit(Faulty, &rdev->flags))
  1950. rdev = NULL;
  1951. if (rdev)
  1952. atomic_inc(&rdev->nr_pending);
  1953. rcu_read_unlock();
  1954. if (rdev) {
  1955. if (s.syncing || s.expanding || s.expanded)
  1956. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1957. bi->bi_bdev = rdev->bdev;
  1958. pr_debug("for %llu schedule op %ld on disc %d\n",
  1959. (unsigned long long)sh->sector, bi->bi_rw, i);
  1960. atomic_inc(&sh->count);
  1961. bi->bi_sector = sh->sector + rdev->data_offset;
  1962. bi->bi_flags = 1 << BIO_UPTODATE;
  1963. bi->bi_vcnt = 1;
  1964. bi->bi_max_vecs = 1;
  1965. bi->bi_idx = 0;
  1966. bi->bi_io_vec = &sh->dev[i].vec;
  1967. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1968. bi->bi_io_vec[0].bv_offset = 0;
  1969. bi->bi_size = STRIPE_SIZE;
  1970. bi->bi_next = NULL;
  1971. if (rw == WRITE &&
  1972. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1973. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1974. generic_make_request(bi);
  1975. } else {
  1976. if (rw == WRITE)
  1977. set_bit(STRIPE_DEGRADED, &sh->state);
  1978. pr_debug("skip op %ld on disc %d for sector %llu\n",
  1979. bi->bi_rw, i, (unsigned long long)sh->sector);
  1980. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1981. set_bit(STRIPE_HANDLE, &sh->state);
  1982. }
  1983. }
  1984. }
  1985. static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  1986. {
  1987. raid6_conf_t *conf = sh->raid_conf;
  1988. int disks = sh->disks;
  1989. struct bio *return_bi = NULL;
  1990. int i, pd_idx = sh->pd_idx;
  1991. struct stripe_head_state s;
  1992. struct r6_state r6s;
  1993. struct r5dev *dev, *pdev, *qdev;
  1994. r6s.qd_idx = raid6_next_disk(pd_idx, disks);
  1995. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  1996. "pd_idx=%d, qd_idx=%d\n",
  1997. (unsigned long long)sh->sector, sh->state,
  1998. atomic_read(&sh->count), pd_idx, r6s.qd_idx);
  1999. memset(&s, 0, sizeof(s));
  2000. spin_lock(&sh->lock);
  2001. clear_bit(STRIPE_HANDLE, &sh->state);
  2002. clear_bit(STRIPE_DELAYED, &sh->state);
  2003. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2004. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2005. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2006. /* Now to look around and see what can be done */
  2007. rcu_read_lock();
  2008. for (i=disks; i--; ) {
  2009. mdk_rdev_t *rdev;
  2010. dev = &sh->dev[i];
  2011. clear_bit(R5_Insync, &dev->flags);
  2012. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2013. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2014. /* maybe we can reply to a read */
  2015. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  2016. struct bio *rbi, *rbi2;
  2017. pr_debug("Return read for disc %d\n", i);
  2018. spin_lock_irq(&conf->device_lock);
  2019. rbi = dev->toread;
  2020. dev->toread = NULL;
  2021. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  2022. wake_up(&conf->wait_for_overlap);
  2023. spin_unlock_irq(&conf->device_lock);
  2024. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  2025. copy_data(0, rbi, dev->page, dev->sector);
  2026. rbi2 = r5_next_bio(rbi, dev->sector);
  2027. spin_lock_irq(&conf->device_lock);
  2028. if (--rbi->bi_phys_segments == 0) {
  2029. rbi->bi_next = return_bi;
  2030. return_bi = rbi;
  2031. }
  2032. spin_unlock_irq(&conf->device_lock);
  2033. rbi = rbi2;
  2034. }
  2035. }
  2036. /* now count some things */
  2037. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2038. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2039. if (dev->toread)
  2040. s.to_read++;
  2041. if (dev->towrite) {
  2042. s.to_write++;
  2043. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2044. s.non_overwrite++;
  2045. }
  2046. if (dev->written)
  2047. s.written++;
  2048. rdev = rcu_dereference(conf->disks[i].rdev);
  2049. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2050. /* The ReadError flag will just be confusing now */
  2051. clear_bit(R5_ReadError, &dev->flags);
  2052. clear_bit(R5_ReWrite, &dev->flags);
  2053. }
  2054. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2055. || test_bit(R5_ReadError, &dev->flags)) {
  2056. if (s.failed < 2)
  2057. r6s.failed_num[s.failed] = i;
  2058. s.failed++;
  2059. } else
  2060. set_bit(R5_Insync, &dev->flags);
  2061. }
  2062. rcu_read_unlock();
  2063. pr_debug("locked=%d uptodate=%d to_read=%d"
  2064. " to_write=%d failed=%d failed_num=%d,%d\n",
  2065. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2066. r6s.failed_num[0], r6s.failed_num[1]);
  2067. /* check if the array has lost >2 devices and, if so, some requests
  2068. * might need to be failed
  2069. */
  2070. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2071. handle_requests_to_failed_array(conf, sh, &s, disks,
  2072. &return_bi);
  2073. if (s.failed > 2 && s.syncing) {
  2074. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2075. clear_bit(STRIPE_SYNCING, &sh->state);
  2076. s.syncing = 0;
  2077. }
  2078. /*
  2079. * might be able to return some write requests if the parity blocks
  2080. * are safe, or on a failed drive
  2081. */
  2082. pdev = &sh->dev[pd_idx];
  2083. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2084. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2085. qdev = &sh->dev[r6s.qd_idx];
  2086. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
  2087. || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
  2088. if ( s.written &&
  2089. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2090. && !test_bit(R5_LOCKED, &pdev->flags)
  2091. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2092. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2093. && !test_bit(R5_LOCKED, &qdev->flags)
  2094. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2095. handle_completed_write_requests(conf, sh, disks, &return_bi);
  2096. /* Now we might consider reading some blocks, either to check/generate
  2097. * parity, or to satisfy requests
  2098. * or to load a block that is being partially written.
  2099. */
  2100. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2101. (s.syncing && (s.uptodate < disks)) || s.expanding)
  2102. handle_issuing_new_read_requests6(sh, &s, &r6s, disks);
  2103. /* now to consider writing and what else, if anything should be read */
  2104. if (s.to_write)
  2105. handle_issuing_new_write_requests6(conf, sh, &s, &r6s, disks);
  2106. /* maybe we need to check and possibly fix the parity for this stripe
  2107. * Any reads will already have been scheduled, so we just see if enough
  2108. * data is available
  2109. */
  2110. if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
  2111. handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
  2112. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2113. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2114. clear_bit(STRIPE_SYNCING, &sh->state);
  2115. }
  2116. /* If the failed drives are just a ReadError, then we might need
  2117. * to progress the repair/check process
  2118. */
  2119. if (s.failed <= 2 && !conf->mddev->ro)
  2120. for (i = 0; i < s.failed; i++) {
  2121. dev = &sh->dev[r6s.failed_num[i]];
  2122. if (test_bit(R5_ReadError, &dev->flags)
  2123. && !test_bit(R5_LOCKED, &dev->flags)
  2124. && test_bit(R5_UPTODATE, &dev->flags)
  2125. ) {
  2126. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2127. set_bit(R5_Wantwrite, &dev->flags);
  2128. set_bit(R5_ReWrite, &dev->flags);
  2129. set_bit(R5_LOCKED, &dev->flags);
  2130. } else {
  2131. /* let's read it back */
  2132. set_bit(R5_Wantread, &dev->flags);
  2133. set_bit(R5_LOCKED, &dev->flags);
  2134. }
  2135. }
  2136. }
  2137. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2138. /* Need to write out all blocks after computing P&Q */
  2139. sh->disks = conf->raid_disks;
  2140. sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
  2141. conf->raid_disks);
  2142. compute_parity6(sh, RECONSTRUCT_WRITE);
  2143. for (i = conf->raid_disks ; i-- ; ) {
  2144. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2145. s.locked++;
  2146. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2147. }
  2148. clear_bit(STRIPE_EXPANDING, &sh->state);
  2149. } else if (s.expanded) {
  2150. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2151. atomic_dec(&conf->reshape_stripes);
  2152. wake_up(&conf->wait_for_overlap);
  2153. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2154. }
  2155. if (s.expanding && s.locked == 0)
  2156. handle_stripe_expansion(conf, sh, &r6s);
  2157. spin_unlock(&sh->lock);
  2158. return_io(return_bi);
  2159. for (i=disks; i-- ;) {
  2160. int rw;
  2161. struct bio *bi;
  2162. mdk_rdev_t *rdev;
  2163. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  2164. rw = WRITE;
  2165. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  2166. rw = READ;
  2167. else
  2168. continue;
  2169. bi = &sh->dev[i].req;
  2170. bi->bi_rw = rw;
  2171. if (rw == WRITE)
  2172. bi->bi_end_io = raid5_end_write_request;
  2173. else
  2174. bi->bi_end_io = raid5_end_read_request;
  2175. rcu_read_lock();
  2176. rdev = rcu_dereference(conf->disks[i].rdev);
  2177. if (rdev && test_bit(Faulty, &rdev->flags))
  2178. rdev = NULL;
  2179. if (rdev)
  2180. atomic_inc(&rdev->nr_pending);
  2181. rcu_read_unlock();
  2182. if (rdev) {
  2183. if (s.syncing || s.expanding || s.expanded)
  2184. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  2185. bi->bi_bdev = rdev->bdev;
  2186. pr_debug("for %llu schedule op %ld on disc %d\n",
  2187. (unsigned long long)sh->sector, bi->bi_rw, i);
  2188. atomic_inc(&sh->count);
  2189. bi->bi_sector = sh->sector + rdev->data_offset;
  2190. bi->bi_flags = 1 << BIO_UPTODATE;
  2191. bi->bi_vcnt = 1;
  2192. bi->bi_max_vecs = 1;
  2193. bi->bi_idx = 0;
  2194. bi->bi_io_vec = &sh->dev[i].vec;
  2195. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  2196. bi->bi_io_vec[0].bv_offset = 0;
  2197. bi->bi_size = STRIPE_SIZE;
  2198. bi->bi_next = NULL;
  2199. if (rw == WRITE &&
  2200. test_bit(R5_ReWrite, &sh->dev[i].flags))
  2201. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2202. generic_make_request(bi);
  2203. } else {
  2204. if (rw == WRITE)
  2205. set_bit(STRIPE_DEGRADED, &sh->state);
  2206. pr_debug("skip op %ld on disc %d for sector %llu\n",
  2207. bi->bi_rw, i, (unsigned long long)sh->sector);
  2208. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2209. set_bit(STRIPE_HANDLE, &sh->state);
  2210. }
  2211. }
  2212. }
  2213. static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2214. {
  2215. if (sh->raid_conf->level == 6)
  2216. handle_stripe6(sh, tmp_page);
  2217. else
  2218. handle_stripe5(sh);
  2219. }
  2220. static void raid5_activate_delayed(raid5_conf_t *conf)
  2221. {
  2222. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2223. while (!list_empty(&conf->delayed_list)) {
  2224. struct list_head *l = conf->delayed_list.next;
  2225. struct stripe_head *sh;
  2226. sh = list_entry(l, struct stripe_head, lru);
  2227. list_del_init(l);
  2228. clear_bit(STRIPE_DELAYED, &sh->state);
  2229. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2230. atomic_inc(&conf->preread_active_stripes);
  2231. list_add_tail(&sh->lru, &conf->handle_list);
  2232. }
  2233. }
  2234. }
  2235. static void activate_bit_delay(raid5_conf_t *conf)
  2236. {
  2237. /* device_lock is held */
  2238. struct list_head head;
  2239. list_add(&head, &conf->bitmap_list);
  2240. list_del_init(&conf->bitmap_list);
  2241. while (!list_empty(&head)) {
  2242. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2243. list_del_init(&sh->lru);
  2244. atomic_inc(&sh->count);
  2245. __release_stripe(conf, sh);
  2246. }
  2247. }
  2248. static void unplug_slaves(mddev_t *mddev)
  2249. {
  2250. raid5_conf_t *conf = mddev_to_conf(mddev);
  2251. int i;
  2252. rcu_read_lock();
  2253. for (i=0; i<mddev->raid_disks; i++) {
  2254. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2255. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2256. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  2257. atomic_inc(&rdev->nr_pending);
  2258. rcu_read_unlock();
  2259. if (r_queue->unplug_fn)
  2260. r_queue->unplug_fn(r_queue);
  2261. rdev_dec_pending(rdev, mddev);
  2262. rcu_read_lock();
  2263. }
  2264. }
  2265. rcu_read_unlock();
  2266. }
  2267. static void raid5_unplug_device(request_queue_t *q)
  2268. {
  2269. mddev_t *mddev = q->queuedata;
  2270. raid5_conf_t *conf = mddev_to_conf(mddev);
  2271. unsigned long flags;
  2272. spin_lock_irqsave(&conf->device_lock, flags);
  2273. if (blk_remove_plug(q)) {
  2274. conf->seq_flush++;
  2275. raid5_activate_delayed(conf);
  2276. }
  2277. md_wakeup_thread(mddev->thread);
  2278. spin_unlock_irqrestore(&conf->device_lock, flags);
  2279. unplug_slaves(mddev);
  2280. }
  2281. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  2282. sector_t *error_sector)
  2283. {
  2284. mddev_t *mddev = q->queuedata;
  2285. raid5_conf_t *conf = mddev_to_conf(mddev);
  2286. int i, ret = 0;
  2287. rcu_read_lock();
  2288. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  2289. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2290. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  2291. struct block_device *bdev = rdev->bdev;
  2292. request_queue_t *r_queue = bdev_get_queue(bdev);
  2293. if (!r_queue->issue_flush_fn)
  2294. ret = -EOPNOTSUPP;
  2295. else {
  2296. atomic_inc(&rdev->nr_pending);
  2297. rcu_read_unlock();
  2298. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  2299. error_sector);
  2300. rdev_dec_pending(rdev, mddev);
  2301. rcu_read_lock();
  2302. }
  2303. }
  2304. }
  2305. rcu_read_unlock();
  2306. return ret;
  2307. }
  2308. static int raid5_congested(void *data, int bits)
  2309. {
  2310. mddev_t *mddev = data;
  2311. raid5_conf_t *conf = mddev_to_conf(mddev);
  2312. /* No difference between reads and writes. Just check
  2313. * how busy the stripe_cache is
  2314. */
  2315. if (conf->inactive_blocked)
  2316. return 1;
  2317. if (conf->quiesce)
  2318. return 1;
  2319. if (list_empty_careful(&conf->inactive_list))
  2320. return 1;
  2321. return 0;
  2322. }
  2323. /* We want read requests to align with chunks where possible,
  2324. * but write requests don't need to.
  2325. */
  2326. static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
  2327. {
  2328. mddev_t *mddev = q->queuedata;
  2329. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2330. int max;
  2331. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2332. unsigned int bio_sectors = bio->bi_size >> 9;
  2333. if (bio_data_dir(bio) == WRITE)
  2334. return biovec->bv_len; /* always allow writes to be mergeable */
  2335. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2336. if (max < 0) max = 0;
  2337. if (max <= biovec->bv_len && bio_sectors == 0)
  2338. return biovec->bv_len;
  2339. else
  2340. return max;
  2341. }
  2342. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2343. {
  2344. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2345. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2346. unsigned int bio_sectors = bio->bi_size >> 9;
  2347. return chunk_sectors >=
  2348. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2349. }
  2350. /*
  2351. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2352. * later sampled by raid5d.
  2353. */
  2354. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2355. {
  2356. unsigned long flags;
  2357. spin_lock_irqsave(&conf->device_lock, flags);
  2358. bi->bi_next = conf->retry_read_aligned_list;
  2359. conf->retry_read_aligned_list = bi;
  2360. spin_unlock_irqrestore(&conf->device_lock, flags);
  2361. md_wakeup_thread(conf->mddev->thread);
  2362. }
  2363. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  2364. {
  2365. struct bio *bi;
  2366. bi = conf->retry_read_aligned;
  2367. if (bi) {
  2368. conf->retry_read_aligned = NULL;
  2369. return bi;
  2370. }
  2371. bi = conf->retry_read_aligned_list;
  2372. if(bi) {
  2373. conf->retry_read_aligned_list = bi->bi_next;
  2374. bi->bi_next = NULL;
  2375. bi->bi_phys_segments = 1; /* biased count of active stripes */
  2376. bi->bi_hw_segments = 0; /* count of processed stripes */
  2377. }
  2378. return bi;
  2379. }
  2380. /*
  2381. * The "raid5_align_endio" should check if the read succeeded and if it
  2382. * did, call bio_endio on the original bio (having bio_put the new bio
  2383. * first).
  2384. * If the read failed..
  2385. */
  2386. static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
  2387. {
  2388. struct bio* raid_bi = bi->bi_private;
  2389. mddev_t *mddev;
  2390. raid5_conf_t *conf;
  2391. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  2392. mdk_rdev_t *rdev;
  2393. if (bi->bi_size)
  2394. return 1;
  2395. bio_put(bi);
  2396. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  2397. conf = mddev_to_conf(mddev);
  2398. rdev = (void*)raid_bi->bi_next;
  2399. raid_bi->bi_next = NULL;
  2400. rdev_dec_pending(rdev, conf->mddev);
  2401. if (!error && uptodate) {
  2402. bio_endio(raid_bi, bytes, 0);
  2403. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2404. wake_up(&conf->wait_for_stripe);
  2405. return 0;
  2406. }
  2407. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  2408. add_bio_to_retry(raid_bi, conf);
  2409. return 0;
  2410. }
  2411. static int bio_fits_rdev(struct bio *bi)
  2412. {
  2413. request_queue_t *q = bdev_get_queue(bi->bi_bdev);
  2414. if ((bi->bi_size>>9) > q->max_sectors)
  2415. return 0;
  2416. blk_recount_segments(q, bi);
  2417. if (bi->bi_phys_segments > q->max_phys_segments ||
  2418. bi->bi_hw_segments > q->max_hw_segments)
  2419. return 0;
  2420. if (q->merge_bvec_fn)
  2421. /* it's too hard to apply the merge_bvec_fn at this stage,
  2422. * just just give up
  2423. */
  2424. return 0;
  2425. return 1;
  2426. }
  2427. static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
  2428. {
  2429. mddev_t *mddev = q->queuedata;
  2430. raid5_conf_t *conf = mddev_to_conf(mddev);
  2431. const unsigned int raid_disks = conf->raid_disks;
  2432. const unsigned int data_disks = raid_disks - conf->max_degraded;
  2433. unsigned int dd_idx, pd_idx;
  2434. struct bio* align_bi;
  2435. mdk_rdev_t *rdev;
  2436. if (!in_chunk_boundary(mddev, raid_bio)) {
  2437. pr_debug("chunk_aligned_read : non aligned\n");
  2438. return 0;
  2439. }
  2440. /*
  2441. * use bio_clone to make a copy of the bio
  2442. */
  2443. align_bi = bio_clone(raid_bio, GFP_NOIO);
  2444. if (!align_bi)
  2445. return 0;
  2446. /*
  2447. * set bi_end_io to a new function, and set bi_private to the
  2448. * original bio.
  2449. */
  2450. align_bi->bi_end_io = raid5_align_endio;
  2451. align_bi->bi_private = raid_bio;
  2452. /*
  2453. * compute position
  2454. */
  2455. align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
  2456. raid_disks,
  2457. data_disks,
  2458. &dd_idx,
  2459. &pd_idx,
  2460. conf);
  2461. rcu_read_lock();
  2462. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  2463. if (rdev && test_bit(In_sync, &rdev->flags)) {
  2464. atomic_inc(&rdev->nr_pending);
  2465. rcu_read_unlock();
  2466. raid_bio->bi_next = (void*)rdev;
  2467. align_bi->bi_bdev = rdev->bdev;
  2468. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  2469. align_bi->bi_sector += rdev->data_offset;
  2470. if (!bio_fits_rdev(align_bi)) {
  2471. /* too big in some way */
  2472. bio_put(align_bi);
  2473. rdev_dec_pending(rdev, mddev);
  2474. return 0;
  2475. }
  2476. spin_lock_irq(&conf->device_lock);
  2477. wait_event_lock_irq(conf->wait_for_stripe,
  2478. conf->quiesce == 0,
  2479. conf->device_lock, /* nothing */);
  2480. atomic_inc(&conf->active_aligned_reads);
  2481. spin_unlock_irq(&conf->device_lock);
  2482. generic_make_request(align_bi);
  2483. return 1;
  2484. } else {
  2485. rcu_read_unlock();
  2486. bio_put(align_bi);
  2487. return 0;
  2488. }
  2489. }
  2490. static int make_request(request_queue_t *q, struct bio * bi)
  2491. {
  2492. mddev_t *mddev = q->queuedata;
  2493. raid5_conf_t *conf = mddev_to_conf(mddev);
  2494. unsigned int dd_idx, pd_idx;
  2495. sector_t new_sector;
  2496. sector_t logical_sector, last_sector;
  2497. struct stripe_head *sh;
  2498. const int rw = bio_data_dir(bi);
  2499. int remaining;
  2500. if (unlikely(bio_barrier(bi))) {
  2501. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  2502. return 0;
  2503. }
  2504. md_write_start(mddev, bi);
  2505. disk_stat_inc(mddev->gendisk, ios[rw]);
  2506. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  2507. if (rw == READ &&
  2508. mddev->reshape_position == MaxSector &&
  2509. chunk_aligned_read(q,bi))
  2510. return 0;
  2511. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2512. last_sector = bi->bi_sector + (bi->bi_size>>9);
  2513. bi->bi_next = NULL;
  2514. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  2515. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  2516. DEFINE_WAIT(w);
  2517. int disks, data_disks;
  2518. retry:
  2519. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  2520. if (likely(conf->expand_progress == MaxSector))
  2521. disks = conf->raid_disks;
  2522. else {
  2523. /* spinlock is needed as expand_progress may be
  2524. * 64bit on a 32bit platform, and so it might be
  2525. * possible to see a half-updated value
  2526. * Ofcourse expand_progress could change after
  2527. * the lock is dropped, so once we get a reference
  2528. * to the stripe that we think it is, we will have
  2529. * to check again.
  2530. */
  2531. spin_lock_irq(&conf->device_lock);
  2532. disks = conf->raid_disks;
  2533. if (logical_sector >= conf->expand_progress)
  2534. disks = conf->previous_raid_disks;
  2535. else {
  2536. if (logical_sector >= conf->expand_lo) {
  2537. spin_unlock_irq(&conf->device_lock);
  2538. schedule();
  2539. goto retry;
  2540. }
  2541. }
  2542. spin_unlock_irq(&conf->device_lock);
  2543. }
  2544. data_disks = disks - conf->max_degraded;
  2545. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  2546. &dd_idx, &pd_idx, conf);
  2547. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  2548. (unsigned long long)new_sector,
  2549. (unsigned long long)logical_sector);
  2550. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  2551. if (sh) {
  2552. if (unlikely(conf->expand_progress != MaxSector)) {
  2553. /* expansion might have moved on while waiting for a
  2554. * stripe, so we must do the range check again.
  2555. * Expansion could still move past after this
  2556. * test, but as we are holding a reference to
  2557. * 'sh', we know that if that happens,
  2558. * STRIPE_EXPANDING will get set and the expansion
  2559. * won't proceed until we finish with the stripe.
  2560. */
  2561. int must_retry = 0;
  2562. spin_lock_irq(&conf->device_lock);
  2563. if (logical_sector < conf->expand_progress &&
  2564. disks == conf->previous_raid_disks)
  2565. /* mismatch, need to try again */
  2566. must_retry = 1;
  2567. spin_unlock_irq(&conf->device_lock);
  2568. if (must_retry) {
  2569. release_stripe(sh);
  2570. goto retry;
  2571. }
  2572. }
  2573. /* FIXME what if we get a false positive because these
  2574. * are being updated.
  2575. */
  2576. if (logical_sector >= mddev->suspend_lo &&
  2577. logical_sector < mddev->suspend_hi) {
  2578. release_stripe(sh);
  2579. schedule();
  2580. goto retry;
  2581. }
  2582. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  2583. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  2584. /* Stripe is busy expanding or
  2585. * add failed due to overlap. Flush everything
  2586. * and wait a while
  2587. */
  2588. raid5_unplug_device(mddev->queue);
  2589. release_stripe(sh);
  2590. schedule();
  2591. goto retry;
  2592. }
  2593. finish_wait(&conf->wait_for_overlap, &w);
  2594. handle_stripe(sh, NULL);
  2595. release_stripe(sh);
  2596. } else {
  2597. /* cannot get stripe for read-ahead, just give-up */
  2598. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2599. finish_wait(&conf->wait_for_overlap, &w);
  2600. break;
  2601. }
  2602. }
  2603. spin_lock_irq(&conf->device_lock);
  2604. remaining = --bi->bi_phys_segments;
  2605. spin_unlock_irq(&conf->device_lock);
  2606. if (remaining == 0) {
  2607. int bytes = bi->bi_size;
  2608. if ( rw == WRITE )
  2609. md_write_end(mddev);
  2610. bi->bi_size = 0;
  2611. bi->bi_end_io(bi, bytes,
  2612. test_bit(BIO_UPTODATE, &bi->bi_flags)
  2613. ? 0 : -EIO);
  2614. }
  2615. return 0;
  2616. }
  2617. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  2618. {
  2619. /* reshaping is quite different to recovery/resync so it is
  2620. * handled quite separately ... here.
  2621. *
  2622. * On each call to sync_request, we gather one chunk worth of
  2623. * destination stripes and flag them as expanding.
  2624. * Then we find all the source stripes and request reads.
  2625. * As the reads complete, handle_stripe will copy the data
  2626. * into the destination stripe and release that stripe.
  2627. */
  2628. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2629. struct stripe_head *sh;
  2630. int pd_idx;
  2631. sector_t first_sector, last_sector;
  2632. int raid_disks = conf->previous_raid_disks;
  2633. int data_disks = raid_disks - conf->max_degraded;
  2634. int new_data_disks = conf->raid_disks - conf->max_degraded;
  2635. int i;
  2636. int dd_idx;
  2637. sector_t writepos, safepos, gap;
  2638. if (sector_nr == 0 &&
  2639. conf->expand_progress != 0) {
  2640. /* restarting in the middle, skip the initial sectors */
  2641. sector_nr = conf->expand_progress;
  2642. sector_div(sector_nr, new_data_disks);
  2643. *skipped = 1;
  2644. return sector_nr;
  2645. }
  2646. /* we update the metadata when there is more than 3Meg
  2647. * in the block range (that is rather arbitrary, should
  2648. * probably be time based) or when the data about to be
  2649. * copied would over-write the source of the data at
  2650. * the front of the range.
  2651. * i.e. one new_stripe forward from expand_progress new_maps
  2652. * to after where expand_lo old_maps to
  2653. */
  2654. writepos = conf->expand_progress +
  2655. conf->chunk_size/512*(new_data_disks);
  2656. sector_div(writepos, new_data_disks);
  2657. safepos = conf->expand_lo;
  2658. sector_div(safepos, data_disks);
  2659. gap = conf->expand_progress - conf->expand_lo;
  2660. if (writepos >= safepos ||
  2661. gap > (new_data_disks)*3000*2 /*3Meg*/) {
  2662. /* Cannot proceed until we've updated the superblock... */
  2663. wait_event(conf->wait_for_overlap,
  2664. atomic_read(&conf->reshape_stripes)==0);
  2665. mddev->reshape_position = conf->expand_progress;
  2666. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2667. md_wakeup_thread(mddev->thread);
  2668. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  2669. kthread_should_stop());
  2670. spin_lock_irq(&conf->device_lock);
  2671. conf->expand_lo = mddev->reshape_position;
  2672. spin_unlock_irq(&conf->device_lock);
  2673. wake_up(&conf->wait_for_overlap);
  2674. }
  2675. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  2676. int j;
  2677. int skipped = 0;
  2678. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  2679. sh = get_active_stripe(conf, sector_nr+i,
  2680. conf->raid_disks, pd_idx, 0);
  2681. set_bit(STRIPE_EXPANDING, &sh->state);
  2682. atomic_inc(&conf->reshape_stripes);
  2683. /* If any of this stripe is beyond the end of the old
  2684. * array, then we need to zero those blocks
  2685. */
  2686. for (j=sh->disks; j--;) {
  2687. sector_t s;
  2688. if (j == sh->pd_idx)
  2689. continue;
  2690. if (conf->level == 6 &&
  2691. j == raid6_next_disk(sh->pd_idx, sh->disks))
  2692. continue;
  2693. s = compute_blocknr(sh, j);
  2694. if (s < (mddev->array_size<<1)) {
  2695. skipped = 1;
  2696. continue;
  2697. }
  2698. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  2699. set_bit(R5_Expanded, &sh->dev[j].flags);
  2700. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  2701. }
  2702. if (!skipped) {
  2703. set_bit(STRIPE_EXPAND_READY, &sh->state);
  2704. set_bit(STRIPE_HANDLE, &sh->state);
  2705. }
  2706. release_stripe(sh);
  2707. }
  2708. spin_lock_irq(&conf->device_lock);
  2709. conf->expand_progress = (sector_nr + i) * new_data_disks;
  2710. spin_unlock_irq(&conf->device_lock);
  2711. /* Ok, those stripe are ready. We can start scheduling
  2712. * reads on the source stripes.
  2713. * The source stripes are determined by mapping the first and last
  2714. * block on the destination stripes.
  2715. */
  2716. first_sector =
  2717. raid5_compute_sector(sector_nr*(new_data_disks),
  2718. raid_disks, data_disks,
  2719. &dd_idx, &pd_idx, conf);
  2720. last_sector =
  2721. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  2722. *(new_data_disks) -1,
  2723. raid_disks, data_disks,
  2724. &dd_idx, &pd_idx, conf);
  2725. if (last_sector >= (mddev->size<<1))
  2726. last_sector = (mddev->size<<1)-1;
  2727. while (first_sector <= last_sector) {
  2728. pd_idx = stripe_to_pdidx(first_sector, conf,
  2729. conf->previous_raid_disks);
  2730. sh = get_active_stripe(conf, first_sector,
  2731. conf->previous_raid_disks, pd_idx, 0);
  2732. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2733. set_bit(STRIPE_HANDLE, &sh->state);
  2734. release_stripe(sh);
  2735. first_sector += STRIPE_SECTORS;
  2736. }
  2737. return conf->chunk_size>>9;
  2738. }
  2739. /* FIXME go_faster isn't used */
  2740. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  2741. {
  2742. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2743. struct stripe_head *sh;
  2744. int pd_idx;
  2745. int raid_disks = conf->raid_disks;
  2746. sector_t max_sector = mddev->size << 1;
  2747. int sync_blocks;
  2748. int still_degraded = 0;
  2749. int i;
  2750. if (sector_nr >= max_sector) {
  2751. /* just being told to finish up .. nothing much to do */
  2752. unplug_slaves(mddev);
  2753. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2754. end_reshape(conf);
  2755. return 0;
  2756. }
  2757. if (mddev->curr_resync < max_sector) /* aborted */
  2758. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2759. &sync_blocks, 1);
  2760. else /* completed sync */
  2761. conf->fullsync = 0;
  2762. bitmap_close_sync(mddev->bitmap);
  2763. return 0;
  2764. }
  2765. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2766. return reshape_request(mddev, sector_nr, skipped);
  2767. /* if there is too many failed drives and we are trying
  2768. * to resync, then assert that we are finished, because there is
  2769. * nothing we can do.
  2770. */
  2771. if (mddev->degraded >= conf->max_degraded &&
  2772. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2773. sector_t rv = (mddev->size << 1) - sector_nr;
  2774. *skipped = 1;
  2775. return rv;
  2776. }
  2777. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2778. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2779. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  2780. /* we can skip this block, and probably more */
  2781. sync_blocks /= STRIPE_SECTORS;
  2782. *skipped = 1;
  2783. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  2784. }
  2785. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  2786. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  2787. if (sh == NULL) {
  2788. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  2789. /* make sure we don't swamp the stripe cache if someone else
  2790. * is trying to get access
  2791. */
  2792. schedule_timeout_uninterruptible(1);
  2793. }
  2794. /* Need to check if array will still be degraded after recovery/resync
  2795. * We don't need to check the 'failed' flag as when that gets set,
  2796. * recovery aborts.
  2797. */
  2798. for (i=0; i<mddev->raid_disks; i++)
  2799. if (conf->disks[i].rdev == NULL)
  2800. still_degraded = 1;
  2801. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  2802. spin_lock(&sh->lock);
  2803. set_bit(STRIPE_SYNCING, &sh->state);
  2804. clear_bit(STRIPE_INSYNC, &sh->state);
  2805. spin_unlock(&sh->lock);
  2806. handle_stripe(sh, NULL);
  2807. release_stripe(sh);
  2808. return STRIPE_SECTORS;
  2809. }
  2810. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  2811. {
  2812. /* We may not be able to submit a whole bio at once as there
  2813. * may not be enough stripe_heads available.
  2814. * We cannot pre-allocate enough stripe_heads as we may need
  2815. * more than exist in the cache (if we allow ever large chunks).
  2816. * So we do one stripe head at a time and record in
  2817. * ->bi_hw_segments how many have been done.
  2818. *
  2819. * We *know* that this entire raid_bio is in one chunk, so
  2820. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  2821. */
  2822. struct stripe_head *sh;
  2823. int dd_idx, pd_idx;
  2824. sector_t sector, logical_sector, last_sector;
  2825. int scnt = 0;
  2826. int remaining;
  2827. int handled = 0;
  2828. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2829. sector = raid5_compute_sector( logical_sector,
  2830. conf->raid_disks,
  2831. conf->raid_disks - conf->max_degraded,
  2832. &dd_idx,
  2833. &pd_idx,
  2834. conf);
  2835. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  2836. for (; logical_sector < last_sector;
  2837. logical_sector += STRIPE_SECTORS,
  2838. sector += STRIPE_SECTORS,
  2839. scnt++) {
  2840. if (scnt < raid_bio->bi_hw_segments)
  2841. /* already done this stripe */
  2842. continue;
  2843. sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
  2844. if (!sh) {
  2845. /* failed to get a stripe - must wait */
  2846. raid_bio->bi_hw_segments = scnt;
  2847. conf->retry_read_aligned = raid_bio;
  2848. return handled;
  2849. }
  2850. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  2851. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  2852. release_stripe(sh);
  2853. raid_bio->bi_hw_segments = scnt;
  2854. conf->retry_read_aligned = raid_bio;
  2855. return handled;
  2856. }
  2857. handle_stripe(sh, NULL);
  2858. release_stripe(sh);
  2859. handled++;
  2860. }
  2861. spin_lock_irq(&conf->device_lock);
  2862. remaining = --raid_bio->bi_phys_segments;
  2863. spin_unlock_irq(&conf->device_lock);
  2864. if (remaining == 0) {
  2865. int bytes = raid_bio->bi_size;
  2866. raid_bio->bi_size = 0;
  2867. raid_bio->bi_end_io(raid_bio, bytes,
  2868. test_bit(BIO_UPTODATE, &raid_bio->bi_flags)
  2869. ? 0 : -EIO);
  2870. }
  2871. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2872. wake_up(&conf->wait_for_stripe);
  2873. return handled;
  2874. }
  2875. /*
  2876. * This is our raid5 kernel thread.
  2877. *
  2878. * We scan the hash table for stripes which can be handled now.
  2879. * During the scan, completed stripes are saved for us by the interrupt
  2880. * handler, so that they will not have to wait for our next wakeup.
  2881. */
  2882. static void raid5d (mddev_t *mddev)
  2883. {
  2884. struct stripe_head *sh;
  2885. raid5_conf_t *conf = mddev_to_conf(mddev);
  2886. int handled;
  2887. pr_debug("+++ raid5d active\n");
  2888. md_check_recovery(mddev);
  2889. handled = 0;
  2890. spin_lock_irq(&conf->device_lock);
  2891. while (1) {
  2892. struct list_head *first;
  2893. struct bio *bio;
  2894. if (conf->seq_flush != conf->seq_write) {
  2895. int seq = conf->seq_flush;
  2896. spin_unlock_irq(&conf->device_lock);
  2897. bitmap_unplug(mddev->bitmap);
  2898. spin_lock_irq(&conf->device_lock);
  2899. conf->seq_write = seq;
  2900. activate_bit_delay(conf);
  2901. }
  2902. if (list_empty(&conf->handle_list) &&
  2903. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  2904. !blk_queue_plugged(mddev->queue) &&
  2905. !list_empty(&conf->delayed_list))
  2906. raid5_activate_delayed(conf);
  2907. while ((bio = remove_bio_from_retry(conf))) {
  2908. int ok;
  2909. spin_unlock_irq(&conf->device_lock);
  2910. ok = retry_aligned_read(conf, bio);
  2911. spin_lock_irq(&conf->device_lock);
  2912. if (!ok)
  2913. break;
  2914. handled++;
  2915. }
  2916. if (list_empty(&conf->handle_list))
  2917. break;
  2918. first = conf->handle_list.next;
  2919. sh = list_entry(first, struct stripe_head, lru);
  2920. list_del_init(first);
  2921. atomic_inc(&sh->count);
  2922. BUG_ON(atomic_read(&sh->count)!= 1);
  2923. spin_unlock_irq(&conf->device_lock);
  2924. handled++;
  2925. handle_stripe(sh, conf->spare_page);
  2926. release_stripe(sh);
  2927. spin_lock_irq(&conf->device_lock);
  2928. }
  2929. pr_debug("%d stripes handled\n", handled);
  2930. spin_unlock_irq(&conf->device_lock);
  2931. unplug_slaves(mddev);
  2932. pr_debug("--- raid5d inactive\n");
  2933. }
  2934. static ssize_t
  2935. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  2936. {
  2937. raid5_conf_t *conf = mddev_to_conf(mddev);
  2938. if (conf)
  2939. return sprintf(page, "%d\n", conf->max_nr_stripes);
  2940. else
  2941. return 0;
  2942. }
  2943. static ssize_t
  2944. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  2945. {
  2946. raid5_conf_t *conf = mddev_to_conf(mddev);
  2947. char *end;
  2948. int new;
  2949. if (len >= PAGE_SIZE)
  2950. return -EINVAL;
  2951. if (!conf)
  2952. return -ENODEV;
  2953. new = simple_strtoul(page, &end, 10);
  2954. if (!*page || (*end && *end != '\n') )
  2955. return -EINVAL;
  2956. if (new <= 16 || new > 32768)
  2957. return -EINVAL;
  2958. while (new < conf->max_nr_stripes) {
  2959. if (drop_one_stripe(conf))
  2960. conf->max_nr_stripes--;
  2961. else
  2962. break;
  2963. }
  2964. md_allow_write(mddev);
  2965. while (new > conf->max_nr_stripes) {
  2966. if (grow_one_stripe(conf))
  2967. conf->max_nr_stripes++;
  2968. else break;
  2969. }
  2970. return len;
  2971. }
  2972. static struct md_sysfs_entry
  2973. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  2974. raid5_show_stripe_cache_size,
  2975. raid5_store_stripe_cache_size);
  2976. static ssize_t
  2977. stripe_cache_active_show(mddev_t *mddev, char *page)
  2978. {
  2979. raid5_conf_t *conf = mddev_to_conf(mddev);
  2980. if (conf)
  2981. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  2982. else
  2983. return 0;
  2984. }
  2985. static struct md_sysfs_entry
  2986. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  2987. static struct attribute *raid5_attrs[] = {
  2988. &raid5_stripecache_size.attr,
  2989. &raid5_stripecache_active.attr,
  2990. NULL,
  2991. };
  2992. static struct attribute_group raid5_attrs_group = {
  2993. .name = NULL,
  2994. .attrs = raid5_attrs,
  2995. };
  2996. static int run(mddev_t *mddev)
  2997. {
  2998. raid5_conf_t *conf;
  2999. int raid_disk, memory;
  3000. mdk_rdev_t *rdev;
  3001. struct disk_info *disk;
  3002. struct list_head *tmp;
  3003. int working_disks = 0;
  3004. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  3005. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3006. mdname(mddev), mddev->level);
  3007. return -EIO;
  3008. }
  3009. if (mddev->reshape_position != MaxSector) {
  3010. /* Check that we can continue the reshape.
  3011. * Currently only disks can change, it must
  3012. * increase, and we must be past the point where
  3013. * a stripe over-writes itself
  3014. */
  3015. sector_t here_new, here_old;
  3016. int old_disks;
  3017. int max_degraded = (mddev->level == 5 ? 1 : 2);
  3018. if (mddev->new_level != mddev->level ||
  3019. mddev->new_layout != mddev->layout ||
  3020. mddev->new_chunk != mddev->chunk_size) {
  3021. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3022. "required - aborting.\n",
  3023. mdname(mddev));
  3024. return -EINVAL;
  3025. }
  3026. if (mddev->delta_disks <= 0) {
  3027. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3028. "(reduce disks) required - aborting.\n",
  3029. mdname(mddev));
  3030. return -EINVAL;
  3031. }
  3032. old_disks = mddev->raid_disks - mddev->delta_disks;
  3033. /* reshape_position must be on a new-stripe boundary, and one
  3034. * further up in new geometry must map after here in old
  3035. * geometry.
  3036. */
  3037. here_new = mddev->reshape_position;
  3038. if (sector_div(here_new, (mddev->chunk_size>>9)*
  3039. (mddev->raid_disks - max_degraded))) {
  3040. printk(KERN_ERR "raid5: reshape_position not "
  3041. "on a stripe boundary\n");
  3042. return -EINVAL;
  3043. }
  3044. /* here_new is the stripe we will write to */
  3045. here_old = mddev->reshape_position;
  3046. sector_div(here_old, (mddev->chunk_size>>9)*
  3047. (old_disks-max_degraded));
  3048. /* here_old is the first stripe that we might need to read
  3049. * from */
  3050. if (here_new >= here_old) {
  3051. /* Reading from the same stripe as writing to - bad */
  3052. printk(KERN_ERR "raid5: reshape_position too early for "
  3053. "auto-recovery - aborting.\n");
  3054. return -EINVAL;
  3055. }
  3056. printk(KERN_INFO "raid5: reshape will continue\n");
  3057. /* OK, we should be able to continue; */
  3058. }
  3059. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  3060. if ((conf = mddev->private) == NULL)
  3061. goto abort;
  3062. if (mddev->reshape_position == MaxSector) {
  3063. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  3064. } else {
  3065. conf->raid_disks = mddev->raid_disks;
  3066. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3067. }
  3068. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3069. GFP_KERNEL);
  3070. if (!conf->disks)
  3071. goto abort;
  3072. conf->mddev = mddev;
  3073. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3074. goto abort;
  3075. if (mddev->level == 6) {
  3076. conf->spare_page = alloc_page(GFP_KERNEL);
  3077. if (!conf->spare_page)
  3078. goto abort;
  3079. }
  3080. spin_lock_init(&conf->device_lock);
  3081. init_waitqueue_head(&conf->wait_for_stripe);
  3082. init_waitqueue_head(&conf->wait_for_overlap);
  3083. INIT_LIST_HEAD(&conf->handle_list);
  3084. INIT_LIST_HEAD(&conf->delayed_list);
  3085. INIT_LIST_HEAD(&conf->bitmap_list);
  3086. INIT_LIST_HEAD(&conf->inactive_list);
  3087. atomic_set(&conf->active_stripes, 0);
  3088. atomic_set(&conf->preread_active_stripes, 0);
  3089. atomic_set(&conf->active_aligned_reads, 0);
  3090. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  3091. ITERATE_RDEV(mddev,rdev,tmp) {
  3092. raid_disk = rdev->raid_disk;
  3093. if (raid_disk >= conf->raid_disks
  3094. || raid_disk < 0)
  3095. continue;
  3096. disk = conf->disks + raid_disk;
  3097. disk->rdev = rdev;
  3098. if (test_bit(In_sync, &rdev->flags)) {
  3099. char b[BDEVNAME_SIZE];
  3100. printk(KERN_INFO "raid5: device %s operational as raid"
  3101. " disk %d\n", bdevname(rdev->bdev,b),
  3102. raid_disk);
  3103. working_disks++;
  3104. }
  3105. }
  3106. /*
  3107. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3108. */
  3109. mddev->degraded = conf->raid_disks - working_disks;
  3110. conf->mddev = mddev;
  3111. conf->chunk_size = mddev->chunk_size;
  3112. conf->level = mddev->level;
  3113. if (conf->level == 6)
  3114. conf->max_degraded = 2;
  3115. else
  3116. conf->max_degraded = 1;
  3117. conf->algorithm = mddev->layout;
  3118. conf->max_nr_stripes = NR_STRIPES;
  3119. conf->expand_progress = mddev->reshape_position;
  3120. /* device size must be a multiple of chunk size */
  3121. mddev->size &= ~(mddev->chunk_size/1024 -1);
  3122. mddev->resync_max_sectors = mddev->size << 1;
  3123. if (conf->level == 6 && conf->raid_disks < 4) {
  3124. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3125. mdname(mddev), conf->raid_disks);
  3126. goto abort;
  3127. }
  3128. if (!conf->chunk_size || conf->chunk_size % 4) {
  3129. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3130. conf->chunk_size, mdname(mddev));
  3131. goto abort;
  3132. }
  3133. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  3134. printk(KERN_ERR
  3135. "raid5: unsupported parity algorithm %d for %s\n",
  3136. conf->algorithm, mdname(mddev));
  3137. goto abort;
  3138. }
  3139. if (mddev->degraded > conf->max_degraded) {
  3140. printk(KERN_ERR "raid5: not enough operational devices for %s"
  3141. " (%d/%d failed)\n",
  3142. mdname(mddev), mddev->degraded, conf->raid_disks);
  3143. goto abort;
  3144. }
  3145. if (mddev->degraded > 0 &&
  3146. mddev->recovery_cp != MaxSector) {
  3147. if (mddev->ok_start_degraded)
  3148. printk(KERN_WARNING
  3149. "raid5: starting dirty degraded array: %s"
  3150. "- data corruption possible.\n",
  3151. mdname(mddev));
  3152. else {
  3153. printk(KERN_ERR
  3154. "raid5: cannot start dirty degraded array for %s\n",
  3155. mdname(mddev));
  3156. goto abort;
  3157. }
  3158. }
  3159. {
  3160. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3161. if (!mddev->thread) {
  3162. printk(KERN_ERR
  3163. "raid5: couldn't allocate thread for %s\n",
  3164. mdname(mddev));
  3165. goto abort;
  3166. }
  3167. }
  3168. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3169. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3170. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3171. printk(KERN_ERR
  3172. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3173. shrink_stripes(conf);
  3174. md_unregister_thread(mddev->thread);
  3175. goto abort;
  3176. } else
  3177. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3178. memory, mdname(mddev));
  3179. if (mddev->degraded == 0)
  3180. printk("raid5: raid level %d set %s active with %d out of %d"
  3181. " devices, algorithm %d\n", conf->level, mdname(mddev),
  3182. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  3183. conf->algorithm);
  3184. else
  3185. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  3186. " out of %d devices, algorithm %d\n", conf->level,
  3187. mdname(mddev), mddev->raid_disks - mddev->degraded,
  3188. mddev->raid_disks, conf->algorithm);
  3189. print_raid5_conf(conf);
  3190. if (conf->expand_progress != MaxSector) {
  3191. printk("...ok start reshape thread\n");
  3192. conf->expand_lo = conf->expand_progress;
  3193. atomic_set(&conf->reshape_stripes, 0);
  3194. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3195. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3196. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3197. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3198. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3199. "%s_reshape");
  3200. }
  3201. /* read-ahead size must cover two whole stripes, which is
  3202. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3203. */
  3204. {
  3205. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3206. int stripe = data_disks *
  3207. (mddev->chunk_size / PAGE_SIZE);
  3208. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3209. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3210. }
  3211. /* Ok, everything is just fine now */
  3212. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  3213. printk(KERN_WARNING
  3214. "raid5: failed to create sysfs attributes for %s\n",
  3215. mdname(mddev));
  3216. mddev->queue->unplug_fn = raid5_unplug_device;
  3217. mddev->queue->issue_flush_fn = raid5_issue_flush;
  3218. mddev->queue->backing_dev_info.congested_data = mddev;
  3219. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  3220. mddev->array_size = mddev->size * (conf->previous_raid_disks -
  3221. conf->max_degraded);
  3222. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  3223. return 0;
  3224. abort:
  3225. if (conf) {
  3226. print_raid5_conf(conf);
  3227. safe_put_page(conf->spare_page);
  3228. kfree(conf->disks);
  3229. kfree(conf->stripe_hashtbl);
  3230. kfree(conf);
  3231. }
  3232. mddev->private = NULL;
  3233. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  3234. return -EIO;
  3235. }
  3236. static int stop(mddev_t *mddev)
  3237. {
  3238. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3239. md_unregister_thread(mddev->thread);
  3240. mddev->thread = NULL;
  3241. shrink_stripes(conf);
  3242. kfree(conf->stripe_hashtbl);
  3243. mddev->queue->backing_dev_info.congested_fn = NULL;
  3244. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3245. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3246. kfree(conf->disks);
  3247. kfree(conf);
  3248. mddev->private = NULL;
  3249. return 0;
  3250. }
  3251. #ifdef DEBUG
  3252. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  3253. {
  3254. int i;
  3255. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3256. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3257. seq_printf(seq, "sh %llu, count %d.\n",
  3258. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3259. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3260. for (i = 0; i < sh->disks; i++) {
  3261. seq_printf(seq, "(cache%d: %p %ld) ",
  3262. i, sh->dev[i].page, sh->dev[i].flags);
  3263. }
  3264. seq_printf(seq, "\n");
  3265. }
  3266. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  3267. {
  3268. struct stripe_head *sh;
  3269. struct hlist_node *hn;
  3270. int i;
  3271. spin_lock_irq(&conf->device_lock);
  3272. for (i = 0; i < NR_HASH; i++) {
  3273. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3274. if (sh->raid_conf != conf)
  3275. continue;
  3276. print_sh(seq, sh);
  3277. }
  3278. }
  3279. spin_unlock_irq(&conf->device_lock);
  3280. }
  3281. #endif
  3282. static void status (struct seq_file *seq, mddev_t *mddev)
  3283. {
  3284. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3285. int i;
  3286. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3287. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  3288. for (i = 0; i < conf->raid_disks; i++)
  3289. seq_printf (seq, "%s",
  3290. conf->disks[i].rdev &&
  3291. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3292. seq_printf (seq, "]");
  3293. #ifdef DEBUG
  3294. seq_printf (seq, "\n");
  3295. printall(seq, conf);
  3296. #endif
  3297. }
  3298. static void print_raid5_conf (raid5_conf_t *conf)
  3299. {
  3300. int i;
  3301. struct disk_info *tmp;
  3302. printk("RAID5 conf printout:\n");
  3303. if (!conf) {
  3304. printk("(conf==NULL)\n");
  3305. return;
  3306. }
  3307. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  3308. conf->raid_disks - conf->mddev->degraded);
  3309. for (i = 0; i < conf->raid_disks; i++) {
  3310. char b[BDEVNAME_SIZE];
  3311. tmp = conf->disks + i;
  3312. if (tmp->rdev)
  3313. printk(" disk %d, o:%d, dev:%s\n",
  3314. i, !test_bit(Faulty, &tmp->rdev->flags),
  3315. bdevname(tmp->rdev->bdev,b));
  3316. }
  3317. }
  3318. static int raid5_spare_active(mddev_t *mddev)
  3319. {
  3320. int i;
  3321. raid5_conf_t *conf = mddev->private;
  3322. struct disk_info *tmp;
  3323. for (i = 0; i < conf->raid_disks; i++) {
  3324. tmp = conf->disks + i;
  3325. if (tmp->rdev
  3326. && !test_bit(Faulty, &tmp->rdev->flags)
  3327. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  3328. unsigned long flags;
  3329. spin_lock_irqsave(&conf->device_lock, flags);
  3330. mddev->degraded--;
  3331. spin_unlock_irqrestore(&conf->device_lock, flags);
  3332. }
  3333. }
  3334. print_raid5_conf(conf);
  3335. return 0;
  3336. }
  3337. static int raid5_remove_disk(mddev_t *mddev, int number)
  3338. {
  3339. raid5_conf_t *conf = mddev->private;
  3340. int err = 0;
  3341. mdk_rdev_t *rdev;
  3342. struct disk_info *p = conf->disks + number;
  3343. print_raid5_conf(conf);
  3344. rdev = p->rdev;
  3345. if (rdev) {
  3346. if (test_bit(In_sync, &rdev->flags) ||
  3347. atomic_read(&rdev->nr_pending)) {
  3348. err = -EBUSY;
  3349. goto abort;
  3350. }
  3351. p->rdev = NULL;
  3352. synchronize_rcu();
  3353. if (atomic_read(&rdev->nr_pending)) {
  3354. /* lost the race, try later */
  3355. err = -EBUSY;
  3356. p->rdev = rdev;
  3357. }
  3358. }
  3359. abort:
  3360. print_raid5_conf(conf);
  3361. return err;
  3362. }
  3363. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3364. {
  3365. raid5_conf_t *conf = mddev->private;
  3366. int found = 0;
  3367. int disk;
  3368. struct disk_info *p;
  3369. if (mddev->degraded > conf->max_degraded)
  3370. /* no point adding a device */
  3371. return 0;
  3372. /*
  3373. * find the disk ... but prefer rdev->saved_raid_disk
  3374. * if possible.
  3375. */
  3376. if (rdev->saved_raid_disk >= 0 &&
  3377. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3378. disk = rdev->saved_raid_disk;
  3379. else
  3380. disk = 0;
  3381. for ( ; disk < conf->raid_disks; disk++)
  3382. if ((p=conf->disks + disk)->rdev == NULL) {
  3383. clear_bit(In_sync, &rdev->flags);
  3384. rdev->raid_disk = disk;
  3385. found = 1;
  3386. if (rdev->saved_raid_disk != disk)
  3387. conf->fullsync = 1;
  3388. rcu_assign_pointer(p->rdev, rdev);
  3389. break;
  3390. }
  3391. print_raid5_conf(conf);
  3392. return found;
  3393. }
  3394. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3395. {
  3396. /* no resync is happening, and there is enough space
  3397. * on all devices, so we can resize.
  3398. * We need to make sure resync covers any new space.
  3399. * If the array is shrinking we should possibly wait until
  3400. * any io in the removed space completes, but it hardly seems
  3401. * worth it.
  3402. */
  3403. raid5_conf_t *conf = mddev_to_conf(mddev);
  3404. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3405. mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
  3406. set_capacity(mddev->gendisk, mddev->array_size << 1);
  3407. mddev->changed = 1;
  3408. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3409. mddev->recovery_cp = mddev->size << 1;
  3410. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3411. }
  3412. mddev->size = sectors /2;
  3413. mddev->resync_max_sectors = sectors;
  3414. return 0;
  3415. }
  3416. #ifdef CONFIG_MD_RAID5_RESHAPE
  3417. static int raid5_check_reshape(mddev_t *mddev)
  3418. {
  3419. raid5_conf_t *conf = mddev_to_conf(mddev);
  3420. int err;
  3421. if (mddev->delta_disks < 0 ||
  3422. mddev->new_level != mddev->level)
  3423. return -EINVAL; /* Cannot shrink array or change level yet */
  3424. if (mddev->delta_disks == 0)
  3425. return 0; /* nothing to do */
  3426. /* Can only proceed if there are plenty of stripe_heads.
  3427. * We need a minimum of one full stripe,, and for sensible progress
  3428. * it is best to have about 4 times that.
  3429. * If we require 4 times, then the default 256 4K stripe_heads will
  3430. * allow for chunk sizes up to 256K, which is probably OK.
  3431. * If the chunk size is greater, user-space should request more
  3432. * stripe_heads first.
  3433. */
  3434. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3435. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3436. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3437. (mddev->chunk_size / STRIPE_SIZE)*4);
  3438. return -ENOSPC;
  3439. }
  3440. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  3441. if (err)
  3442. return err;
  3443. if (mddev->degraded > conf->max_degraded)
  3444. return -EINVAL;
  3445. /* looks like we might be able to manage this */
  3446. return 0;
  3447. }
  3448. static int raid5_start_reshape(mddev_t *mddev)
  3449. {
  3450. raid5_conf_t *conf = mddev_to_conf(mddev);
  3451. mdk_rdev_t *rdev;
  3452. struct list_head *rtmp;
  3453. int spares = 0;
  3454. int added_devices = 0;
  3455. unsigned long flags;
  3456. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3457. return -EBUSY;
  3458. ITERATE_RDEV(mddev, rdev, rtmp)
  3459. if (rdev->raid_disk < 0 &&
  3460. !test_bit(Faulty, &rdev->flags))
  3461. spares++;
  3462. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  3463. /* Not enough devices even to make a degraded array
  3464. * of that size
  3465. */
  3466. return -EINVAL;
  3467. atomic_set(&conf->reshape_stripes, 0);
  3468. spin_lock_irq(&conf->device_lock);
  3469. conf->previous_raid_disks = conf->raid_disks;
  3470. conf->raid_disks += mddev->delta_disks;
  3471. conf->expand_progress = 0;
  3472. conf->expand_lo = 0;
  3473. spin_unlock_irq(&conf->device_lock);
  3474. /* Add some new drives, as many as will fit.
  3475. * We know there are enough to make the newly sized array work.
  3476. */
  3477. ITERATE_RDEV(mddev, rdev, rtmp)
  3478. if (rdev->raid_disk < 0 &&
  3479. !test_bit(Faulty, &rdev->flags)) {
  3480. if (raid5_add_disk(mddev, rdev)) {
  3481. char nm[20];
  3482. set_bit(In_sync, &rdev->flags);
  3483. added_devices++;
  3484. rdev->recovery_offset = 0;
  3485. sprintf(nm, "rd%d", rdev->raid_disk);
  3486. if (sysfs_create_link(&mddev->kobj,
  3487. &rdev->kobj, nm))
  3488. printk(KERN_WARNING
  3489. "raid5: failed to create "
  3490. " link %s for %s\n",
  3491. nm, mdname(mddev));
  3492. } else
  3493. break;
  3494. }
  3495. spin_lock_irqsave(&conf->device_lock, flags);
  3496. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  3497. spin_unlock_irqrestore(&conf->device_lock, flags);
  3498. mddev->raid_disks = conf->raid_disks;
  3499. mddev->reshape_position = 0;
  3500. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3501. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3502. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3503. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3504. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3505. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3506. "%s_reshape");
  3507. if (!mddev->sync_thread) {
  3508. mddev->recovery = 0;
  3509. spin_lock_irq(&conf->device_lock);
  3510. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  3511. conf->expand_progress = MaxSector;
  3512. spin_unlock_irq(&conf->device_lock);
  3513. return -EAGAIN;
  3514. }
  3515. md_wakeup_thread(mddev->sync_thread);
  3516. md_new_event(mddev);
  3517. return 0;
  3518. }
  3519. #endif
  3520. static void end_reshape(raid5_conf_t *conf)
  3521. {
  3522. struct block_device *bdev;
  3523. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  3524. conf->mddev->array_size = conf->mddev->size *
  3525. (conf->raid_disks - conf->max_degraded);
  3526. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  3527. conf->mddev->changed = 1;
  3528. bdev = bdget_disk(conf->mddev->gendisk, 0);
  3529. if (bdev) {
  3530. mutex_lock(&bdev->bd_inode->i_mutex);
  3531. i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
  3532. mutex_unlock(&bdev->bd_inode->i_mutex);
  3533. bdput(bdev);
  3534. }
  3535. spin_lock_irq(&conf->device_lock);
  3536. conf->expand_progress = MaxSector;
  3537. spin_unlock_irq(&conf->device_lock);
  3538. conf->mddev->reshape_position = MaxSector;
  3539. /* read-ahead size must cover two whole stripes, which is
  3540. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3541. */
  3542. {
  3543. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3544. int stripe = data_disks *
  3545. (conf->mddev->chunk_size / PAGE_SIZE);
  3546. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3547. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3548. }
  3549. }
  3550. }
  3551. static void raid5_quiesce(mddev_t *mddev, int state)
  3552. {
  3553. raid5_conf_t *conf = mddev_to_conf(mddev);
  3554. switch(state) {
  3555. case 2: /* resume for a suspend */
  3556. wake_up(&conf->wait_for_overlap);
  3557. break;
  3558. case 1: /* stop all writes */
  3559. spin_lock_irq(&conf->device_lock);
  3560. conf->quiesce = 1;
  3561. wait_event_lock_irq(conf->wait_for_stripe,
  3562. atomic_read(&conf->active_stripes) == 0 &&
  3563. atomic_read(&conf->active_aligned_reads) == 0,
  3564. conf->device_lock, /* nothing */);
  3565. spin_unlock_irq(&conf->device_lock);
  3566. break;
  3567. case 0: /* re-enable writes */
  3568. spin_lock_irq(&conf->device_lock);
  3569. conf->quiesce = 0;
  3570. wake_up(&conf->wait_for_stripe);
  3571. wake_up(&conf->wait_for_overlap);
  3572. spin_unlock_irq(&conf->device_lock);
  3573. break;
  3574. }
  3575. }
  3576. static struct mdk_personality raid6_personality =
  3577. {
  3578. .name = "raid6",
  3579. .level = 6,
  3580. .owner = THIS_MODULE,
  3581. .make_request = make_request,
  3582. .run = run,
  3583. .stop = stop,
  3584. .status = status,
  3585. .error_handler = error,
  3586. .hot_add_disk = raid5_add_disk,
  3587. .hot_remove_disk= raid5_remove_disk,
  3588. .spare_active = raid5_spare_active,
  3589. .sync_request = sync_request,
  3590. .resize = raid5_resize,
  3591. #ifdef CONFIG_MD_RAID5_RESHAPE
  3592. .check_reshape = raid5_check_reshape,
  3593. .start_reshape = raid5_start_reshape,
  3594. #endif
  3595. .quiesce = raid5_quiesce,
  3596. };
  3597. static struct mdk_personality raid5_personality =
  3598. {
  3599. .name = "raid5",
  3600. .level = 5,
  3601. .owner = THIS_MODULE,
  3602. .make_request = make_request,
  3603. .run = run,
  3604. .stop = stop,
  3605. .status = status,
  3606. .error_handler = error,
  3607. .hot_add_disk = raid5_add_disk,
  3608. .hot_remove_disk= raid5_remove_disk,
  3609. .spare_active = raid5_spare_active,
  3610. .sync_request = sync_request,
  3611. .resize = raid5_resize,
  3612. #ifdef CONFIG_MD_RAID5_RESHAPE
  3613. .check_reshape = raid5_check_reshape,
  3614. .start_reshape = raid5_start_reshape,
  3615. #endif
  3616. .quiesce = raid5_quiesce,
  3617. };
  3618. static struct mdk_personality raid4_personality =
  3619. {
  3620. .name = "raid4",
  3621. .level = 4,
  3622. .owner = THIS_MODULE,
  3623. .make_request = make_request,
  3624. .run = run,
  3625. .stop = stop,
  3626. .status = status,
  3627. .error_handler = error,
  3628. .hot_add_disk = raid5_add_disk,
  3629. .hot_remove_disk= raid5_remove_disk,
  3630. .spare_active = raid5_spare_active,
  3631. .sync_request = sync_request,
  3632. .resize = raid5_resize,
  3633. #ifdef CONFIG_MD_RAID5_RESHAPE
  3634. .check_reshape = raid5_check_reshape,
  3635. .start_reshape = raid5_start_reshape,
  3636. #endif
  3637. .quiesce = raid5_quiesce,
  3638. };
  3639. static int __init raid5_init(void)
  3640. {
  3641. int e;
  3642. e = raid6_select_algo();
  3643. if ( e )
  3644. return e;
  3645. register_md_personality(&raid6_personality);
  3646. register_md_personality(&raid5_personality);
  3647. register_md_personality(&raid4_personality);
  3648. return 0;
  3649. }
  3650. static void raid5_exit(void)
  3651. {
  3652. unregister_md_personality(&raid6_personality);
  3653. unregister_md_personality(&raid5_personality);
  3654. unregister_md_personality(&raid4_personality);
  3655. }
  3656. module_init(raid5_init);
  3657. module_exit(raid5_exit);
  3658. MODULE_LICENSE("GPL");
  3659. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  3660. MODULE_ALIAS("md-raid5");
  3661. MODULE_ALIAS("md-raid4");
  3662. MODULE_ALIAS("md-level-5");
  3663. MODULE_ALIAS("md-level-4");
  3664. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  3665. MODULE_ALIAS("md-raid6");
  3666. MODULE_ALIAS("md-level-6");
  3667. /* This used to be two separate modules, they were: */
  3668. MODULE_ALIAS("raid5");
  3669. MODULE_ALIAS("raid6");