raid5-cache.c 87 KB

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