raid5-cache.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179
  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. 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 bio *bio;
  620. struct r5l_meta_block *block;
  621. unsigned long flags;
  622. u32 crc;
  623. bool do_submit = true;
  624. if (!io)
  625. return;
  626. block = page_address(io->meta_page);
  627. block->meta_size = cpu_to_le32(io->meta_offset);
  628. crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
  629. block->checksum = cpu_to_le32(crc);
  630. bio = io->current_bio;
  631. log->current_io = NULL;
  632. spin_lock_irqsave(&log->io_list_lock, flags);
  633. if (io->has_flush || io->has_fua) {
  634. if (io != list_first_entry(&log->running_ios,
  635. struct r5l_io_unit, log_sibling)) {
  636. io->io_deferred = 1;
  637. do_submit = false;
  638. }
  639. }
  640. spin_unlock_irqrestore(&log->io_list_lock, flags);
  641. if (do_submit)
  642. r5l_do_submit_io(log, io);
  643. }
  644. static struct bio *r5l_bio_alloc(struct r5l_log *log)
  645. {
  646. struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
  647. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  648. bio_set_dev(bio, log->rdev->bdev);
  649. bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
  650. return bio;
  651. }
  652. static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
  653. {
  654. log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
  655. r5c_update_log_state(log);
  656. /*
  657. * If we filled up the log device start from the beginning again,
  658. * which will require a new bio.
  659. *
  660. * Note: for this to work properly the log size needs to me a multiple
  661. * of BLOCK_SECTORS.
  662. */
  663. if (log->log_start == 0)
  664. io->need_split_bio = true;
  665. io->log_end = log->log_start;
  666. }
  667. static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
  668. {
  669. struct r5l_io_unit *io;
  670. struct r5l_meta_block *block;
  671. io = mempool_alloc(log->io_pool, GFP_ATOMIC);
  672. if (!io)
  673. return NULL;
  674. memset(io, 0, sizeof(*io));
  675. io->log = log;
  676. INIT_LIST_HEAD(&io->log_sibling);
  677. INIT_LIST_HEAD(&io->stripe_list);
  678. bio_list_init(&io->flush_barriers);
  679. io->state = IO_UNIT_RUNNING;
  680. io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
  681. block = page_address(io->meta_page);
  682. clear_page(block);
  683. block->magic = cpu_to_le32(R5LOG_MAGIC);
  684. block->version = R5LOG_VERSION;
  685. block->seq = cpu_to_le64(log->seq);
  686. block->position = cpu_to_le64(log->log_start);
  687. io->log_start = log->log_start;
  688. io->meta_offset = sizeof(struct r5l_meta_block);
  689. io->seq = log->seq++;
  690. io->current_bio = r5l_bio_alloc(log);
  691. io->current_bio->bi_end_io = r5l_log_endio;
  692. io->current_bio->bi_private = io;
  693. bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
  694. r5_reserve_log_entry(log, io);
  695. spin_lock_irq(&log->io_list_lock);
  696. list_add_tail(&io->log_sibling, &log->running_ios);
  697. spin_unlock_irq(&log->io_list_lock);
  698. return io;
  699. }
  700. static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
  701. {
  702. if (log->current_io &&
  703. log->current_io->meta_offset + payload_size > PAGE_SIZE)
  704. r5l_submit_current_io(log);
  705. if (!log->current_io) {
  706. log->current_io = r5l_new_meta(log);
  707. if (!log->current_io)
  708. return -ENOMEM;
  709. }
  710. return 0;
  711. }
  712. static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
  713. sector_t location,
  714. u32 checksum1, u32 checksum2,
  715. bool checksum2_valid)
  716. {
  717. struct r5l_io_unit *io = log->current_io;
  718. struct r5l_payload_data_parity *payload;
  719. payload = page_address(io->meta_page) + io->meta_offset;
  720. payload->header.type = cpu_to_le16(type);
  721. payload->header.flags = cpu_to_le16(0);
  722. payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
  723. (PAGE_SHIFT - 9));
  724. payload->location = cpu_to_le64(location);
  725. payload->checksum[0] = cpu_to_le32(checksum1);
  726. if (checksum2_valid)
  727. payload->checksum[1] = cpu_to_le32(checksum2);
  728. io->meta_offset += sizeof(struct r5l_payload_data_parity) +
  729. sizeof(__le32) * (1 + !!checksum2_valid);
  730. }
  731. static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
  732. {
  733. struct r5l_io_unit *io = log->current_io;
  734. if (io->need_split_bio) {
  735. BUG_ON(io->split_bio);
  736. io->split_bio = io->current_bio;
  737. io->current_bio = r5l_bio_alloc(log);
  738. bio_chain(io->current_bio, io->split_bio);
  739. io->need_split_bio = false;
  740. }
  741. if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
  742. BUG();
  743. r5_reserve_log_entry(log, io);
  744. }
  745. static void r5l_append_flush_payload(struct r5l_log *log, sector_t sect)
  746. {
  747. struct mddev *mddev = log->rdev->mddev;
  748. struct r5conf *conf = mddev->private;
  749. struct r5l_io_unit *io;
  750. struct r5l_payload_flush *payload;
  751. int meta_size;
  752. /*
  753. * payload_flush requires extra writes to the journal.
  754. * To avoid handling the extra IO in quiesce, just skip
  755. * flush_payload
  756. */
  757. if (conf->quiesce)
  758. return;
  759. mutex_lock(&log->io_mutex);
  760. meta_size = sizeof(struct r5l_payload_flush) + sizeof(__le64);
  761. if (r5l_get_meta(log, meta_size)) {
  762. mutex_unlock(&log->io_mutex);
  763. return;
  764. }
  765. /* current implementation is one stripe per flush payload */
  766. io = log->current_io;
  767. payload = page_address(io->meta_page) + io->meta_offset;
  768. payload->header.type = cpu_to_le16(R5LOG_PAYLOAD_FLUSH);
  769. payload->header.flags = cpu_to_le16(0);
  770. payload->size = cpu_to_le32(sizeof(__le64));
  771. payload->flush_stripes[0] = cpu_to_le64(sect);
  772. io->meta_offset += meta_size;
  773. /* multiple flush payloads count as one pending_stripe */
  774. if (!io->has_flush_payload) {
  775. io->has_flush_payload = 1;
  776. atomic_inc(&io->pending_stripe);
  777. }
  778. mutex_unlock(&log->io_mutex);
  779. }
  780. static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
  781. int data_pages, int parity_pages)
  782. {
  783. int i;
  784. int meta_size;
  785. int ret;
  786. struct r5l_io_unit *io;
  787. meta_size =
  788. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
  789. * data_pages) +
  790. sizeof(struct r5l_payload_data_parity) +
  791. sizeof(__le32) * parity_pages;
  792. ret = r5l_get_meta(log, meta_size);
  793. if (ret)
  794. return ret;
  795. io = log->current_io;
  796. if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
  797. io->has_flush = 1;
  798. for (i = 0; i < sh->disks; i++) {
  799. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  800. test_bit(R5_InJournal, &sh->dev[i].flags))
  801. continue;
  802. if (i == sh->pd_idx || i == sh->qd_idx)
  803. continue;
  804. if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
  805. log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
  806. io->has_fua = 1;
  807. /*
  808. * we need to flush journal to make sure recovery can
  809. * reach the data with fua flag
  810. */
  811. io->has_flush = 1;
  812. }
  813. r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
  814. raid5_compute_blocknr(sh, i, 0),
  815. sh->dev[i].log_checksum, 0, false);
  816. r5l_append_payload_page(log, sh->dev[i].page);
  817. }
  818. if (parity_pages == 2) {
  819. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  820. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  821. sh->dev[sh->qd_idx].log_checksum, true);
  822. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  823. r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
  824. } else if (parity_pages == 1) {
  825. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  826. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  827. 0, false);
  828. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  829. } else /* Just writing data, not parity, in caching phase */
  830. BUG_ON(parity_pages != 0);
  831. list_add_tail(&sh->log_list, &io->stripe_list);
  832. atomic_inc(&io->pending_stripe);
  833. sh->log_io = io;
  834. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  835. return 0;
  836. if (sh->log_start == MaxSector) {
  837. BUG_ON(!list_empty(&sh->r5c));
  838. sh->log_start = io->log_start;
  839. spin_lock_irq(&log->stripe_in_journal_lock);
  840. list_add_tail(&sh->r5c,
  841. &log->stripe_in_journal_list);
  842. spin_unlock_irq(&log->stripe_in_journal_lock);
  843. atomic_inc(&log->stripe_in_journal_count);
  844. }
  845. return 0;
  846. }
  847. /* add stripe to no_space_stripes, and then wake up reclaim */
  848. static inline void r5l_add_no_space_stripe(struct r5l_log *log,
  849. struct stripe_head *sh)
  850. {
  851. spin_lock(&log->no_space_stripes_lock);
  852. list_add_tail(&sh->log_list, &log->no_space_stripes);
  853. spin_unlock(&log->no_space_stripes_lock);
  854. }
  855. /*
  856. * running in raid5d, where reclaim could wait for raid5d too (when it flushes
  857. * data from log to raid disks), so we shouldn't wait for reclaim here
  858. */
  859. int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
  860. {
  861. struct r5conf *conf = sh->raid_conf;
  862. int write_disks = 0;
  863. int data_pages, parity_pages;
  864. int reserve;
  865. int i;
  866. int ret = 0;
  867. bool wake_reclaim = false;
  868. if (!log)
  869. return -EAGAIN;
  870. /* Don't support stripe batch */
  871. if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
  872. test_bit(STRIPE_SYNCING, &sh->state)) {
  873. /* the stripe is written to log, we start writing it to raid */
  874. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  875. return -EAGAIN;
  876. }
  877. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  878. for (i = 0; i < sh->disks; i++) {
  879. void *addr;
  880. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  881. test_bit(R5_InJournal, &sh->dev[i].flags))
  882. continue;
  883. write_disks++;
  884. /* checksum is already calculated in last run */
  885. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  886. continue;
  887. addr = kmap_atomic(sh->dev[i].page);
  888. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  889. addr, PAGE_SIZE);
  890. kunmap_atomic(addr);
  891. }
  892. parity_pages = 1 + !!(sh->qd_idx >= 0);
  893. data_pages = write_disks - parity_pages;
  894. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  895. /*
  896. * The stripe must enter state machine again to finish the write, so
  897. * don't delay.
  898. */
  899. clear_bit(STRIPE_DELAYED, &sh->state);
  900. atomic_inc(&sh->count);
  901. mutex_lock(&log->io_mutex);
  902. /* meta + data */
  903. reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
  904. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  905. if (!r5l_has_free_space(log, reserve)) {
  906. r5l_add_no_space_stripe(log, sh);
  907. wake_reclaim = true;
  908. } else {
  909. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  910. if (ret) {
  911. spin_lock_irq(&log->io_list_lock);
  912. list_add_tail(&sh->log_list,
  913. &log->no_mem_stripes);
  914. spin_unlock_irq(&log->io_list_lock);
  915. }
  916. }
  917. } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
  918. /*
  919. * log space critical, do not process stripes that are
  920. * not in cache yet (sh->log_start == MaxSector).
  921. */
  922. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  923. sh->log_start == MaxSector) {
  924. r5l_add_no_space_stripe(log, sh);
  925. wake_reclaim = true;
  926. reserve = 0;
  927. } else if (!r5l_has_free_space(log, reserve)) {
  928. if (sh->log_start == log->last_checkpoint)
  929. BUG();
  930. else
  931. r5l_add_no_space_stripe(log, sh);
  932. } else {
  933. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  934. if (ret) {
  935. spin_lock_irq(&log->io_list_lock);
  936. list_add_tail(&sh->log_list,
  937. &log->no_mem_stripes);
  938. spin_unlock_irq(&log->io_list_lock);
  939. }
  940. }
  941. }
  942. mutex_unlock(&log->io_mutex);
  943. if (wake_reclaim)
  944. r5l_wake_reclaim(log, reserve);
  945. return 0;
  946. }
  947. void r5l_write_stripe_run(struct r5l_log *log)
  948. {
  949. if (!log)
  950. return;
  951. mutex_lock(&log->io_mutex);
  952. r5l_submit_current_io(log);
  953. mutex_unlock(&log->io_mutex);
  954. }
  955. int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
  956. {
  957. if (!log)
  958. return -ENODEV;
  959. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  960. /*
  961. * in write through (journal only)
  962. * we flush log disk cache first, then write stripe data to
  963. * raid disks. So if bio is finished, the log disk cache is
  964. * flushed already. The recovery guarantees we can recovery
  965. * the bio from log disk, so we don't need to flush again
  966. */
  967. if (bio->bi_iter.bi_size == 0) {
  968. bio_endio(bio);
  969. return 0;
  970. }
  971. bio->bi_opf &= ~REQ_PREFLUSH;
  972. } else {
  973. /* write back (with cache) */
  974. if (bio->bi_iter.bi_size == 0) {
  975. mutex_lock(&log->io_mutex);
  976. r5l_get_meta(log, 0);
  977. bio_list_add(&log->current_io->flush_barriers, bio);
  978. log->current_io->has_flush = 1;
  979. log->current_io->has_null_flush = 1;
  980. atomic_inc(&log->current_io->pending_stripe);
  981. r5l_submit_current_io(log);
  982. mutex_unlock(&log->io_mutex);
  983. return 0;
  984. }
  985. }
  986. return -EAGAIN;
  987. }
  988. /* This will run after log space is reclaimed */
  989. static void r5l_run_no_space_stripes(struct r5l_log *log)
  990. {
  991. struct stripe_head *sh;
  992. spin_lock(&log->no_space_stripes_lock);
  993. while (!list_empty(&log->no_space_stripes)) {
  994. sh = list_first_entry(&log->no_space_stripes,
  995. struct stripe_head, log_list);
  996. list_del_init(&sh->log_list);
  997. set_bit(STRIPE_HANDLE, &sh->state);
  998. raid5_release_stripe(sh);
  999. }
  1000. spin_unlock(&log->no_space_stripes_lock);
  1001. }
  1002. /*
  1003. * calculate new last_checkpoint
  1004. * for write through mode, returns log->next_checkpoint
  1005. * for write back, returns log_start of first sh in stripe_in_journal_list
  1006. */
  1007. static sector_t r5c_calculate_new_cp(struct r5conf *conf)
  1008. {
  1009. struct stripe_head *sh;
  1010. struct r5l_log *log = conf->log;
  1011. sector_t new_cp;
  1012. unsigned long flags;
  1013. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  1014. return log->next_checkpoint;
  1015. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  1016. if (list_empty(&conf->log->stripe_in_journal_list)) {
  1017. /* all stripes flushed */
  1018. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1019. return log->next_checkpoint;
  1020. }
  1021. sh = list_first_entry(&conf->log->stripe_in_journal_list,
  1022. struct stripe_head, r5c);
  1023. new_cp = sh->log_start;
  1024. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1025. return new_cp;
  1026. }
  1027. static sector_t r5l_reclaimable_space(struct r5l_log *log)
  1028. {
  1029. struct r5conf *conf = log->rdev->mddev->private;
  1030. return r5l_ring_distance(log, log->last_checkpoint,
  1031. r5c_calculate_new_cp(conf));
  1032. }
  1033. static void r5l_run_no_mem_stripe(struct r5l_log *log)
  1034. {
  1035. struct stripe_head *sh;
  1036. lockdep_assert_held(&log->io_list_lock);
  1037. if (!list_empty(&log->no_mem_stripes)) {
  1038. sh = list_first_entry(&log->no_mem_stripes,
  1039. struct stripe_head, log_list);
  1040. list_del_init(&sh->log_list);
  1041. set_bit(STRIPE_HANDLE, &sh->state);
  1042. raid5_release_stripe(sh);
  1043. }
  1044. }
  1045. static bool r5l_complete_finished_ios(struct r5l_log *log)
  1046. {
  1047. struct r5l_io_unit *io, *next;
  1048. bool found = false;
  1049. lockdep_assert_held(&log->io_list_lock);
  1050. list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
  1051. /* don't change list order */
  1052. if (io->state < IO_UNIT_STRIPE_END)
  1053. break;
  1054. log->next_checkpoint = io->log_start;
  1055. list_del(&io->log_sibling);
  1056. mempool_free(io, log->io_pool);
  1057. r5l_run_no_mem_stripe(log);
  1058. found = true;
  1059. }
  1060. return found;
  1061. }
  1062. static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
  1063. {
  1064. struct r5l_log *log = io->log;
  1065. struct r5conf *conf = log->rdev->mddev->private;
  1066. unsigned long flags;
  1067. spin_lock_irqsave(&log->io_list_lock, flags);
  1068. __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
  1069. if (!r5l_complete_finished_ios(log)) {
  1070. spin_unlock_irqrestore(&log->io_list_lock, flags);
  1071. return;
  1072. }
  1073. if (r5l_reclaimable_space(log) > log->max_free_space ||
  1074. test_bit(R5C_LOG_TIGHT, &conf->cache_state))
  1075. r5l_wake_reclaim(log, 0);
  1076. spin_unlock_irqrestore(&log->io_list_lock, flags);
  1077. wake_up(&log->iounit_wait);
  1078. }
  1079. void r5l_stripe_write_finished(struct stripe_head *sh)
  1080. {
  1081. struct r5l_io_unit *io;
  1082. io = sh->log_io;
  1083. sh->log_io = NULL;
  1084. if (io && atomic_dec_and_test(&io->pending_stripe))
  1085. __r5l_stripe_write_finished(io);
  1086. }
  1087. static void r5l_log_flush_endio(struct bio *bio)
  1088. {
  1089. struct r5l_log *log = container_of(bio, struct r5l_log,
  1090. flush_bio);
  1091. unsigned long flags;
  1092. struct r5l_io_unit *io;
  1093. if (bio->bi_status)
  1094. md_error(log->rdev->mddev, log->rdev);
  1095. spin_lock_irqsave(&log->io_list_lock, flags);
  1096. list_for_each_entry(io, &log->flushing_ios, log_sibling)
  1097. r5l_io_run_stripes(io);
  1098. list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
  1099. spin_unlock_irqrestore(&log->io_list_lock, flags);
  1100. }
  1101. /*
  1102. * Starting dispatch IO to raid.
  1103. * io_unit(meta) consists of a log. There is one situation we want to avoid. A
  1104. * broken meta in the middle of a log causes recovery can't find meta at the
  1105. * head of log. If operations require meta at the head persistent in log, we
  1106. * must make sure meta before it persistent in log too. A case is:
  1107. *
  1108. * stripe data/parity is in log, we start write stripe to raid disks. stripe
  1109. * data/parity must be persistent in log before we do the write to raid disks.
  1110. *
  1111. * The solution is we restrictly maintain io_unit list order. In this case, we
  1112. * only write stripes of an io_unit to raid disks till the io_unit is the first
  1113. * one whose data/parity is in log.
  1114. */
  1115. void r5l_flush_stripe_to_raid(struct r5l_log *log)
  1116. {
  1117. bool do_flush;
  1118. if (!log || !log->need_cache_flush)
  1119. return;
  1120. spin_lock_irq(&log->io_list_lock);
  1121. /* flush bio is running */
  1122. if (!list_empty(&log->flushing_ios)) {
  1123. spin_unlock_irq(&log->io_list_lock);
  1124. return;
  1125. }
  1126. list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
  1127. do_flush = !list_empty(&log->flushing_ios);
  1128. spin_unlock_irq(&log->io_list_lock);
  1129. if (!do_flush)
  1130. return;
  1131. bio_reset(&log->flush_bio);
  1132. bio_set_dev(&log->flush_bio, log->rdev->bdev);
  1133. log->flush_bio.bi_end_io = r5l_log_flush_endio;
  1134. log->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  1135. submit_bio(&log->flush_bio);
  1136. }
  1137. static void r5l_write_super(struct r5l_log *log, sector_t cp);
  1138. static void r5l_write_super_and_discard_space(struct r5l_log *log,
  1139. sector_t end)
  1140. {
  1141. struct block_device *bdev = log->rdev->bdev;
  1142. struct mddev *mddev;
  1143. r5l_write_super(log, end);
  1144. if (!blk_queue_discard(bdev_get_queue(bdev)))
  1145. return;
  1146. mddev = log->rdev->mddev;
  1147. /*
  1148. * Discard could zero data, so before discard we must make sure
  1149. * superblock is updated to new log tail. Updating superblock (either
  1150. * directly call md_update_sb() or depend on md thread) must hold
  1151. * reconfig mutex. On the other hand, raid5_quiesce is called with
  1152. * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
  1153. * for all IO finish, hence waitting for reclaim thread, while reclaim
  1154. * thread is calling this function and waitting for reconfig mutex. So
  1155. * there is a deadlock. We workaround this issue with a trylock.
  1156. * FIXME: we could miss discard if we can't take reconfig mutex
  1157. */
  1158. set_mask_bits(&mddev->sb_flags, 0,
  1159. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  1160. if (!mddev_trylock(mddev))
  1161. return;
  1162. md_update_sb(mddev, 1);
  1163. mddev_unlock(mddev);
  1164. /* discard IO error really doesn't matter, ignore it */
  1165. if (log->last_checkpoint < end) {
  1166. blkdev_issue_discard(bdev,
  1167. log->last_checkpoint + log->rdev->data_offset,
  1168. end - log->last_checkpoint, GFP_NOIO, 0);
  1169. } else {
  1170. blkdev_issue_discard(bdev,
  1171. log->last_checkpoint + log->rdev->data_offset,
  1172. log->device_size - log->last_checkpoint,
  1173. GFP_NOIO, 0);
  1174. blkdev_issue_discard(bdev, log->rdev->data_offset, end,
  1175. GFP_NOIO, 0);
  1176. }
  1177. }
  1178. /*
  1179. * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
  1180. * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
  1181. *
  1182. * must hold conf->device_lock
  1183. */
  1184. static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
  1185. {
  1186. BUG_ON(list_empty(&sh->lru));
  1187. BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  1188. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  1189. /*
  1190. * The stripe is not ON_RELEASE_LIST, so it is safe to call
  1191. * raid5_release_stripe() while holding conf->device_lock
  1192. */
  1193. BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
  1194. lockdep_assert_held(&conf->device_lock);
  1195. list_del_init(&sh->lru);
  1196. atomic_inc(&sh->count);
  1197. set_bit(STRIPE_HANDLE, &sh->state);
  1198. atomic_inc(&conf->active_stripes);
  1199. r5c_make_stripe_write_out(sh);
  1200. if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
  1201. atomic_inc(&conf->r5c_flushing_partial_stripes);
  1202. else
  1203. atomic_inc(&conf->r5c_flushing_full_stripes);
  1204. raid5_release_stripe(sh);
  1205. }
  1206. /*
  1207. * if num == 0, flush all full stripes
  1208. * if num > 0, flush all full stripes. If less than num full stripes are
  1209. * flushed, flush some partial stripes until totally num stripes are
  1210. * flushed or there is no more cached stripes.
  1211. */
  1212. void r5c_flush_cache(struct r5conf *conf, int num)
  1213. {
  1214. int count;
  1215. struct stripe_head *sh, *next;
  1216. lockdep_assert_held(&conf->device_lock);
  1217. if (!conf->log)
  1218. return;
  1219. count = 0;
  1220. list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
  1221. r5c_flush_stripe(conf, sh);
  1222. count++;
  1223. }
  1224. if (count >= num)
  1225. return;
  1226. list_for_each_entry_safe(sh, next,
  1227. &conf->r5c_partial_stripe_list, lru) {
  1228. r5c_flush_stripe(conf, sh);
  1229. if (++count >= num)
  1230. break;
  1231. }
  1232. }
  1233. static void r5c_do_reclaim(struct r5conf *conf)
  1234. {
  1235. struct r5l_log *log = conf->log;
  1236. struct stripe_head *sh;
  1237. int count = 0;
  1238. unsigned long flags;
  1239. int total_cached;
  1240. int stripes_to_flush;
  1241. int flushing_partial, flushing_full;
  1242. if (!r5c_is_writeback(log))
  1243. return;
  1244. flushing_partial = atomic_read(&conf->r5c_flushing_partial_stripes);
  1245. flushing_full = atomic_read(&conf->r5c_flushing_full_stripes);
  1246. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  1247. atomic_read(&conf->r5c_cached_full_stripes) -
  1248. flushing_full - flushing_partial;
  1249. if (total_cached > conf->min_nr_stripes * 3 / 4 ||
  1250. atomic_read(&conf->empty_inactive_list_nr) > 0)
  1251. /*
  1252. * if stripe cache pressure high, flush all full stripes and
  1253. * some partial stripes
  1254. */
  1255. stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
  1256. else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  1257. atomic_read(&conf->r5c_cached_full_stripes) - flushing_full >
  1258. R5C_FULL_STRIPE_FLUSH_BATCH(conf))
  1259. /*
  1260. * if stripe cache pressure moderate, or if there is many full
  1261. * stripes,flush all full stripes
  1262. */
  1263. stripes_to_flush = 0;
  1264. else
  1265. /* no need to flush */
  1266. stripes_to_flush = -1;
  1267. if (stripes_to_flush >= 0) {
  1268. spin_lock_irqsave(&conf->device_lock, flags);
  1269. r5c_flush_cache(conf, stripes_to_flush);
  1270. spin_unlock_irqrestore(&conf->device_lock, flags);
  1271. }
  1272. /* if log space is tight, flush stripes on stripe_in_journal_list */
  1273. if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
  1274. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  1275. spin_lock(&conf->device_lock);
  1276. list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
  1277. /*
  1278. * stripes on stripe_in_journal_list could be in any
  1279. * state of the stripe_cache state machine. In this
  1280. * case, we only want to flush stripe on
  1281. * r5c_cached_full/partial_stripes. The following
  1282. * condition makes sure the stripe is on one of the
  1283. * two lists.
  1284. */
  1285. if (!list_empty(&sh->lru) &&
  1286. !test_bit(STRIPE_HANDLE, &sh->state) &&
  1287. atomic_read(&sh->count) == 0) {
  1288. r5c_flush_stripe(conf, sh);
  1289. if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
  1290. break;
  1291. }
  1292. }
  1293. spin_unlock(&conf->device_lock);
  1294. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1295. }
  1296. if (!test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
  1297. r5l_run_no_space_stripes(log);
  1298. md_wakeup_thread(conf->mddev->thread);
  1299. }
  1300. static void r5l_do_reclaim(struct r5l_log *log)
  1301. {
  1302. struct r5conf *conf = log->rdev->mddev->private;
  1303. sector_t reclaim_target = xchg(&log->reclaim_target, 0);
  1304. sector_t reclaimable;
  1305. sector_t next_checkpoint;
  1306. bool write_super;
  1307. spin_lock_irq(&log->io_list_lock);
  1308. write_super = r5l_reclaimable_space(log) > log->max_free_space ||
  1309. reclaim_target != 0 || !list_empty(&log->no_space_stripes);
  1310. /*
  1311. * move proper io_unit to reclaim list. We should not change the order.
  1312. * reclaimable/unreclaimable io_unit can be mixed in the list, we
  1313. * shouldn't reuse space of an unreclaimable io_unit
  1314. */
  1315. while (1) {
  1316. reclaimable = r5l_reclaimable_space(log);
  1317. if (reclaimable >= reclaim_target ||
  1318. (list_empty(&log->running_ios) &&
  1319. list_empty(&log->io_end_ios) &&
  1320. list_empty(&log->flushing_ios) &&
  1321. list_empty(&log->finished_ios)))
  1322. break;
  1323. md_wakeup_thread(log->rdev->mddev->thread);
  1324. wait_event_lock_irq(log->iounit_wait,
  1325. r5l_reclaimable_space(log) > reclaimable,
  1326. log->io_list_lock);
  1327. }
  1328. next_checkpoint = r5c_calculate_new_cp(conf);
  1329. spin_unlock_irq(&log->io_list_lock);
  1330. if (reclaimable == 0 || !write_super)
  1331. return;
  1332. /*
  1333. * write_super will flush cache of each raid disk. We must write super
  1334. * here, because the log area might be reused soon and we don't want to
  1335. * confuse recovery
  1336. */
  1337. r5l_write_super_and_discard_space(log, next_checkpoint);
  1338. mutex_lock(&log->io_mutex);
  1339. log->last_checkpoint = next_checkpoint;
  1340. r5c_update_log_state(log);
  1341. mutex_unlock(&log->io_mutex);
  1342. r5l_run_no_space_stripes(log);
  1343. }
  1344. static void r5l_reclaim_thread(struct md_thread *thread)
  1345. {
  1346. struct mddev *mddev = thread->mddev;
  1347. struct r5conf *conf = mddev->private;
  1348. struct r5l_log *log = conf->log;
  1349. if (!log)
  1350. return;
  1351. r5c_do_reclaim(conf);
  1352. r5l_do_reclaim(log);
  1353. }
  1354. void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
  1355. {
  1356. unsigned long target;
  1357. unsigned long new = (unsigned long)space; /* overflow in theory */
  1358. if (!log)
  1359. return;
  1360. do {
  1361. target = log->reclaim_target;
  1362. if (new < target)
  1363. return;
  1364. } while (cmpxchg(&log->reclaim_target, target, new) != target);
  1365. md_wakeup_thread(log->reclaim_thread);
  1366. }
  1367. void r5l_quiesce(struct r5l_log *log, int quiesce)
  1368. {
  1369. struct mddev *mddev;
  1370. if (!log)
  1371. return;
  1372. if (quiesce) {
  1373. /* make sure r5l_write_super_and_discard_space exits */
  1374. mddev = log->rdev->mddev;
  1375. wake_up(&mddev->sb_wait);
  1376. kthread_park(log->reclaim_thread->tsk);
  1377. r5l_wake_reclaim(log, MaxSector);
  1378. r5l_do_reclaim(log);
  1379. } else
  1380. kthread_unpark(log->reclaim_thread->tsk);
  1381. }
  1382. bool r5l_log_disk_error(struct r5conf *conf)
  1383. {
  1384. struct r5l_log *log;
  1385. bool ret;
  1386. /* don't allow write if journal disk is missing */
  1387. rcu_read_lock();
  1388. log = rcu_dereference(conf->log);
  1389. if (!log)
  1390. ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  1391. else
  1392. ret = test_bit(Faulty, &log->rdev->flags);
  1393. rcu_read_unlock();
  1394. return ret;
  1395. }
  1396. #define R5L_RECOVERY_PAGE_POOL_SIZE 256
  1397. struct r5l_recovery_ctx {
  1398. struct page *meta_page; /* current meta */
  1399. sector_t meta_total_blocks; /* total size of current meta and data */
  1400. sector_t pos; /* recovery position */
  1401. u64 seq; /* recovery position seq */
  1402. int data_parity_stripes; /* number of data_parity stripes */
  1403. int data_only_stripes; /* number of data_only stripes */
  1404. struct list_head cached_list;
  1405. /*
  1406. * read ahead page pool (ra_pool)
  1407. * in recovery, log is read sequentially. It is not efficient to
  1408. * read every page with sync_page_io(). The read ahead page pool
  1409. * reads multiple pages with one IO, so further log read can
  1410. * just copy data from the pool.
  1411. */
  1412. struct page *ra_pool[R5L_RECOVERY_PAGE_POOL_SIZE];
  1413. sector_t pool_offset; /* offset of first page in the pool */
  1414. int total_pages; /* total allocated pages */
  1415. int valid_pages; /* pages with valid data */
  1416. struct bio *ra_bio; /* bio to do the read ahead */
  1417. };
  1418. static int r5l_recovery_allocate_ra_pool(struct r5l_log *log,
  1419. struct r5l_recovery_ctx *ctx)
  1420. {
  1421. struct page *page;
  1422. ctx->ra_bio = bio_alloc_bioset(GFP_KERNEL, BIO_MAX_PAGES, log->bs);
  1423. if (!ctx->ra_bio)
  1424. return -ENOMEM;
  1425. ctx->valid_pages = 0;
  1426. ctx->total_pages = 0;
  1427. while (ctx->total_pages < R5L_RECOVERY_PAGE_POOL_SIZE) {
  1428. page = alloc_page(GFP_KERNEL);
  1429. if (!page)
  1430. break;
  1431. ctx->ra_pool[ctx->total_pages] = page;
  1432. ctx->total_pages += 1;
  1433. }
  1434. if (ctx->total_pages == 0) {
  1435. bio_put(ctx->ra_bio);
  1436. return -ENOMEM;
  1437. }
  1438. ctx->pool_offset = 0;
  1439. return 0;
  1440. }
  1441. static void r5l_recovery_free_ra_pool(struct r5l_log *log,
  1442. struct r5l_recovery_ctx *ctx)
  1443. {
  1444. int i;
  1445. for (i = 0; i < ctx->total_pages; ++i)
  1446. put_page(ctx->ra_pool[i]);
  1447. bio_put(ctx->ra_bio);
  1448. }
  1449. /*
  1450. * fetch ctx->valid_pages pages from offset
  1451. * In normal cases, ctx->valid_pages == ctx->total_pages after the call.
  1452. * However, if the offset is close to the end of the journal device,
  1453. * ctx->valid_pages could be smaller than ctx->total_pages
  1454. */
  1455. static int r5l_recovery_fetch_ra_pool(struct r5l_log *log,
  1456. struct r5l_recovery_ctx *ctx,
  1457. sector_t offset)
  1458. {
  1459. bio_reset(ctx->ra_bio);
  1460. bio_set_dev(ctx->ra_bio, log->rdev->bdev);
  1461. bio_set_op_attrs(ctx->ra_bio, REQ_OP_READ, 0);
  1462. ctx->ra_bio->bi_iter.bi_sector = log->rdev->data_offset + offset;
  1463. ctx->valid_pages = 0;
  1464. ctx->pool_offset = offset;
  1465. while (ctx->valid_pages < ctx->total_pages) {
  1466. bio_add_page(ctx->ra_bio,
  1467. ctx->ra_pool[ctx->valid_pages], PAGE_SIZE, 0);
  1468. ctx->valid_pages += 1;
  1469. offset = r5l_ring_add(log, offset, BLOCK_SECTORS);
  1470. if (offset == 0) /* reached end of the device */
  1471. break;
  1472. }
  1473. return submit_bio_wait(ctx->ra_bio);
  1474. }
  1475. /*
  1476. * try read a page from the read ahead page pool, if the page is not in the
  1477. * pool, call r5l_recovery_fetch_ra_pool
  1478. */
  1479. static int r5l_recovery_read_page(struct r5l_log *log,
  1480. struct r5l_recovery_ctx *ctx,
  1481. struct page *page,
  1482. sector_t offset)
  1483. {
  1484. int ret;
  1485. if (offset < ctx->pool_offset ||
  1486. offset >= ctx->pool_offset + ctx->valid_pages * BLOCK_SECTORS) {
  1487. ret = r5l_recovery_fetch_ra_pool(log, ctx, offset);
  1488. if (ret)
  1489. return ret;
  1490. }
  1491. BUG_ON(offset < ctx->pool_offset ||
  1492. offset >= ctx->pool_offset + ctx->valid_pages * BLOCK_SECTORS);
  1493. memcpy(page_address(page),
  1494. page_address(ctx->ra_pool[(offset - ctx->pool_offset) >>
  1495. BLOCK_SECTOR_SHIFT]),
  1496. PAGE_SIZE);
  1497. return 0;
  1498. }
  1499. static int r5l_recovery_read_meta_block(struct r5l_log *log,
  1500. struct r5l_recovery_ctx *ctx)
  1501. {
  1502. struct page *page = ctx->meta_page;
  1503. struct r5l_meta_block *mb;
  1504. u32 crc, stored_crc;
  1505. int ret;
  1506. ret = r5l_recovery_read_page(log, ctx, page, ctx->pos);
  1507. if (ret != 0)
  1508. return ret;
  1509. mb = page_address(page);
  1510. stored_crc = le32_to_cpu(mb->checksum);
  1511. mb->checksum = 0;
  1512. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  1513. le64_to_cpu(mb->seq) != ctx->seq ||
  1514. mb->version != R5LOG_VERSION ||
  1515. le64_to_cpu(mb->position) != ctx->pos)
  1516. return -EINVAL;
  1517. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  1518. if (stored_crc != crc)
  1519. return -EINVAL;
  1520. if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
  1521. return -EINVAL;
  1522. ctx->meta_total_blocks = BLOCK_SECTORS;
  1523. return 0;
  1524. }
  1525. static void
  1526. r5l_recovery_create_empty_meta_block(struct r5l_log *log,
  1527. struct page *page,
  1528. sector_t pos, u64 seq)
  1529. {
  1530. struct r5l_meta_block *mb;
  1531. mb = page_address(page);
  1532. clear_page(mb);
  1533. mb->magic = cpu_to_le32(R5LOG_MAGIC);
  1534. mb->version = R5LOG_VERSION;
  1535. mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
  1536. mb->seq = cpu_to_le64(seq);
  1537. mb->position = cpu_to_le64(pos);
  1538. }
  1539. static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
  1540. u64 seq)
  1541. {
  1542. struct page *page;
  1543. struct r5l_meta_block *mb;
  1544. page = alloc_page(GFP_KERNEL);
  1545. if (!page)
  1546. return -ENOMEM;
  1547. r5l_recovery_create_empty_meta_block(log, page, pos, seq);
  1548. mb = page_address(page);
  1549. mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
  1550. mb, PAGE_SIZE));
  1551. if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
  1552. REQ_SYNC | REQ_FUA, false)) {
  1553. __free_page(page);
  1554. return -EIO;
  1555. }
  1556. __free_page(page);
  1557. return 0;
  1558. }
  1559. /*
  1560. * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
  1561. * to mark valid (potentially not flushed) data in the journal.
  1562. *
  1563. * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
  1564. * so there should not be any mismatch here.
  1565. */
  1566. static void r5l_recovery_load_data(struct r5l_log *log,
  1567. struct stripe_head *sh,
  1568. struct r5l_recovery_ctx *ctx,
  1569. struct r5l_payload_data_parity *payload,
  1570. sector_t log_offset)
  1571. {
  1572. struct mddev *mddev = log->rdev->mddev;
  1573. struct r5conf *conf = mddev->private;
  1574. int dd_idx;
  1575. raid5_compute_sector(conf,
  1576. le64_to_cpu(payload->location), 0,
  1577. &dd_idx, sh);
  1578. r5l_recovery_read_page(log, ctx, sh->dev[dd_idx].page, log_offset);
  1579. sh->dev[dd_idx].log_checksum =
  1580. le32_to_cpu(payload->checksum[0]);
  1581. ctx->meta_total_blocks += BLOCK_SECTORS;
  1582. set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
  1583. set_bit(STRIPE_R5C_CACHING, &sh->state);
  1584. }
  1585. static void r5l_recovery_load_parity(struct r5l_log *log,
  1586. struct stripe_head *sh,
  1587. struct r5l_recovery_ctx *ctx,
  1588. struct r5l_payload_data_parity *payload,
  1589. sector_t log_offset)
  1590. {
  1591. struct mddev *mddev = log->rdev->mddev;
  1592. struct r5conf *conf = mddev->private;
  1593. ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
  1594. r5l_recovery_read_page(log, ctx, sh->dev[sh->pd_idx].page, log_offset);
  1595. sh->dev[sh->pd_idx].log_checksum =
  1596. le32_to_cpu(payload->checksum[0]);
  1597. set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
  1598. if (sh->qd_idx >= 0) {
  1599. r5l_recovery_read_page(
  1600. log, ctx, sh->dev[sh->qd_idx].page,
  1601. r5l_ring_add(log, log_offset, BLOCK_SECTORS));
  1602. sh->dev[sh->qd_idx].log_checksum =
  1603. le32_to_cpu(payload->checksum[1]);
  1604. set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
  1605. }
  1606. clear_bit(STRIPE_R5C_CACHING, &sh->state);
  1607. }
  1608. static void r5l_recovery_reset_stripe(struct stripe_head *sh)
  1609. {
  1610. int i;
  1611. sh->state = 0;
  1612. sh->log_start = MaxSector;
  1613. for (i = sh->disks; i--; )
  1614. sh->dev[i].flags = 0;
  1615. }
  1616. static void
  1617. r5l_recovery_replay_one_stripe(struct r5conf *conf,
  1618. struct stripe_head *sh,
  1619. struct r5l_recovery_ctx *ctx)
  1620. {
  1621. struct md_rdev *rdev, *rrdev;
  1622. int disk_index;
  1623. int data_count = 0;
  1624. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1625. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1626. continue;
  1627. if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
  1628. continue;
  1629. data_count++;
  1630. }
  1631. /*
  1632. * stripes that only have parity must have been flushed
  1633. * before the crash that we are now recovering from, so
  1634. * there is nothing more to recovery.
  1635. */
  1636. if (data_count == 0)
  1637. goto out;
  1638. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1639. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1640. continue;
  1641. /* in case device is broken */
  1642. rcu_read_lock();
  1643. rdev = rcu_dereference(conf->disks[disk_index].rdev);
  1644. if (rdev) {
  1645. atomic_inc(&rdev->nr_pending);
  1646. rcu_read_unlock();
  1647. sync_page_io(rdev, sh->sector, PAGE_SIZE,
  1648. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1649. false);
  1650. rdev_dec_pending(rdev, rdev->mddev);
  1651. rcu_read_lock();
  1652. }
  1653. rrdev = rcu_dereference(conf->disks[disk_index].replacement);
  1654. if (rrdev) {
  1655. atomic_inc(&rrdev->nr_pending);
  1656. rcu_read_unlock();
  1657. sync_page_io(rrdev, sh->sector, PAGE_SIZE,
  1658. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1659. false);
  1660. rdev_dec_pending(rrdev, rrdev->mddev);
  1661. rcu_read_lock();
  1662. }
  1663. rcu_read_unlock();
  1664. }
  1665. ctx->data_parity_stripes++;
  1666. out:
  1667. r5l_recovery_reset_stripe(sh);
  1668. }
  1669. static struct stripe_head *
  1670. r5c_recovery_alloc_stripe(struct r5conf *conf,
  1671. sector_t stripe_sect)
  1672. {
  1673. struct stripe_head *sh;
  1674. sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
  1675. if (!sh)
  1676. return NULL; /* no more stripe available */
  1677. r5l_recovery_reset_stripe(sh);
  1678. return sh;
  1679. }
  1680. static struct stripe_head *
  1681. r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
  1682. {
  1683. struct stripe_head *sh;
  1684. list_for_each_entry(sh, list, lru)
  1685. if (sh->sector == sect)
  1686. return sh;
  1687. return NULL;
  1688. }
  1689. static void
  1690. r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
  1691. struct r5l_recovery_ctx *ctx)
  1692. {
  1693. struct stripe_head *sh, *next;
  1694. list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
  1695. r5l_recovery_reset_stripe(sh);
  1696. list_del_init(&sh->lru);
  1697. raid5_release_stripe(sh);
  1698. }
  1699. }
  1700. static void
  1701. r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
  1702. struct r5l_recovery_ctx *ctx)
  1703. {
  1704. struct stripe_head *sh, *next;
  1705. list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
  1706. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  1707. r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
  1708. list_del_init(&sh->lru);
  1709. raid5_release_stripe(sh);
  1710. }
  1711. }
  1712. /* if matches return 0; otherwise return -EINVAL */
  1713. static int
  1714. r5l_recovery_verify_data_checksum(struct r5l_log *log,
  1715. struct r5l_recovery_ctx *ctx,
  1716. struct page *page,
  1717. sector_t log_offset, __le32 log_checksum)
  1718. {
  1719. void *addr;
  1720. u32 checksum;
  1721. r5l_recovery_read_page(log, ctx, page, log_offset);
  1722. addr = kmap_atomic(page);
  1723. checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
  1724. kunmap_atomic(addr);
  1725. return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
  1726. }
  1727. /*
  1728. * before loading data to stripe cache, we need verify checksum for all data,
  1729. * if there is mismatch for any data page, we drop all data in the mata block
  1730. */
  1731. static int
  1732. r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
  1733. struct r5l_recovery_ctx *ctx)
  1734. {
  1735. struct mddev *mddev = log->rdev->mddev;
  1736. struct r5conf *conf = mddev->private;
  1737. struct r5l_meta_block *mb = page_address(ctx->meta_page);
  1738. sector_t mb_offset = sizeof(struct r5l_meta_block);
  1739. sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1740. struct page *page;
  1741. struct r5l_payload_data_parity *payload;
  1742. struct r5l_payload_flush *payload_flush;
  1743. page = alloc_page(GFP_KERNEL);
  1744. if (!page)
  1745. return -ENOMEM;
  1746. while (mb_offset < le32_to_cpu(mb->meta_size)) {
  1747. payload = (void *)mb + mb_offset;
  1748. payload_flush = (void *)mb + mb_offset;
  1749. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
  1750. if (r5l_recovery_verify_data_checksum(
  1751. log, ctx, page, log_offset,
  1752. payload->checksum[0]) < 0)
  1753. goto mismatch;
  1754. } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY) {
  1755. if (r5l_recovery_verify_data_checksum(
  1756. log, ctx, page, log_offset,
  1757. payload->checksum[0]) < 0)
  1758. goto mismatch;
  1759. if (conf->max_degraded == 2 && /* q for RAID 6 */
  1760. r5l_recovery_verify_data_checksum(
  1761. log, ctx, page,
  1762. r5l_ring_add(log, log_offset,
  1763. BLOCK_SECTORS),
  1764. payload->checksum[1]) < 0)
  1765. goto mismatch;
  1766. } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) {
  1767. /* nothing to do for R5LOG_PAYLOAD_FLUSH here */
  1768. } else /* not R5LOG_PAYLOAD_DATA/PARITY/FLUSH */
  1769. goto mismatch;
  1770. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) {
  1771. mb_offset += sizeof(struct r5l_payload_flush) +
  1772. le32_to_cpu(payload_flush->size);
  1773. } else {
  1774. /* DATA or PARITY payload */
  1775. log_offset = r5l_ring_add(log, log_offset,
  1776. le32_to_cpu(payload->size));
  1777. mb_offset += sizeof(struct r5l_payload_data_parity) +
  1778. sizeof(__le32) *
  1779. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  1780. }
  1781. }
  1782. put_page(page);
  1783. return 0;
  1784. mismatch:
  1785. put_page(page);
  1786. return -EINVAL;
  1787. }
  1788. /*
  1789. * Analyze all data/parity pages in one meta block
  1790. * Returns:
  1791. * 0 for success
  1792. * -EINVAL for unknown playload type
  1793. * -EAGAIN for checksum mismatch of data page
  1794. * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
  1795. */
  1796. static int
  1797. r5c_recovery_analyze_meta_block(struct r5l_log *log,
  1798. struct r5l_recovery_ctx *ctx,
  1799. struct list_head *cached_stripe_list)
  1800. {
  1801. struct mddev *mddev = log->rdev->mddev;
  1802. struct r5conf *conf = mddev->private;
  1803. struct r5l_meta_block *mb;
  1804. struct r5l_payload_data_parity *payload;
  1805. struct r5l_payload_flush *payload_flush;
  1806. int mb_offset;
  1807. sector_t log_offset;
  1808. sector_t stripe_sect;
  1809. struct stripe_head *sh;
  1810. int ret;
  1811. /*
  1812. * for mismatch in data blocks, we will drop all data in this mb, but
  1813. * we will still read next mb for other data with FLUSH flag, as
  1814. * io_unit could finish out of order.
  1815. */
  1816. ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
  1817. if (ret == -EINVAL)
  1818. return -EAGAIN;
  1819. else if (ret)
  1820. return ret; /* -ENOMEM duo to alloc_page() failed */
  1821. mb = page_address(ctx->meta_page);
  1822. mb_offset = sizeof(struct r5l_meta_block);
  1823. log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1824. while (mb_offset < le32_to_cpu(mb->meta_size)) {
  1825. int dd;
  1826. payload = (void *)mb + mb_offset;
  1827. payload_flush = (void *)mb + mb_offset;
  1828. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) {
  1829. int i, count;
  1830. count = le32_to_cpu(payload_flush->size) / sizeof(__le64);
  1831. for (i = 0; i < count; ++i) {
  1832. stripe_sect = le64_to_cpu(payload_flush->flush_stripes[i]);
  1833. sh = r5c_recovery_lookup_stripe(cached_stripe_list,
  1834. stripe_sect);
  1835. if (sh) {
  1836. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  1837. r5l_recovery_reset_stripe(sh);
  1838. list_del_init(&sh->lru);
  1839. raid5_release_stripe(sh);
  1840. }
  1841. }
  1842. mb_offset += sizeof(struct r5l_payload_flush) +
  1843. le32_to_cpu(payload_flush->size);
  1844. continue;
  1845. }
  1846. /* DATA or PARITY payload */
  1847. stripe_sect = (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) ?
  1848. raid5_compute_sector(
  1849. conf, le64_to_cpu(payload->location), 0, &dd,
  1850. NULL)
  1851. : le64_to_cpu(payload->location);
  1852. sh = r5c_recovery_lookup_stripe(cached_stripe_list,
  1853. stripe_sect);
  1854. if (!sh) {
  1855. sh = r5c_recovery_alloc_stripe(conf, stripe_sect);
  1856. /*
  1857. * cannot get stripe from raid5_get_active_stripe
  1858. * try replay some stripes
  1859. */
  1860. if (!sh) {
  1861. r5c_recovery_replay_stripes(
  1862. cached_stripe_list, ctx);
  1863. sh = r5c_recovery_alloc_stripe(
  1864. conf, stripe_sect);
  1865. }
  1866. if (!sh) {
  1867. pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
  1868. mdname(mddev),
  1869. conf->min_nr_stripes * 2);
  1870. raid5_set_cache_size(mddev,
  1871. conf->min_nr_stripes * 2);
  1872. sh = r5c_recovery_alloc_stripe(conf,
  1873. stripe_sect);
  1874. }
  1875. if (!sh) {
  1876. pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
  1877. mdname(mddev));
  1878. return -ENOMEM;
  1879. }
  1880. list_add_tail(&sh->lru, cached_stripe_list);
  1881. }
  1882. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
  1883. if (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
  1884. test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) {
  1885. r5l_recovery_replay_one_stripe(conf, sh, ctx);
  1886. list_move_tail(&sh->lru, cached_stripe_list);
  1887. }
  1888. r5l_recovery_load_data(log, sh, ctx, payload,
  1889. log_offset);
  1890. } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
  1891. r5l_recovery_load_parity(log, sh, ctx, payload,
  1892. log_offset);
  1893. else
  1894. return -EINVAL;
  1895. log_offset = r5l_ring_add(log, log_offset,
  1896. le32_to_cpu(payload->size));
  1897. mb_offset += sizeof(struct r5l_payload_data_parity) +
  1898. sizeof(__le32) *
  1899. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  1900. }
  1901. return 0;
  1902. }
  1903. /*
  1904. * Load the stripe into cache. The stripe will be written out later by
  1905. * the stripe cache state machine.
  1906. */
  1907. static void r5c_recovery_load_one_stripe(struct r5l_log *log,
  1908. struct stripe_head *sh)
  1909. {
  1910. struct r5dev *dev;
  1911. int i;
  1912. for (i = sh->disks; i--; ) {
  1913. dev = sh->dev + i;
  1914. if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
  1915. set_bit(R5_InJournal, &dev->flags);
  1916. set_bit(R5_UPTODATE, &dev->flags);
  1917. }
  1918. }
  1919. }
  1920. /*
  1921. * Scan through the log for all to-be-flushed data
  1922. *
  1923. * For stripes with data and parity, namely Data-Parity stripe
  1924. * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
  1925. *
  1926. * For stripes with only data, namely Data-Only stripe
  1927. * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
  1928. *
  1929. * For a stripe, if we see data after parity, we should discard all previous
  1930. * data and parity for this stripe, as these data are already flushed to
  1931. * the array.
  1932. *
  1933. * At the end of the scan, we return the new journal_tail, which points to
  1934. * first data-only stripe on the journal device, or next invalid meta block.
  1935. */
  1936. static int r5c_recovery_flush_log(struct r5l_log *log,
  1937. struct r5l_recovery_ctx *ctx)
  1938. {
  1939. struct stripe_head *sh;
  1940. int ret = 0;
  1941. /* scan through the log */
  1942. while (1) {
  1943. if (r5l_recovery_read_meta_block(log, ctx))
  1944. break;
  1945. ret = r5c_recovery_analyze_meta_block(log, ctx,
  1946. &ctx->cached_list);
  1947. /*
  1948. * -EAGAIN means mismatch in data block, in this case, we still
  1949. * try scan the next metablock
  1950. */
  1951. if (ret && ret != -EAGAIN)
  1952. break; /* ret == -EINVAL or -ENOMEM */
  1953. ctx->seq++;
  1954. ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
  1955. }
  1956. if (ret == -ENOMEM) {
  1957. r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
  1958. return ret;
  1959. }
  1960. /* replay data-parity stripes */
  1961. r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
  1962. /* load data-only stripes to stripe cache */
  1963. list_for_each_entry(sh, &ctx->cached_list, lru) {
  1964. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  1965. r5c_recovery_load_one_stripe(log, sh);
  1966. ctx->data_only_stripes++;
  1967. }
  1968. return 0;
  1969. }
  1970. /*
  1971. * we did a recovery. Now ctx.pos points to an invalid meta block. New
  1972. * log will start here. but we can't let superblock point to last valid
  1973. * meta block. The log might looks like:
  1974. * | meta 1| meta 2| meta 3|
  1975. * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
  1976. * superblock points to meta 1, we write a new valid meta 2n. if crash
  1977. * happens again, new recovery will start from meta 1. Since meta 2n is
  1978. * valid now, recovery will think meta 3 is valid, which is wrong.
  1979. * The solution is we create a new meta in meta2 with its seq == meta
  1980. * 1's seq + 10000 and let superblock points to meta2. The same recovery
  1981. * will not think meta 3 is a valid meta, because its seq doesn't match
  1982. */
  1983. /*
  1984. * Before recovery, the log looks like the following
  1985. *
  1986. * ---------------------------------------------
  1987. * | valid log | invalid log |
  1988. * ---------------------------------------------
  1989. * ^
  1990. * |- log->last_checkpoint
  1991. * |- log->last_cp_seq
  1992. *
  1993. * Now we scan through the log until we see invalid entry
  1994. *
  1995. * ---------------------------------------------
  1996. * | valid log | invalid log |
  1997. * ---------------------------------------------
  1998. * ^ ^
  1999. * |- log->last_checkpoint |- ctx->pos
  2000. * |- log->last_cp_seq |- ctx->seq
  2001. *
  2002. * From this point, we need to increase seq number by 10 to avoid
  2003. * confusing next recovery.
  2004. *
  2005. * ---------------------------------------------
  2006. * | valid log | invalid log |
  2007. * ---------------------------------------------
  2008. * ^ ^
  2009. * |- log->last_checkpoint |- ctx->pos+1
  2010. * |- log->last_cp_seq |- ctx->seq+10001
  2011. *
  2012. * However, it is not safe to start the state machine yet, because data only
  2013. * parities are not yet secured in RAID. To save these data only parities, we
  2014. * rewrite them from seq+11.
  2015. *
  2016. * -----------------------------------------------------------------
  2017. * | valid log | data only stripes | invalid log |
  2018. * -----------------------------------------------------------------
  2019. * ^ ^
  2020. * |- log->last_checkpoint |- ctx->pos+n
  2021. * |- log->last_cp_seq |- ctx->seq+10000+n
  2022. *
  2023. * If failure happens again during this process, the recovery can safe start
  2024. * again from log->last_checkpoint.
  2025. *
  2026. * Once data only stripes are rewritten to journal, we move log_tail
  2027. *
  2028. * -----------------------------------------------------------------
  2029. * | old log | data only stripes | invalid log |
  2030. * -----------------------------------------------------------------
  2031. * ^ ^
  2032. * |- log->last_checkpoint |- ctx->pos+n
  2033. * |- log->last_cp_seq |- ctx->seq+10000+n
  2034. *
  2035. * Then we can safely start the state machine. If failure happens from this
  2036. * point on, the recovery will start from new log->last_checkpoint.
  2037. */
  2038. static int
  2039. r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
  2040. struct r5l_recovery_ctx *ctx)
  2041. {
  2042. struct stripe_head *sh;
  2043. struct mddev *mddev = log->rdev->mddev;
  2044. struct page *page;
  2045. sector_t next_checkpoint = MaxSector;
  2046. page = alloc_page(GFP_KERNEL);
  2047. if (!page) {
  2048. pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
  2049. mdname(mddev));
  2050. return -ENOMEM;
  2051. }
  2052. WARN_ON(list_empty(&ctx->cached_list));
  2053. list_for_each_entry(sh, &ctx->cached_list, lru) {
  2054. struct r5l_meta_block *mb;
  2055. int i;
  2056. int offset;
  2057. sector_t write_pos;
  2058. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  2059. r5l_recovery_create_empty_meta_block(log, page,
  2060. ctx->pos, ctx->seq);
  2061. mb = page_address(page);
  2062. offset = le32_to_cpu(mb->meta_size);
  2063. write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  2064. for (i = sh->disks; i--; ) {
  2065. struct r5dev *dev = &sh->dev[i];
  2066. struct r5l_payload_data_parity *payload;
  2067. void *addr;
  2068. if (test_bit(R5_InJournal, &dev->flags)) {
  2069. payload = (void *)mb + offset;
  2070. payload->header.type = cpu_to_le16(
  2071. R5LOG_PAYLOAD_DATA);
  2072. payload->size = cpu_to_le32(BLOCK_SECTORS);
  2073. payload->location = cpu_to_le64(
  2074. raid5_compute_blocknr(sh, i, 0));
  2075. addr = kmap_atomic(dev->page);
  2076. payload->checksum[0] = cpu_to_le32(
  2077. crc32c_le(log->uuid_checksum, addr,
  2078. PAGE_SIZE));
  2079. kunmap_atomic(addr);
  2080. sync_page_io(log->rdev, write_pos, PAGE_SIZE,
  2081. dev->page, REQ_OP_WRITE, 0, false);
  2082. write_pos = r5l_ring_add(log, write_pos,
  2083. BLOCK_SECTORS);
  2084. offset += sizeof(__le32) +
  2085. sizeof(struct r5l_payload_data_parity);
  2086. }
  2087. }
  2088. mb->meta_size = cpu_to_le32(offset);
  2089. mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
  2090. mb, PAGE_SIZE));
  2091. sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
  2092. REQ_OP_WRITE, REQ_SYNC | REQ_FUA, false);
  2093. sh->log_start = ctx->pos;
  2094. list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
  2095. atomic_inc(&log->stripe_in_journal_count);
  2096. ctx->pos = write_pos;
  2097. ctx->seq += 1;
  2098. next_checkpoint = sh->log_start;
  2099. }
  2100. log->next_checkpoint = next_checkpoint;
  2101. __free_page(page);
  2102. return 0;
  2103. }
  2104. static void r5c_recovery_flush_data_only_stripes(struct r5l_log *log,
  2105. struct r5l_recovery_ctx *ctx)
  2106. {
  2107. struct mddev *mddev = log->rdev->mddev;
  2108. struct r5conf *conf = mddev->private;
  2109. struct stripe_head *sh, *next;
  2110. if (ctx->data_only_stripes == 0)
  2111. return;
  2112. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_BACK;
  2113. list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
  2114. r5c_make_stripe_write_out(sh);
  2115. set_bit(STRIPE_HANDLE, &sh->state);
  2116. list_del_init(&sh->lru);
  2117. raid5_release_stripe(sh);
  2118. }
  2119. md_wakeup_thread(conf->mddev->thread);
  2120. /* reuse conf->wait_for_quiescent in recovery */
  2121. wait_event(conf->wait_for_quiescent,
  2122. atomic_read(&conf->active_stripes) == 0);
  2123. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  2124. }
  2125. static int r5l_recovery_log(struct r5l_log *log)
  2126. {
  2127. struct mddev *mddev = log->rdev->mddev;
  2128. struct r5l_recovery_ctx *ctx;
  2129. int ret;
  2130. sector_t pos;
  2131. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  2132. if (!ctx)
  2133. return -ENOMEM;
  2134. ctx->pos = log->last_checkpoint;
  2135. ctx->seq = log->last_cp_seq;
  2136. INIT_LIST_HEAD(&ctx->cached_list);
  2137. ctx->meta_page = alloc_page(GFP_KERNEL);
  2138. if (!ctx->meta_page) {
  2139. ret = -ENOMEM;
  2140. goto meta_page;
  2141. }
  2142. if (r5l_recovery_allocate_ra_pool(log, ctx) != 0) {
  2143. ret = -ENOMEM;
  2144. goto ra_pool;
  2145. }
  2146. ret = r5c_recovery_flush_log(log, ctx);
  2147. if (ret)
  2148. goto error;
  2149. pos = ctx->pos;
  2150. ctx->seq += 10000;
  2151. if ((ctx->data_only_stripes == 0) && (ctx->data_parity_stripes == 0))
  2152. pr_debug("md/raid:%s: starting from clean shutdown\n",
  2153. mdname(mddev));
  2154. else
  2155. pr_debug("md/raid:%s: recovering %d data-only stripes and %d data-parity stripes\n",
  2156. mdname(mddev), ctx->data_only_stripes,
  2157. ctx->data_parity_stripes);
  2158. if (ctx->data_only_stripes == 0) {
  2159. log->next_checkpoint = ctx->pos;
  2160. r5l_log_write_empty_meta_block(log, ctx->pos, ctx->seq++);
  2161. ctx->pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  2162. } else if (r5c_recovery_rewrite_data_only_stripes(log, ctx)) {
  2163. pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
  2164. mdname(mddev));
  2165. ret = -EIO;
  2166. goto error;
  2167. }
  2168. log->log_start = ctx->pos;
  2169. log->seq = ctx->seq;
  2170. log->last_checkpoint = pos;
  2171. r5l_write_super(log, pos);
  2172. r5c_recovery_flush_data_only_stripes(log, ctx);
  2173. ret = 0;
  2174. error:
  2175. r5l_recovery_free_ra_pool(log, ctx);
  2176. ra_pool:
  2177. __free_page(ctx->meta_page);
  2178. meta_page:
  2179. kfree(ctx);
  2180. return ret;
  2181. }
  2182. static void r5l_write_super(struct r5l_log *log, sector_t cp)
  2183. {
  2184. struct mddev *mddev = log->rdev->mddev;
  2185. log->rdev->journal_tail = cp;
  2186. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  2187. }
  2188. static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
  2189. {
  2190. struct r5conf *conf;
  2191. int ret;
  2192. ret = mddev_lock(mddev);
  2193. if (ret)
  2194. return ret;
  2195. conf = mddev->private;
  2196. if (!conf || !conf->log) {
  2197. mddev_unlock(mddev);
  2198. return 0;
  2199. }
  2200. switch (conf->log->r5c_journal_mode) {
  2201. case R5C_JOURNAL_MODE_WRITE_THROUGH:
  2202. ret = snprintf(
  2203. page, PAGE_SIZE, "[%s] %s\n",
  2204. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
  2205. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
  2206. break;
  2207. case R5C_JOURNAL_MODE_WRITE_BACK:
  2208. ret = snprintf(
  2209. page, PAGE_SIZE, "%s [%s]\n",
  2210. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
  2211. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
  2212. break;
  2213. default:
  2214. ret = 0;
  2215. }
  2216. mddev_unlock(mddev);
  2217. return ret;
  2218. }
  2219. /*
  2220. * Set journal cache mode on @mddev (external API initially needed by dm-raid).
  2221. *
  2222. * @mode as defined in 'enum r5c_journal_mode'.
  2223. *
  2224. */
  2225. int r5c_journal_mode_set(struct mddev *mddev, int mode)
  2226. {
  2227. struct r5conf *conf;
  2228. if (mode < R5C_JOURNAL_MODE_WRITE_THROUGH ||
  2229. mode > R5C_JOURNAL_MODE_WRITE_BACK)
  2230. return -EINVAL;
  2231. conf = mddev->private;
  2232. if (!conf || !conf->log)
  2233. return -ENODEV;
  2234. if (raid5_calc_degraded(conf) > 0 &&
  2235. mode == R5C_JOURNAL_MODE_WRITE_BACK)
  2236. return -EINVAL;
  2237. mddev_suspend(mddev);
  2238. conf->log->r5c_journal_mode = mode;
  2239. mddev_resume(mddev);
  2240. pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
  2241. mdname(mddev), mode, r5c_journal_mode_str[mode]);
  2242. return 0;
  2243. }
  2244. EXPORT_SYMBOL(r5c_journal_mode_set);
  2245. static ssize_t r5c_journal_mode_store(struct mddev *mddev,
  2246. const char *page, size_t length)
  2247. {
  2248. int mode = ARRAY_SIZE(r5c_journal_mode_str);
  2249. size_t len = length;
  2250. int ret;
  2251. if (len < 2)
  2252. return -EINVAL;
  2253. if (page[len - 1] == '\n')
  2254. len--;
  2255. while (mode--)
  2256. if (strlen(r5c_journal_mode_str[mode]) == len &&
  2257. !strncmp(page, r5c_journal_mode_str[mode], len))
  2258. break;
  2259. ret = mddev_lock(mddev);
  2260. if (ret)
  2261. return ret;
  2262. ret = r5c_journal_mode_set(mddev, mode);
  2263. mddev_unlock(mddev);
  2264. return ret ?: length;
  2265. }
  2266. struct md_sysfs_entry
  2267. r5c_journal_mode = __ATTR(journal_mode, 0644,
  2268. r5c_journal_mode_show, r5c_journal_mode_store);
  2269. /*
  2270. * Try handle write operation in caching phase. This function should only
  2271. * be called in write-back mode.
  2272. *
  2273. * If all outstanding writes can be handled in caching phase, returns 0
  2274. * If writes requires write-out phase, call r5c_make_stripe_write_out()
  2275. * and returns -EAGAIN
  2276. */
  2277. int r5c_try_caching_write(struct r5conf *conf,
  2278. struct stripe_head *sh,
  2279. struct stripe_head_state *s,
  2280. int disks)
  2281. {
  2282. struct r5l_log *log = conf->log;
  2283. int i;
  2284. struct r5dev *dev;
  2285. int to_cache = 0;
  2286. void **pslot;
  2287. sector_t tree_index;
  2288. int ret;
  2289. uintptr_t refcount;
  2290. BUG_ON(!r5c_is_writeback(log));
  2291. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  2292. /*
  2293. * There are two different scenarios here:
  2294. * 1. The stripe has some data cached, and it is sent to
  2295. * write-out phase for reclaim
  2296. * 2. The stripe is clean, and this is the first write
  2297. *
  2298. * For 1, return -EAGAIN, so we continue with
  2299. * handle_stripe_dirtying().
  2300. *
  2301. * For 2, set STRIPE_R5C_CACHING and continue with caching
  2302. * write.
  2303. */
  2304. /* case 1: anything injournal or anything in written */
  2305. if (s->injournal > 0 || s->written > 0)
  2306. return -EAGAIN;
  2307. /* case 2 */
  2308. set_bit(STRIPE_R5C_CACHING, &sh->state);
  2309. }
  2310. /*
  2311. * When run in degraded mode, array is set to write-through mode.
  2312. * This check helps drain pending write safely in the transition to
  2313. * write-through mode.
  2314. *
  2315. * When a stripe is syncing, the write is also handled in write
  2316. * through mode.
  2317. */
  2318. if (s->failed || test_bit(STRIPE_SYNCING, &sh->state)) {
  2319. r5c_make_stripe_write_out(sh);
  2320. return -EAGAIN;
  2321. }
  2322. for (i = disks; i--; ) {
  2323. dev = &sh->dev[i];
  2324. /* if non-overwrite, use writing-out phase */
  2325. if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
  2326. !test_bit(R5_InJournal, &dev->flags)) {
  2327. r5c_make_stripe_write_out(sh);
  2328. return -EAGAIN;
  2329. }
  2330. }
  2331. /* if the stripe is not counted in big_stripe_tree, add it now */
  2332. if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
  2333. !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
  2334. tree_index = r5c_tree_index(conf, sh->sector);
  2335. spin_lock(&log->tree_lock);
  2336. pslot = radix_tree_lookup_slot(&log->big_stripe_tree,
  2337. tree_index);
  2338. if (pslot) {
  2339. refcount = (uintptr_t)radix_tree_deref_slot_protected(
  2340. pslot, &log->tree_lock) >>
  2341. R5C_RADIX_COUNT_SHIFT;
  2342. radix_tree_replace_slot(
  2343. &log->big_stripe_tree, pslot,
  2344. (void *)((refcount + 1) << R5C_RADIX_COUNT_SHIFT));
  2345. } else {
  2346. /*
  2347. * this radix_tree_insert can fail safely, so no
  2348. * need to call radix_tree_preload()
  2349. */
  2350. ret = radix_tree_insert(
  2351. &log->big_stripe_tree, tree_index,
  2352. (void *)(1 << R5C_RADIX_COUNT_SHIFT));
  2353. if (ret) {
  2354. spin_unlock(&log->tree_lock);
  2355. r5c_make_stripe_write_out(sh);
  2356. return -EAGAIN;
  2357. }
  2358. }
  2359. spin_unlock(&log->tree_lock);
  2360. /*
  2361. * set STRIPE_R5C_PARTIAL_STRIPE, this shows the stripe is
  2362. * counted in the radix tree
  2363. */
  2364. set_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state);
  2365. atomic_inc(&conf->r5c_cached_partial_stripes);
  2366. }
  2367. for (i = disks; i--; ) {
  2368. dev = &sh->dev[i];
  2369. if (dev->towrite) {
  2370. set_bit(R5_Wantwrite, &dev->flags);
  2371. set_bit(R5_Wantdrain, &dev->flags);
  2372. set_bit(R5_LOCKED, &dev->flags);
  2373. to_cache++;
  2374. }
  2375. }
  2376. if (to_cache) {
  2377. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  2378. /*
  2379. * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
  2380. * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
  2381. * r5c_handle_data_cached()
  2382. */
  2383. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  2384. }
  2385. return 0;
  2386. }
  2387. /*
  2388. * free extra pages (orig_page) we allocated for prexor
  2389. */
  2390. void r5c_release_extra_page(struct stripe_head *sh)
  2391. {
  2392. struct r5conf *conf = sh->raid_conf;
  2393. int i;
  2394. bool using_disk_info_extra_page;
  2395. using_disk_info_extra_page =
  2396. sh->dev[0].orig_page == conf->disks[0].extra_page;
  2397. for (i = sh->disks; i--; )
  2398. if (sh->dev[i].page != sh->dev[i].orig_page) {
  2399. struct page *p = sh->dev[i].orig_page;
  2400. sh->dev[i].orig_page = sh->dev[i].page;
  2401. clear_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
  2402. if (!using_disk_info_extra_page)
  2403. put_page(p);
  2404. }
  2405. if (using_disk_info_extra_page) {
  2406. clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
  2407. md_wakeup_thread(conf->mddev->thread);
  2408. }
  2409. }
  2410. void r5c_use_extra_page(struct stripe_head *sh)
  2411. {
  2412. struct r5conf *conf = sh->raid_conf;
  2413. int i;
  2414. struct r5dev *dev;
  2415. for (i = sh->disks; i--; ) {
  2416. dev = &sh->dev[i];
  2417. if (dev->orig_page != dev->page)
  2418. put_page(dev->orig_page);
  2419. dev->orig_page = conf->disks[i].extra_page;
  2420. }
  2421. }
  2422. /*
  2423. * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
  2424. * stripe is committed to RAID disks.
  2425. */
  2426. void r5c_finish_stripe_write_out(struct r5conf *conf,
  2427. struct stripe_head *sh,
  2428. struct stripe_head_state *s)
  2429. {
  2430. struct r5l_log *log = conf->log;
  2431. int i;
  2432. int do_wakeup = 0;
  2433. sector_t tree_index;
  2434. void **pslot;
  2435. uintptr_t refcount;
  2436. if (!log || !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
  2437. return;
  2438. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  2439. clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  2440. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  2441. return;
  2442. for (i = sh->disks; i--; ) {
  2443. clear_bit(R5_InJournal, &sh->dev[i].flags);
  2444. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  2445. do_wakeup = 1;
  2446. }
  2447. /*
  2448. * analyse_stripe() runs before r5c_finish_stripe_write_out(),
  2449. * We updated R5_InJournal, so we also update s->injournal.
  2450. */
  2451. s->injournal = 0;
  2452. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2453. if (atomic_dec_and_test(&conf->pending_full_writes))
  2454. md_wakeup_thread(conf->mddev->thread);
  2455. if (do_wakeup)
  2456. wake_up(&conf->wait_for_overlap);
  2457. spin_lock_irq(&log->stripe_in_journal_lock);
  2458. list_del_init(&sh->r5c);
  2459. spin_unlock_irq(&log->stripe_in_journal_lock);
  2460. sh->log_start = MaxSector;
  2461. atomic_dec(&log->stripe_in_journal_count);
  2462. r5c_update_log_state(log);
  2463. /* stop counting this stripe in big_stripe_tree */
  2464. if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) ||
  2465. test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
  2466. tree_index = r5c_tree_index(conf, sh->sector);
  2467. spin_lock(&log->tree_lock);
  2468. pslot = radix_tree_lookup_slot(&log->big_stripe_tree,
  2469. tree_index);
  2470. BUG_ON(pslot == NULL);
  2471. refcount = (uintptr_t)radix_tree_deref_slot_protected(
  2472. pslot, &log->tree_lock) >>
  2473. R5C_RADIX_COUNT_SHIFT;
  2474. if (refcount == 1)
  2475. radix_tree_delete(&log->big_stripe_tree, tree_index);
  2476. else
  2477. radix_tree_replace_slot(
  2478. &log->big_stripe_tree, pslot,
  2479. (void *)((refcount - 1) << R5C_RADIX_COUNT_SHIFT));
  2480. spin_unlock(&log->tree_lock);
  2481. }
  2482. if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
  2483. BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
  2484. atomic_dec(&conf->r5c_flushing_partial_stripes);
  2485. atomic_dec(&conf->r5c_cached_partial_stripes);
  2486. }
  2487. if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
  2488. BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
  2489. atomic_dec(&conf->r5c_flushing_full_stripes);
  2490. atomic_dec(&conf->r5c_cached_full_stripes);
  2491. }
  2492. r5l_append_flush_payload(log, sh->sector);
  2493. /* stripe is flused to raid disks, we can do resync now */
  2494. if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
  2495. set_bit(STRIPE_HANDLE, &sh->state);
  2496. }
  2497. int r5c_cache_data(struct r5l_log *log, struct stripe_head *sh)
  2498. {
  2499. struct r5conf *conf = sh->raid_conf;
  2500. int pages = 0;
  2501. int reserve;
  2502. int i;
  2503. int ret = 0;
  2504. BUG_ON(!log);
  2505. for (i = 0; i < sh->disks; i++) {
  2506. void *addr;
  2507. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
  2508. continue;
  2509. addr = kmap_atomic(sh->dev[i].page);
  2510. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  2511. addr, PAGE_SIZE);
  2512. kunmap_atomic(addr);
  2513. pages++;
  2514. }
  2515. WARN_ON(pages == 0);
  2516. /*
  2517. * The stripe must enter state machine again to call endio, so
  2518. * don't delay.
  2519. */
  2520. clear_bit(STRIPE_DELAYED, &sh->state);
  2521. atomic_inc(&sh->count);
  2522. mutex_lock(&log->io_mutex);
  2523. /* meta + data */
  2524. reserve = (1 + pages) << (PAGE_SHIFT - 9);
  2525. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  2526. sh->log_start == MaxSector)
  2527. r5l_add_no_space_stripe(log, sh);
  2528. else if (!r5l_has_free_space(log, reserve)) {
  2529. if (sh->log_start == log->last_checkpoint)
  2530. BUG();
  2531. else
  2532. r5l_add_no_space_stripe(log, sh);
  2533. } else {
  2534. ret = r5l_log_stripe(log, sh, pages, 0);
  2535. if (ret) {
  2536. spin_lock_irq(&log->io_list_lock);
  2537. list_add_tail(&sh->log_list, &log->no_mem_stripes);
  2538. spin_unlock_irq(&log->io_list_lock);
  2539. }
  2540. }
  2541. mutex_unlock(&log->io_mutex);
  2542. return 0;
  2543. }
  2544. /* check whether this big stripe is in write back cache. */
  2545. bool r5c_big_stripe_cached(struct r5conf *conf, sector_t sect)
  2546. {
  2547. struct r5l_log *log = conf->log;
  2548. sector_t tree_index;
  2549. void *slot;
  2550. if (!log)
  2551. return false;
  2552. WARN_ON_ONCE(!rcu_read_lock_held());
  2553. tree_index = r5c_tree_index(conf, sect);
  2554. slot = radix_tree_lookup(&log->big_stripe_tree, tree_index);
  2555. return slot != NULL;
  2556. }
  2557. static int r5l_load_log(struct r5l_log *log)
  2558. {
  2559. struct md_rdev *rdev = log->rdev;
  2560. struct page *page;
  2561. struct r5l_meta_block *mb;
  2562. sector_t cp = log->rdev->journal_tail;
  2563. u32 stored_crc, expected_crc;
  2564. bool create_super = false;
  2565. int ret = 0;
  2566. /* Make sure it's valid */
  2567. if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
  2568. cp = 0;
  2569. page = alloc_page(GFP_KERNEL);
  2570. if (!page)
  2571. return -ENOMEM;
  2572. if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
  2573. ret = -EIO;
  2574. goto ioerr;
  2575. }
  2576. mb = page_address(page);
  2577. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  2578. mb->version != R5LOG_VERSION) {
  2579. create_super = true;
  2580. goto create;
  2581. }
  2582. stored_crc = le32_to_cpu(mb->checksum);
  2583. mb->checksum = 0;
  2584. expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  2585. if (stored_crc != expected_crc) {
  2586. create_super = true;
  2587. goto create;
  2588. }
  2589. if (le64_to_cpu(mb->position) != cp) {
  2590. create_super = true;
  2591. goto create;
  2592. }
  2593. create:
  2594. if (create_super) {
  2595. log->last_cp_seq = prandom_u32();
  2596. cp = 0;
  2597. r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
  2598. /*
  2599. * Make sure super points to correct address. Log might have
  2600. * data very soon. If super hasn't correct log tail address,
  2601. * recovery can't find the log
  2602. */
  2603. r5l_write_super(log, cp);
  2604. } else
  2605. log->last_cp_seq = le64_to_cpu(mb->seq);
  2606. log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
  2607. log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
  2608. if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
  2609. log->max_free_space = RECLAIM_MAX_FREE_SPACE;
  2610. log->last_checkpoint = cp;
  2611. __free_page(page);
  2612. if (create_super) {
  2613. log->log_start = r5l_ring_add(log, cp, BLOCK_SECTORS);
  2614. log->seq = log->last_cp_seq + 1;
  2615. log->next_checkpoint = cp;
  2616. } else
  2617. ret = r5l_recovery_log(log);
  2618. r5c_update_log_state(log);
  2619. return ret;
  2620. ioerr:
  2621. __free_page(page);
  2622. return ret;
  2623. }
  2624. void r5c_update_on_rdev_error(struct mddev *mddev, struct md_rdev *rdev)
  2625. {
  2626. struct r5conf *conf = mddev->private;
  2627. struct r5l_log *log = conf->log;
  2628. if (!log)
  2629. return;
  2630. if ((raid5_calc_degraded(conf) > 0 ||
  2631. test_bit(Journal, &rdev->flags)) &&
  2632. conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK)
  2633. schedule_work(&log->disable_writeback_work);
  2634. }
  2635. int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
  2636. {
  2637. struct request_queue *q = bdev_get_queue(rdev->bdev);
  2638. struct r5l_log *log;
  2639. char b[BDEVNAME_SIZE];
  2640. pr_debug("md/raid:%s: using device %s as journal\n",
  2641. mdname(conf->mddev), bdevname(rdev->bdev, b));
  2642. if (PAGE_SIZE != 4096)
  2643. return -EINVAL;
  2644. /*
  2645. * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
  2646. * raid_disks r5l_payload_data_parity.
  2647. *
  2648. * Write journal and cache does not work for very big array
  2649. * (raid_disks > 203)
  2650. */
  2651. if (sizeof(struct r5l_meta_block) +
  2652. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
  2653. conf->raid_disks) > PAGE_SIZE) {
  2654. pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
  2655. mdname(conf->mddev), conf->raid_disks);
  2656. return -EINVAL;
  2657. }
  2658. log = kzalloc(sizeof(*log), GFP_KERNEL);
  2659. if (!log)
  2660. return -ENOMEM;
  2661. log->rdev = rdev;
  2662. log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
  2663. log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
  2664. sizeof(rdev->mddev->uuid));
  2665. mutex_init(&log->io_mutex);
  2666. spin_lock_init(&log->io_list_lock);
  2667. INIT_LIST_HEAD(&log->running_ios);
  2668. INIT_LIST_HEAD(&log->io_end_ios);
  2669. INIT_LIST_HEAD(&log->flushing_ios);
  2670. INIT_LIST_HEAD(&log->finished_ios);
  2671. bio_init(&log->flush_bio, NULL, 0);
  2672. log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
  2673. if (!log->io_kc)
  2674. goto io_kc;
  2675. log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
  2676. if (!log->io_pool)
  2677. goto io_pool;
  2678. log->bs = bioset_create(R5L_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  2679. if (!log->bs)
  2680. goto io_bs;
  2681. log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
  2682. if (!log->meta_pool)
  2683. goto out_mempool;
  2684. spin_lock_init(&log->tree_lock);
  2685. INIT_RADIX_TREE(&log->big_stripe_tree, GFP_NOWAIT | __GFP_NOWARN);
  2686. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  2687. log->rdev->mddev, "reclaim");
  2688. if (!log->reclaim_thread)
  2689. goto reclaim_thread;
  2690. log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
  2691. init_waitqueue_head(&log->iounit_wait);
  2692. INIT_LIST_HEAD(&log->no_mem_stripes);
  2693. INIT_LIST_HEAD(&log->no_space_stripes);
  2694. spin_lock_init(&log->no_space_stripes_lock);
  2695. INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
  2696. INIT_WORK(&log->disable_writeback_work, r5c_disable_writeback_async);
  2697. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  2698. INIT_LIST_HEAD(&log->stripe_in_journal_list);
  2699. spin_lock_init(&log->stripe_in_journal_lock);
  2700. atomic_set(&log->stripe_in_journal_count, 0);
  2701. rcu_assign_pointer(conf->log, log);
  2702. if (r5l_load_log(log))
  2703. goto error;
  2704. set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  2705. return 0;
  2706. error:
  2707. rcu_assign_pointer(conf->log, NULL);
  2708. md_unregister_thread(&log->reclaim_thread);
  2709. reclaim_thread:
  2710. mempool_destroy(log->meta_pool);
  2711. out_mempool:
  2712. bioset_free(log->bs);
  2713. io_bs:
  2714. mempool_destroy(log->io_pool);
  2715. io_pool:
  2716. kmem_cache_destroy(log->io_kc);
  2717. io_kc:
  2718. kfree(log);
  2719. return -EINVAL;
  2720. }
  2721. void r5l_exit_log(struct r5conf *conf)
  2722. {
  2723. struct r5l_log *log = conf->log;
  2724. conf->log = NULL;
  2725. synchronize_rcu();
  2726. /* Ensure disable_writeback_work wakes up and exits */
  2727. wake_up(&conf->mddev->sb_wait);
  2728. flush_work(&log->disable_writeback_work);
  2729. md_unregister_thread(&log->reclaim_thread);
  2730. mempool_destroy(log->meta_pool);
  2731. bioset_free(log->bs);
  2732. mempool_destroy(log->io_pool);
  2733. kmem_cache_destroy(log->io_kc);
  2734. kfree(log);
  2735. }