raid5-cache.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  1. /*
  2. * Copyright (C) 2015 Shaohua Li <shli@fb.com>
  3. * Copyright (C) 2016 Song Liu <songliubraving@fb.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/wait.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/raid/md_p.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/random.h>
  22. #include <linux/kthread.h>
  23. #include "md.h"
  24. #include "raid5.h"
  25. #include "bitmap.h"
  26. /*
  27. * metadata/data stored in disk with 4k size unit (a block) regardless
  28. * underneath hardware sector size. only works with PAGE_SIZE == 4096
  29. */
  30. #define BLOCK_SECTORS (8)
  31. /*
  32. * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
  33. *
  34. * In write through mode, the reclaim runs every log->max_free_space.
  35. * This can prevent the recovery scans for too long
  36. */
  37. #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
  38. #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
  39. /* wake up reclaim thread periodically */
  40. #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
  41. /* start flush with these full stripes */
  42. #define R5C_FULL_STRIPE_FLUSH_BATCH 256
  43. /* reclaim stripes in groups */
  44. #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
  45. /*
  46. * We only need 2 bios per I/O unit to make progress, but ensure we
  47. * have a few more available to not get too tight.
  48. */
  49. #define R5L_POOL_SIZE 4
  50. /*
  51. * r5c journal modes of the array: write-back or write-through.
  52. * write-through mode has identical behavior as existing log only
  53. * implementation.
  54. */
  55. enum r5c_journal_mode {
  56. R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
  57. R5C_JOURNAL_MODE_WRITE_BACK = 1,
  58. };
  59. static char *r5c_journal_mode_str[] = {"write-through",
  60. "write-back"};
  61. /*
  62. * raid5 cache state machine
  63. *
  64. * With the RAID cache, each stripe works in two phases:
  65. * - caching phase
  66. * - writing-out phase
  67. *
  68. * These two phases are controlled by bit STRIPE_R5C_CACHING:
  69. * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
  70. * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
  71. *
  72. * When there is no journal, or the journal is in write-through mode,
  73. * the stripe is always in writing-out phase.
  74. *
  75. * For write-back journal, the stripe is sent to caching phase on write
  76. * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
  77. * the write-out phase by clearing STRIPE_R5C_CACHING.
  78. *
  79. * Stripes in caching phase do not write the raid disks. Instead, all
  80. * writes are committed from the log device. Therefore, a stripe in
  81. * caching phase handles writes as:
  82. * - write to log device
  83. * - return IO
  84. *
  85. * Stripes in writing-out phase handle writes as:
  86. * - calculate parity
  87. * - write pending data and parity to journal
  88. * - write data and parity to raid disks
  89. * - return IO for pending writes
  90. */
  91. struct r5l_log {
  92. struct md_rdev *rdev;
  93. u32 uuid_checksum;
  94. sector_t device_size; /* log device size, round to
  95. * BLOCK_SECTORS */
  96. sector_t max_free_space; /* reclaim run if free space is at
  97. * this size */
  98. sector_t last_checkpoint; /* log tail. where recovery scan
  99. * starts from */
  100. u64 last_cp_seq; /* log tail sequence */
  101. sector_t log_start; /* log head. where new data appends */
  102. u64 seq; /* log head sequence */
  103. sector_t next_checkpoint;
  104. struct mutex io_mutex;
  105. struct r5l_io_unit *current_io; /* current io_unit accepting new data */
  106. spinlock_t io_list_lock;
  107. struct list_head running_ios; /* io_units which are still running,
  108. * and have not yet been completely
  109. * written to the log */
  110. struct list_head io_end_ios; /* io_units which have been completely
  111. * written to the log but not yet written
  112. * to the RAID */
  113. struct list_head flushing_ios; /* io_units which are waiting for log
  114. * cache flush */
  115. struct list_head finished_ios; /* io_units which settle down in log disk */
  116. struct bio flush_bio;
  117. struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
  118. struct kmem_cache *io_kc;
  119. mempool_t *io_pool;
  120. struct bio_set *bs;
  121. mempool_t *meta_pool;
  122. struct md_thread *reclaim_thread;
  123. unsigned long reclaim_target; /* number of space that need to be
  124. * reclaimed. if it's 0, reclaim spaces
  125. * used by io_units which are in
  126. * IO_UNIT_STRIPE_END state (eg, reclaim
  127. * dones't wait for specific io_unit
  128. * switching to IO_UNIT_STRIPE_END
  129. * state) */
  130. wait_queue_head_t iounit_wait;
  131. struct list_head no_space_stripes; /* pending stripes, log has no space */
  132. spinlock_t no_space_stripes_lock;
  133. bool need_cache_flush;
  134. /* for r5c_cache */
  135. enum r5c_journal_mode r5c_journal_mode;
  136. /* all stripes in r5cache, in the order of seq at sh->log_start */
  137. struct list_head stripe_in_journal_list;
  138. spinlock_t stripe_in_journal_lock;
  139. atomic_t stripe_in_journal_count;
  140. /* to submit async io_units, to fulfill ordering of flush */
  141. struct work_struct deferred_io_work;
  142. /* to disable write back during in degraded mode */
  143. struct work_struct disable_writeback_work;
  144. };
  145. /*
  146. * an IO range starts from a meta data block and end at the next meta data
  147. * block. The io unit's the meta data block tracks data/parity followed it. io
  148. * unit is written to log disk with normal write, as we always flush log disk
  149. * first and then start move data to raid disks, there is no requirement to
  150. * write io unit with FLUSH/FUA
  151. */
  152. struct r5l_io_unit {
  153. struct r5l_log *log;
  154. struct page *meta_page; /* store meta block */
  155. int meta_offset; /* current offset in meta_page */
  156. struct bio *current_bio;/* current_bio accepting new data */
  157. atomic_t pending_stripe;/* how many stripes not flushed to raid */
  158. u64 seq; /* seq number of the metablock */
  159. sector_t log_start; /* where the io_unit starts */
  160. sector_t log_end; /* where the io_unit ends */
  161. struct list_head log_sibling; /* log->running_ios */
  162. struct list_head stripe_list; /* stripes added to the io_unit */
  163. int state;
  164. bool need_split_bio;
  165. struct bio *split_bio;
  166. unsigned int has_flush:1; /* include flush request */
  167. unsigned int has_fua:1; /* include fua request */
  168. unsigned int has_null_flush:1; /* include empty flush request */
  169. /*
  170. * io isn't sent yet, flush/fua request can only be submitted till it's
  171. * the first IO in running_ios list
  172. */
  173. unsigned int io_deferred:1;
  174. struct bio_list flush_barriers; /* size == 0 flush bios */
  175. };
  176. /* r5l_io_unit state */
  177. enum r5l_io_unit_state {
  178. IO_UNIT_RUNNING = 0, /* accepting new IO */
  179. IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
  180. * don't accepting new bio */
  181. IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
  182. IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
  183. };
  184. bool r5c_is_writeback(struct r5l_log *log)
  185. {
  186. return (log != NULL &&
  187. log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
  188. }
  189. static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
  190. {
  191. start += inc;
  192. if (start >= log->device_size)
  193. start = start - log->device_size;
  194. return start;
  195. }
  196. static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
  197. sector_t end)
  198. {
  199. if (end >= start)
  200. return end - start;
  201. else
  202. return end + log->device_size - start;
  203. }
  204. static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
  205. {
  206. sector_t used_size;
  207. used_size = r5l_ring_distance(log, log->last_checkpoint,
  208. log->log_start);
  209. return log->device_size > used_size + size;
  210. }
  211. static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
  212. enum r5l_io_unit_state state)
  213. {
  214. if (WARN_ON(io->state >= state))
  215. return;
  216. io->state = state;
  217. }
  218. static void
  219. r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
  220. struct bio_list *return_bi)
  221. {
  222. struct bio *wbi, *wbi2;
  223. wbi = dev->written;
  224. dev->written = NULL;
  225. while (wbi && wbi->bi_iter.bi_sector <
  226. dev->sector + STRIPE_SECTORS) {
  227. wbi2 = r5_next_bio(wbi, dev->sector);
  228. if (!raid5_dec_bi_active_stripes(wbi)) {
  229. md_write_end(conf->mddev);
  230. bio_list_add(return_bi, wbi);
  231. }
  232. wbi = wbi2;
  233. }
  234. }
  235. void r5c_handle_cached_data_endio(struct r5conf *conf,
  236. struct stripe_head *sh, int disks, struct bio_list *return_bi)
  237. {
  238. int i;
  239. for (i = sh->disks; i--; ) {
  240. if (sh->dev[i].written) {
  241. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  242. r5c_return_dev_pending_writes(conf, &sh->dev[i],
  243. return_bi);
  244. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  245. STRIPE_SECTORS,
  246. !test_bit(STRIPE_DEGRADED, &sh->state),
  247. 0);
  248. }
  249. }
  250. }
  251. /* Check whether we should flush some stripes to free up stripe cache */
  252. void r5c_check_stripe_cache_usage(struct r5conf *conf)
  253. {
  254. int total_cached;
  255. if (!r5c_is_writeback(conf->log))
  256. return;
  257. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  258. atomic_read(&conf->r5c_cached_full_stripes);
  259. /*
  260. * The following condition is true for either of the following:
  261. * - stripe cache pressure high:
  262. * total_cached > 3/4 min_nr_stripes ||
  263. * empty_inactive_list_nr > 0
  264. * - stripe cache pressure moderate:
  265. * total_cached > 1/2 min_nr_stripes
  266. */
  267. if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  268. atomic_read(&conf->empty_inactive_list_nr) > 0)
  269. r5l_wake_reclaim(conf->log, 0);
  270. }
  271. /*
  272. * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
  273. * stripes in the cache
  274. */
  275. void r5c_check_cached_full_stripe(struct r5conf *conf)
  276. {
  277. if (!r5c_is_writeback(conf->log))
  278. return;
  279. /*
  280. * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
  281. * or a full stripe (chunk size / 4k stripes).
  282. */
  283. if (atomic_read(&conf->r5c_cached_full_stripes) >=
  284. min(R5C_FULL_STRIPE_FLUSH_BATCH,
  285. conf->chunk_sectors >> STRIPE_SHIFT))
  286. r5l_wake_reclaim(conf->log, 0);
  287. }
  288. /*
  289. * Total log space (in sectors) needed to flush all data in cache
  290. *
  291. * Currently, writing-out phase automatically includes all pending writes
  292. * to the same sector. So the reclaim of each stripe takes up to
  293. * (conf->raid_disks + 1) pages of log space.
  294. *
  295. * To totally avoid deadlock due to log space, the code reserves
  296. * (conf->raid_disks + 1) pages for each stripe in cache, which is not
  297. * necessary in most cases.
  298. *
  299. * To improve this, we will need writing-out phase to be able to NOT include
  300. * pending writes, which will reduce the requirement to
  301. * (conf->max_degraded + 1) pages per stripe in cache.
  302. */
  303. static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
  304. {
  305. struct r5l_log *log = conf->log;
  306. if (!r5c_is_writeback(log))
  307. return 0;
  308. return BLOCK_SECTORS * (conf->raid_disks + 1) *
  309. atomic_read(&log->stripe_in_journal_count);
  310. }
  311. /*
  312. * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
  313. *
  314. * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
  315. * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
  316. * device is less than 2x of reclaim_required_space.
  317. */
  318. static inline void r5c_update_log_state(struct r5l_log *log)
  319. {
  320. struct r5conf *conf = log->rdev->mddev->private;
  321. sector_t free_space;
  322. sector_t reclaim_space;
  323. bool wake_reclaim = false;
  324. if (!r5c_is_writeback(log))
  325. return;
  326. free_space = r5l_ring_distance(log, log->log_start,
  327. log->last_checkpoint);
  328. reclaim_space = r5c_log_required_to_flush_cache(conf);
  329. if (free_space < 2 * reclaim_space)
  330. set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
  331. else {
  332. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
  333. wake_reclaim = true;
  334. clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
  335. }
  336. if (free_space < 3 * reclaim_space)
  337. set_bit(R5C_LOG_TIGHT, &conf->cache_state);
  338. else
  339. clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
  340. if (wake_reclaim)
  341. r5l_wake_reclaim(log, 0);
  342. }
  343. /*
  344. * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
  345. * This function should only be called in write-back mode.
  346. */
  347. void r5c_make_stripe_write_out(struct stripe_head *sh)
  348. {
  349. struct r5conf *conf = sh->raid_conf;
  350. struct r5l_log *log = conf->log;
  351. BUG_ON(!r5c_is_writeback(log));
  352. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  353. clear_bit(STRIPE_R5C_CACHING, &sh->state);
  354. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  355. atomic_inc(&conf->preread_active_stripes);
  356. if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
  357. BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
  358. atomic_dec(&conf->r5c_cached_partial_stripes);
  359. }
  360. if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
  361. BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
  362. atomic_dec(&conf->r5c_cached_full_stripes);
  363. }
  364. }
  365. static void r5c_handle_data_cached(struct stripe_head *sh)
  366. {
  367. int i;
  368. for (i = sh->disks; i--; )
  369. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  370. set_bit(R5_InJournal, &sh->dev[i].flags);
  371. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  372. }
  373. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  374. }
  375. /*
  376. * this journal write must contain full parity,
  377. * it may also contain some data pages
  378. */
  379. static void r5c_handle_parity_cached(struct stripe_head *sh)
  380. {
  381. int i;
  382. for (i = sh->disks; i--; )
  383. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  384. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  385. }
  386. /*
  387. * Setting proper flags after writing (or flushing) data and/or parity to the
  388. * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
  389. */
  390. static void r5c_finish_cache_stripe(struct stripe_head *sh)
  391. {
  392. struct r5l_log *log = sh->raid_conf->log;
  393. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  394. BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  395. /*
  396. * Set R5_InJournal for parity dev[pd_idx]. This means
  397. * all data AND parity in the journal. For RAID 6, it is
  398. * NOT necessary to set the flag for dev[qd_idx], as the
  399. * two parities are written out together.
  400. */
  401. set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  402. } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  403. r5c_handle_data_cached(sh);
  404. } else {
  405. r5c_handle_parity_cached(sh);
  406. set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  407. }
  408. }
  409. static void r5l_io_run_stripes(struct r5l_io_unit *io)
  410. {
  411. struct stripe_head *sh, *next;
  412. list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
  413. list_del_init(&sh->log_list);
  414. r5c_finish_cache_stripe(sh);
  415. set_bit(STRIPE_HANDLE, &sh->state);
  416. raid5_release_stripe(sh);
  417. }
  418. }
  419. static void r5l_log_run_stripes(struct r5l_log *log)
  420. {
  421. struct r5l_io_unit *io, *next;
  422. assert_spin_locked(&log->io_list_lock);
  423. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  424. /* don't change list order */
  425. if (io->state < IO_UNIT_IO_END)
  426. break;
  427. list_move_tail(&io->log_sibling, &log->finished_ios);
  428. r5l_io_run_stripes(io);
  429. }
  430. }
  431. static void r5l_move_to_end_ios(struct r5l_log *log)
  432. {
  433. struct r5l_io_unit *io, *next;
  434. assert_spin_locked(&log->io_list_lock);
  435. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  436. /* don't change list order */
  437. if (io->state < IO_UNIT_IO_END)
  438. break;
  439. list_move_tail(&io->log_sibling, &log->io_end_ios);
  440. }
  441. }
  442. static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
  443. static void r5l_log_endio(struct bio *bio)
  444. {
  445. struct r5l_io_unit *io = bio->bi_private;
  446. struct r5l_io_unit *io_deferred;
  447. struct r5l_log *log = io->log;
  448. unsigned long flags;
  449. if (bio->bi_error)
  450. md_error(log->rdev->mddev, log->rdev);
  451. bio_put(bio);
  452. mempool_free(io->meta_page, log->meta_pool);
  453. spin_lock_irqsave(&log->io_list_lock, flags);
  454. __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
  455. if (log->need_cache_flush)
  456. r5l_move_to_end_ios(log);
  457. else
  458. r5l_log_run_stripes(log);
  459. if (!list_empty(&log->running_ios)) {
  460. /*
  461. * FLUSH/FUA io_unit is deferred because of ordering, now we
  462. * can dispatch it
  463. */
  464. io_deferred = list_first_entry(&log->running_ios,
  465. struct r5l_io_unit, log_sibling);
  466. if (io_deferred->io_deferred)
  467. schedule_work(&log->deferred_io_work);
  468. }
  469. spin_unlock_irqrestore(&log->io_list_lock, flags);
  470. if (log->need_cache_flush)
  471. md_wakeup_thread(log->rdev->mddev->thread);
  472. if (io->has_null_flush) {
  473. struct bio *bi;
  474. WARN_ON(bio_list_empty(&io->flush_barriers));
  475. while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
  476. bio_endio(bi);
  477. atomic_dec(&io->pending_stripe);
  478. }
  479. if (atomic_read(&io->pending_stripe) == 0)
  480. __r5l_stripe_write_finished(io);
  481. }
  482. }
  483. static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
  484. {
  485. unsigned long flags;
  486. spin_lock_irqsave(&log->io_list_lock, flags);
  487. __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
  488. spin_unlock_irqrestore(&log->io_list_lock, flags);
  489. if (io->has_flush)
  490. io->current_bio->bi_opf |= REQ_PREFLUSH;
  491. if (io->has_fua)
  492. io->current_bio->bi_opf |= REQ_FUA;
  493. submit_bio(io->current_bio);
  494. if (!io->split_bio)
  495. return;
  496. if (io->has_flush)
  497. io->split_bio->bi_opf |= REQ_PREFLUSH;
  498. if (io->has_fua)
  499. io->split_bio->bi_opf |= REQ_FUA;
  500. submit_bio(io->split_bio);
  501. }
  502. /* deferred io_unit will be dispatched here */
  503. static void r5l_submit_io_async(struct work_struct *work)
  504. {
  505. struct r5l_log *log = container_of(work, struct r5l_log,
  506. deferred_io_work);
  507. struct r5l_io_unit *io = NULL;
  508. unsigned long flags;
  509. spin_lock_irqsave(&log->io_list_lock, flags);
  510. if (!list_empty(&log->running_ios)) {
  511. io = list_first_entry(&log->running_ios, struct r5l_io_unit,
  512. log_sibling);
  513. if (!io->io_deferred)
  514. io = NULL;
  515. else
  516. io->io_deferred = 0;
  517. }
  518. spin_unlock_irqrestore(&log->io_list_lock, flags);
  519. if (io)
  520. r5l_do_submit_io(log, io);
  521. }
  522. static void r5c_disable_writeback_async(struct work_struct *work)
  523. {
  524. struct r5l_log *log = container_of(work, struct r5l_log,
  525. disable_writeback_work);
  526. struct mddev *mddev = log->rdev->mddev;
  527. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  528. return;
  529. pr_info("md/raid:%s: Disabling writeback cache for degraded array.\n",
  530. mdname(mddev));
  531. mddev_suspend(mddev);
  532. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  533. mddev_resume(mddev);
  534. }
  535. static void r5l_submit_current_io(struct r5l_log *log)
  536. {
  537. struct r5l_io_unit *io = log->current_io;
  538. struct bio *bio;
  539. struct r5l_meta_block *block;
  540. unsigned long flags;
  541. u32 crc;
  542. bool do_submit = true;
  543. if (!io)
  544. return;
  545. block = page_address(io->meta_page);
  546. block->meta_size = cpu_to_le32(io->meta_offset);
  547. crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
  548. block->checksum = cpu_to_le32(crc);
  549. bio = io->current_bio;
  550. log->current_io = NULL;
  551. spin_lock_irqsave(&log->io_list_lock, flags);
  552. if (io->has_flush || io->has_fua) {
  553. if (io != list_first_entry(&log->running_ios,
  554. struct r5l_io_unit, log_sibling)) {
  555. io->io_deferred = 1;
  556. do_submit = false;
  557. }
  558. }
  559. spin_unlock_irqrestore(&log->io_list_lock, flags);
  560. if (do_submit)
  561. r5l_do_submit_io(log, io);
  562. }
  563. static struct bio *r5l_bio_alloc(struct r5l_log *log)
  564. {
  565. struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
  566. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  567. bio->bi_bdev = log->rdev->bdev;
  568. bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
  569. return bio;
  570. }
  571. static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
  572. {
  573. log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
  574. r5c_update_log_state(log);
  575. /*
  576. * If we filled up the log device start from the beginning again,
  577. * which will require a new bio.
  578. *
  579. * Note: for this to work properly the log size needs to me a multiple
  580. * of BLOCK_SECTORS.
  581. */
  582. if (log->log_start == 0)
  583. io->need_split_bio = true;
  584. io->log_end = log->log_start;
  585. }
  586. static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
  587. {
  588. struct r5l_io_unit *io;
  589. struct r5l_meta_block *block;
  590. io = mempool_alloc(log->io_pool, GFP_ATOMIC);
  591. if (!io)
  592. return NULL;
  593. memset(io, 0, sizeof(*io));
  594. io->log = log;
  595. INIT_LIST_HEAD(&io->log_sibling);
  596. INIT_LIST_HEAD(&io->stripe_list);
  597. bio_list_init(&io->flush_barriers);
  598. io->state = IO_UNIT_RUNNING;
  599. io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
  600. block = page_address(io->meta_page);
  601. clear_page(block);
  602. block->magic = cpu_to_le32(R5LOG_MAGIC);
  603. block->version = R5LOG_VERSION;
  604. block->seq = cpu_to_le64(log->seq);
  605. block->position = cpu_to_le64(log->log_start);
  606. io->log_start = log->log_start;
  607. io->meta_offset = sizeof(struct r5l_meta_block);
  608. io->seq = log->seq++;
  609. io->current_bio = r5l_bio_alloc(log);
  610. io->current_bio->bi_end_io = r5l_log_endio;
  611. io->current_bio->bi_private = io;
  612. bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
  613. r5_reserve_log_entry(log, io);
  614. spin_lock_irq(&log->io_list_lock);
  615. list_add_tail(&io->log_sibling, &log->running_ios);
  616. spin_unlock_irq(&log->io_list_lock);
  617. return io;
  618. }
  619. static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
  620. {
  621. if (log->current_io &&
  622. log->current_io->meta_offset + payload_size > PAGE_SIZE)
  623. r5l_submit_current_io(log);
  624. if (!log->current_io) {
  625. log->current_io = r5l_new_meta(log);
  626. if (!log->current_io)
  627. return -ENOMEM;
  628. }
  629. return 0;
  630. }
  631. static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
  632. sector_t location,
  633. u32 checksum1, u32 checksum2,
  634. bool checksum2_valid)
  635. {
  636. struct r5l_io_unit *io = log->current_io;
  637. struct r5l_payload_data_parity *payload;
  638. payload = page_address(io->meta_page) + io->meta_offset;
  639. payload->header.type = cpu_to_le16(type);
  640. payload->header.flags = cpu_to_le16(0);
  641. payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
  642. (PAGE_SHIFT - 9));
  643. payload->location = cpu_to_le64(location);
  644. payload->checksum[0] = cpu_to_le32(checksum1);
  645. if (checksum2_valid)
  646. payload->checksum[1] = cpu_to_le32(checksum2);
  647. io->meta_offset += sizeof(struct r5l_payload_data_parity) +
  648. sizeof(__le32) * (1 + !!checksum2_valid);
  649. }
  650. static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
  651. {
  652. struct r5l_io_unit *io = log->current_io;
  653. if (io->need_split_bio) {
  654. BUG_ON(io->split_bio);
  655. io->split_bio = io->current_bio;
  656. io->current_bio = r5l_bio_alloc(log);
  657. bio_chain(io->current_bio, io->split_bio);
  658. io->need_split_bio = false;
  659. }
  660. if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
  661. BUG();
  662. r5_reserve_log_entry(log, io);
  663. }
  664. static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
  665. int data_pages, int parity_pages)
  666. {
  667. int i;
  668. int meta_size;
  669. int ret;
  670. struct r5l_io_unit *io;
  671. meta_size =
  672. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
  673. * data_pages) +
  674. sizeof(struct r5l_payload_data_parity) +
  675. sizeof(__le32) * parity_pages;
  676. ret = r5l_get_meta(log, meta_size);
  677. if (ret)
  678. return ret;
  679. io = log->current_io;
  680. if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
  681. io->has_flush = 1;
  682. for (i = 0; i < sh->disks; i++) {
  683. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  684. test_bit(R5_InJournal, &sh->dev[i].flags))
  685. continue;
  686. if (i == sh->pd_idx || i == sh->qd_idx)
  687. continue;
  688. if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
  689. log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
  690. io->has_fua = 1;
  691. /*
  692. * we need to flush journal to make sure recovery can
  693. * reach the data with fua flag
  694. */
  695. io->has_flush = 1;
  696. }
  697. r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
  698. raid5_compute_blocknr(sh, i, 0),
  699. sh->dev[i].log_checksum, 0, false);
  700. r5l_append_payload_page(log, sh->dev[i].page);
  701. }
  702. if (parity_pages == 2) {
  703. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  704. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  705. sh->dev[sh->qd_idx].log_checksum, true);
  706. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  707. r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
  708. } else if (parity_pages == 1) {
  709. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  710. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  711. 0, false);
  712. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  713. } else /* Just writing data, not parity, in caching phase */
  714. BUG_ON(parity_pages != 0);
  715. list_add_tail(&sh->log_list, &io->stripe_list);
  716. atomic_inc(&io->pending_stripe);
  717. sh->log_io = io;
  718. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  719. return 0;
  720. if (sh->log_start == MaxSector) {
  721. BUG_ON(!list_empty(&sh->r5c));
  722. sh->log_start = io->log_start;
  723. spin_lock_irq(&log->stripe_in_journal_lock);
  724. list_add_tail(&sh->r5c,
  725. &log->stripe_in_journal_list);
  726. spin_unlock_irq(&log->stripe_in_journal_lock);
  727. atomic_inc(&log->stripe_in_journal_count);
  728. }
  729. return 0;
  730. }
  731. /* add stripe to no_space_stripes, and then wake up reclaim */
  732. static inline void r5l_add_no_space_stripe(struct r5l_log *log,
  733. struct stripe_head *sh)
  734. {
  735. spin_lock(&log->no_space_stripes_lock);
  736. list_add_tail(&sh->log_list, &log->no_space_stripes);
  737. spin_unlock(&log->no_space_stripes_lock);
  738. }
  739. /*
  740. * running in raid5d, where reclaim could wait for raid5d too (when it flushes
  741. * data from log to raid disks), so we shouldn't wait for reclaim here
  742. */
  743. int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
  744. {
  745. struct r5conf *conf = sh->raid_conf;
  746. int write_disks = 0;
  747. int data_pages, parity_pages;
  748. int reserve;
  749. int i;
  750. int ret = 0;
  751. bool wake_reclaim = false;
  752. if (!log)
  753. return -EAGAIN;
  754. /* Don't support stripe batch */
  755. if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
  756. test_bit(STRIPE_SYNCING, &sh->state)) {
  757. /* the stripe is written to log, we start writing it to raid */
  758. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  759. return -EAGAIN;
  760. }
  761. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  762. for (i = 0; i < sh->disks; i++) {
  763. void *addr;
  764. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  765. test_bit(R5_InJournal, &sh->dev[i].flags))
  766. continue;
  767. write_disks++;
  768. /* checksum is already calculated in last run */
  769. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  770. continue;
  771. addr = kmap_atomic(sh->dev[i].page);
  772. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  773. addr, PAGE_SIZE);
  774. kunmap_atomic(addr);
  775. }
  776. parity_pages = 1 + !!(sh->qd_idx >= 0);
  777. data_pages = write_disks - parity_pages;
  778. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  779. /*
  780. * The stripe must enter state machine again to finish the write, so
  781. * don't delay.
  782. */
  783. clear_bit(STRIPE_DELAYED, &sh->state);
  784. atomic_inc(&sh->count);
  785. mutex_lock(&log->io_mutex);
  786. /* meta + data */
  787. reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
  788. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  789. if (!r5l_has_free_space(log, reserve)) {
  790. r5l_add_no_space_stripe(log, sh);
  791. wake_reclaim = true;
  792. } else {
  793. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  794. if (ret) {
  795. spin_lock_irq(&log->io_list_lock);
  796. list_add_tail(&sh->log_list,
  797. &log->no_mem_stripes);
  798. spin_unlock_irq(&log->io_list_lock);
  799. }
  800. }
  801. } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
  802. /*
  803. * log space critical, do not process stripes that are
  804. * not in cache yet (sh->log_start == MaxSector).
  805. */
  806. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  807. sh->log_start == MaxSector) {
  808. r5l_add_no_space_stripe(log, sh);
  809. wake_reclaim = true;
  810. reserve = 0;
  811. } else if (!r5l_has_free_space(log, reserve)) {
  812. if (sh->log_start == log->last_checkpoint)
  813. BUG();
  814. else
  815. r5l_add_no_space_stripe(log, sh);
  816. } else {
  817. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  818. if (ret) {
  819. spin_lock_irq(&log->io_list_lock);
  820. list_add_tail(&sh->log_list,
  821. &log->no_mem_stripes);
  822. spin_unlock_irq(&log->io_list_lock);
  823. }
  824. }
  825. }
  826. mutex_unlock(&log->io_mutex);
  827. if (wake_reclaim)
  828. r5l_wake_reclaim(log, reserve);
  829. return 0;
  830. }
  831. void r5l_write_stripe_run(struct r5l_log *log)
  832. {
  833. if (!log)
  834. return;
  835. mutex_lock(&log->io_mutex);
  836. r5l_submit_current_io(log);
  837. mutex_unlock(&log->io_mutex);
  838. }
  839. int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
  840. {
  841. if (!log)
  842. return -ENODEV;
  843. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  844. /*
  845. * in write through (journal only)
  846. * we flush log disk cache first, then write stripe data to
  847. * raid disks. So if bio is finished, the log disk cache is
  848. * flushed already. The recovery guarantees we can recovery
  849. * the bio from log disk, so we don't need to flush again
  850. */
  851. if (bio->bi_iter.bi_size == 0) {
  852. bio_endio(bio);
  853. return 0;
  854. }
  855. bio->bi_opf &= ~REQ_PREFLUSH;
  856. } else {
  857. /* write back (with cache) */
  858. if (bio->bi_iter.bi_size == 0) {
  859. mutex_lock(&log->io_mutex);
  860. r5l_get_meta(log, 0);
  861. bio_list_add(&log->current_io->flush_barriers, bio);
  862. log->current_io->has_flush = 1;
  863. log->current_io->has_null_flush = 1;
  864. atomic_inc(&log->current_io->pending_stripe);
  865. r5l_submit_current_io(log);
  866. mutex_unlock(&log->io_mutex);
  867. return 0;
  868. }
  869. }
  870. return -EAGAIN;
  871. }
  872. /* This will run after log space is reclaimed */
  873. static void r5l_run_no_space_stripes(struct r5l_log *log)
  874. {
  875. struct stripe_head *sh;
  876. spin_lock(&log->no_space_stripes_lock);
  877. while (!list_empty(&log->no_space_stripes)) {
  878. sh = list_first_entry(&log->no_space_stripes,
  879. struct stripe_head, log_list);
  880. list_del_init(&sh->log_list);
  881. set_bit(STRIPE_HANDLE, &sh->state);
  882. raid5_release_stripe(sh);
  883. }
  884. spin_unlock(&log->no_space_stripes_lock);
  885. }
  886. /*
  887. * calculate new last_checkpoint
  888. * for write through mode, returns log->next_checkpoint
  889. * for write back, returns log_start of first sh in stripe_in_journal_list
  890. */
  891. static sector_t r5c_calculate_new_cp(struct r5conf *conf)
  892. {
  893. struct stripe_head *sh;
  894. struct r5l_log *log = conf->log;
  895. sector_t new_cp;
  896. unsigned long flags;
  897. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  898. return log->next_checkpoint;
  899. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  900. if (list_empty(&conf->log->stripe_in_journal_list)) {
  901. /* all stripes flushed */
  902. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  903. return log->next_checkpoint;
  904. }
  905. sh = list_first_entry(&conf->log->stripe_in_journal_list,
  906. struct stripe_head, r5c);
  907. new_cp = sh->log_start;
  908. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  909. return new_cp;
  910. }
  911. static sector_t r5l_reclaimable_space(struct r5l_log *log)
  912. {
  913. struct r5conf *conf = log->rdev->mddev->private;
  914. return r5l_ring_distance(log, log->last_checkpoint,
  915. r5c_calculate_new_cp(conf));
  916. }
  917. static void r5l_run_no_mem_stripe(struct r5l_log *log)
  918. {
  919. struct stripe_head *sh;
  920. assert_spin_locked(&log->io_list_lock);
  921. if (!list_empty(&log->no_mem_stripes)) {
  922. sh = list_first_entry(&log->no_mem_stripes,
  923. struct stripe_head, log_list);
  924. list_del_init(&sh->log_list);
  925. set_bit(STRIPE_HANDLE, &sh->state);
  926. raid5_release_stripe(sh);
  927. }
  928. }
  929. static bool r5l_complete_finished_ios(struct r5l_log *log)
  930. {
  931. struct r5l_io_unit *io, *next;
  932. bool found = false;
  933. assert_spin_locked(&log->io_list_lock);
  934. list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
  935. /* don't change list order */
  936. if (io->state < IO_UNIT_STRIPE_END)
  937. break;
  938. log->next_checkpoint = io->log_start;
  939. list_del(&io->log_sibling);
  940. mempool_free(io, log->io_pool);
  941. r5l_run_no_mem_stripe(log);
  942. found = true;
  943. }
  944. return found;
  945. }
  946. static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
  947. {
  948. struct r5l_log *log = io->log;
  949. struct r5conf *conf = log->rdev->mddev->private;
  950. unsigned long flags;
  951. spin_lock_irqsave(&log->io_list_lock, flags);
  952. __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
  953. if (!r5l_complete_finished_ios(log)) {
  954. spin_unlock_irqrestore(&log->io_list_lock, flags);
  955. return;
  956. }
  957. if (r5l_reclaimable_space(log) > log->max_free_space ||
  958. test_bit(R5C_LOG_TIGHT, &conf->cache_state))
  959. r5l_wake_reclaim(log, 0);
  960. spin_unlock_irqrestore(&log->io_list_lock, flags);
  961. wake_up(&log->iounit_wait);
  962. }
  963. void r5l_stripe_write_finished(struct stripe_head *sh)
  964. {
  965. struct r5l_io_unit *io;
  966. io = sh->log_io;
  967. sh->log_io = NULL;
  968. if (io && atomic_dec_and_test(&io->pending_stripe))
  969. __r5l_stripe_write_finished(io);
  970. }
  971. static void r5l_log_flush_endio(struct bio *bio)
  972. {
  973. struct r5l_log *log = container_of(bio, struct r5l_log,
  974. flush_bio);
  975. unsigned long flags;
  976. struct r5l_io_unit *io;
  977. if (bio->bi_error)
  978. md_error(log->rdev->mddev, log->rdev);
  979. spin_lock_irqsave(&log->io_list_lock, flags);
  980. list_for_each_entry(io, &log->flushing_ios, log_sibling)
  981. r5l_io_run_stripes(io);
  982. list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
  983. spin_unlock_irqrestore(&log->io_list_lock, flags);
  984. }
  985. /*
  986. * Starting dispatch IO to raid.
  987. * io_unit(meta) consists of a log. There is one situation we want to avoid. A
  988. * broken meta in the middle of a log causes recovery can't find meta at the
  989. * head of log. If operations require meta at the head persistent in log, we
  990. * must make sure meta before it persistent in log too. A case is:
  991. *
  992. * stripe data/parity is in log, we start write stripe to raid disks. stripe
  993. * data/parity must be persistent in log before we do the write to raid disks.
  994. *
  995. * The solution is we restrictly maintain io_unit list order. In this case, we
  996. * only write stripes of an io_unit to raid disks till the io_unit is the first
  997. * one whose data/parity is in log.
  998. */
  999. void r5l_flush_stripe_to_raid(struct r5l_log *log)
  1000. {
  1001. bool do_flush;
  1002. if (!log || !log->need_cache_flush)
  1003. return;
  1004. spin_lock_irq(&log->io_list_lock);
  1005. /* flush bio is running */
  1006. if (!list_empty(&log->flushing_ios)) {
  1007. spin_unlock_irq(&log->io_list_lock);
  1008. return;
  1009. }
  1010. list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
  1011. do_flush = !list_empty(&log->flushing_ios);
  1012. spin_unlock_irq(&log->io_list_lock);
  1013. if (!do_flush)
  1014. return;
  1015. bio_reset(&log->flush_bio);
  1016. log->flush_bio.bi_bdev = log->rdev->bdev;
  1017. log->flush_bio.bi_end_io = r5l_log_flush_endio;
  1018. log->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  1019. submit_bio(&log->flush_bio);
  1020. }
  1021. static void r5l_write_super(struct r5l_log *log, sector_t cp);
  1022. static void r5l_write_super_and_discard_space(struct r5l_log *log,
  1023. sector_t end)
  1024. {
  1025. struct block_device *bdev = log->rdev->bdev;
  1026. struct mddev *mddev;
  1027. r5l_write_super(log, end);
  1028. if (!blk_queue_discard(bdev_get_queue(bdev)))
  1029. return;
  1030. mddev = log->rdev->mddev;
  1031. /*
  1032. * Discard could zero data, so before discard we must make sure
  1033. * superblock is updated to new log tail. Updating superblock (either
  1034. * directly call md_update_sb() or depend on md thread) must hold
  1035. * reconfig mutex. On the other hand, raid5_quiesce is called with
  1036. * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
  1037. * for all IO finish, hence waitting for reclaim thread, while reclaim
  1038. * thread is calling this function and waitting for reconfig mutex. So
  1039. * there is a deadlock. We workaround this issue with a trylock.
  1040. * FIXME: we could miss discard if we can't take reconfig mutex
  1041. */
  1042. set_mask_bits(&mddev->sb_flags, 0,
  1043. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  1044. if (!mddev_trylock(mddev))
  1045. return;
  1046. md_update_sb(mddev, 1);
  1047. mddev_unlock(mddev);
  1048. /* discard IO error really doesn't matter, ignore it */
  1049. if (log->last_checkpoint < end) {
  1050. blkdev_issue_discard(bdev,
  1051. log->last_checkpoint + log->rdev->data_offset,
  1052. end - log->last_checkpoint, GFP_NOIO, 0);
  1053. } else {
  1054. blkdev_issue_discard(bdev,
  1055. log->last_checkpoint + log->rdev->data_offset,
  1056. log->device_size - log->last_checkpoint,
  1057. GFP_NOIO, 0);
  1058. blkdev_issue_discard(bdev, log->rdev->data_offset, end,
  1059. GFP_NOIO, 0);
  1060. }
  1061. }
  1062. /*
  1063. * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
  1064. * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
  1065. *
  1066. * must hold conf->device_lock
  1067. */
  1068. static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
  1069. {
  1070. BUG_ON(list_empty(&sh->lru));
  1071. BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  1072. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  1073. /*
  1074. * The stripe is not ON_RELEASE_LIST, so it is safe to call
  1075. * raid5_release_stripe() while holding conf->device_lock
  1076. */
  1077. BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
  1078. assert_spin_locked(&conf->device_lock);
  1079. list_del_init(&sh->lru);
  1080. atomic_inc(&sh->count);
  1081. set_bit(STRIPE_HANDLE, &sh->state);
  1082. atomic_inc(&conf->active_stripes);
  1083. r5c_make_stripe_write_out(sh);
  1084. raid5_release_stripe(sh);
  1085. }
  1086. /*
  1087. * if num == 0, flush all full stripes
  1088. * if num > 0, flush all full stripes. If less than num full stripes are
  1089. * flushed, flush some partial stripes until totally num stripes are
  1090. * flushed or there is no more cached stripes.
  1091. */
  1092. void r5c_flush_cache(struct r5conf *conf, int num)
  1093. {
  1094. int count;
  1095. struct stripe_head *sh, *next;
  1096. assert_spin_locked(&conf->device_lock);
  1097. if (!conf->log)
  1098. return;
  1099. count = 0;
  1100. list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
  1101. r5c_flush_stripe(conf, sh);
  1102. count++;
  1103. }
  1104. if (count >= num)
  1105. return;
  1106. list_for_each_entry_safe(sh, next,
  1107. &conf->r5c_partial_stripe_list, lru) {
  1108. r5c_flush_stripe(conf, sh);
  1109. if (++count >= num)
  1110. break;
  1111. }
  1112. }
  1113. static void r5c_do_reclaim(struct r5conf *conf)
  1114. {
  1115. struct r5l_log *log = conf->log;
  1116. struct stripe_head *sh;
  1117. int count = 0;
  1118. unsigned long flags;
  1119. int total_cached;
  1120. int stripes_to_flush;
  1121. if (!r5c_is_writeback(log))
  1122. return;
  1123. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  1124. atomic_read(&conf->r5c_cached_full_stripes);
  1125. if (total_cached > conf->min_nr_stripes * 3 / 4 ||
  1126. atomic_read(&conf->empty_inactive_list_nr) > 0)
  1127. /*
  1128. * if stripe cache pressure high, flush all full stripes and
  1129. * some partial stripes
  1130. */
  1131. stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
  1132. else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  1133. atomic_read(&conf->r5c_cached_full_stripes) >
  1134. R5C_FULL_STRIPE_FLUSH_BATCH)
  1135. /*
  1136. * if stripe cache pressure moderate, or if there is many full
  1137. * stripes,flush all full stripes
  1138. */
  1139. stripes_to_flush = 0;
  1140. else
  1141. /* no need to flush */
  1142. stripes_to_flush = -1;
  1143. if (stripes_to_flush >= 0) {
  1144. spin_lock_irqsave(&conf->device_lock, flags);
  1145. r5c_flush_cache(conf, stripes_to_flush);
  1146. spin_unlock_irqrestore(&conf->device_lock, flags);
  1147. }
  1148. /* if log space is tight, flush stripes on stripe_in_journal_list */
  1149. if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
  1150. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  1151. spin_lock(&conf->device_lock);
  1152. list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
  1153. /*
  1154. * stripes on stripe_in_journal_list could be in any
  1155. * state of the stripe_cache state machine. In this
  1156. * case, we only want to flush stripe on
  1157. * r5c_cached_full/partial_stripes. The following
  1158. * condition makes sure the stripe is on one of the
  1159. * two lists.
  1160. */
  1161. if (!list_empty(&sh->lru) &&
  1162. !test_bit(STRIPE_HANDLE, &sh->state) &&
  1163. atomic_read(&sh->count) == 0) {
  1164. r5c_flush_stripe(conf, sh);
  1165. }
  1166. if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
  1167. break;
  1168. }
  1169. spin_unlock(&conf->device_lock);
  1170. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1171. }
  1172. if (!test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
  1173. r5l_run_no_space_stripes(log);
  1174. md_wakeup_thread(conf->mddev->thread);
  1175. }
  1176. static void r5l_do_reclaim(struct r5l_log *log)
  1177. {
  1178. struct r5conf *conf = log->rdev->mddev->private;
  1179. sector_t reclaim_target = xchg(&log->reclaim_target, 0);
  1180. sector_t reclaimable;
  1181. sector_t next_checkpoint;
  1182. bool write_super;
  1183. spin_lock_irq(&log->io_list_lock);
  1184. write_super = r5l_reclaimable_space(log) > log->max_free_space ||
  1185. reclaim_target != 0 || !list_empty(&log->no_space_stripes);
  1186. /*
  1187. * move proper io_unit to reclaim list. We should not change the order.
  1188. * reclaimable/unreclaimable io_unit can be mixed in the list, we
  1189. * shouldn't reuse space of an unreclaimable io_unit
  1190. */
  1191. while (1) {
  1192. reclaimable = r5l_reclaimable_space(log);
  1193. if (reclaimable >= reclaim_target ||
  1194. (list_empty(&log->running_ios) &&
  1195. list_empty(&log->io_end_ios) &&
  1196. list_empty(&log->flushing_ios) &&
  1197. list_empty(&log->finished_ios)))
  1198. break;
  1199. md_wakeup_thread(log->rdev->mddev->thread);
  1200. wait_event_lock_irq(log->iounit_wait,
  1201. r5l_reclaimable_space(log) > reclaimable,
  1202. log->io_list_lock);
  1203. }
  1204. next_checkpoint = r5c_calculate_new_cp(conf);
  1205. spin_unlock_irq(&log->io_list_lock);
  1206. if (reclaimable == 0 || !write_super)
  1207. return;
  1208. /*
  1209. * write_super will flush cache of each raid disk. We must write super
  1210. * here, because the log area might be reused soon and we don't want to
  1211. * confuse recovery
  1212. */
  1213. r5l_write_super_and_discard_space(log, next_checkpoint);
  1214. mutex_lock(&log->io_mutex);
  1215. log->last_checkpoint = next_checkpoint;
  1216. r5c_update_log_state(log);
  1217. mutex_unlock(&log->io_mutex);
  1218. r5l_run_no_space_stripes(log);
  1219. }
  1220. static void r5l_reclaim_thread(struct md_thread *thread)
  1221. {
  1222. struct mddev *mddev = thread->mddev;
  1223. struct r5conf *conf = mddev->private;
  1224. struct r5l_log *log = conf->log;
  1225. if (!log)
  1226. return;
  1227. r5c_do_reclaim(conf);
  1228. r5l_do_reclaim(log);
  1229. }
  1230. void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
  1231. {
  1232. unsigned long target;
  1233. unsigned long new = (unsigned long)space; /* overflow in theory */
  1234. if (!log)
  1235. return;
  1236. do {
  1237. target = log->reclaim_target;
  1238. if (new < target)
  1239. return;
  1240. } while (cmpxchg(&log->reclaim_target, target, new) != target);
  1241. md_wakeup_thread(log->reclaim_thread);
  1242. }
  1243. void r5l_quiesce(struct r5l_log *log, int state)
  1244. {
  1245. struct mddev *mddev;
  1246. if (!log || state == 2)
  1247. return;
  1248. if (state == 0)
  1249. kthread_unpark(log->reclaim_thread->tsk);
  1250. else if (state == 1) {
  1251. /* make sure r5l_write_super_and_discard_space exits */
  1252. mddev = log->rdev->mddev;
  1253. wake_up(&mddev->sb_wait);
  1254. kthread_park(log->reclaim_thread->tsk);
  1255. r5l_wake_reclaim(log, MaxSector);
  1256. r5l_do_reclaim(log);
  1257. }
  1258. }
  1259. bool r5l_log_disk_error(struct r5conf *conf)
  1260. {
  1261. struct r5l_log *log;
  1262. bool ret;
  1263. /* don't allow write if journal disk is missing */
  1264. rcu_read_lock();
  1265. log = rcu_dereference(conf->log);
  1266. if (!log)
  1267. ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  1268. else
  1269. ret = test_bit(Faulty, &log->rdev->flags);
  1270. rcu_read_unlock();
  1271. return ret;
  1272. }
  1273. struct r5l_recovery_ctx {
  1274. struct page *meta_page; /* current meta */
  1275. sector_t meta_total_blocks; /* total size of current meta and data */
  1276. sector_t pos; /* recovery position */
  1277. u64 seq; /* recovery position seq */
  1278. int data_parity_stripes; /* number of data_parity stripes */
  1279. int data_only_stripes; /* number of data_only stripes */
  1280. struct list_head cached_list;
  1281. };
  1282. static int r5l_recovery_read_meta_block(struct r5l_log *log,
  1283. struct r5l_recovery_ctx *ctx)
  1284. {
  1285. struct page *page = ctx->meta_page;
  1286. struct r5l_meta_block *mb;
  1287. u32 crc, stored_crc;
  1288. if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
  1289. false))
  1290. return -EIO;
  1291. mb = page_address(page);
  1292. stored_crc = le32_to_cpu(mb->checksum);
  1293. mb->checksum = 0;
  1294. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  1295. le64_to_cpu(mb->seq) != ctx->seq ||
  1296. mb->version != R5LOG_VERSION ||
  1297. le64_to_cpu(mb->position) != ctx->pos)
  1298. return -EINVAL;
  1299. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  1300. if (stored_crc != crc)
  1301. return -EINVAL;
  1302. if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
  1303. return -EINVAL;
  1304. ctx->meta_total_blocks = BLOCK_SECTORS;
  1305. return 0;
  1306. }
  1307. static void
  1308. r5l_recovery_create_empty_meta_block(struct r5l_log *log,
  1309. struct page *page,
  1310. sector_t pos, u64 seq)
  1311. {
  1312. struct r5l_meta_block *mb;
  1313. mb = page_address(page);
  1314. clear_page(mb);
  1315. mb->magic = cpu_to_le32(R5LOG_MAGIC);
  1316. mb->version = R5LOG_VERSION;
  1317. mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
  1318. mb->seq = cpu_to_le64(seq);
  1319. mb->position = cpu_to_le64(pos);
  1320. }
  1321. static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
  1322. u64 seq)
  1323. {
  1324. struct page *page;
  1325. struct r5l_meta_block *mb;
  1326. page = alloc_page(GFP_KERNEL);
  1327. if (!page)
  1328. return -ENOMEM;
  1329. r5l_recovery_create_empty_meta_block(log, page, pos, seq);
  1330. mb = page_address(page);
  1331. mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
  1332. mb, PAGE_SIZE));
  1333. if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
  1334. REQ_FUA, false)) {
  1335. __free_page(page);
  1336. return -EIO;
  1337. }
  1338. __free_page(page);
  1339. return 0;
  1340. }
  1341. /*
  1342. * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
  1343. * to mark valid (potentially not flushed) data in the journal.
  1344. *
  1345. * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
  1346. * so there should not be any mismatch here.
  1347. */
  1348. static void r5l_recovery_load_data(struct r5l_log *log,
  1349. struct stripe_head *sh,
  1350. struct r5l_recovery_ctx *ctx,
  1351. struct r5l_payload_data_parity *payload,
  1352. sector_t log_offset)
  1353. {
  1354. struct mddev *mddev = log->rdev->mddev;
  1355. struct r5conf *conf = mddev->private;
  1356. int dd_idx;
  1357. raid5_compute_sector(conf,
  1358. le64_to_cpu(payload->location), 0,
  1359. &dd_idx, sh);
  1360. sync_page_io(log->rdev, log_offset, PAGE_SIZE,
  1361. sh->dev[dd_idx].page, REQ_OP_READ, 0, false);
  1362. sh->dev[dd_idx].log_checksum =
  1363. le32_to_cpu(payload->checksum[0]);
  1364. ctx->meta_total_blocks += BLOCK_SECTORS;
  1365. set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
  1366. set_bit(STRIPE_R5C_CACHING, &sh->state);
  1367. }
  1368. static void r5l_recovery_load_parity(struct r5l_log *log,
  1369. struct stripe_head *sh,
  1370. struct r5l_recovery_ctx *ctx,
  1371. struct r5l_payload_data_parity *payload,
  1372. sector_t log_offset)
  1373. {
  1374. struct mddev *mddev = log->rdev->mddev;
  1375. struct r5conf *conf = mddev->private;
  1376. ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
  1377. sync_page_io(log->rdev, log_offset, PAGE_SIZE,
  1378. sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false);
  1379. sh->dev[sh->pd_idx].log_checksum =
  1380. le32_to_cpu(payload->checksum[0]);
  1381. set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
  1382. if (sh->qd_idx >= 0) {
  1383. sync_page_io(log->rdev,
  1384. r5l_ring_add(log, log_offset, BLOCK_SECTORS),
  1385. PAGE_SIZE, sh->dev[sh->qd_idx].page,
  1386. REQ_OP_READ, 0, false);
  1387. sh->dev[sh->qd_idx].log_checksum =
  1388. le32_to_cpu(payload->checksum[1]);
  1389. set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
  1390. }
  1391. clear_bit(STRIPE_R5C_CACHING, &sh->state);
  1392. }
  1393. static void r5l_recovery_reset_stripe(struct stripe_head *sh)
  1394. {
  1395. int i;
  1396. sh->state = 0;
  1397. sh->log_start = MaxSector;
  1398. for (i = sh->disks; i--; )
  1399. sh->dev[i].flags = 0;
  1400. }
  1401. static void
  1402. r5l_recovery_replay_one_stripe(struct r5conf *conf,
  1403. struct stripe_head *sh,
  1404. struct r5l_recovery_ctx *ctx)
  1405. {
  1406. struct md_rdev *rdev, *rrdev;
  1407. int disk_index;
  1408. int data_count = 0;
  1409. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1410. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1411. continue;
  1412. if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
  1413. continue;
  1414. data_count++;
  1415. }
  1416. /*
  1417. * stripes that only have parity must have been flushed
  1418. * before the crash that we are now recovering from, so
  1419. * there is nothing more to recovery.
  1420. */
  1421. if (data_count == 0)
  1422. goto out;
  1423. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1424. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1425. continue;
  1426. /* in case device is broken */
  1427. rcu_read_lock();
  1428. rdev = rcu_dereference(conf->disks[disk_index].rdev);
  1429. if (rdev) {
  1430. atomic_inc(&rdev->nr_pending);
  1431. rcu_read_unlock();
  1432. sync_page_io(rdev, sh->sector, PAGE_SIZE,
  1433. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1434. false);
  1435. rdev_dec_pending(rdev, rdev->mddev);
  1436. rcu_read_lock();
  1437. }
  1438. rrdev = rcu_dereference(conf->disks[disk_index].replacement);
  1439. if (rrdev) {
  1440. atomic_inc(&rrdev->nr_pending);
  1441. rcu_read_unlock();
  1442. sync_page_io(rrdev, sh->sector, PAGE_SIZE,
  1443. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1444. false);
  1445. rdev_dec_pending(rrdev, rrdev->mddev);
  1446. rcu_read_lock();
  1447. }
  1448. rcu_read_unlock();
  1449. }
  1450. ctx->data_parity_stripes++;
  1451. out:
  1452. r5l_recovery_reset_stripe(sh);
  1453. }
  1454. static struct stripe_head *
  1455. r5c_recovery_alloc_stripe(struct r5conf *conf,
  1456. sector_t stripe_sect)
  1457. {
  1458. struct stripe_head *sh;
  1459. sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
  1460. if (!sh)
  1461. return NULL; /* no more stripe available */
  1462. r5l_recovery_reset_stripe(sh);
  1463. return sh;
  1464. }
  1465. static struct stripe_head *
  1466. r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
  1467. {
  1468. struct stripe_head *sh;
  1469. list_for_each_entry(sh, list, lru)
  1470. if (sh->sector == sect)
  1471. return sh;
  1472. return NULL;
  1473. }
  1474. static void
  1475. r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
  1476. struct r5l_recovery_ctx *ctx)
  1477. {
  1478. struct stripe_head *sh, *next;
  1479. list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
  1480. r5l_recovery_reset_stripe(sh);
  1481. list_del_init(&sh->lru);
  1482. raid5_release_stripe(sh);
  1483. }
  1484. }
  1485. static void
  1486. r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
  1487. struct r5l_recovery_ctx *ctx)
  1488. {
  1489. struct stripe_head *sh, *next;
  1490. list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
  1491. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  1492. r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
  1493. list_del_init(&sh->lru);
  1494. raid5_release_stripe(sh);
  1495. }
  1496. }
  1497. /* if matches return 0; otherwise return -EINVAL */
  1498. static int
  1499. r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page,
  1500. sector_t log_offset, __le32 log_checksum)
  1501. {
  1502. void *addr;
  1503. u32 checksum;
  1504. sync_page_io(log->rdev, log_offset, PAGE_SIZE,
  1505. page, REQ_OP_READ, 0, false);
  1506. addr = kmap_atomic(page);
  1507. checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
  1508. kunmap_atomic(addr);
  1509. return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
  1510. }
  1511. /*
  1512. * before loading data to stripe cache, we need verify checksum for all data,
  1513. * if there is mismatch for any data page, we drop all data in the mata block
  1514. */
  1515. static int
  1516. r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
  1517. struct r5l_recovery_ctx *ctx)
  1518. {
  1519. struct mddev *mddev = log->rdev->mddev;
  1520. struct r5conf *conf = mddev->private;
  1521. struct r5l_meta_block *mb = page_address(ctx->meta_page);
  1522. sector_t mb_offset = sizeof(struct r5l_meta_block);
  1523. sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1524. struct page *page;
  1525. struct r5l_payload_data_parity *payload;
  1526. page = alloc_page(GFP_KERNEL);
  1527. if (!page)
  1528. return -ENOMEM;
  1529. while (mb_offset < le32_to_cpu(mb->meta_size)) {
  1530. payload = (void *)mb + mb_offset;
  1531. if (payload->header.type == R5LOG_PAYLOAD_DATA) {
  1532. if (r5l_recovery_verify_data_checksum(
  1533. log, page, log_offset,
  1534. payload->checksum[0]) < 0)
  1535. goto mismatch;
  1536. } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) {
  1537. if (r5l_recovery_verify_data_checksum(
  1538. log, page, log_offset,
  1539. payload->checksum[0]) < 0)
  1540. goto mismatch;
  1541. if (conf->max_degraded == 2 && /* q for RAID 6 */
  1542. r5l_recovery_verify_data_checksum(
  1543. log, page,
  1544. r5l_ring_add(log, log_offset,
  1545. BLOCK_SECTORS),
  1546. payload->checksum[1]) < 0)
  1547. goto mismatch;
  1548. } else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */
  1549. goto mismatch;
  1550. log_offset = r5l_ring_add(log, log_offset,
  1551. le32_to_cpu(payload->size));
  1552. mb_offset += sizeof(struct r5l_payload_data_parity) +
  1553. sizeof(__le32) *
  1554. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  1555. }
  1556. put_page(page);
  1557. return 0;
  1558. mismatch:
  1559. put_page(page);
  1560. return -EINVAL;
  1561. }
  1562. /*
  1563. * Analyze all data/parity pages in one meta block
  1564. * Returns:
  1565. * 0 for success
  1566. * -EINVAL for unknown playload type
  1567. * -EAGAIN for checksum mismatch of data page
  1568. * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
  1569. */
  1570. static int
  1571. r5c_recovery_analyze_meta_block(struct r5l_log *log,
  1572. struct r5l_recovery_ctx *ctx,
  1573. struct list_head *cached_stripe_list)
  1574. {
  1575. struct mddev *mddev = log->rdev->mddev;
  1576. struct r5conf *conf = mddev->private;
  1577. struct r5l_meta_block *mb;
  1578. struct r5l_payload_data_parity *payload;
  1579. int mb_offset;
  1580. sector_t log_offset;
  1581. sector_t stripe_sect;
  1582. struct stripe_head *sh;
  1583. int ret;
  1584. /*
  1585. * for mismatch in data blocks, we will drop all data in this mb, but
  1586. * we will still read next mb for other data with FLUSH flag, as
  1587. * io_unit could finish out of order.
  1588. */
  1589. ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
  1590. if (ret == -EINVAL)
  1591. return -EAGAIN;
  1592. else if (ret)
  1593. return ret; /* -ENOMEM duo to alloc_page() failed */
  1594. mb = page_address(ctx->meta_page);
  1595. mb_offset = sizeof(struct r5l_meta_block);
  1596. log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1597. while (mb_offset < le32_to_cpu(mb->meta_size)) {
  1598. int dd;
  1599. payload = (void *)mb + mb_offset;
  1600. stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ?
  1601. raid5_compute_sector(
  1602. conf, le64_to_cpu(payload->location), 0, &dd,
  1603. NULL)
  1604. : le64_to_cpu(payload->location);
  1605. sh = r5c_recovery_lookup_stripe(cached_stripe_list,
  1606. stripe_sect);
  1607. if (!sh) {
  1608. sh = r5c_recovery_alloc_stripe(conf, stripe_sect);
  1609. /*
  1610. * cannot get stripe from raid5_get_active_stripe
  1611. * try replay some stripes
  1612. */
  1613. if (!sh) {
  1614. r5c_recovery_replay_stripes(
  1615. cached_stripe_list, ctx);
  1616. sh = r5c_recovery_alloc_stripe(
  1617. conf, stripe_sect);
  1618. }
  1619. if (!sh) {
  1620. pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
  1621. mdname(mddev),
  1622. conf->min_nr_stripes * 2);
  1623. raid5_set_cache_size(mddev,
  1624. conf->min_nr_stripes * 2);
  1625. sh = r5c_recovery_alloc_stripe(conf,
  1626. stripe_sect);
  1627. }
  1628. if (!sh) {
  1629. pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
  1630. mdname(mddev));
  1631. return -ENOMEM;
  1632. }
  1633. list_add_tail(&sh->lru, cached_stripe_list);
  1634. }
  1635. if (payload->header.type == R5LOG_PAYLOAD_DATA) {
  1636. if (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
  1637. test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) {
  1638. r5l_recovery_replay_one_stripe(conf, sh, ctx);
  1639. list_move_tail(&sh->lru, cached_stripe_list);
  1640. }
  1641. r5l_recovery_load_data(log, sh, ctx, payload,
  1642. log_offset);
  1643. } else if (payload->header.type == R5LOG_PAYLOAD_PARITY)
  1644. r5l_recovery_load_parity(log, sh, ctx, payload,
  1645. log_offset);
  1646. else
  1647. return -EINVAL;
  1648. log_offset = r5l_ring_add(log, log_offset,
  1649. le32_to_cpu(payload->size));
  1650. mb_offset += sizeof(struct r5l_payload_data_parity) +
  1651. sizeof(__le32) *
  1652. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  1653. }
  1654. return 0;
  1655. }
  1656. /*
  1657. * Load the stripe into cache. The stripe will be written out later by
  1658. * the stripe cache state machine.
  1659. */
  1660. static void r5c_recovery_load_one_stripe(struct r5l_log *log,
  1661. struct stripe_head *sh)
  1662. {
  1663. struct r5dev *dev;
  1664. int i;
  1665. for (i = sh->disks; i--; ) {
  1666. dev = sh->dev + i;
  1667. if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
  1668. set_bit(R5_InJournal, &dev->flags);
  1669. set_bit(R5_UPTODATE, &dev->flags);
  1670. }
  1671. }
  1672. }
  1673. /*
  1674. * Scan through the log for all to-be-flushed data
  1675. *
  1676. * For stripes with data and parity, namely Data-Parity stripe
  1677. * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
  1678. *
  1679. * For stripes with only data, namely Data-Only stripe
  1680. * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
  1681. *
  1682. * For a stripe, if we see data after parity, we should discard all previous
  1683. * data and parity for this stripe, as these data are already flushed to
  1684. * the array.
  1685. *
  1686. * At the end of the scan, we return the new journal_tail, which points to
  1687. * first data-only stripe on the journal device, or next invalid meta block.
  1688. */
  1689. static int r5c_recovery_flush_log(struct r5l_log *log,
  1690. struct r5l_recovery_ctx *ctx)
  1691. {
  1692. struct stripe_head *sh;
  1693. int ret = 0;
  1694. /* scan through the log */
  1695. while (1) {
  1696. if (r5l_recovery_read_meta_block(log, ctx))
  1697. break;
  1698. ret = r5c_recovery_analyze_meta_block(log, ctx,
  1699. &ctx->cached_list);
  1700. /*
  1701. * -EAGAIN means mismatch in data block, in this case, we still
  1702. * try scan the next metablock
  1703. */
  1704. if (ret && ret != -EAGAIN)
  1705. break; /* ret == -EINVAL or -ENOMEM */
  1706. ctx->seq++;
  1707. ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
  1708. }
  1709. if (ret == -ENOMEM) {
  1710. r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
  1711. return ret;
  1712. }
  1713. /* replay data-parity stripes */
  1714. r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
  1715. /* load data-only stripes to stripe cache */
  1716. list_for_each_entry(sh, &ctx->cached_list, lru) {
  1717. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  1718. r5c_recovery_load_one_stripe(log, sh);
  1719. ctx->data_only_stripes++;
  1720. }
  1721. return 0;
  1722. }
  1723. /*
  1724. * we did a recovery. Now ctx.pos points to an invalid meta block. New
  1725. * log will start here. but we can't let superblock point to last valid
  1726. * meta block. The log might looks like:
  1727. * | meta 1| meta 2| meta 3|
  1728. * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
  1729. * superblock points to meta 1, we write a new valid meta 2n. if crash
  1730. * happens again, new recovery will start from meta 1. Since meta 2n is
  1731. * valid now, recovery will think meta 3 is valid, which is wrong.
  1732. * The solution is we create a new meta in meta2 with its seq == meta
  1733. * 1's seq + 10000 and let superblock points to meta2. The same recovery
  1734. * will not think meta 3 is a valid meta, because its seq doesn't match
  1735. */
  1736. /*
  1737. * Before recovery, the log looks like the following
  1738. *
  1739. * ---------------------------------------------
  1740. * | valid log | invalid log |
  1741. * ---------------------------------------------
  1742. * ^
  1743. * |- log->last_checkpoint
  1744. * |- log->last_cp_seq
  1745. *
  1746. * Now we scan through the log until we see invalid entry
  1747. *
  1748. * ---------------------------------------------
  1749. * | valid log | invalid log |
  1750. * ---------------------------------------------
  1751. * ^ ^
  1752. * |- log->last_checkpoint |- ctx->pos
  1753. * |- log->last_cp_seq |- ctx->seq
  1754. *
  1755. * From this point, we need to increase seq number by 10 to avoid
  1756. * confusing next recovery.
  1757. *
  1758. * ---------------------------------------------
  1759. * | valid log | invalid log |
  1760. * ---------------------------------------------
  1761. * ^ ^
  1762. * |- log->last_checkpoint |- ctx->pos+1
  1763. * |- log->last_cp_seq |- ctx->seq+10001
  1764. *
  1765. * However, it is not safe to start the state machine yet, because data only
  1766. * parities are not yet secured in RAID. To save these data only parities, we
  1767. * rewrite them from seq+11.
  1768. *
  1769. * -----------------------------------------------------------------
  1770. * | valid log | data only stripes | invalid log |
  1771. * -----------------------------------------------------------------
  1772. * ^ ^
  1773. * |- log->last_checkpoint |- ctx->pos+n
  1774. * |- log->last_cp_seq |- ctx->seq+10000+n
  1775. *
  1776. * If failure happens again during this process, the recovery can safe start
  1777. * again from log->last_checkpoint.
  1778. *
  1779. * Once data only stripes are rewritten to journal, we move log_tail
  1780. *
  1781. * -----------------------------------------------------------------
  1782. * | old log | data only stripes | invalid log |
  1783. * -----------------------------------------------------------------
  1784. * ^ ^
  1785. * |- log->last_checkpoint |- ctx->pos+n
  1786. * |- log->last_cp_seq |- ctx->seq+10000+n
  1787. *
  1788. * Then we can safely start the state machine. If failure happens from this
  1789. * point on, the recovery will start from new log->last_checkpoint.
  1790. */
  1791. static int
  1792. r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
  1793. struct r5l_recovery_ctx *ctx)
  1794. {
  1795. struct stripe_head *sh;
  1796. struct mddev *mddev = log->rdev->mddev;
  1797. struct page *page;
  1798. sector_t next_checkpoint = MaxSector;
  1799. page = alloc_page(GFP_KERNEL);
  1800. if (!page) {
  1801. pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
  1802. mdname(mddev));
  1803. return -ENOMEM;
  1804. }
  1805. WARN_ON(list_empty(&ctx->cached_list));
  1806. list_for_each_entry(sh, &ctx->cached_list, lru) {
  1807. struct r5l_meta_block *mb;
  1808. int i;
  1809. int offset;
  1810. sector_t write_pos;
  1811. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  1812. r5l_recovery_create_empty_meta_block(log, page,
  1813. ctx->pos, ctx->seq);
  1814. mb = page_address(page);
  1815. offset = le32_to_cpu(mb->meta_size);
  1816. write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1817. for (i = sh->disks; i--; ) {
  1818. struct r5dev *dev = &sh->dev[i];
  1819. struct r5l_payload_data_parity *payload;
  1820. void *addr;
  1821. if (test_bit(R5_InJournal, &dev->flags)) {
  1822. payload = (void *)mb + offset;
  1823. payload->header.type = cpu_to_le16(
  1824. R5LOG_PAYLOAD_DATA);
  1825. payload->size = BLOCK_SECTORS;
  1826. payload->location = cpu_to_le64(
  1827. raid5_compute_blocknr(sh, i, 0));
  1828. addr = kmap_atomic(dev->page);
  1829. payload->checksum[0] = cpu_to_le32(
  1830. crc32c_le(log->uuid_checksum, addr,
  1831. PAGE_SIZE));
  1832. kunmap_atomic(addr);
  1833. sync_page_io(log->rdev, write_pos, PAGE_SIZE,
  1834. dev->page, REQ_OP_WRITE, 0, false);
  1835. write_pos = r5l_ring_add(log, write_pos,
  1836. BLOCK_SECTORS);
  1837. offset += sizeof(__le32) +
  1838. sizeof(struct r5l_payload_data_parity);
  1839. }
  1840. }
  1841. mb->meta_size = cpu_to_le32(offset);
  1842. mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
  1843. mb, PAGE_SIZE));
  1844. sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
  1845. REQ_OP_WRITE, REQ_FUA, false);
  1846. sh->log_start = ctx->pos;
  1847. list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
  1848. atomic_inc(&log->stripe_in_journal_count);
  1849. ctx->pos = write_pos;
  1850. ctx->seq += 1;
  1851. next_checkpoint = sh->log_start;
  1852. }
  1853. log->next_checkpoint = next_checkpoint;
  1854. __free_page(page);
  1855. return 0;
  1856. }
  1857. static void r5c_recovery_flush_data_only_stripes(struct r5l_log *log,
  1858. struct r5l_recovery_ctx *ctx)
  1859. {
  1860. struct mddev *mddev = log->rdev->mddev;
  1861. struct r5conf *conf = mddev->private;
  1862. struct stripe_head *sh, *next;
  1863. if (ctx->data_only_stripes == 0)
  1864. return;
  1865. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_BACK;
  1866. list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
  1867. r5c_make_stripe_write_out(sh);
  1868. set_bit(STRIPE_HANDLE, &sh->state);
  1869. list_del_init(&sh->lru);
  1870. raid5_release_stripe(sh);
  1871. }
  1872. md_wakeup_thread(conf->mddev->thread);
  1873. /* reuse conf->wait_for_quiescent in recovery */
  1874. wait_event(conf->wait_for_quiescent,
  1875. atomic_read(&conf->active_stripes) == 0);
  1876. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  1877. }
  1878. static int r5l_recovery_log(struct r5l_log *log)
  1879. {
  1880. struct mddev *mddev = log->rdev->mddev;
  1881. struct r5l_recovery_ctx ctx;
  1882. int ret;
  1883. sector_t pos;
  1884. ctx.pos = log->last_checkpoint;
  1885. ctx.seq = log->last_cp_seq;
  1886. ctx.meta_page = alloc_page(GFP_KERNEL);
  1887. ctx.data_only_stripes = 0;
  1888. ctx.data_parity_stripes = 0;
  1889. INIT_LIST_HEAD(&ctx.cached_list);
  1890. if (!ctx.meta_page)
  1891. return -ENOMEM;
  1892. ret = r5c_recovery_flush_log(log, &ctx);
  1893. __free_page(ctx.meta_page);
  1894. if (ret)
  1895. return ret;
  1896. pos = ctx.pos;
  1897. ctx.seq += 10000;
  1898. if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
  1899. pr_debug("md/raid:%s: starting from clean shutdown\n",
  1900. mdname(mddev));
  1901. else
  1902. pr_debug("md/raid:%s: recovering %d data-only stripes and %d data-parity stripes\n",
  1903. mdname(mddev), ctx.data_only_stripes,
  1904. ctx.data_parity_stripes);
  1905. if (ctx.data_only_stripes == 0) {
  1906. log->next_checkpoint = ctx.pos;
  1907. r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq++);
  1908. ctx.pos = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
  1909. } else if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
  1910. pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
  1911. mdname(mddev));
  1912. return -EIO;
  1913. }
  1914. log->log_start = ctx.pos;
  1915. log->seq = ctx.seq;
  1916. log->last_checkpoint = pos;
  1917. r5l_write_super(log, pos);
  1918. r5c_recovery_flush_data_only_stripes(log, &ctx);
  1919. return 0;
  1920. }
  1921. static void r5l_write_super(struct r5l_log *log, sector_t cp)
  1922. {
  1923. struct mddev *mddev = log->rdev->mddev;
  1924. log->rdev->journal_tail = cp;
  1925. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  1926. }
  1927. static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
  1928. {
  1929. struct r5conf *conf = mddev->private;
  1930. int ret;
  1931. if (!conf->log)
  1932. return 0;
  1933. switch (conf->log->r5c_journal_mode) {
  1934. case R5C_JOURNAL_MODE_WRITE_THROUGH:
  1935. ret = snprintf(
  1936. page, PAGE_SIZE, "[%s] %s\n",
  1937. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
  1938. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
  1939. break;
  1940. case R5C_JOURNAL_MODE_WRITE_BACK:
  1941. ret = snprintf(
  1942. page, PAGE_SIZE, "%s [%s]\n",
  1943. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
  1944. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
  1945. break;
  1946. default:
  1947. ret = 0;
  1948. }
  1949. return ret;
  1950. }
  1951. static ssize_t r5c_journal_mode_store(struct mddev *mddev,
  1952. const char *page, size_t length)
  1953. {
  1954. struct r5conf *conf = mddev->private;
  1955. struct r5l_log *log = conf->log;
  1956. int val = -1, i;
  1957. int len = length;
  1958. if (!log)
  1959. return -ENODEV;
  1960. if (len && page[len - 1] == '\n')
  1961. len -= 1;
  1962. for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
  1963. if (strlen(r5c_journal_mode_str[i]) == len &&
  1964. strncmp(page, r5c_journal_mode_str[i], len) == 0) {
  1965. val = i;
  1966. break;
  1967. }
  1968. if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
  1969. val > R5C_JOURNAL_MODE_WRITE_BACK)
  1970. return -EINVAL;
  1971. if (raid5_calc_degraded(conf) > 0 &&
  1972. val == R5C_JOURNAL_MODE_WRITE_BACK)
  1973. return -EINVAL;
  1974. mddev_suspend(mddev);
  1975. conf->log->r5c_journal_mode = val;
  1976. mddev_resume(mddev);
  1977. pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
  1978. mdname(mddev), val, r5c_journal_mode_str[val]);
  1979. return length;
  1980. }
  1981. struct md_sysfs_entry
  1982. r5c_journal_mode = __ATTR(journal_mode, 0644,
  1983. r5c_journal_mode_show, r5c_journal_mode_store);
  1984. /*
  1985. * Try handle write operation in caching phase. This function should only
  1986. * be called in write-back mode.
  1987. *
  1988. * If all outstanding writes can be handled in caching phase, returns 0
  1989. * If writes requires write-out phase, call r5c_make_stripe_write_out()
  1990. * and returns -EAGAIN
  1991. */
  1992. int r5c_try_caching_write(struct r5conf *conf,
  1993. struct stripe_head *sh,
  1994. struct stripe_head_state *s,
  1995. int disks)
  1996. {
  1997. struct r5l_log *log = conf->log;
  1998. int i;
  1999. struct r5dev *dev;
  2000. int to_cache = 0;
  2001. BUG_ON(!r5c_is_writeback(log));
  2002. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  2003. /*
  2004. * There are two different scenarios here:
  2005. * 1. The stripe has some data cached, and it is sent to
  2006. * write-out phase for reclaim
  2007. * 2. The stripe is clean, and this is the first write
  2008. *
  2009. * For 1, return -EAGAIN, so we continue with
  2010. * handle_stripe_dirtying().
  2011. *
  2012. * For 2, set STRIPE_R5C_CACHING and continue with caching
  2013. * write.
  2014. */
  2015. /* case 1: anything injournal or anything in written */
  2016. if (s->injournal > 0 || s->written > 0)
  2017. return -EAGAIN;
  2018. /* case 2 */
  2019. set_bit(STRIPE_R5C_CACHING, &sh->state);
  2020. }
  2021. /*
  2022. * When run in degraded mode, array is set to write-through mode.
  2023. * This check helps drain pending write safely in the transition to
  2024. * write-through mode.
  2025. */
  2026. if (s->failed) {
  2027. r5c_make_stripe_write_out(sh);
  2028. return -EAGAIN;
  2029. }
  2030. for (i = disks; i--; ) {
  2031. dev = &sh->dev[i];
  2032. /* if non-overwrite, use writing-out phase */
  2033. if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
  2034. !test_bit(R5_InJournal, &dev->flags)) {
  2035. r5c_make_stripe_write_out(sh);
  2036. return -EAGAIN;
  2037. }
  2038. }
  2039. for (i = disks; i--; ) {
  2040. dev = &sh->dev[i];
  2041. if (dev->towrite) {
  2042. set_bit(R5_Wantwrite, &dev->flags);
  2043. set_bit(R5_Wantdrain, &dev->flags);
  2044. set_bit(R5_LOCKED, &dev->flags);
  2045. to_cache++;
  2046. }
  2047. }
  2048. if (to_cache) {
  2049. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2050. /*
  2051. * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
  2052. * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
  2053. * r5c_handle_data_cached()
  2054. */
  2055. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  2056. }
  2057. return 0;
  2058. }
  2059. /*
  2060. * free extra pages (orig_page) we allocated for prexor
  2061. */
  2062. void r5c_release_extra_page(struct stripe_head *sh)
  2063. {
  2064. struct r5conf *conf = sh->raid_conf;
  2065. int i;
  2066. bool using_disk_info_extra_page;
  2067. using_disk_info_extra_page =
  2068. sh->dev[0].orig_page == conf->disks[0].extra_page;
  2069. for (i = sh->disks; i--; )
  2070. if (sh->dev[i].page != sh->dev[i].orig_page) {
  2071. struct page *p = sh->dev[i].orig_page;
  2072. sh->dev[i].orig_page = sh->dev[i].page;
  2073. clear_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
  2074. if (!using_disk_info_extra_page)
  2075. put_page(p);
  2076. }
  2077. if (using_disk_info_extra_page) {
  2078. clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
  2079. md_wakeup_thread(conf->mddev->thread);
  2080. }
  2081. }
  2082. void r5c_use_extra_page(struct stripe_head *sh)
  2083. {
  2084. struct r5conf *conf = sh->raid_conf;
  2085. int i;
  2086. struct r5dev *dev;
  2087. for (i = sh->disks; i--; ) {
  2088. dev = &sh->dev[i];
  2089. if (dev->orig_page != dev->page)
  2090. put_page(dev->orig_page);
  2091. dev->orig_page = conf->disks[i].extra_page;
  2092. }
  2093. }
  2094. /*
  2095. * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
  2096. * stripe is committed to RAID disks.
  2097. */
  2098. void r5c_finish_stripe_write_out(struct r5conf *conf,
  2099. struct stripe_head *sh,
  2100. struct stripe_head_state *s)
  2101. {
  2102. int i;
  2103. int do_wakeup = 0;
  2104. if (!conf->log ||
  2105. !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
  2106. return;
  2107. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  2108. clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  2109. if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  2110. return;
  2111. for (i = sh->disks; i--; ) {
  2112. clear_bit(R5_InJournal, &sh->dev[i].flags);
  2113. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2114. do_wakeup = 1;
  2115. }
  2116. /*
  2117. * analyse_stripe() runs before r5c_finish_stripe_write_out(),
  2118. * We updated R5_InJournal, so we also update s->injournal.
  2119. */
  2120. s->injournal = 0;
  2121. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2122. if (atomic_dec_and_test(&conf->pending_full_writes))
  2123. md_wakeup_thread(conf->mddev->thread);
  2124. if (do_wakeup)
  2125. wake_up(&conf->wait_for_overlap);
  2126. spin_lock_irq(&conf->log->stripe_in_journal_lock);
  2127. list_del_init(&sh->r5c);
  2128. spin_unlock_irq(&conf->log->stripe_in_journal_lock);
  2129. sh->log_start = MaxSector;
  2130. atomic_dec(&conf->log->stripe_in_journal_count);
  2131. r5c_update_log_state(conf->log);
  2132. }
  2133. int
  2134. r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
  2135. struct stripe_head_state *s)
  2136. {
  2137. struct r5conf *conf = sh->raid_conf;
  2138. int pages = 0;
  2139. int reserve;
  2140. int i;
  2141. int ret = 0;
  2142. BUG_ON(!log);
  2143. for (i = 0; i < sh->disks; i++) {
  2144. void *addr;
  2145. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
  2146. continue;
  2147. addr = kmap_atomic(sh->dev[i].page);
  2148. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  2149. addr, PAGE_SIZE);
  2150. kunmap_atomic(addr);
  2151. pages++;
  2152. }
  2153. WARN_ON(pages == 0);
  2154. /*
  2155. * The stripe must enter state machine again to call endio, so
  2156. * don't delay.
  2157. */
  2158. clear_bit(STRIPE_DELAYED, &sh->state);
  2159. atomic_inc(&sh->count);
  2160. mutex_lock(&log->io_mutex);
  2161. /* meta + data */
  2162. reserve = (1 + pages) << (PAGE_SHIFT - 9);
  2163. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  2164. sh->log_start == MaxSector)
  2165. r5l_add_no_space_stripe(log, sh);
  2166. else if (!r5l_has_free_space(log, reserve)) {
  2167. if (sh->log_start == log->last_checkpoint)
  2168. BUG();
  2169. else
  2170. r5l_add_no_space_stripe(log, sh);
  2171. } else {
  2172. ret = r5l_log_stripe(log, sh, pages, 0);
  2173. if (ret) {
  2174. spin_lock_irq(&log->io_list_lock);
  2175. list_add_tail(&sh->log_list, &log->no_mem_stripes);
  2176. spin_unlock_irq(&log->io_list_lock);
  2177. }
  2178. }
  2179. mutex_unlock(&log->io_mutex);
  2180. return 0;
  2181. }
  2182. static int r5l_load_log(struct r5l_log *log)
  2183. {
  2184. struct md_rdev *rdev = log->rdev;
  2185. struct page *page;
  2186. struct r5l_meta_block *mb;
  2187. sector_t cp = log->rdev->journal_tail;
  2188. u32 stored_crc, expected_crc;
  2189. bool create_super = false;
  2190. int ret = 0;
  2191. /* Make sure it's valid */
  2192. if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
  2193. cp = 0;
  2194. page = alloc_page(GFP_KERNEL);
  2195. if (!page)
  2196. return -ENOMEM;
  2197. if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
  2198. ret = -EIO;
  2199. goto ioerr;
  2200. }
  2201. mb = page_address(page);
  2202. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  2203. mb->version != R5LOG_VERSION) {
  2204. create_super = true;
  2205. goto create;
  2206. }
  2207. stored_crc = le32_to_cpu(mb->checksum);
  2208. mb->checksum = 0;
  2209. expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  2210. if (stored_crc != expected_crc) {
  2211. create_super = true;
  2212. goto create;
  2213. }
  2214. if (le64_to_cpu(mb->position) != cp) {
  2215. create_super = true;
  2216. goto create;
  2217. }
  2218. create:
  2219. if (create_super) {
  2220. log->last_cp_seq = prandom_u32();
  2221. cp = 0;
  2222. r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
  2223. /*
  2224. * Make sure super points to correct address. Log might have
  2225. * data very soon. If super hasn't correct log tail address,
  2226. * recovery can't find the log
  2227. */
  2228. r5l_write_super(log, cp);
  2229. } else
  2230. log->last_cp_seq = le64_to_cpu(mb->seq);
  2231. log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
  2232. log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
  2233. if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
  2234. log->max_free_space = RECLAIM_MAX_FREE_SPACE;
  2235. log->last_checkpoint = cp;
  2236. __free_page(page);
  2237. if (create_super) {
  2238. log->log_start = r5l_ring_add(log, cp, BLOCK_SECTORS);
  2239. log->seq = log->last_cp_seq + 1;
  2240. log->next_checkpoint = cp;
  2241. } else
  2242. ret = r5l_recovery_log(log);
  2243. r5c_update_log_state(log);
  2244. return ret;
  2245. ioerr:
  2246. __free_page(page);
  2247. return ret;
  2248. }
  2249. void r5c_update_on_rdev_error(struct mddev *mddev)
  2250. {
  2251. struct r5conf *conf = mddev->private;
  2252. struct r5l_log *log = conf->log;
  2253. if (!log)
  2254. return;
  2255. if (raid5_calc_degraded(conf) > 0 &&
  2256. conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK)
  2257. schedule_work(&log->disable_writeback_work);
  2258. }
  2259. int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
  2260. {
  2261. struct request_queue *q = bdev_get_queue(rdev->bdev);
  2262. struct r5l_log *log;
  2263. if (PAGE_SIZE != 4096)
  2264. return -EINVAL;
  2265. /*
  2266. * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
  2267. * raid_disks r5l_payload_data_parity.
  2268. *
  2269. * Write journal and cache does not work for very big array
  2270. * (raid_disks > 203)
  2271. */
  2272. if (sizeof(struct r5l_meta_block) +
  2273. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
  2274. conf->raid_disks) > PAGE_SIZE) {
  2275. pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
  2276. mdname(conf->mddev), conf->raid_disks);
  2277. return -EINVAL;
  2278. }
  2279. log = kzalloc(sizeof(*log), GFP_KERNEL);
  2280. if (!log)
  2281. return -ENOMEM;
  2282. log->rdev = rdev;
  2283. log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
  2284. log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
  2285. sizeof(rdev->mddev->uuid));
  2286. mutex_init(&log->io_mutex);
  2287. spin_lock_init(&log->io_list_lock);
  2288. INIT_LIST_HEAD(&log->running_ios);
  2289. INIT_LIST_HEAD(&log->io_end_ios);
  2290. INIT_LIST_HEAD(&log->flushing_ios);
  2291. INIT_LIST_HEAD(&log->finished_ios);
  2292. bio_init(&log->flush_bio, NULL, 0);
  2293. log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
  2294. if (!log->io_kc)
  2295. goto io_kc;
  2296. log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
  2297. if (!log->io_pool)
  2298. goto io_pool;
  2299. log->bs = bioset_create(R5L_POOL_SIZE, 0);
  2300. if (!log->bs)
  2301. goto io_bs;
  2302. log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
  2303. if (!log->meta_pool)
  2304. goto out_mempool;
  2305. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  2306. log->rdev->mddev, "reclaim");
  2307. if (!log->reclaim_thread)
  2308. goto reclaim_thread;
  2309. log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
  2310. init_waitqueue_head(&log->iounit_wait);
  2311. INIT_LIST_HEAD(&log->no_mem_stripes);
  2312. INIT_LIST_HEAD(&log->no_space_stripes);
  2313. spin_lock_init(&log->no_space_stripes_lock);
  2314. INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
  2315. INIT_WORK(&log->disable_writeback_work, r5c_disable_writeback_async);
  2316. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  2317. INIT_LIST_HEAD(&log->stripe_in_journal_list);
  2318. spin_lock_init(&log->stripe_in_journal_lock);
  2319. atomic_set(&log->stripe_in_journal_count, 0);
  2320. rcu_assign_pointer(conf->log, log);
  2321. if (r5l_load_log(log))
  2322. goto error;
  2323. set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  2324. return 0;
  2325. error:
  2326. rcu_assign_pointer(conf->log, NULL);
  2327. md_unregister_thread(&log->reclaim_thread);
  2328. reclaim_thread:
  2329. mempool_destroy(log->meta_pool);
  2330. out_mempool:
  2331. bioset_free(log->bs);
  2332. io_bs:
  2333. mempool_destroy(log->io_pool);
  2334. io_pool:
  2335. kmem_cache_destroy(log->io_kc);
  2336. io_kc:
  2337. kfree(log);
  2338. return -EINVAL;
  2339. }
  2340. void r5l_exit_log(struct r5l_log *log)
  2341. {
  2342. flush_work(&log->disable_writeback_work);
  2343. md_unregister_thread(&log->reclaim_thread);
  2344. mempool_destroy(log->meta_pool);
  2345. bioset_free(log->bs);
  2346. mempool_destroy(log->io_pool);
  2347. kmem_cache_destroy(log->io_kc);
  2348. kfree(log);
  2349. }