raid5.c 64 KB

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