raid5-cache.c 87 KB

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