raid5.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833
  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. *
  6. * RAID-5 management functions.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * (for example /usr/src/linux/COPYING); if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/raid/raid5.h>
  21. #include <linux/highmem.h>
  22. #include <linux/bitops.h>
  23. #include <linux/kthread.h>
  24. #include <asm/atomic.h>
  25. #include <linux/raid/bitmap.h>
  26. /*
  27. * Stripe cache
  28. */
  29. #define NR_STRIPES 256
  30. #define STRIPE_SIZE PAGE_SIZE
  31. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  32. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  33. #define IO_THRESHOLD 1
  34. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  35. #define HASH_MASK (NR_HASH - 1)
  36. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  37. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  38. * order without overlap. There may be several bio's per stripe+device, and
  39. * a bio could span several devices.
  40. * When walking this list for a particular stripe+device, we must never proceed
  41. * beyond a bio that extends past this device, as the next bio might no longer
  42. * be valid.
  43. * This macro is used to determine the 'next' bio in the list, given the sector
  44. * of the current stripe+device
  45. */
  46. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  47. /*
  48. * The following can be used to debug the driver
  49. */
  50. #define RAID5_DEBUG 0
  51. #define RAID5_PARANOIA 1
  52. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  53. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  54. #else
  55. # define CHECK_DEVLOCK()
  56. #endif
  57. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  58. #if RAID5_DEBUG
  59. #define inline
  60. #define __inline__
  61. #endif
  62. static void print_raid5_conf (raid5_conf_t *conf);
  63. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  64. {
  65. if (atomic_dec_and_test(&sh->count)) {
  66. if (!list_empty(&sh->lru))
  67. BUG();
  68. if (atomic_read(&conf->active_stripes)==0)
  69. BUG();
  70. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  71. if (test_bit(STRIPE_DELAYED, &sh->state))
  72. list_add_tail(&sh->lru, &conf->delayed_list);
  73. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  74. conf->seq_write == sh->bm_seq)
  75. list_add_tail(&sh->lru, &conf->bitmap_list);
  76. else {
  77. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  78. list_add_tail(&sh->lru, &conf->handle_list);
  79. }
  80. md_wakeup_thread(conf->mddev->thread);
  81. } else {
  82. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  83. atomic_dec(&conf->preread_active_stripes);
  84. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  85. md_wakeup_thread(conf->mddev->thread);
  86. }
  87. atomic_dec(&conf->active_stripes);
  88. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  89. list_add_tail(&sh->lru, &conf->inactive_list);
  90. wake_up(&conf->wait_for_stripe);
  91. }
  92. }
  93. }
  94. }
  95. static void release_stripe(struct stripe_head *sh)
  96. {
  97. raid5_conf_t *conf = sh->raid_conf;
  98. unsigned long flags;
  99. spin_lock_irqsave(&conf->device_lock, flags);
  100. __release_stripe(conf, sh);
  101. spin_unlock_irqrestore(&conf->device_lock, flags);
  102. }
  103. static inline void remove_hash(struct stripe_head *sh)
  104. {
  105. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  106. hlist_del_init(&sh->hash);
  107. }
  108. static void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  109. {
  110. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  111. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  112. CHECK_DEVLOCK();
  113. hlist_add_head(&sh->hash, hp);
  114. }
  115. /* find an idle stripe, make sure it is unhashed, and return it. */
  116. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  117. {
  118. struct stripe_head *sh = NULL;
  119. struct list_head *first;
  120. CHECK_DEVLOCK();
  121. if (list_empty(&conf->inactive_list))
  122. goto out;
  123. first = conf->inactive_list.next;
  124. sh = list_entry(first, struct stripe_head, lru);
  125. list_del_init(first);
  126. remove_hash(sh);
  127. atomic_inc(&conf->active_stripes);
  128. out:
  129. return sh;
  130. }
  131. static void shrink_buffers(struct stripe_head *sh, int num)
  132. {
  133. struct page *p;
  134. int i;
  135. for (i=0; i<num ; i++) {
  136. p = sh->dev[i].page;
  137. if (!p)
  138. continue;
  139. sh->dev[i].page = NULL;
  140. put_page(p);
  141. }
  142. }
  143. static int grow_buffers(struct stripe_head *sh, int num)
  144. {
  145. int i;
  146. for (i=0; i<num; i++) {
  147. struct page *page;
  148. if (!(page = alloc_page(GFP_KERNEL))) {
  149. return 1;
  150. }
  151. sh->dev[i].page = page;
  152. }
  153. return 0;
  154. }
  155. static void raid5_build_block (struct stripe_head *sh, int i);
  156. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  157. {
  158. raid5_conf_t *conf = sh->raid_conf;
  159. int i;
  160. if (atomic_read(&sh->count) != 0)
  161. BUG();
  162. if (test_bit(STRIPE_HANDLE, &sh->state))
  163. BUG();
  164. CHECK_DEVLOCK();
  165. PRINTK("init_stripe called, stripe %llu\n",
  166. (unsigned long long)sh->sector);
  167. remove_hash(sh);
  168. sh->sector = sector;
  169. sh->pd_idx = pd_idx;
  170. sh->state = 0;
  171. sh->disks = disks;
  172. for (i = sh->disks; i--; ) {
  173. struct r5dev *dev = &sh->dev[i];
  174. if (dev->toread || dev->towrite || dev->written ||
  175. test_bit(R5_LOCKED, &dev->flags)) {
  176. printk("sector=%llx i=%d %p %p %p %d\n",
  177. (unsigned long long)sh->sector, i, dev->toread,
  178. dev->towrite, dev->written,
  179. test_bit(R5_LOCKED, &dev->flags));
  180. BUG();
  181. }
  182. dev->flags = 0;
  183. raid5_build_block(sh, i);
  184. }
  185. insert_hash(conf, sh);
  186. }
  187. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  188. {
  189. struct stripe_head *sh;
  190. struct hlist_node *hn;
  191. CHECK_DEVLOCK();
  192. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  193. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  194. if (sh->sector == sector && sh->disks == disks)
  195. return sh;
  196. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  197. return NULL;
  198. }
  199. static void unplug_slaves(mddev_t *mddev);
  200. static void raid5_unplug_device(request_queue_t *q);
  201. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  202. int pd_idx, int noblock)
  203. {
  204. struct stripe_head *sh;
  205. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  206. spin_lock_irq(&conf->device_lock);
  207. do {
  208. wait_event_lock_irq(conf->wait_for_stripe,
  209. conf->quiesce == 0,
  210. conf->device_lock, /* nothing */);
  211. sh = __find_stripe(conf, sector, disks);
  212. if (!sh) {
  213. if (!conf->inactive_blocked)
  214. sh = get_free_stripe(conf);
  215. if (noblock && sh == NULL)
  216. break;
  217. if (!sh) {
  218. conf->inactive_blocked = 1;
  219. wait_event_lock_irq(conf->wait_for_stripe,
  220. !list_empty(&conf->inactive_list) &&
  221. (atomic_read(&conf->active_stripes)
  222. < (conf->max_nr_stripes *3/4)
  223. || !conf->inactive_blocked),
  224. conf->device_lock,
  225. unplug_slaves(conf->mddev);
  226. );
  227. conf->inactive_blocked = 0;
  228. } else
  229. init_stripe(sh, sector, pd_idx, disks);
  230. } else {
  231. if (atomic_read(&sh->count)) {
  232. if (!list_empty(&sh->lru))
  233. BUG();
  234. } else {
  235. if (!test_bit(STRIPE_HANDLE, &sh->state))
  236. atomic_inc(&conf->active_stripes);
  237. if (!list_empty(&sh->lru))
  238. list_del_init(&sh->lru);
  239. }
  240. }
  241. } while (sh == NULL);
  242. if (sh)
  243. atomic_inc(&sh->count);
  244. spin_unlock_irq(&conf->device_lock);
  245. return sh;
  246. }
  247. static int grow_one_stripe(raid5_conf_t *conf)
  248. {
  249. struct stripe_head *sh;
  250. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  251. if (!sh)
  252. return 0;
  253. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  254. sh->raid_conf = conf;
  255. spin_lock_init(&sh->lock);
  256. if (grow_buffers(sh, conf->raid_disks)) {
  257. shrink_buffers(sh, conf->raid_disks);
  258. kmem_cache_free(conf->slab_cache, sh);
  259. return 0;
  260. }
  261. sh->disks = conf->raid_disks;
  262. /* we just created an active stripe so... */
  263. atomic_set(&sh->count, 1);
  264. atomic_inc(&conf->active_stripes);
  265. INIT_LIST_HEAD(&sh->lru);
  266. release_stripe(sh);
  267. return 1;
  268. }
  269. static int grow_stripes(raid5_conf_t *conf, int num)
  270. {
  271. kmem_cache_t *sc;
  272. int devs = conf->raid_disks;
  273. sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
  274. sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
  275. conf->active_name = 0;
  276. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  277. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  278. 0, 0, NULL, NULL);
  279. if (!sc)
  280. return 1;
  281. conf->slab_cache = sc;
  282. conf->pool_size = devs;
  283. while (num--) {
  284. if (!grow_one_stripe(conf))
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. #ifdef CONFIG_MD_RAID5_RESHAPE
  290. static int resize_stripes(raid5_conf_t *conf, int newsize)
  291. {
  292. /* Make all the stripes able to hold 'newsize' devices.
  293. * New slots in each stripe get 'page' set to a new page.
  294. *
  295. * This happens in stages:
  296. * 1/ create a new kmem_cache and allocate the required number of
  297. * stripe_heads.
  298. * 2/ gather all the old stripe_heads and tranfer the pages across
  299. * to the new stripe_heads. This will have the side effect of
  300. * freezing the array as once all stripe_heads have been collected,
  301. * no IO will be possible. Old stripe heads are freed once their
  302. * pages have been transferred over, and the old kmem_cache is
  303. * freed when all stripes are done.
  304. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  305. * we simple return a failre status - no need to clean anything up.
  306. * 4/ allocate new pages for the new slots in the new stripe_heads.
  307. * If this fails, we don't bother trying the shrink the
  308. * stripe_heads down again, we just leave them as they are.
  309. * As each stripe_head is processed the new one is released into
  310. * active service.
  311. *
  312. * Once step2 is started, we cannot afford to wait for a write,
  313. * so we use GFP_NOIO allocations.
  314. */
  315. struct stripe_head *osh, *nsh;
  316. LIST_HEAD(newstripes);
  317. struct disk_info *ndisks;
  318. int err = 0;
  319. kmem_cache_t *sc;
  320. int i;
  321. if (newsize <= conf->pool_size)
  322. return 0; /* never bother to shrink */
  323. /* Step 1 */
  324. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  325. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  326. 0, 0, NULL, NULL);
  327. if (!sc)
  328. return -ENOMEM;
  329. for (i = conf->max_nr_stripes; i; i--) {
  330. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  331. if (!nsh)
  332. break;
  333. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  334. nsh->raid_conf = conf;
  335. spin_lock_init(&nsh->lock);
  336. list_add(&nsh->lru, &newstripes);
  337. }
  338. if (i) {
  339. /* didn't get enough, give up */
  340. while (!list_empty(&newstripes)) {
  341. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  342. list_del(&nsh->lru);
  343. kmem_cache_free(sc, nsh);
  344. }
  345. kmem_cache_destroy(sc);
  346. return -ENOMEM;
  347. }
  348. /* Step 2 - Must use GFP_NOIO now.
  349. * OK, we have enough stripes, start collecting inactive
  350. * stripes and copying them over
  351. */
  352. list_for_each_entry(nsh, &newstripes, lru) {
  353. spin_lock_irq(&conf->device_lock);
  354. wait_event_lock_irq(conf->wait_for_stripe,
  355. !list_empty(&conf->inactive_list),
  356. conf->device_lock,
  357. unplug_slaves(conf->mddev);
  358. );
  359. osh = get_free_stripe(conf);
  360. spin_unlock_irq(&conf->device_lock);
  361. atomic_set(&nsh->count, 1);
  362. for(i=0; i<conf->pool_size; i++)
  363. nsh->dev[i].page = osh->dev[i].page;
  364. for( ; i<newsize; i++)
  365. nsh->dev[i].page = NULL;
  366. kmem_cache_free(conf->slab_cache, osh);
  367. }
  368. kmem_cache_destroy(conf->slab_cache);
  369. /* Step 3.
  370. * At this point, we are holding all the stripes so the array
  371. * is completely stalled, so now is a good time to resize
  372. * conf->disks.
  373. */
  374. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  375. if (ndisks) {
  376. for (i=0; i<conf->raid_disks; i++)
  377. ndisks[i] = conf->disks[i];
  378. kfree(conf->disks);
  379. conf->disks = ndisks;
  380. } else
  381. err = -ENOMEM;
  382. /* Step 4, return new stripes to service */
  383. while(!list_empty(&newstripes)) {
  384. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  385. list_del_init(&nsh->lru);
  386. for (i=conf->raid_disks; i < newsize; i++)
  387. if (nsh->dev[i].page == NULL) {
  388. struct page *p = alloc_page(GFP_NOIO);
  389. nsh->dev[i].page = p;
  390. if (!p)
  391. err = -ENOMEM;
  392. }
  393. release_stripe(nsh);
  394. }
  395. /* critical section pass, GFP_NOIO no longer needed */
  396. conf->slab_cache = sc;
  397. conf->active_name = 1-conf->active_name;
  398. conf->pool_size = newsize;
  399. return err;
  400. }
  401. #endif
  402. static int drop_one_stripe(raid5_conf_t *conf)
  403. {
  404. struct stripe_head *sh;
  405. spin_lock_irq(&conf->device_lock);
  406. sh = get_free_stripe(conf);
  407. spin_unlock_irq(&conf->device_lock);
  408. if (!sh)
  409. return 0;
  410. if (atomic_read(&sh->count))
  411. BUG();
  412. shrink_buffers(sh, conf->pool_size);
  413. kmem_cache_free(conf->slab_cache, sh);
  414. atomic_dec(&conf->active_stripes);
  415. return 1;
  416. }
  417. static void shrink_stripes(raid5_conf_t *conf)
  418. {
  419. while (drop_one_stripe(conf))
  420. ;
  421. if (conf->slab_cache)
  422. kmem_cache_destroy(conf->slab_cache);
  423. conf->slab_cache = NULL;
  424. }
  425. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  426. int error)
  427. {
  428. struct stripe_head *sh = bi->bi_private;
  429. raid5_conf_t *conf = sh->raid_conf;
  430. int disks = sh->disks, i;
  431. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  432. if (bi->bi_size)
  433. return 1;
  434. for (i=0 ; i<disks; i++)
  435. if (bi == &sh->dev[i].req)
  436. break;
  437. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  438. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  439. uptodate);
  440. if (i == disks) {
  441. BUG();
  442. return 0;
  443. }
  444. if (uptodate) {
  445. #if 0
  446. struct bio *bio;
  447. unsigned long flags;
  448. spin_lock_irqsave(&conf->device_lock, flags);
  449. /* we can return a buffer if we bypassed the cache or
  450. * if the top buffer is not in highmem. If there are
  451. * multiple buffers, leave the extra work to
  452. * handle_stripe
  453. */
  454. buffer = sh->bh_read[i];
  455. if (buffer &&
  456. (!PageHighMem(buffer->b_page)
  457. || buffer->b_page == bh->b_page )
  458. ) {
  459. sh->bh_read[i] = buffer->b_reqnext;
  460. buffer->b_reqnext = NULL;
  461. } else
  462. buffer = NULL;
  463. spin_unlock_irqrestore(&conf->device_lock, flags);
  464. if (sh->bh_page[i]==bh->b_page)
  465. set_buffer_uptodate(bh);
  466. if (buffer) {
  467. if (buffer->b_page != bh->b_page)
  468. memcpy(buffer->b_data, bh->b_data, bh->b_size);
  469. buffer->b_end_io(buffer, 1);
  470. }
  471. #else
  472. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  473. #endif
  474. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  475. printk(KERN_INFO "raid5: read error corrected!!\n");
  476. clear_bit(R5_ReadError, &sh->dev[i].flags);
  477. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  478. }
  479. if (atomic_read(&conf->disks[i].rdev->read_errors))
  480. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  481. } else {
  482. int retry = 0;
  483. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  484. atomic_inc(&conf->disks[i].rdev->read_errors);
  485. if (conf->mddev->degraded)
  486. printk(KERN_WARNING "raid5: read error not correctable.\n");
  487. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  488. /* Oh, no!!! */
  489. printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
  490. else if (atomic_read(&conf->disks[i].rdev->read_errors)
  491. > conf->max_nr_stripes)
  492. printk(KERN_WARNING
  493. "raid5: Too many read errors, failing device.\n");
  494. else
  495. retry = 1;
  496. if (retry)
  497. set_bit(R5_ReadError, &sh->dev[i].flags);
  498. else {
  499. clear_bit(R5_ReadError, &sh->dev[i].flags);
  500. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  501. md_error(conf->mddev, conf->disks[i].rdev);
  502. }
  503. }
  504. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  505. #if 0
  506. /* must restore b_page before unlocking buffer... */
  507. if (sh->bh_page[i] != bh->b_page) {
  508. bh->b_page = sh->bh_page[i];
  509. bh->b_data = page_address(bh->b_page);
  510. clear_buffer_uptodate(bh);
  511. }
  512. #endif
  513. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  514. set_bit(STRIPE_HANDLE, &sh->state);
  515. release_stripe(sh);
  516. return 0;
  517. }
  518. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  519. int error)
  520. {
  521. struct stripe_head *sh = bi->bi_private;
  522. raid5_conf_t *conf = sh->raid_conf;
  523. int disks = sh->disks, i;
  524. unsigned long flags;
  525. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  526. if (bi->bi_size)
  527. return 1;
  528. for (i=0 ; i<disks; i++)
  529. if (bi == &sh->dev[i].req)
  530. break;
  531. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  532. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  533. uptodate);
  534. if (i == disks) {
  535. BUG();
  536. return 0;
  537. }
  538. spin_lock_irqsave(&conf->device_lock, flags);
  539. if (!uptodate)
  540. md_error(conf->mddev, conf->disks[i].rdev);
  541. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  542. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  543. set_bit(STRIPE_HANDLE, &sh->state);
  544. __release_stripe(conf, sh);
  545. spin_unlock_irqrestore(&conf->device_lock, flags);
  546. return 0;
  547. }
  548. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  549. static void raid5_build_block (struct stripe_head *sh, int i)
  550. {
  551. struct r5dev *dev = &sh->dev[i];
  552. bio_init(&dev->req);
  553. dev->req.bi_io_vec = &dev->vec;
  554. dev->req.bi_vcnt++;
  555. dev->req.bi_max_vecs++;
  556. dev->vec.bv_page = dev->page;
  557. dev->vec.bv_len = STRIPE_SIZE;
  558. dev->vec.bv_offset = 0;
  559. dev->req.bi_sector = sh->sector;
  560. dev->req.bi_private = sh;
  561. dev->flags = 0;
  562. if (i != sh->pd_idx)
  563. dev->sector = compute_blocknr(sh, i);
  564. }
  565. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  566. {
  567. char b[BDEVNAME_SIZE];
  568. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  569. PRINTK("raid5: error called\n");
  570. if (!test_bit(Faulty, &rdev->flags)) {
  571. mddev->sb_dirty = 1;
  572. if (test_bit(In_sync, &rdev->flags)) {
  573. conf->working_disks--;
  574. mddev->degraded++;
  575. conf->failed_disks++;
  576. clear_bit(In_sync, &rdev->flags);
  577. /*
  578. * if recovery was running, make sure it aborts.
  579. */
  580. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  581. }
  582. set_bit(Faulty, &rdev->flags);
  583. printk (KERN_ALERT
  584. "raid5: Disk failure on %s, disabling device."
  585. " Operation continuing on %d devices\n",
  586. bdevname(rdev->bdev,b), conf->working_disks);
  587. }
  588. }
  589. /*
  590. * Input: a 'big' sector number,
  591. * Output: index of the data and parity disk, and the sector # in them.
  592. */
  593. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  594. unsigned int data_disks, unsigned int * dd_idx,
  595. unsigned int * pd_idx, raid5_conf_t *conf)
  596. {
  597. long stripe;
  598. unsigned long chunk_number;
  599. unsigned int chunk_offset;
  600. sector_t new_sector;
  601. int sectors_per_chunk = conf->chunk_size >> 9;
  602. /* First compute the information on this sector */
  603. /*
  604. * Compute the chunk number and the sector offset inside the chunk
  605. */
  606. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  607. chunk_number = r_sector;
  608. BUG_ON(r_sector != chunk_number);
  609. /*
  610. * Compute the stripe number
  611. */
  612. stripe = chunk_number / data_disks;
  613. /*
  614. * Compute the data disk and parity disk indexes inside the stripe
  615. */
  616. *dd_idx = chunk_number % data_disks;
  617. /*
  618. * Select the parity disk based on the user selected algorithm.
  619. */
  620. if (conf->level == 4)
  621. *pd_idx = data_disks;
  622. else switch (conf->algorithm) {
  623. case ALGORITHM_LEFT_ASYMMETRIC:
  624. *pd_idx = data_disks - stripe % raid_disks;
  625. if (*dd_idx >= *pd_idx)
  626. (*dd_idx)++;
  627. break;
  628. case ALGORITHM_RIGHT_ASYMMETRIC:
  629. *pd_idx = stripe % raid_disks;
  630. if (*dd_idx >= *pd_idx)
  631. (*dd_idx)++;
  632. break;
  633. case ALGORITHM_LEFT_SYMMETRIC:
  634. *pd_idx = data_disks - stripe % raid_disks;
  635. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  636. break;
  637. case ALGORITHM_RIGHT_SYMMETRIC:
  638. *pd_idx = stripe % raid_disks;
  639. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  640. break;
  641. default:
  642. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  643. conf->algorithm);
  644. }
  645. /*
  646. * Finally, compute the new sector number
  647. */
  648. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  649. return new_sector;
  650. }
  651. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  652. {
  653. raid5_conf_t *conf = sh->raid_conf;
  654. int raid_disks = sh->disks, data_disks = raid_disks - 1;
  655. sector_t new_sector = sh->sector, check;
  656. int sectors_per_chunk = conf->chunk_size >> 9;
  657. sector_t stripe;
  658. int chunk_offset;
  659. int chunk_number, dummy1, dummy2, dd_idx = i;
  660. sector_t r_sector;
  661. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  662. stripe = new_sector;
  663. BUG_ON(new_sector != stripe);
  664. switch (conf->algorithm) {
  665. case ALGORITHM_LEFT_ASYMMETRIC:
  666. case ALGORITHM_RIGHT_ASYMMETRIC:
  667. if (i > sh->pd_idx)
  668. i--;
  669. break;
  670. case ALGORITHM_LEFT_SYMMETRIC:
  671. case ALGORITHM_RIGHT_SYMMETRIC:
  672. if (i < sh->pd_idx)
  673. i += raid_disks;
  674. i -= (sh->pd_idx + 1);
  675. break;
  676. default:
  677. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  678. conf->algorithm);
  679. }
  680. chunk_number = stripe * data_disks + i;
  681. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  682. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  683. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  684. printk(KERN_ERR "compute_blocknr: map not correct\n");
  685. return 0;
  686. }
  687. return r_sector;
  688. }
  689. /*
  690. * Copy data between a page in the stripe cache, and a bio.
  691. * There are no alignment or size guarantees between the page or the
  692. * bio except that there is some overlap.
  693. * All iovecs in the bio must be considered.
  694. */
  695. static void copy_data(int frombio, struct bio *bio,
  696. struct page *page,
  697. sector_t sector)
  698. {
  699. char *pa = page_address(page);
  700. struct bio_vec *bvl;
  701. int i;
  702. int page_offset;
  703. if (bio->bi_sector >= sector)
  704. page_offset = (signed)(bio->bi_sector - sector) * 512;
  705. else
  706. page_offset = (signed)(sector - bio->bi_sector) * -512;
  707. bio_for_each_segment(bvl, bio, i) {
  708. int len = bio_iovec_idx(bio,i)->bv_len;
  709. int clen;
  710. int b_offset = 0;
  711. if (page_offset < 0) {
  712. b_offset = -page_offset;
  713. page_offset += b_offset;
  714. len -= b_offset;
  715. }
  716. if (len > 0 && page_offset + len > STRIPE_SIZE)
  717. clen = STRIPE_SIZE - page_offset;
  718. else clen = len;
  719. if (clen > 0) {
  720. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  721. if (frombio)
  722. memcpy(pa+page_offset, ba+b_offset, clen);
  723. else
  724. memcpy(ba+b_offset, pa+page_offset, clen);
  725. __bio_kunmap_atomic(ba, KM_USER0);
  726. }
  727. if (clen < len) /* hit end of page */
  728. break;
  729. page_offset += len;
  730. }
  731. }
  732. #define check_xor() do { \
  733. if (count == MAX_XOR_BLOCKS) { \
  734. xor_block(count, STRIPE_SIZE, ptr); \
  735. count = 1; \
  736. } \
  737. } while(0)
  738. static void compute_block(struct stripe_head *sh, int dd_idx)
  739. {
  740. int i, count, disks = sh->disks;
  741. void *ptr[MAX_XOR_BLOCKS], *p;
  742. PRINTK("compute_block, stripe %llu, idx %d\n",
  743. (unsigned long long)sh->sector, dd_idx);
  744. ptr[0] = page_address(sh->dev[dd_idx].page);
  745. memset(ptr[0], 0, STRIPE_SIZE);
  746. count = 1;
  747. for (i = disks ; i--; ) {
  748. if (i == dd_idx)
  749. continue;
  750. p = page_address(sh->dev[i].page);
  751. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  752. ptr[count++] = p;
  753. else
  754. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  755. " not present\n", dd_idx,
  756. (unsigned long long)sh->sector, i);
  757. check_xor();
  758. }
  759. if (count != 1)
  760. xor_block(count, STRIPE_SIZE, ptr);
  761. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  762. }
  763. static void compute_parity(struct stripe_head *sh, int method)
  764. {
  765. raid5_conf_t *conf = sh->raid_conf;
  766. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  767. void *ptr[MAX_XOR_BLOCKS];
  768. struct bio *chosen;
  769. PRINTK("compute_parity, stripe %llu, method %d\n",
  770. (unsigned long long)sh->sector, method);
  771. count = 1;
  772. ptr[0] = page_address(sh->dev[pd_idx].page);
  773. switch(method) {
  774. case READ_MODIFY_WRITE:
  775. if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
  776. BUG();
  777. for (i=disks ; i-- ;) {
  778. if (i==pd_idx)
  779. continue;
  780. if (sh->dev[i].towrite &&
  781. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  782. ptr[count++] = page_address(sh->dev[i].page);
  783. chosen = sh->dev[i].towrite;
  784. sh->dev[i].towrite = NULL;
  785. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  786. wake_up(&conf->wait_for_overlap);
  787. if (sh->dev[i].written) BUG();
  788. sh->dev[i].written = chosen;
  789. check_xor();
  790. }
  791. }
  792. break;
  793. case RECONSTRUCT_WRITE:
  794. memset(ptr[0], 0, STRIPE_SIZE);
  795. for (i= disks; i-- ;)
  796. if (i!=pd_idx && sh->dev[i].towrite) {
  797. chosen = sh->dev[i].towrite;
  798. sh->dev[i].towrite = NULL;
  799. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  800. wake_up(&conf->wait_for_overlap);
  801. if (sh->dev[i].written) BUG();
  802. sh->dev[i].written = chosen;
  803. }
  804. break;
  805. case CHECK_PARITY:
  806. break;
  807. }
  808. if (count>1) {
  809. xor_block(count, STRIPE_SIZE, ptr);
  810. count = 1;
  811. }
  812. for (i = disks; i--;)
  813. if (sh->dev[i].written) {
  814. sector_t sector = sh->dev[i].sector;
  815. struct bio *wbi = sh->dev[i].written;
  816. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  817. copy_data(1, wbi, sh->dev[i].page, sector);
  818. wbi = r5_next_bio(wbi, sector);
  819. }
  820. set_bit(R5_LOCKED, &sh->dev[i].flags);
  821. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  822. }
  823. switch(method) {
  824. case RECONSTRUCT_WRITE:
  825. case CHECK_PARITY:
  826. for (i=disks; i--;)
  827. if (i != pd_idx) {
  828. ptr[count++] = page_address(sh->dev[i].page);
  829. check_xor();
  830. }
  831. break;
  832. case READ_MODIFY_WRITE:
  833. for (i = disks; i--;)
  834. if (sh->dev[i].written) {
  835. ptr[count++] = page_address(sh->dev[i].page);
  836. check_xor();
  837. }
  838. }
  839. if (count != 1)
  840. xor_block(count, STRIPE_SIZE, ptr);
  841. if (method != CHECK_PARITY) {
  842. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  843. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  844. } else
  845. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  846. }
  847. /*
  848. * Each stripe/dev can have one or more bion attached.
  849. * toread/towrite point to the first in a chain.
  850. * The bi_next chain must be in order.
  851. */
  852. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  853. {
  854. struct bio **bip;
  855. raid5_conf_t *conf = sh->raid_conf;
  856. int firstwrite=0;
  857. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  858. (unsigned long long)bi->bi_sector,
  859. (unsigned long long)sh->sector);
  860. spin_lock(&sh->lock);
  861. spin_lock_irq(&conf->device_lock);
  862. if (forwrite) {
  863. bip = &sh->dev[dd_idx].towrite;
  864. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  865. firstwrite = 1;
  866. } else
  867. bip = &sh->dev[dd_idx].toread;
  868. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  869. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  870. goto overlap;
  871. bip = & (*bip)->bi_next;
  872. }
  873. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  874. goto overlap;
  875. if (*bip && bi->bi_next && (*bip) != bi->bi_next)
  876. BUG();
  877. if (*bip)
  878. bi->bi_next = *bip;
  879. *bip = bi;
  880. bi->bi_phys_segments ++;
  881. spin_unlock_irq(&conf->device_lock);
  882. spin_unlock(&sh->lock);
  883. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  884. (unsigned long long)bi->bi_sector,
  885. (unsigned long long)sh->sector, dd_idx);
  886. if (conf->mddev->bitmap && firstwrite) {
  887. sh->bm_seq = conf->seq_write;
  888. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  889. STRIPE_SECTORS, 0);
  890. set_bit(STRIPE_BIT_DELAY, &sh->state);
  891. }
  892. if (forwrite) {
  893. /* check if page is covered */
  894. sector_t sector = sh->dev[dd_idx].sector;
  895. for (bi=sh->dev[dd_idx].towrite;
  896. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  897. bi && bi->bi_sector <= sector;
  898. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  899. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  900. sector = bi->bi_sector + (bi->bi_size>>9);
  901. }
  902. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  903. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  904. }
  905. return 1;
  906. overlap:
  907. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  908. spin_unlock_irq(&conf->device_lock);
  909. spin_unlock(&sh->lock);
  910. return 0;
  911. }
  912. static void end_reshape(raid5_conf_t *conf);
  913. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  914. {
  915. int sectors_per_chunk = conf->chunk_size >> 9;
  916. sector_t x = stripe;
  917. int pd_idx, dd_idx;
  918. int chunk_offset = sector_div(x, sectors_per_chunk);
  919. stripe = x;
  920. raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
  921. + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
  922. return pd_idx;
  923. }
  924. /*
  925. * handle_stripe - do things to a stripe.
  926. *
  927. * We lock the stripe and then examine the state of various bits
  928. * to see what needs to be done.
  929. * Possible results:
  930. * return some read request which now have data
  931. * return some write requests which are safely on disc
  932. * schedule a read on some buffers
  933. * schedule a write of some buffers
  934. * return confirmation of parity correctness
  935. *
  936. * Parity calculations are done inside the stripe lock
  937. * buffers are taken off read_list or write_list, and bh_cache buffers
  938. * get BH_Lock set before the stripe lock is released.
  939. *
  940. */
  941. static void handle_stripe(struct stripe_head *sh)
  942. {
  943. raid5_conf_t *conf = sh->raid_conf;
  944. int disks = sh->disks;
  945. struct bio *return_bi= NULL;
  946. struct bio *bi;
  947. int i;
  948. int syncing, expanding, expanded;
  949. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  950. int non_overwrite = 0;
  951. int failed_num=0;
  952. struct r5dev *dev;
  953. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  954. (unsigned long long)sh->sector, atomic_read(&sh->count),
  955. sh->pd_idx);
  956. spin_lock(&sh->lock);
  957. clear_bit(STRIPE_HANDLE, &sh->state);
  958. clear_bit(STRIPE_DELAYED, &sh->state);
  959. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  960. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  961. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  962. /* Now to look around and see what can be done */
  963. rcu_read_lock();
  964. for (i=disks; i--; ) {
  965. mdk_rdev_t *rdev;
  966. dev = &sh->dev[i];
  967. clear_bit(R5_Insync, &dev->flags);
  968. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  969. i, dev->flags, dev->toread, dev->towrite, dev->written);
  970. /* maybe we can reply to a read */
  971. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  972. struct bio *rbi, *rbi2;
  973. PRINTK("Return read for disc %d\n", i);
  974. spin_lock_irq(&conf->device_lock);
  975. rbi = dev->toread;
  976. dev->toread = NULL;
  977. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  978. wake_up(&conf->wait_for_overlap);
  979. spin_unlock_irq(&conf->device_lock);
  980. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  981. copy_data(0, rbi, dev->page, dev->sector);
  982. rbi2 = r5_next_bio(rbi, dev->sector);
  983. spin_lock_irq(&conf->device_lock);
  984. if (--rbi->bi_phys_segments == 0) {
  985. rbi->bi_next = return_bi;
  986. return_bi = rbi;
  987. }
  988. spin_unlock_irq(&conf->device_lock);
  989. rbi = rbi2;
  990. }
  991. }
  992. /* now count some things */
  993. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  994. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  995. if (dev->toread) to_read++;
  996. if (dev->towrite) {
  997. to_write++;
  998. if (!test_bit(R5_OVERWRITE, &dev->flags))
  999. non_overwrite++;
  1000. }
  1001. if (dev->written) written++;
  1002. rdev = rcu_dereference(conf->disks[i].rdev);
  1003. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1004. /* The ReadError flag will just be confusing now */
  1005. clear_bit(R5_ReadError, &dev->flags);
  1006. clear_bit(R5_ReWrite, &dev->flags);
  1007. }
  1008. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1009. || test_bit(R5_ReadError, &dev->flags)) {
  1010. failed++;
  1011. failed_num = i;
  1012. } else
  1013. set_bit(R5_Insync, &dev->flags);
  1014. }
  1015. rcu_read_unlock();
  1016. PRINTK("locked=%d uptodate=%d to_read=%d"
  1017. " to_write=%d failed=%d failed_num=%d\n",
  1018. locked, uptodate, to_read, to_write, failed, failed_num);
  1019. /* check if the array has lost two devices and, if so, some requests might
  1020. * need to be failed
  1021. */
  1022. if (failed > 1 && to_read+to_write+written) {
  1023. for (i=disks; i--; ) {
  1024. int bitmap_end = 0;
  1025. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1026. mdk_rdev_t *rdev;
  1027. rcu_read_lock();
  1028. rdev = rcu_dereference(conf->disks[i].rdev);
  1029. if (rdev && test_bit(In_sync, &rdev->flags))
  1030. /* multiple read failures in one stripe */
  1031. md_error(conf->mddev, rdev);
  1032. rcu_read_unlock();
  1033. }
  1034. spin_lock_irq(&conf->device_lock);
  1035. /* fail all writes first */
  1036. bi = sh->dev[i].towrite;
  1037. sh->dev[i].towrite = NULL;
  1038. if (bi) { to_write--; bitmap_end = 1; }
  1039. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1040. wake_up(&conf->wait_for_overlap);
  1041. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1042. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1043. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1044. if (--bi->bi_phys_segments == 0) {
  1045. md_write_end(conf->mddev);
  1046. bi->bi_next = return_bi;
  1047. return_bi = bi;
  1048. }
  1049. bi = nextbi;
  1050. }
  1051. /* and fail all 'written' */
  1052. bi = sh->dev[i].written;
  1053. sh->dev[i].written = NULL;
  1054. if (bi) bitmap_end = 1;
  1055. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1056. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1057. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1058. if (--bi->bi_phys_segments == 0) {
  1059. md_write_end(conf->mddev);
  1060. bi->bi_next = return_bi;
  1061. return_bi = bi;
  1062. }
  1063. bi = bi2;
  1064. }
  1065. /* fail any reads if this device is non-operational */
  1066. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1067. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1068. bi = sh->dev[i].toread;
  1069. sh->dev[i].toread = NULL;
  1070. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1071. wake_up(&conf->wait_for_overlap);
  1072. if (bi) to_read--;
  1073. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1074. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1075. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1076. if (--bi->bi_phys_segments == 0) {
  1077. bi->bi_next = return_bi;
  1078. return_bi = bi;
  1079. }
  1080. bi = nextbi;
  1081. }
  1082. }
  1083. spin_unlock_irq(&conf->device_lock);
  1084. if (bitmap_end)
  1085. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1086. STRIPE_SECTORS, 0, 0);
  1087. }
  1088. }
  1089. if (failed > 1 && syncing) {
  1090. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1091. clear_bit(STRIPE_SYNCING, &sh->state);
  1092. syncing = 0;
  1093. }
  1094. /* might be able to return some write requests if the parity block
  1095. * is safe, or on a failed drive
  1096. */
  1097. dev = &sh->dev[sh->pd_idx];
  1098. if ( written &&
  1099. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  1100. test_bit(R5_UPTODATE, &dev->flags))
  1101. || (failed == 1 && failed_num == sh->pd_idx))
  1102. ) {
  1103. /* any written block on an uptodate or failed drive can be returned.
  1104. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1105. * never LOCKED, so we don't need to test 'failed' directly.
  1106. */
  1107. for (i=disks; i--; )
  1108. if (sh->dev[i].written) {
  1109. dev = &sh->dev[i];
  1110. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1111. test_bit(R5_UPTODATE, &dev->flags) ) {
  1112. /* We can return any write requests */
  1113. struct bio *wbi, *wbi2;
  1114. int bitmap_end = 0;
  1115. PRINTK("Return write for disc %d\n", i);
  1116. spin_lock_irq(&conf->device_lock);
  1117. wbi = dev->written;
  1118. dev->written = NULL;
  1119. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1120. wbi2 = r5_next_bio(wbi, dev->sector);
  1121. if (--wbi->bi_phys_segments == 0) {
  1122. md_write_end(conf->mddev);
  1123. wbi->bi_next = return_bi;
  1124. return_bi = wbi;
  1125. }
  1126. wbi = wbi2;
  1127. }
  1128. if (dev->towrite == NULL)
  1129. bitmap_end = 1;
  1130. spin_unlock_irq(&conf->device_lock);
  1131. if (bitmap_end)
  1132. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1133. STRIPE_SECTORS,
  1134. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1135. }
  1136. }
  1137. }
  1138. /* Now we might consider reading some blocks, either to check/generate
  1139. * parity, or to satisfy requests
  1140. * or to load a block that is being partially written.
  1141. */
  1142. if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
  1143. for (i=disks; i--;) {
  1144. dev = &sh->dev[i];
  1145. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1146. (dev->toread ||
  1147. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1148. syncing ||
  1149. expanding ||
  1150. (failed && (sh->dev[failed_num].toread ||
  1151. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1152. )
  1153. ) {
  1154. /* we would like to get this block, possibly
  1155. * by computing it, but we might not be able to
  1156. */
  1157. if (uptodate == disks-1) {
  1158. PRINTK("Computing block %d\n", i);
  1159. compute_block(sh, i);
  1160. uptodate++;
  1161. } else if (test_bit(R5_Insync, &dev->flags)) {
  1162. set_bit(R5_LOCKED, &dev->flags);
  1163. set_bit(R5_Wantread, &dev->flags);
  1164. #if 0
  1165. /* if I am just reading this block and we don't have
  1166. a failed drive, or any pending writes then sidestep the cache */
  1167. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1168. ! syncing && !failed && !to_write) {
  1169. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1170. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1171. }
  1172. #endif
  1173. locked++;
  1174. PRINTK("Reading block %d (sync=%d)\n",
  1175. i, syncing);
  1176. }
  1177. }
  1178. }
  1179. set_bit(STRIPE_HANDLE, &sh->state);
  1180. }
  1181. /* now to consider writing and what else, if anything should be read */
  1182. if (to_write) {
  1183. int rmw=0, rcw=0;
  1184. for (i=disks ; i--;) {
  1185. /* would I have to read this buffer for read_modify_write */
  1186. dev = &sh->dev[i];
  1187. if ((dev->towrite || i == sh->pd_idx) &&
  1188. (!test_bit(R5_LOCKED, &dev->flags)
  1189. #if 0
  1190. || sh->bh_page[i]!=bh->b_page
  1191. #endif
  1192. ) &&
  1193. !test_bit(R5_UPTODATE, &dev->flags)) {
  1194. if (test_bit(R5_Insync, &dev->flags)
  1195. /* && !(!mddev->insync && i == sh->pd_idx) */
  1196. )
  1197. rmw++;
  1198. else rmw += 2*disks; /* cannot read it */
  1199. }
  1200. /* Would I have to read this buffer for reconstruct_write */
  1201. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1202. (!test_bit(R5_LOCKED, &dev->flags)
  1203. #if 0
  1204. || sh->bh_page[i] != bh->b_page
  1205. #endif
  1206. ) &&
  1207. !test_bit(R5_UPTODATE, &dev->flags)) {
  1208. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1209. else rcw += 2*disks;
  1210. }
  1211. }
  1212. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1213. (unsigned long long)sh->sector, rmw, rcw);
  1214. set_bit(STRIPE_HANDLE, &sh->state);
  1215. if (rmw < rcw && rmw > 0)
  1216. /* prefer read-modify-write, but need to get some data */
  1217. for (i=disks; i--;) {
  1218. dev = &sh->dev[i];
  1219. if ((dev->towrite || i == sh->pd_idx) &&
  1220. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1221. test_bit(R5_Insync, &dev->flags)) {
  1222. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1223. {
  1224. PRINTK("Read_old block %d for r-m-w\n", i);
  1225. set_bit(R5_LOCKED, &dev->flags);
  1226. set_bit(R5_Wantread, &dev->flags);
  1227. locked++;
  1228. } else {
  1229. set_bit(STRIPE_DELAYED, &sh->state);
  1230. set_bit(STRIPE_HANDLE, &sh->state);
  1231. }
  1232. }
  1233. }
  1234. if (rcw <= rmw && rcw > 0)
  1235. /* want reconstruct write, but need to get some data */
  1236. for (i=disks; i--;) {
  1237. dev = &sh->dev[i];
  1238. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1239. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1240. test_bit(R5_Insync, &dev->flags)) {
  1241. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1242. {
  1243. PRINTK("Read_old block %d for Reconstruct\n", i);
  1244. set_bit(R5_LOCKED, &dev->flags);
  1245. set_bit(R5_Wantread, &dev->flags);
  1246. locked++;
  1247. } else {
  1248. set_bit(STRIPE_DELAYED, &sh->state);
  1249. set_bit(STRIPE_HANDLE, &sh->state);
  1250. }
  1251. }
  1252. }
  1253. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1254. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1255. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1256. PRINTK("Computing parity...\n");
  1257. compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1258. /* now every locked buffer is ready to be written */
  1259. for (i=disks; i--;)
  1260. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1261. PRINTK("Writing block %d\n", i);
  1262. locked++;
  1263. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1264. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1265. || (i==sh->pd_idx && failed == 0))
  1266. set_bit(STRIPE_INSYNC, &sh->state);
  1267. }
  1268. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1269. atomic_dec(&conf->preread_active_stripes);
  1270. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1271. md_wakeup_thread(conf->mddev->thread);
  1272. }
  1273. }
  1274. }
  1275. /* maybe we need to check and possibly fix the parity for this stripe
  1276. * Any reads will already have been scheduled, so we just see if enough data
  1277. * is available
  1278. */
  1279. if (syncing && locked == 0 &&
  1280. !test_bit(STRIPE_INSYNC, &sh->state)) {
  1281. set_bit(STRIPE_HANDLE, &sh->state);
  1282. if (failed == 0) {
  1283. char *pagea;
  1284. if (uptodate != disks)
  1285. BUG();
  1286. compute_parity(sh, CHECK_PARITY);
  1287. uptodate--;
  1288. pagea = page_address(sh->dev[sh->pd_idx].page);
  1289. if ((*(u32*)pagea) == 0 &&
  1290. !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
  1291. /* parity is correct (on disc, not in buffer any more) */
  1292. set_bit(STRIPE_INSYNC, &sh->state);
  1293. } else {
  1294. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1295. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1296. /* don't try to repair!! */
  1297. set_bit(STRIPE_INSYNC, &sh->state);
  1298. else {
  1299. compute_block(sh, sh->pd_idx);
  1300. uptodate++;
  1301. }
  1302. }
  1303. }
  1304. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1305. /* either failed parity check, or recovery is happening */
  1306. if (failed==0)
  1307. failed_num = sh->pd_idx;
  1308. dev = &sh->dev[failed_num];
  1309. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1310. BUG_ON(uptodate != disks);
  1311. set_bit(R5_LOCKED, &dev->flags);
  1312. set_bit(R5_Wantwrite, &dev->flags);
  1313. clear_bit(STRIPE_DEGRADED, &sh->state);
  1314. locked++;
  1315. set_bit(STRIPE_INSYNC, &sh->state);
  1316. }
  1317. }
  1318. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1319. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1320. clear_bit(STRIPE_SYNCING, &sh->state);
  1321. }
  1322. /* If the failed drive is just a ReadError, then we might need to progress
  1323. * the repair/check process
  1324. */
  1325. if (failed == 1 && ! conf->mddev->ro &&
  1326. test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1327. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1328. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1329. ) {
  1330. dev = &sh->dev[failed_num];
  1331. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1332. set_bit(R5_Wantwrite, &dev->flags);
  1333. set_bit(R5_ReWrite, &dev->flags);
  1334. set_bit(R5_LOCKED, &dev->flags);
  1335. locked++;
  1336. } else {
  1337. /* let's read it back */
  1338. set_bit(R5_Wantread, &dev->flags);
  1339. set_bit(R5_LOCKED, &dev->flags);
  1340. locked++;
  1341. }
  1342. }
  1343. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1344. /* Need to write out all blocks after computing parity */
  1345. sh->disks = conf->raid_disks;
  1346. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1347. compute_parity(sh, RECONSTRUCT_WRITE);
  1348. for (i= conf->raid_disks; i--;) {
  1349. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1350. locked++;
  1351. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1352. }
  1353. clear_bit(STRIPE_EXPANDING, &sh->state);
  1354. } else if (expanded) {
  1355. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1356. atomic_dec(&conf->reshape_stripes);
  1357. wake_up(&conf->wait_for_overlap);
  1358. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1359. }
  1360. if (expanding && locked == 0) {
  1361. /* We have read all the blocks in this stripe and now we need to
  1362. * copy some of them into a target stripe for expand.
  1363. */
  1364. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1365. for (i=0; i< sh->disks; i++)
  1366. if (i != sh->pd_idx) {
  1367. int dd_idx, pd_idx, j;
  1368. struct stripe_head *sh2;
  1369. sector_t bn = compute_blocknr(sh, i);
  1370. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1371. conf->raid_disks-1,
  1372. &dd_idx, &pd_idx, conf);
  1373. sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
  1374. if (sh2 == NULL)
  1375. /* so far only the early blocks of this stripe
  1376. * have been requested. When later blocks
  1377. * get requested, we will try again
  1378. */
  1379. continue;
  1380. if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1381. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1382. /* must have already done this block */
  1383. release_stripe(sh2);
  1384. continue;
  1385. }
  1386. memcpy(page_address(sh2->dev[dd_idx].page),
  1387. page_address(sh->dev[i].page),
  1388. STRIPE_SIZE);
  1389. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1390. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1391. for (j=0; j<conf->raid_disks; j++)
  1392. if (j != sh2->pd_idx &&
  1393. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1394. break;
  1395. if (j == conf->raid_disks) {
  1396. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1397. set_bit(STRIPE_HANDLE, &sh2->state);
  1398. }
  1399. release_stripe(sh2);
  1400. }
  1401. }
  1402. spin_unlock(&sh->lock);
  1403. while ((bi=return_bi)) {
  1404. int bytes = bi->bi_size;
  1405. return_bi = bi->bi_next;
  1406. bi->bi_next = NULL;
  1407. bi->bi_size = 0;
  1408. bi->bi_end_io(bi, bytes, 0);
  1409. }
  1410. for (i=disks; i-- ;) {
  1411. int rw;
  1412. struct bio *bi;
  1413. mdk_rdev_t *rdev;
  1414. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1415. rw = 1;
  1416. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1417. rw = 0;
  1418. else
  1419. continue;
  1420. bi = &sh->dev[i].req;
  1421. bi->bi_rw = rw;
  1422. if (rw)
  1423. bi->bi_end_io = raid5_end_write_request;
  1424. else
  1425. bi->bi_end_io = raid5_end_read_request;
  1426. rcu_read_lock();
  1427. rdev = rcu_dereference(conf->disks[i].rdev);
  1428. if (rdev && test_bit(Faulty, &rdev->flags))
  1429. rdev = NULL;
  1430. if (rdev)
  1431. atomic_inc(&rdev->nr_pending);
  1432. rcu_read_unlock();
  1433. if (rdev) {
  1434. if (syncing || expanding || expanded)
  1435. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1436. bi->bi_bdev = rdev->bdev;
  1437. PRINTK("for %llu schedule op %ld on disc %d\n",
  1438. (unsigned long long)sh->sector, bi->bi_rw, i);
  1439. atomic_inc(&sh->count);
  1440. bi->bi_sector = sh->sector + rdev->data_offset;
  1441. bi->bi_flags = 1 << BIO_UPTODATE;
  1442. bi->bi_vcnt = 1;
  1443. bi->bi_max_vecs = 1;
  1444. bi->bi_idx = 0;
  1445. bi->bi_io_vec = &sh->dev[i].vec;
  1446. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1447. bi->bi_io_vec[0].bv_offset = 0;
  1448. bi->bi_size = STRIPE_SIZE;
  1449. bi->bi_next = NULL;
  1450. if (rw == WRITE &&
  1451. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1452. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1453. generic_make_request(bi);
  1454. } else {
  1455. if (rw == 1)
  1456. set_bit(STRIPE_DEGRADED, &sh->state);
  1457. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1458. bi->bi_rw, i, (unsigned long long)sh->sector);
  1459. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1460. set_bit(STRIPE_HANDLE, &sh->state);
  1461. }
  1462. }
  1463. }
  1464. static void raid5_activate_delayed(raid5_conf_t *conf)
  1465. {
  1466. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  1467. while (!list_empty(&conf->delayed_list)) {
  1468. struct list_head *l = conf->delayed_list.next;
  1469. struct stripe_head *sh;
  1470. sh = list_entry(l, struct stripe_head, lru);
  1471. list_del_init(l);
  1472. clear_bit(STRIPE_DELAYED, &sh->state);
  1473. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1474. atomic_inc(&conf->preread_active_stripes);
  1475. list_add_tail(&sh->lru, &conf->handle_list);
  1476. }
  1477. }
  1478. }
  1479. static void activate_bit_delay(raid5_conf_t *conf)
  1480. {
  1481. /* device_lock is held */
  1482. struct list_head head;
  1483. list_add(&head, &conf->bitmap_list);
  1484. list_del_init(&conf->bitmap_list);
  1485. while (!list_empty(&head)) {
  1486. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  1487. list_del_init(&sh->lru);
  1488. atomic_inc(&sh->count);
  1489. __release_stripe(conf, sh);
  1490. }
  1491. }
  1492. static void unplug_slaves(mddev_t *mddev)
  1493. {
  1494. raid5_conf_t *conf = mddev_to_conf(mddev);
  1495. int i;
  1496. rcu_read_lock();
  1497. for (i=0; i<mddev->raid_disks; i++) {
  1498. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  1499. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  1500. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  1501. atomic_inc(&rdev->nr_pending);
  1502. rcu_read_unlock();
  1503. if (r_queue->unplug_fn)
  1504. r_queue->unplug_fn(r_queue);
  1505. rdev_dec_pending(rdev, mddev);
  1506. rcu_read_lock();
  1507. }
  1508. }
  1509. rcu_read_unlock();
  1510. }
  1511. static void raid5_unplug_device(request_queue_t *q)
  1512. {
  1513. mddev_t *mddev = q->queuedata;
  1514. raid5_conf_t *conf = mddev_to_conf(mddev);
  1515. unsigned long flags;
  1516. spin_lock_irqsave(&conf->device_lock, flags);
  1517. if (blk_remove_plug(q)) {
  1518. conf->seq_flush++;
  1519. raid5_activate_delayed(conf);
  1520. }
  1521. md_wakeup_thread(mddev->thread);
  1522. spin_unlock_irqrestore(&conf->device_lock, flags);
  1523. unplug_slaves(mddev);
  1524. }
  1525. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  1526. sector_t *error_sector)
  1527. {
  1528. mddev_t *mddev = q->queuedata;
  1529. raid5_conf_t *conf = mddev_to_conf(mddev);
  1530. int i, ret = 0;
  1531. rcu_read_lock();
  1532. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  1533. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  1534. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  1535. struct block_device *bdev = rdev->bdev;
  1536. request_queue_t *r_queue = bdev_get_queue(bdev);
  1537. if (!r_queue->issue_flush_fn)
  1538. ret = -EOPNOTSUPP;
  1539. else {
  1540. atomic_inc(&rdev->nr_pending);
  1541. rcu_read_unlock();
  1542. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  1543. error_sector);
  1544. rdev_dec_pending(rdev, mddev);
  1545. rcu_read_lock();
  1546. }
  1547. }
  1548. }
  1549. rcu_read_unlock();
  1550. return ret;
  1551. }
  1552. static inline void raid5_plug_device(raid5_conf_t *conf)
  1553. {
  1554. spin_lock_irq(&conf->device_lock);
  1555. blk_plug_device(conf->mddev->queue);
  1556. spin_unlock_irq(&conf->device_lock);
  1557. }
  1558. static int make_request(request_queue_t *q, struct bio * bi)
  1559. {
  1560. mddev_t *mddev = q->queuedata;
  1561. raid5_conf_t *conf = mddev_to_conf(mddev);
  1562. unsigned int dd_idx, pd_idx;
  1563. sector_t new_sector;
  1564. sector_t logical_sector, last_sector;
  1565. struct stripe_head *sh;
  1566. const int rw = bio_data_dir(bi);
  1567. if (unlikely(bio_barrier(bi))) {
  1568. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  1569. return 0;
  1570. }
  1571. md_write_start(mddev, bi);
  1572. disk_stat_inc(mddev->gendisk, ios[rw]);
  1573. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  1574. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  1575. last_sector = bi->bi_sector + (bi->bi_size>>9);
  1576. bi->bi_next = NULL;
  1577. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  1578. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  1579. DEFINE_WAIT(w);
  1580. int disks;
  1581. retry:
  1582. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  1583. if (likely(conf->expand_progress == MaxSector))
  1584. disks = conf->raid_disks;
  1585. else {
  1586. /* spinlock is needed as expand_progress may be
  1587. * 64bit on a 32bit platform, and so it might be
  1588. * possible to see a half-updated value
  1589. * Ofcourse expand_progress could change after
  1590. * the lock is dropped, so once we get a reference
  1591. * to the stripe that we think it is, we will have
  1592. * to check again.
  1593. */
  1594. spin_lock_irq(&conf->device_lock);
  1595. disks = conf->raid_disks;
  1596. if (logical_sector >= conf->expand_progress)
  1597. disks = conf->previous_raid_disks;
  1598. else {
  1599. if (logical_sector >= conf->expand_lo) {
  1600. spin_unlock_irq(&conf->device_lock);
  1601. schedule();
  1602. goto retry;
  1603. }
  1604. }
  1605. spin_unlock_irq(&conf->device_lock);
  1606. }
  1607. new_sector = raid5_compute_sector(logical_sector, disks, disks - 1,
  1608. &dd_idx, &pd_idx, conf);
  1609. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  1610. (unsigned long long)new_sector,
  1611. (unsigned long long)logical_sector);
  1612. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  1613. if (sh) {
  1614. if (unlikely(conf->expand_progress != MaxSector)) {
  1615. /* expansion might have moved on while waiting for a
  1616. * stripe, so we must do the range check again.
  1617. * Expansion could still move past after this
  1618. * test, but as we are holding a reference to
  1619. * 'sh', we know that if that happens,
  1620. * STRIPE_EXPANDING will get set and the expansion
  1621. * won't proceed until we finish with the stripe.
  1622. */
  1623. int must_retry = 0;
  1624. spin_lock_irq(&conf->device_lock);
  1625. if (logical_sector < conf->expand_progress &&
  1626. disks == conf->previous_raid_disks)
  1627. /* mismatch, need to try again */
  1628. must_retry = 1;
  1629. spin_unlock_irq(&conf->device_lock);
  1630. if (must_retry) {
  1631. release_stripe(sh);
  1632. goto retry;
  1633. }
  1634. }
  1635. /* FIXME what if we get a false positive because these
  1636. * are being updated.
  1637. */
  1638. if (logical_sector >= mddev->suspend_lo &&
  1639. logical_sector < mddev->suspend_hi) {
  1640. release_stripe(sh);
  1641. schedule();
  1642. goto retry;
  1643. }
  1644. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  1645. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  1646. /* Stripe is busy expanding or
  1647. * add failed due to overlap. Flush everything
  1648. * and wait a while
  1649. */
  1650. raid5_unplug_device(mddev->queue);
  1651. release_stripe(sh);
  1652. schedule();
  1653. goto retry;
  1654. }
  1655. finish_wait(&conf->wait_for_overlap, &w);
  1656. raid5_plug_device(conf);
  1657. handle_stripe(sh);
  1658. release_stripe(sh);
  1659. } else {
  1660. /* cannot get stripe for read-ahead, just give-up */
  1661. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1662. finish_wait(&conf->wait_for_overlap, &w);
  1663. break;
  1664. }
  1665. }
  1666. spin_lock_irq(&conf->device_lock);
  1667. if (--bi->bi_phys_segments == 0) {
  1668. int bytes = bi->bi_size;
  1669. if ( bio_data_dir(bi) == WRITE )
  1670. md_write_end(mddev);
  1671. bi->bi_size = 0;
  1672. bi->bi_end_io(bi, bytes, 0);
  1673. }
  1674. spin_unlock_irq(&conf->device_lock);
  1675. return 0;
  1676. }
  1677. /* FIXME go_faster isn't used */
  1678. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1679. {
  1680. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1681. struct stripe_head *sh;
  1682. int pd_idx;
  1683. sector_t first_sector, last_sector;
  1684. int raid_disks = conf->raid_disks;
  1685. int data_disks = raid_disks-1;
  1686. sector_t max_sector = mddev->size << 1;
  1687. int sync_blocks;
  1688. if (sector_nr >= max_sector) {
  1689. /* just being told to finish up .. nothing much to do */
  1690. unplug_slaves(mddev);
  1691. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  1692. end_reshape(conf);
  1693. return 0;
  1694. }
  1695. if (mddev->curr_resync < max_sector) /* aborted */
  1696. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1697. &sync_blocks, 1);
  1698. else /* compelted sync */
  1699. conf->fullsync = 0;
  1700. bitmap_close_sync(mddev->bitmap);
  1701. return 0;
  1702. }
  1703. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  1704. /* reshaping is quite different to recovery/resync so it is
  1705. * handled quite separately ... here.
  1706. *
  1707. * On each call to sync_request, we gather one chunk worth of
  1708. * destination stripes and flag them as expanding.
  1709. * Then we find all the source stripes and request reads.
  1710. * As the reads complete, handle_stripe will copy the data
  1711. * into the destination stripe and release that stripe.
  1712. */
  1713. int i;
  1714. int dd_idx;
  1715. sector_t writepos, safepos, gap;
  1716. if (sector_nr == 0 &&
  1717. conf->expand_progress != 0) {
  1718. /* restarting in the middle, skip the initial sectors */
  1719. sector_nr = conf->expand_progress;
  1720. sector_div(sector_nr, conf->raid_disks-1);
  1721. *skipped = 1;
  1722. return sector_nr;
  1723. }
  1724. /* we update the metadata when there is more than 3Meg
  1725. * in the block range (that is rather arbitrary, should
  1726. * probably be time based) or when the data about to be
  1727. * copied would over-write the source of the data at
  1728. * the front of the range.
  1729. * i.e. one new_stripe forward from expand_progress new_maps
  1730. * to after where expand_lo old_maps to
  1731. */
  1732. writepos = conf->expand_progress +
  1733. conf->chunk_size/512*(conf->raid_disks-1);
  1734. sector_div(writepos, conf->raid_disks-1);
  1735. safepos = conf->expand_lo;
  1736. sector_div(safepos, conf->previous_raid_disks-1);
  1737. gap = conf->expand_progress - conf->expand_lo;
  1738. if (writepos >= safepos ||
  1739. gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
  1740. /* Cannot proceed until we've updated the superblock... */
  1741. wait_event(conf->wait_for_overlap,
  1742. atomic_read(&conf->reshape_stripes)==0);
  1743. mddev->reshape_position = conf->expand_progress;
  1744. mddev->sb_dirty = 1;
  1745. md_wakeup_thread(mddev->thread);
  1746. wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
  1747. kthread_should_stop());
  1748. spin_lock_irq(&conf->device_lock);
  1749. conf->expand_lo = mddev->reshape_position;
  1750. spin_unlock_irq(&conf->device_lock);
  1751. wake_up(&conf->wait_for_overlap);
  1752. }
  1753. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  1754. int j;
  1755. int skipped = 0;
  1756. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  1757. sh = get_active_stripe(conf, sector_nr+i,
  1758. conf->raid_disks, pd_idx, 0);
  1759. set_bit(STRIPE_EXPANDING, &sh->state);
  1760. atomic_inc(&conf->reshape_stripes);
  1761. /* If any of this stripe is beyond the end of the old
  1762. * array, then we need to zero those blocks
  1763. */
  1764. for (j=sh->disks; j--;) {
  1765. sector_t s;
  1766. if (j == sh->pd_idx)
  1767. continue;
  1768. s = compute_blocknr(sh, j);
  1769. if (s < (mddev->array_size<<1)) {
  1770. skipped = 1;
  1771. continue;
  1772. }
  1773. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  1774. set_bit(R5_Expanded, &sh->dev[j].flags);
  1775. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  1776. }
  1777. if (!skipped) {
  1778. set_bit(STRIPE_EXPAND_READY, &sh->state);
  1779. set_bit(STRIPE_HANDLE, &sh->state);
  1780. }
  1781. release_stripe(sh);
  1782. }
  1783. spin_lock_irq(&conf->device_lock);
  1784. conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
  1785. spin_unlock_irq(&conf->device_lock);
  1786. /* Ok, those stripe are ready. We can start scheduling
  1787. * reads on the source stripes.
  1788. * The source stripes are determined by mapping the first and last
  1789. * block on the destination stripes.
  1790. */
  1791. raid_disks = conf->previous_raid_disks;
  1792. data_disks = raid_disks - 1;
  1793. first_sector =
  1794. raid5_compute_sector(sector_nr*(conf->raid_disks-1),
  1795. raid_disks, data_disks,
  1796. &dd_idx, &pd_idx, conf);
  1797. last_sector =
  1798. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  1799. *(conf->raid_disks-1) -1,
  1800. raid_disks, data_disks,
  1801. &dd_idx, &pd_idx, conf);
  1802. if (last_sector >= (mddev->size<<1))
  1803. last_sector = (mddev->size<<1)-1;
  1804. while (first_sector <= last_sector) {
  1805. pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
  1806. sh = get_active_stripe(conf, first_sector,
  1807. conf->previous_raid_disks, pd_idx, 0);
  1808. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1809. set_bit(STRIPE_HANDLE, &sh->state);
  1810. release_stripe(sh);
  1811. first_sector += STRIPE_SECTORS;
  1812. }
  1813. return conf->chunk_size>>9;
  1814. }
  1815. /* if there is 1 or more failed drives and we are trying
  1816. * to resync, then assert that we are finished, because there is
  1817. * nothing we can do.
  1818. */
  1819. if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1820. sector_t rv = (mddev->size << 1) - sector_nr;
  1821. *skipped = 1;
  1822. return rv;
  1823. }
  1824. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1825. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  1826. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  1827. /* we can skip this block, and probably more */
  1828. sync_blocks /= STRIPE_SECTORS;
  1829. *skipped = 1;
  1830. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  1831. }
  1832. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  1833. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  1834. if (sh == NULL) {
  1835. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  1836. /* make sure we don't swamp the stripe cache if someone else
  1837. * is trying to get access
  1838. */
  1839. schedule_timeout_uninterruptible(1);
  1840. }
  1841. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
  1842. spin_lock(&sh->lock);
  1843. set_bit(STRIPE_SYNCING, &sh->state);
  1844. clear_bit(STRIPE_INSYNC, &sh->state);
  1845. spin_unlock(&sh->lock);
  1846. handle_stripe(sh);
  1847. release_stripe(sh);
  1848. return STRIPE_SECTORS;
  1849. }
  1850. /*
  1851. * This is our raid5 kernel thread.
  1852. *
  1853. * We scan the hash table for stripes which can be handled now.
  1854. * During the scan, completed stripes are saved for us by the interrupt
  1855. * handler, so that they will not have to wait for our next wakeup.
  1856. */
  1857. static void raid5d (mddev_t *mddev)
  1858. {
  1859. struct stripe_head *sh;
  1860. raid5_conf_t *conf = mddev_to_conf(mddev);
  1861. int handled;
  1862. PRINTK("+++ raid5d active\n");
  1863. md_check_recovery(mddev);
  1864. handled = 0;
  1865. spin_lock_irq(&conf->device_lock);
  1866. while (1) {
  1867. struct list_head *first;
  1868. if (conf->seq_flush - conf->seq_write > 0) {
  1869. int seq = conf->seq_flush;
  1870. spin_unlock_irq(&conf->device_lock);
  1871. bitmap_unplug(mddev->bitmap);
  1872. spin_lock_irq(&conf->device_lock);
  1873. conf->seq_write = seq;
  1874. activate_bit_delay(conf);
  1875. }
  1876. if (list_empty(&conf->handle_list) &&
  1877. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  1878. !blk_queue_plugged(mddev->queue) &&
  1879. !list_empty(&conf->delayed_list))
  1880. raid5_activate_delayed(conf);
  1881. if (list_empty(&conf->handle_list))
  1882. break;
  1883. first = conf->handle_list.next;
  1884. sh = list_entry(first, struct stripe_head, lru);
  1885. list_del_init(first);
  1886. atomic_inc(&sh->count);
  1887. if (atomic_read(&sh->count)!= 1)
  1888. BUG();
  1889. spin_unlock_irq(&conf->device_lock);
  1890. handled++;
  1891. handle_stripe(sh);
  1892. release_stripe(sh);
  1893. spin_lock_irq(&conf->device_lock);
  1894. }
  1895. PRINTK("%d stripes handled\n", handled);
  1896. spin_unlock_irq(&conf->device_lock);
  1897. unplug_slaves(mddev);
  1898. PRINTK("--- raid5d inactive\n");
  1899. }
  1900. static ssize_t
  1901. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  1902. {
  1903. raid5_conf_t *conf = mddev_to_conf(mddev);
  1904. if (conf)
  1905. return sprintf(page, "%d\n", conf->max_nr_stripes);
  1906. else
  1907. return 0;
  1908. }
  1909. static ssize_t
  1910. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  1911. {
  1912. raid5_conf_t *conf = mddev_to_conf(mddev);
  1913. char *end;
  1914. int new;
  1915. if (len >= PAGE_SIZE)
  1916. return -EINVAL;
  1917. if (!conf)
  1918. return -ENODEV;
  1919. new = simple_strtoul(page, &end, 10);
  1920. if (!*page || (*end && *end != '\n') )
  1921. return -EINVAL;
  1922. if (new <= 16 || new > 32768)
  1923. return -EINVAL;
  1924. while (new < conf->max_nr_stripes) {
  1925. if (drop_one_stripe(conf))
  1926. conf->max_nr_stripes--;
  1927. else
  1928. break;
  1929. }
  1930. while (new > conf->max_nr_stripes) {
  1931. if (grow_one_stripe(conf))
  1932. conf->max_nr_stripes++;
  1933. else break;
  1934. }
  1935. return len;
  1936. }
  1937. static struct md_sysfs_entry
  1938. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  1939. raid5_show_stripe_cache_size,
  1940. raid5_store_stripe_cache_size);
  1941. static ssize_t
  1942. stripe_cache_active_show(mddev_t *mddev, char *page)
  1943. {
  1944. raid5_conf_t *conf = mddev_to_conf(mddev);
  1945. if (conf)
  1946. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  1947. else
  1948. return 0;
  1949. }
  1950. static struct md_sysfs_entry
  1951. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  1952. static struct attribute *raid5_attrs[] = {
  1953. &raid5_stripecache_size.attr,
  1954. &raid5_stripecache_active.attr,
  1955. NULL,
  1956. };
  1957. static struct attribute_group raid5_attrs_group = {
  1958. .name = NULL,
  1959. .attrs = raid5_attrs,
  1960. };
  1961. static int run(mddev_t *mddev)
  1962. {
  1963. raid5_conf_t *conf;
  1964. int raid_disk, memory;
  1965. mdk_rdev_t *rdev;
  1966. struct disk_info *disk;
  1967. struct list_head *tmp;
  1968. if (mddev->level != 5 && mddev->level != 4) {
  1969. printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n",
  1970. mdname(mddev), mddev->level);
  1971. return -EIO;
  1972. }
  1973. if (mddev->reshape_position != MaxSector) {
  1974. /* Check that we can continue the reshape.
  1975. * Currently only disks can change, it must
  1976. * increase, and we must be past the point where
  1977. * a stripe over-writes itself
  1978. */
  1979. sector_t here_new, here_old;
  1980. int old_disks;
  1981. if (mddev->new_level != mddev->level ||
  1982. mddev->new_layout != mddev->layout ||
  1983. mddev->new_chunk != mddev->chunk_size) {
  1984. printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
  1985. mdname(mddev));
  1986. return -EINVAL;
  1987. }
  1988. if (mddev->delta_disks <= 0) {
  1989. printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
  1990. mdname(mddev));
  1991. return -EINVAL;
  1992. }
  1993. old_disks = mddev->raid_disks - mddev->delta_disks;
  1994. /* reshape_position must be on a new-stripe boundary, and one
  1995. * further up in new geometry must map after here in old geometry.
  1996. */
  1997. here_new = mddev->reshape_position;
  1998. if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
  1999. printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
  2000. return -EINVAL;
  2001. }
  2002. /* here_new is the stripe we will write to */
  2003. here_old = mddev->reshape_position;
  2004. sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
  2005. /* here_old is the first stripe that we might need to read from */
  2006. if (here_new >= here_old) {
  2007. /* Reading from the same stripe as writing to - bad */
  2008. printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
  2009. return -EINVAL;
  2010. }
  2011. printk(KERN_INFO "raid5: reshape will continue\n");
  2012. /* OK, we should be able to continue; */
  2013. }
  2014. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  2015. if ((conf = mddev->private) == NULL)
  2016. goto abort;
  2017. if (mddev->reshape_position == MaxSector) {
  2018. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  2019. } else {
  2020. conf->raid_disks = mddev->raid_disks;
  2021. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  2022. }
  2023. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  2024. GFP_KERNEL);
  2025. if (!conf->disks)
  2026. goto abort;
  2027. conf->mddev = mddev;
  2028. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  2029. goto abort;
  2030. spin_lock_init(&conf->device_lock);
  2031. init_waitqueue_head(&conf->wait_for_stripe);
  2032. init_waitqueue_head(&conf->wait_for_overlap);
  2033. INIT_LIST_HEAD(&conf->handle_list);
  2034. INIT_LIST_HEAD(&conf->delayed_list);
  2035. INIT_LIST_HEAD(&conf->bitmap_list);
  2036. INIT_LIST_HEAD(&conf->inactive_list);
  2037. atomic_set(&conf->active_stripes, 0);
  2038. atomic_set(&conf->preread_active_stripes, 0);
  2039. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  2040. ITERATE_RDEV(mddev,rdev,tmp) {
  2041. raid_disk = rdev->raid_disk;
  2042. if (raid_disk >= conf->raid_disks
  2043. || raid_disk < 0)
  2044. continue;
  2045. disk = conf->disks + raid_disk;
  2046. disk->rdev = rdev;
  2047. if (test_bit(In_sync, &rdev->flags)) {
  2048. char b[BDEVNAME_SIZE];
  2049. printk(KERN_INFO "raid5: device %s operational as raid"
  2050. " disk %d\n", bdevname(rdev->bdev,b),
  2051. raid_disk);
  2052. conf->working_disks++;
  2053. }
  2054. }
  2055. /*
  2056. * 0 for a fully functional array, 1 for a degraded array.
  2057. */
  2058. mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
  2059. conf->mddev = mddev;
  2060. conf->chunk_size = mddev->chunk_size;
  2061. conf->level = mddev->level;
  2062. conf->algorithm = mddev->layout;
  2063. conf->max_nr_stripes = NR_STRIPES;
  2064. conf->expand_progress = mddev->reshape_position;
  2065. /* device size must be a multiple of chunk size */
  2066. mddev->size &= ~(mddev->chunk_size/1024 -1);
  2067. mddev->resync_max_sectors = mddev->size << 1;
  2068. if (!conf->chunk_size || conf->chunk_size % 4) {
  2069. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  2070. conf->chunk_size, mdname(mddev));
  2071. goto abort;
  2072. }
  2073. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  2074. printk(KERN_ERR
  2075. "raid5: unsupported parity algorithm %d for %s\n",
  2076. conf->algorithm, mdname(mddev));
  2077. goto abort;
  2078. }
  2079. if (mddev->degraded > 1) {
  2080. printk(KERN_ERR "raid5: not enough operational devices for %s"
  2081. " (%d/%d failed)\n",
  2082. mdname(mddev), conf->failed_disks, conf->raid_disks);
  2083. goto abort;
  2084. }
  2085. if (mddev->degraded == 1 &&
  2086. mddev->recovery_cp != MaxSector) {
  2087. if (mddev->ok_start_degraded)
  2088. printk(KERN_WARNING
  2089. "raid5: starting dirty degraded array: %s"
  2090. "- data corruption possible.\n",
  2091. mdname(mddev));
  2092. else {
  2093. printk(KERN_ERR
  2094. "raid5: cannot start dirty degraded array for %s\n",
  2095. mdname(mddev));
  2096. goto abort;
  2097. }
  2098. }
  2099. {
  2100. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  2101. if (!mddev->thread) {
  2102. printk(KERN_ERR
  2103. "raid5: couldn't allocate thread for %s\n",
  2104. mdname(mddev));
  2105. goto abort;
  2106. }
  2107. }
  2108. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  2109. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  2110. if (grow_stripes(conf, conf->max_nr_stripes)) {
  2111. printk(KERN_ERR
  2112. "raid5: couldn't allocate %dkB for buffers\n", memory);
  2113. shrink_stripes(conf);
  2114. md_unregister_thread(mddev->thread);
  2115. goto abort;
  2116. } else
  2117. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  2118. memory, mdname(mddev));
  2119. if (mddev->degraded == 0)
  2120. printk("raid5: raid level %d set %s active with %d out of %d"
  2121. " devices, algorithm %d\n", conf->level, mdname(mddev),
  2122. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  2123. conf->algorithm);
  2124. else
  2125. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  2126. " out of %d devices, algorithm %d\n", conf->level,
  2127. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2128. mddev->raid_disks, conf->algorithm);
  2129. print_raid5_conf(conf);
  2130. if (conf->expand_progress != MaxSector) {
  2131. printk("...ok start reshape thread\n");
  2132. conf->expand_lo = conf->expand_progress;
  2133. atomic_set(&conf->reshape_stripes, 0);
  2134. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  2135. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  2136. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  2137. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  2138. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  2139. "%s_reshape");
  2140. /* FIXME if md_register_thread fails?? */
  2141. md_wakeup_thread(mddev->sync_thread);
  2142. }
  2143. /* read-ahead size must cover two whole stripes, which is
  2144. * 2 * (n-1) * chunksize where 'n' is the number of raid devices
  2145. */
  2146. {
  2147. int stripe = (mddev->raid_disks-1) * mddev->chunk_size
  2148. / PAGE_SIZE;
  2149. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  2150. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  2151. }
  2152. /* Ok, everything is just fine now */
  2153. sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
  2154. mddev->queue->unplug_fn = raid5_unplug_device;
  2155. mddev->queue->issue_flush_fn = raid5_issue_flush;
  2156. mddev->array_size = mddev->size * (conf->previous_raid_disks - 1);
  2157. return 0;
  2158. abort:
  2159. if (conf) {
  2160. print_raid5_conf(conf);
  2161. kfree(conf->disks);
  2162. kfree(conf->stripe_hashtbl);
  2163. kfree(conf);
  2164. }
  2165. mddev->private = NULL;
  2166. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  2167. return -EIO;
  2168. }
  2169. static int stop(mddev_t *mddev)
  2170. {
  2171. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2172. md_unregister_thread(mddev->thread);
  2173. mddev->thread = NULL;
  2174. shrink_stripes(conf);
  2175. kfree(conf->stripe_hashtbl);
  2176. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  2177. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  2178. kfree(conf->disks);
  2179. kfree(conf);
  2180. mddev->private = NULL;
  2181. return 0;
  2182. }
  2183. #if RAID5_DEBUG
  2184. static void print_sh (struct stripe_head *sh)
  2185. {
  2186. int i;
  2187. printk("sh %llu, pd_idx %d, state %ld.\n",
  2188. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  2189. printk("sh %llu, count %d.\n",
  2190. (unsigned long long)sh->sector, atomic_read(&sh->count));
  2191. printk("sh %llu, ", (unsigned long long)sh->sector);
  2192. for (i = 0; i < sh->disks; i++) {
  2193. printk("(cache%d: %p %ld) ",
  2194. i, sh->dev[i].page, sh->dev[i].flags);
  2195. }
  2196. printk("\n");
  2197. }
  2198. static void printall (raid5_conf_t *conf)
  2199. {
  2200. struct stripe_head *sh;
  2201. struct hlist_node *hn;
  2202. int i;
  2203. spin_lock_irq(&conf->device_lock);
  2204. for (i = 0; i < NR_HASH; i++) {
  2205. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  2206. if (sh->raid_conf != conf)
  2207. continue;
  2208. print_sh(sh);
  2209. }
  2210. }
  2211. spin_unlock_irq(&conf->device_lock);
  2212. }
  2213. #endif
  2214. static void status (struct seq_file *seq, mddev_t *mddev)
  2215. {
  2216. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2217. int i;
  2218. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  2219. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  2220. for (i = 0; i < conf->raid_disks; i++)
  2221. seq_printf (seq, "%s",
  2222. conf->disks[i].rdev &&
  2223. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  2224. seq_printf (seq, "]");
  2225. #if RAID5_DEBUG
  2226. #define D(x) \
  2227. seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
  2228. printall(conf);
  2229. #endif
  2230. }
  2231. static void print_raid5_conf (raid5_conf_t *conf)
  2232. {
  2233. int i;
  2234. struct disk_info *tmp;
  2235. printk("RAID5 conf printout:\n");
  2236. if (!conf) {
  2237. printk("(conf==NULL)\n");
  2238. return;
  2239. }
  2240. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  2241. conf->working_disks, conf->failed_disks);
  2242. for (i = 0; i < conf->raid_disks; i++) {
  2243. char b[BDEVNAME_SIZE];
  2244. tmp = conf->disks + i;
  2245. if (tmp->rdev)
  2246. printk(" disk %d, o:%d, dev:%s\n",
  2247. i, !test_bit(Faulty, &tmp->rdev->flags),
  2248. bdevname(tmp->rdev->bdev,b));
  2249. }
  2250. }
  2251. static int raid5_spare_active(mddev_t *mddev)
  2252. {
  2253. int i;
  2254. raid5_conf_t *conf = mddev->private;
  2255. struct disk_info *tmp;
  2256. for (i = 0; i < conf->raid_disks; i++) {
  2257. tmp = conf->disks + i;
  2258. if (tmp->rdev
  2259. && !test_bit(Faulty, &tmp->rdev->flags)
  2260. && !test_bit(In_sync, &tmp->rdev->flags)) {
  2261. mddev->degraded--;
  2262. conf->failed_disks--;
  2263. conf->working_disks++;
  2264. set_bit(In_sync, &tmp->rdev->flags);
  2265. }
  2266. }
  2267. print_raid5_conf(conf);
  2268. return 0;
  2269. }
  2270. static int raid5_remove_disk(mddev_t *mddev, int number)
  2271. {
  2272. raid5_conf_t *conf = mddev->private;
  2273. int err = 0;
  2274. mdk_rdev_t *rdev;
  2275. struct disk_info *p = conf->disks + number;
  2276. print_raid5_conf(conf);
  2277. rdev = p->rdev;
  2278. if (rdev) {
  2279. if (test_bit(In_sync, &rdev->flags) ||
  2280. atomic_read(&rdev->nr_pending)) {
  2281. err = -EBUSY;
  2282. goto abort;
  2283. }
  2284. p->rdev = NULL;
  2285. synchronize_rcu();
  2286. if (atomic_read(&rdev->nr_pending)) {
  2287. /* lost the race, try later */
  2288. err = -EBUSY;
  2289. p->rdev = rdev;
  2290. }
  2291. }
  2292. abort:
  2293. print_raid5_conf(conf);
  2294. return err;
  2295. }
  2296. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  2297. {
  2298. raid5_conf_t *conf = mddev->private;
  2299. int found = 0;
  2300. int disk;
  2301. struct disk_info *p;
  2302. if (mddev->degraded > 1)
  2303. /* no point adding a device */
  2304. return 0;
  2305. /*
  2306. * find the disk ...
  2307. */
  2308. for (disk=0; disk < conf->raid_disks; disk++)
  2309. if ((p=conf->disks + disk)->rdev == NULL) {
  2310. clear_bit(In_sync, &rdev->flags);
  2311. rdev->raid_disk = disk;
  2312. found = 1;
  2313. if (rdev->saved_raid_disk != disk)
  2314. conf->fullsync = 1;
  2315. rcu_assign_pointer(p->rdev, rdev);
  2316. break;
  2317. }
  2318. print_raid5_conf(conf);
  2319. return found;
  2320. }
  2321. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  2322. {
  2323. /* no resync is happening, and there is enough space
  2324. * on all devices, so we can resize.
  2325. * We need to make sure resync covers any new space.
  2326. * If the array is shrinking we should possibly wait until
  2327. * any io in the removed space completes, but it hardly seems
  2328. * worth it.
  2329. */
  2330. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  2331. mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
  2332. set_capacity(mddev->gendisk, mddev->array_size << 1);
  2333. mddev->changed = 1;
  2334. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  2335. mddev->recovery_cp = mddev->size << 1;
  2336. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2337. }
  2338. mddev->size = sectors /2;
  2339. mddev->resync_max_sectors = sectors;
  2340. return 0;
  2341. }
  2342. #ifdef CONFIG_MD_RAID5_RESHAPE
  2343. static int raid5_check_reshape(mddev_t *mddev)
  2344. {
  2345. raid5_conf_t *conf = mddev_to_conf(mddev);
  2346. int err;
  2347. if (mddev->delta_disks < 0 ||
  2348. mddev->new_level != mddev->level)
  2349. return -EINVAL; /* Cannot shrink array or change level yet */
  2350. if (mddev->delta_disks == 0)
  2351. return 0; /* nothing to do */
  2352. /* Can only proceed if there are plenty of stripe_heads.
  2353. * We need a minimum of one full stripe,, and for sensible progress
  2354. * it is best to have about 4 times that.
  2355. * If we require 4 times, then the default 256 4K stripe_heads will
  2356. * allow for chunk sizes up to 256K, which is probably OK.
  2357. * If the chunk size is greater, user-space should request more
  2358. * stripe_heads first.
  2359. */
  2360. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  2361. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  2362. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  2363. (mddev->chunk_size / STRIPE_SIZE)*4);
  2364. return -ENOSPC;
  2365. }
  2366. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  2367. if (err)
  2368. return err;
  2369. /* looks like we might be able to manage this */
  2370. return 0;
  2371. }
  2372. static int raid5_start_reshape(mddev_t *mddev)
  2373. {
  2374. raid5_conf_t *conf = mddev_to_conf(mddev);
  2375. mdk_rdev_t *rdev;
  2376. struct list_head *rtmp;
  2377. int spares = 0;
  2378. int added_devices = 0;
  2379. if (mddev->degraded ||
  2380. test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  2381. return -EBUSY;
  2382. ITERATE_RDEV(mddev, rdev, rtmp)
  2383. if (rdev->raid_disk < 0 &&
  2384. !test_bit(Faulty, &rdev->flags))
  2385. spares++;
  2386. if (spares < mddev->delta_disks-1)
  2387. /* Not enough devices even to make a degraded array
  2388. * of that size
  2389. */
  2390. return -EINVAL;
  2391. atomic_set(&conf->reshape_stripes, 0);
  2392. spin_lock_irq(&conf->device_lock);
  2393. conf->previous_raid_disks = conf->raid_disks;
  2394. conf->raid_disks += mddev->delta_disks;
  2395. conf->expand_progress = 0;
  2396. conf->expand_lo = 0;
  2397. spin_unlock_irq(&conf->device_lock);
  2398. /* Add some new drives, as many as will fit.
  2399. * We know there are enough to make the newly sized array work.
  2400. */
  2401. ITERATE_RDEV(mddev, rdev, rtmp)
  2402. if (rdev->raid_disk < 0 &&
  2403. !test_bit(Faulty, &rdev->flags)) {
  2404. if (raid5_add_disk(mddev, rdev)) {
  2405. char nm[20];
  2406. set_bit(In_sync, &rdev->flags);
  2407. conf->working_disks++;
  2408. added_devices++;
  2409. sprintf(nm, "rd%d", rdev->raid_disk);
  2410. sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
  2411. } else
  2412. break;
  2413. }
  2414. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  2415. mddev->raid_disks = conf->raid_disks;
  2416. mddev->reshape_position = 0;
  2417. mddev->sb_dirty = 1;
  2418. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  2419. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  2420. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  2421. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  2422. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  2423. "%s_reshape");
  2424. if (!mddev->sync_thread) {
  2425. mddev->recovery = 0;
  2426. spin_lock_irq(&conf->device_lock);
  2427. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  2428. conf->expand_progress = MaxSector;
  2429. spin_unlock_irq(&conf->device_lock);
  2430. return -EAGAIN;
  2431. }
  2432. md_wakeup_thread(mddev->sync_thread);
  2433. md_new_event(mddev);
  2434. return 0;
  2435. }
  2436. #endif
  2437. static void end_reshape(raid5_conf_t *conf)
  2438. {
  2439. struct block_device *bdev;
  2440. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  2441. conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
  2442. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  2443. conf->mddev->changed = 1;
  2444. bdev = bdget_disk(conf->mddev->gendisk, 0);
  2445. if (bdev) {
  2446. mutex_lock(&bdev->bd_inode->i_mutex);
  2447. i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
  2448. mutex_unlock(&bdev->bd_inode->i_mutex);
  2449. bdput(bdev);
  2450. }
  2451. spin_lock_irq(&conf->device_lock);
  2452. conf->expand_progress = MaxSector;
  2453. spin_unlock_irq(&conf->device_lock);
  2454. conf->mddev->reshape_position = MaxSector;
  2455. }
  2456. }
  2457. static void raid5_quiesce(mddev_t *mddev, int state)
  2458. {
  2459. raid5_conf_t *conf = mddev_to_conf(mddev);
  2460. switch(state) {
  2461. case 2: /* resume for a suspend */
  2462. wake_up(&conf->wait_for_overlap);
  2463. break;
  2464. case 1: /* stop all writes */
  2465. spin_lock_irq(&conf->device_lock);
  2466. conf->quiesce = 1;
  2467. wait_event_lock_irq(conf->wait_for_stripe,
  2468. atomic_read(&conf->active_stripes) == 0,
  2469. conf->device_lock, /* nothing */);
  2470. spin_unlock_irq(&conf->device_lock);
  2471. break;
  2472. case 0: /* re-enable writes */
  2473. spin_lock_irq(&conf->device_lock);
  2474. conf->quiesce = 0;
  2475. wake_up(&conf->wait_for_stripe);
  2476. wake_up(&conf->wait_for_overlap);
  2477. spin_unlock_irq(&conf->device_lock);
  2478. break;
  2479. }
  2480. }
  2481. static struct mdk_personality raid5_personality =
  2482. {
  2483. .name = "raid5",
  2484. .level = 5,
  2485. .owner = THIS_MODULE,
  2486. .make_request = make_request,
  2487. .run = run,
  2488. .stop = stop,
  2489. .status = status,
  2490. .error_handler = error,
  2491. .hot_add_disk = raid5_add_disk,
  2492. .hot_remove_disk= raid5_remove_disk,
  2493. .spare_active = raid5_spare_active,
  2494. .sync_request = sync_request,
  2495. .resize = raid5_resize,
  2496. #ifdef CONFIG_MD_RAID5_RESHAPE
  2497. .check_reshape = raid5_check_reshape,
  2498. .start_reshape = raid5_start_reshape,
  2499. #endif
  2500. .quiesce = raid5_quiesce,
  2501. };
  2502. static struct mdk_personality raid4_personality =
  2503. {
  2504. .name = "raid4",
  2505. .level = 4,
  2506. .owner = THIS_MODULE,
  2507. .make_request = make_request,
  2508. .run = run,
  2509. .stop = stop,
  2510. .status = status,
  2511. .error_handler = error,
  2512. .hot_add_disk = raid5_add_disk,
  2513. .hot_remove_disk= raid5_remove_disk,
  2514. .spare_active = raid5_spare_active,
  2515. .sync_request = sync_request,
  2516. .resize = raid5_resize,
  2517. .quiesce = raid5_quiesce,
  2518. };
  2519. static int __init raid5_init(void)
  2520. {
  2521. register_md_personality(&raid5_personality);
  2522. register_md_personality(&raid4_personality);
  2523. return 0;
  2524. }
  2525. static void raid5_exit(void)
  2526. {
  2527. unregister_md_personality(&raid5_personality);
  2528. unregister_md_personality(&raid4_personality);
  2529. }
  2530. module_init(raid5_init);
  2531. module_exit(raid5_exit);
  2532. MODULE_LICENSE("GPL");
  2533. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  2534. MODULE_ALIAS("md-raid5");
  2535. MODULE_ALIAS("md-raid4");
  2536. MODULE_ALIAS("md-level-5");
  2537. MODULE_ALIAS("md-level-4");