raid5-cache.c 85 KB

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