raid5-cache.c 88 KB

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