raid5-cache.c 88 KB

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