raid5-cache.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186
  1. /*
  2. * Copyright (C) 2015 Shaohua Li <shli@fb.com>
  3. * Copyright (C) 2016 Song Liu <songliubraving@fb.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/wait.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/raid/md_p.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/random.h>
  22. #include <linux/kthread.h>
  23. #include <linux/types.h>
  24. #include "md.h"
  25. #include "raid5.h"
  26. #include "md-bitmap.h"
  27. #include "raid5-log.h"
  28. /*
  29. * metadata/data stored in disk with 4k size unit (a block) regardless
  30. * underneath hardware sector size. only works with PAGE_SIZE == 4096
  31. */
  32. #define BLOCK_SECTORS (8)
  33. #define BLOCK_SECTOR_SHIFT (3)
  34. /*
  35. * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
  36. *
  37. * In write through mode, the reclaim runs every log->max_free_space.
  38. * This can prevent the recovery scans for too long
  39. */
  40. #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
  41. #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
  42. /* wake up reclaim thread periodically */
  43. #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
  44. /* start flush with these full stripes */
  45. #define R5C_FULL_STRIPE_FLUSH_BATCH(conf) (conf->max_nr_stripes / 4)
  46. /* reclaim stripes in groups */
  47. #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
  48. /*
  49. * We only need 2 bios per I/O unit to make progress, but ensure we
  50. * have a few more available to not get too tight.
  51. */
  52. #define R5L_POOL_SIZE 4
  53. static char *r5c_journal_mode_str[] = {"write-through",
  54. "write-back"};
  55. /*
  56. * raid5 cache state machine
  57. *
  58. * With the RAID cache, each stripe works in two phases:
  59. * - caching phase
  60. * - writing-out phase
  61. *
  62. * These two phases are controlled by bit STRIPE_R5C_CACHING:
  63. * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
  64. * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
  65. *
  66. * When there is no journal, or the journal is in write-through mode,
  67. * the stripe is always in writing-out phase.
  68. *
  69. * For write-back journal, the stripe is sent to caching phase on write
  70. * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
  71. * the write-out phase by clearing STRIPE_R5C_CACHING.
  72. *
  73. * Stripes in caching phase do not write the raid disks. Instead, all
  74. * writes are committed from the log device. Therefore, a stripe in
  75. * caching phase handles writes as:
  76. * - write to log device
  77. * - return IO
  78. *
  79. * Stripes in writing-out phase handle writes as:
  80. * - calculate parity
  81. * - write pending data and parity to journal
  82. * - write data and parity to raid disks
  83. * - return IO for pending writes
  84. */
  85. struct r5l_log {
  86. struct md_rdev *rdev;
  87. u32 uuid_checksum;
  88. sector_t device_size; /* log device size, round to
  89. * BLOCK_SECTORS */
  90. sector_t max_free_space; /* reclaim run if free space is at
  91. * this size */
  92. sector_t last_checkpoint; /* log tail. where recovery scan
  93. * starts from */
  94. u64 last_cp_seq; /* log tail sequence */
  95. sector_t log_start; /* log head. where new data appends */
  96. u64 seq; /* log head sequence */
  97. sector_t next_checkpoint;
  98. struct mutex io_mutex;
  99. struct r5l_io_unit *current_io; /* current io_unit accepting new data */
  100. spinlock_t io_list_lock;
  101. struct list_head running_ios; /* io_units which are still running,
  102. * and have not yet been completely
  103. * written to the log */
  104. struct list_head io_end_ios; /* io_units which have been completely
  105. * written to the log but not yet written
  106. * to the RAID */
  107. struct list_head flushing_ios; /* io_units which are waiting for log
  108. * cache flush */
  109. struct list_head finished_ios; /* io_units which settle down in log disk */
  110. struct bio flush_bio;
  111. struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
  112. struct kmem_cache *io_kc;
  113. mempool_t *io_pool;
  114. struct bio_set *bs;
  115. mempool_t *meta_pool;
  116. struct md_thread *reclaim_thread;
  117. unsigned long reclaim_target; /* number of space that need to be
  118. * reclaimed. if it's 0, reclaim spaces
  119. * used by io_units which are in
  120. * IO_UNIT_STRIPE_END state (eg, reclaim
  121. * dones't wait for specific io_unit
  122. * switching to IO_UNIT_STRIPE_END
  123. * state) */
  124. wait_queue_head_t iounit_wait;
  125. struct list_head no_space_stripes; /* pending stripes, log has no space */
  126. spinlock_t no_space_stripes_lock;
  127. bool need_cache_flush;
  128. /* for r5c_cache */
  129. enum r5c_journal_mode r5c_journal_mode;
  130. /* all stripes in r5cache, in the order of seq at sh->log_start */
  131. struct list_head stripe_in_journal_list;
  132. spinlock_t stripe_in_journal_lock;
  133. atomic_t stripe_in_journal_count;
  134. /* to submit async io_units, to fulfill ordering of flush */
  135. struct work_struct deferred_io_work;
  136. /* to disable write back during in degraded mode */
  137. struct work_struct disable_writeback_work;
  138. /* to for chunk_aligned_read in writeback mode, details below */
  139. spinlock_t tree_lock;
  140. struct radix_tree_root big_stripe_tree;
  141. };
  142. /*
  143. * Enable chunk_aligned_read() with write back cache.
  144. *
  145. * Each chunk may contain more than one stripe (for example, a 256kB
  146. * chunk contains 64 4kB-page, so this chunk contain 64 stripes). For
  147. * chunk_aligned_read, these stripes are grouped into one "big_stripe".
  148. * For each big_stripe, we count how many stripes of this big_stripe
  149. * are in the write back cache. These data are tracked in a radix tree
  150. * (big_stripe_tree). We use radix_tree item pointer as the counter.
  151. * r5c_tree_index() is used to calculate keys for the radix tree.
  152. *
  153. * chunk_aligned_read() calls r5c_big_stripe_cached() to look up
  154. * big_stripe of each chunk in the tree. If this big_stripe is in the
  155. * tree, chunk_aligned_read() aborts. This look up is protected by
  156. * rcu_read_lock().
  157. *
  158. * It is necessary to remember whether a stripe is counted in
  159. * big_stripe_tree. Instead of adding new flag, we reuses existing flags:
  160. * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE. If either of these
  161. * two flags are set, the stripe is counted in big_stripe_tree. This
  162. * requires moving set_bit(STRIPE_R5C_PARTIAL_STRIPE) to
  163. * r5c_try_caching_write(); and moving clear_bit of
  164. * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE to
  165. * r5c_finish_stripe_write_out().
  166. */
  167. /*
  168. * radix tree requests lowest 2 bits of data pointer to be 2b'00.
  169. * So it is necessary to left shift the counter by 2 bits before using it
  170. * as data pointer of the tree.
  171. */
  172. #define R5C_RADIX_COUNT_SHIFT 2
  173. /*
  174. * calculate key for big_stripe_tree
  175. *
  176. * sect: align_bi->bi_iter.bi_sector or sh->sector
  177. */
  178. static inline sector_t r5c_tree_index(struct r5conf *conf,
  179. sector_t sect)
  180. {
  181. sector_t offset;
  182. offset = sector_div(sect, conf->chunk_sectors);
  183. return sect;
  184. }
  185. /*
  186. * an IO range starts from a meta data block and end at the next meta data
  187. * block. The io unit's the meta data block tracks data/parity followed it. io
  188. * unit is written to log disk with normal write, as we always flush log disk
  189. * first and then start move data to raid disks, there is no requirement to
  190. * write io unit with FLUSH/FUA
  191. */
  192. struct r5l_io_unit {
  193. struct r5l_log *log;
  194. struct page *meta_page; /* store meta block */
  195. int meta_offset; /* current offset in meta_page */
  196. struct bio *current_bio;/* current_bio accepting new data */
  197. atomic_t pending_stripe;/* how many stripes not flushed to raid */
  198. u64 seq; /* seq number of the metablock */
  199. sector_t log_start; /* where the io_unit starts */
  200. sector_t log_end; /* where the io_unit ends */
  201. struct list_head log_sibling; /* log->running_ios */
  202. struct list_head stripe_list; /* stripes added to the io_unit */
  203. int state;
  204. bool need_split_bio;
  205. struct bio *split_bio;
  206. unsigned int has_flush:1; /* include flush request */
  207. unsigned int has_fua:1; /* include fua request */
  208. unsigned int has_null_flush:1; /* include null flush request */
  209. unsigned int has_flush_payload:1; /* include flush payload */
  210. /*
  211. * io isn't sent yet, flush/fua request can only be submitted till it's
  212. * the first IO in running_ios list
  213. */
  214. unsigned int io_deferred:1;
  215. struct bio_list flush_barriers; /* size == 0 flush bios */
  216. };
  217. /* r5l_io_unit state */
  218. enum r5l_io_unit_state {
  219. IO_UNIT_RUNNING = 0, /* accepting new IO */
  220. IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
  221. * don't accepting new bio */
  222. IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
  223. IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
  224. };
  225. bool r5c_is_writeback(struct r5l_log *log)
  226. {
  227. return (log != NULL &&
  228. log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
  229. }
  230. static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
  231. {
  232. start += inc;
  233. if (start >= log->device_size)
  234. start = start - log->device_size;
  235. return start;
  236. }
  237. static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
  238. sector_t end)
  239. {
  240. if (end >= start)
  241. return end - start;
  242. else
  243. return end + log->device_size - start;
  244. }
  245. static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
  246. {
  247. sector_t used_size;
  248. used_size = r5l_ring_distance(log, log->last_checkpoint,
  249. log->log_start);
  250. return log->device_size > used_size + size;
  251. }
  252. static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
  253. enum r5l_io_unit_state state)
  254. {
  255. if (WARN_ON(io->state >= state))
  256. return;
  257. io->state = state;
  258. }
  259. static void
  260. r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev)
  261. {
  262. struct bio *wbi, *wbi2;
  263. wbi = dev->written;
  264. dev->written = NULL;
  265. while (wbi && wbi->bi_iter.bi_sector <
  266. dev->sector + STRIPE_SECTORS) {
  267. wbi2 = r5_next_bio(wbi, dev->sector);
  268. md_write_end(conf->mddev);
  269. bio_endio(wbi);
  270. wbi = wbi2;
  271. }
  272. }
  273. void r5c_handle_cached_data_endio(struct r5conf *conf,
  274. struct stripe_head *sh, int disks)
  275. {
  276. int i;
  277. for (i = sh->disks; i--; ) {
  278. if (sh->dev[i].written) {
  279. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  280. r5c_return_dev_pending_writes(conf, &sh->dev[i]);
  281. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  282. STRIPE_SECTORS,
  283. !test_bit(STRIPE_DEGRADED, &sh->state),
  284. 0);
  285. }
  286. }
  287. }
  288. void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
  289. /* Check whether we should flush some stripes to free up stripe cache */
  290. void r5c_check_stripe_cache_usage(struct r5conf *conf)
  291. {
  292. int total_cached;
  293. if (!r5c_is_writeback(conf->log))
  294. return;
  295. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  296. atomic_read(&conf->r5c_cached_full_stripes);
  297. /*
  298. * The following condition is true for either of the following:
  299. * - stripe cache pressure high:
  300. * total_cached > 3/4 min_nr_stripes ||
  301. * empty_inactive_list_nr > 0
  302. * - stripe cache pressure moderate:
  303. * total_cached > 1/2 min_nr_stripes
  304. */
  305. if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  306. atomic_read(&conf->empty_inactive_list_nr) > 0)
  307. r5l_wake_reclaim(conf->log, 0);
  308. }
  309. /*
  310. * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
  311. * stripes in the cache
  312. */
  313. void r5c_check_cached_full_stripe(struct r5conf *conf)
  314. {
  315. if (!r5c_is_writeback(conf->log))
  316. return;
  317. /*
  318. * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
  319. * or a full stripe (chunk size / 4k stripes).
  320. */
  321. if (atomic_read(&conf->r5c_cached_full_stripes) >=
  322. min(R5C_FULL_STRIPE_FLUSH_BATCH(conf),
  323. conf->chunk_sectors >> STRIPE_SHIFT))
  324. r5l_wake_reclaim(conf->log, 0);
  325. }
  326. /*
  327. * Total log space (in sectors) needed to flush all data in cache
  328. *
  329. * To avoid deadlock due to log space, it is necessary to reserve log
  330. * space to flush critical stripes (stripes that occupying log space near
  331. * last_checkpoint). This function helps check how much log space is
  332. * required to flush all cached stripes.
  333. *
  334. * To reduce log space requirements, two mechanisms are used to give cache
  335. * flush higher priorities:
  336. * 1. In handle_stripe_dirtying() and schedule_reconstruction(),
  337. * stripes ALREADY in journal can be flushed w/o pending writes;
  338. * 2. In r5l_write_stripe() and r5c_cache_data(), stripes NOT in journal
  339. * can be delayed (r5l_add_no_space_stripe).
  340. *
  341. * In cache flush, the stripe goes through 1 and then 2. For a stripe that
  342. * already passed 1, flushing it requires at most (conf->max_degraded + 1)
  343. * pages of journal space. For stripes that has not passed 1, flushing it
  344. * requires (conf->raid_disks + 1) pages of journal space. There are at
  345. * most (conf->group_cnt + 1) stripe that passed 1. So total journal space
  346. * required to flush all cached stripes (in pages) is:
  347. *
  348. * (stripe_in_journal_count - group_cnt - 1) * (max_degraded + 1) +
  349. * (group_cnt + 1) * (raid_disks + 1)
  350. * or
  351. * (stripe_in_journal_count) * (max_degraded + 1) +
  352. * (group_cnt + 1) * (raid_disks - max_degraded)
  353. */
  354. static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
  355. {
  356. struct r5l_log *log = conf->log;
  357. if (!r5c_is_writeback(log))
  358. return 0;
  359. return BLOCK_SECTORS *
  360. ((conf->max_degraded + 1) * atomic_read(&log->stripe_in_journal_count) +
  361. (conf->raid_disks - conf->max_degraded) * (conf->group_cnt + 1));
  362. }
  363. /*
  364. * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
  365. *
  366. * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
  367. * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
  368. * device is less than 2x of reclaim_required_space.
  369. */
  370. static inline void r5c_update_log_state(struct r5l_log *log)
  371. {
  372. struct r5conf *conf = log->rdev->mddev->private;
  373. sector_t free_space;
  374. sector_t reclaim_space;
  375. bool wake_reclaim = false;
  376. if (!r5c_is_writeback(log))
  377. return;
  378. free_space = r5l_ring_distance(log, log->log_start,
  379. log->last_checkpoint);
  380. reclaim_space = r5c_log_required_to_flush_cache(conf);
  381. if (free_space < 2 * reclaim_space)
  382. set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
  383. else {
  384. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
  385. wake_reclaim = true;
  386. clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
  387. }
  388. if (free_space < 3 * reclaim_space)
  389. set_bit(R5C_LOG_TIGHT, &conf->cache_state);
  390. else
  391. clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
  392. if (wake_reclaim)
  393. r5l_wake_reclaim(log, 0);
  394. }
  395. /*
  396. * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
  397. * This function should only be called in write-back mode.
  398. */
  399. void r5c_make_stripe_write_out(struct stripe_head *sh)
  400. {
  401. struct r5conf *conf = sh->raid_conf;
  402. struct r5l_log *log = conf->log;
  403. BUG_ON(!r5c_is_writeback(log));
  404. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  405. clear_bit(STRIPE_R5C_CACHING, &sh->state);
  406. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  407. atomic_inc(&conf->preread_active_stripes);
  408. }
  409. static void r5c_handle_data_cached(struct stripe_head *sh)
  410. {
  411. int i;
  412. for (i = sh->disks; i--; )
  413. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  414. set_bit(R5_InJournal, &sh->dev[i].flags);
  415. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  416. }
  417. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  418. }
  419. /*
  420. * this journal write must contain full parity,
  421. * it may also contain some data pages
  422. */
  423. static void r5c_handle_parity_cached(struct stripe_head *sh)
  424. {
  425. int i;
  426. for (i = sh->disks; i--; )
  427. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  428. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  429. }
  430. /*
  431. * Setting proper flags after writing (or flushing) data and/or parity to the
  432. * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
  433. */
  434. static void r5c_finish_cache_stripe(struct stripe_head *sh)
  435. {
  436. struct r5l_log *log = sh->raid_conf->log;
  437. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  438. BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  439. /*
  440. * Set R5_InJournal for parity dev[pd_idx]. This means
  441. * all data AND parity in the journal. For RAID 6, it is
  442. * NOT necessary to set the flag for dev[qd_idx], as the
  443. * two parities are written out together.
  444. */
  445. set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  446. } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  447. r5c_handle_data_cached(sh);
  448. } else {
  449. r5c_handle_parity_cached(sh);
  450. set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  451. }
  452. }
  453. static void r5l_io_run_stripes(struct r5l_io_unit *io)
  454. {
  455. struct stripe_head *sh, *next;
  456. list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
  457. list_del_init(&sh->log_list);
  458. r5c_finish_cache_stripe(sh);
  459. set_bit(STRIPE_HANDLE, &sh->state);
  460. raid5_release_stripe(sh);
  461. }
  462. }
  463. static void r5l_log_run_stripes(struct r5l_log *log)
  464. {
  465. struct r5l_io_unit *io, *next;
  466. lockdep_assert_held(&log->io_list_lock);
  467. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  468. /* don't change list order */
  469. if (io->state < IO_UNIT_IO_END)
  470. break;
  471. list_move_tail(&io->log_sibling, &log->finished_ios);
  472. r5l_io_run_stripes(io);
  473. }
  474. }
  475. static void r5l_move_to_end_ios(struct r5l_log *log)
  476. {
  477. struct r5l_io_unit *io, *next;
  478. lockdep_assert_held(&log->io_list_lock);
  479. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  480. /* don't change list order */
  481. if (io->state < IO_UNIT_IO_END)
  482. break;
  483. list_move_tail(&io->log_sibling, &log->io_end_ios);
  484. }
  485. }
  486. static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
  487. static void r5l_log_endio(struct bio *bio)
  488. {
  489. struct r5l_io_unit *io = bio->bi_private;
  490. struct r5l_io_unit *io_deferred;
  491. struct r5l_log *log = io->log;
  492. unsigned long flags;
  493. bool has_null_flush;
  494. bool has_flush_payload;
  495. if (bio->bi_status)
  496. md_error(log->rdev->mddev, log->rdev);
  497. bio_put(bio);
  498. mempool_free(io->meta_page, log->meta_pool);
  499. spin_lock_irqsave(&log->io_list_lock, flags);
  500. __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
  501. /*
  502. * if the io doesn't not have null_flush or flush payload,
  503. * it is not safe to access it after releasing io_list_lock.
  504. * Therefore, it is necessary to check the condition with
  505. * the lock held.
  506. */
  507. has_null_flush = io->has_null_flush;
  508. has_flush_payload = io->has_flush_payload;
  509. if (log->need_cache_flush && !list_empty(&io->stripe_list))
  510. r5l_move_to_end_ios(log);
  511. else
  512. r5l_log_run_stripes(log);
  513. if (!list_empty(&log->running_ios)) {
  514. /*
  515. * FLUSH/FUA io_unit is deferred because of ordering, now we
  516. * can dispatch it
  517. */
  518. io_deferred = list_first_entry(&log->running_ios,
  519. struct r5l_io_unit, log_sibling);
  520. if (io_deferred->io_deferred)
  521. schedule_work(&log->deferred_io_work);
  522. }
  523. spin_unlock_irqrestore(&log->io_list_lock, flags);
  524. if (log->need_cache_flush)
  525. md_wakeup_thread(log->rdev->mddev->thread);
  526. /* finish flush only io_unit and PAYLOAD_FLUSH only io_unit */
  527. if (has_null_flush) {
  528. struct bio *bi;
  529. WARN_ON(bio_list_empty(&io->flush_barriers));
  530. while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
  531. bio_endio(bi);
  532. if (atomic_dec_and_test(&io->pending_stripe)) {
  533. __r5l_stripe_write_finished(io);
  534. return;
  535. }
  536. }
  537. }
  538. /* decrease pending_stripe for flush payload */
  539. if (has_flush_payload)
  540. if (atomic_dec_and_test(&io->pending_stripe))
  541. __r5l_stripe_write_finished(io);
  542. }
  543. static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
  544. {
  545. unsigned long flags;
  546. spin_lock_irqsave(&log->io_list_lock, flags);
  547. __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
  548. spin_unlock_irqrestore(&log->io_list_lock, flags);
  549. /*
  550. * In case of journal device failures, submit_bio will get error
  551. * and calls endio, then active stripes will continue write
  552. * process. Therefore, it is not necessary to check Faulty bit
  553. * of journal device here.
  554. *
  555. * We can't check split_bio after current_bio is submitted. If
  556. * io->split_bio is null, after current_bio is submitted, current_bio
  557. * might already be completed and the io_unit is freed. We submit
  558. * split_bio first to avoid the issue.
  559. */
  560. if (io->split_bio) {
  561. if (io->has_flush)
  562. io->split_bio->bi_opf |= REQ_PREFLUSH;
  563. if (io->has_fua)
  564. io->split_bio->bi_opf |= REQ_FUA;
  565. submit_bio(io->split_bio);
  566. }
  567. if (io->has_flush)
  568. io->current_bio->bi_opf |= REQ_PREFLUSH;
  569. if (io->has_fua)
  570. io->current_bio->bi_opf |= REQ_FUA;
  571. submit_bio(io->current_bio);
  572. }
  573. /* deferred io_unit will be dispatched here */
  574. static void r5l_submit_io_async(struct work_struct *work)
  575. {
  576. struct r5l_log *log = container_of(work, struct r5l_log,
  577. deferred_io_work);
  578. struct r5l_io_unit *io = NULL;
  579. unsigned long flags;
  580. spin_lock_irqsave(&log->io_list_lock, flags);
  581. if (!list_empty(&log->running_ios)) {
  582. io = list_first_entry(&log->running_ios, struct r5l_io_unit,
  583. log_sibling);
  584. if (!io->io_deferred)
  585. io = NULL;
  586. else
  587. io->io_deferred = 0;
  588. }
  589. spin_unlock_irqrestore(&log->io_list_lock, flags);
  590. if (io)
  591. r5l_do_submit_io(log, io);
  592. }
  593. static void r5c_disable_writeback_async(struct work_struct *work)
  594. {
  595. struct r5l_log *log = container_of(work, struct r5l_log,
  596. disable_writeback_work);
  597. struct mddev *mddev = log->rdev->mddev;
  598. struct r5conf *conf = mddev->private;
  599. int locked = 0;
  600. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  601. return;
  602. pr_info("md/raid:%s: Disabling writeback cache for degraded array.\n",
  603. mdname(mddev));
  604. /* wait superblock change before suspend */
  605. wait_event(mddev->sb_wait,
  606. conf->log == NULL ||
  607. (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) &&
  608. (locked = mddev_trylock(mddev))));
  609. if (locked) {
  610. mddev_suspend(mddev);
  611. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  612. mddev_resume(mddev);
  613. mddev_unlock(mddev);
  614. }
  615. }
  616. static void r5l_submit_current_io(struct r5l_log *log)
  617. {
  618. struct r5l_io_unit *io = log->current_io;
  619. struct bio *bio;
  620. struct r5l_meta_block *block;
  621. unsigned long flags;
  622. u32 crc;
  623. bool do_submit = true;
  624. if (!io)
  625. return;
  626. block = page_address(io->meta_page);
  627. block->meta_size = cpu_to_le32(io->meta_offset);
  628. crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
  629. block->checksum = cpu_to_le32(crc);
  630. bio = io->current_bio;
  631. log->current_io = NULL;
  632. spin_lock_irqsave(&log->io_list_lock, flags);
  633. if (io->has_flush || io->has_fua) {
  634. if (io != list_first_entry(&log->running_ios,
  635. struct r5l_io_unit, log_sibling)) {
  636. io->io_deferred = 1;
  637. do_submit = false;
  638. }
  639. }
  640. spin_unlock_irqrestore(&log->io_list_lock, flags);
  641. if (do_submit)
  642. r5l_do_submit_io(log, io);
  643. }
  644. static struct bio *r5l_bio_alloc(struct r5l_log *log)
  645. {
  646. struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
  647. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  648. bio_set_dev(bio, log->rdev->bdev);
  649. bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
  650. return bio;
  651. }
  652. static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
  653. {
  654. log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
  655. r5c_update_log_state(log);
  656. /*
  657. * If we filled up the log device start from the beginning again,
  658. * which will require a new bio.
  659. *
  660. * Note: for this to work properly the log size needs to me a multiple
  661. * of BLOCK_SECTORS.
  662. */
  663. if (log->log_start == 0)
  664. io->need_split_bio = true;
  665. io->log_end = log->log_start;
  666. }
  667. static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
  668. {
  669. struct r5l_io_unit *io;
  670. struct r5l_meta_block *block;
  671. io = mempool_alloc(log->io_pool, GFP_ATOMIC);
  672. if (!io)
  673. return NULL;
  674. memset(io, 0, sizeof(*io));
  675. io->log = log;
  676. INIT_LIST_HEAD(&io->log_sibling);
  677. INIT_LIST_HEAD(&io->stripe_list);
  678. bio_list_init(&io->flush_barriers);
  679. io->state = IO_UNIT_RUNNING;
  680. io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
  681. block = page_address(io->meta_page);
  682. clear_page(block);
  683. block->magic = cpu_to_le32(R5LOG_MAGIC);
  684. block->version = R5LOG_VERSION;
  685. block->seq = cpu_to_le64(log->seq);
  686. block->position = cpu_to_le64(log->log_start);
  687. io->log_start = log->log_start;
  688. io->meta_offset = sizeof(struct r5l_meta_block);
  689. io->seq = log->seq++;
  690. io->current_bio = r5l_bio_alloc(log);
  691. io->current_bio->bi_end_io = r5l_log_endio;
  692. io->current_bio->bi_private = io;
  693. bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
  694. r5_reserve_log_entry(log, io);
  695. spin_lock_irq(&log->io_list_lock);
  696. list_add_tail(&io->log_sibling, &log->running_ios);
  697. spin_unlock_irq(&log->io_list_lock);
  698. return io;
  699. }
  700. static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
  701. {
  702. if (log->current_io &&
  703. log->current_io->meta_offset + payload_size > PAGE_SIZE)
  704. r5l_submit_current_io(log);
  705. if (!log->current_io) {
  706. log->current_io = r5l_new_meta(log);
  707. if (!log->current_io)
  708. return -ENOMEM;
  709. }
  710. return 0;
  711. }
  712. static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
  713. sector_t location,
  714. u32 checksum1, u32 checksum2,
  715. bool checksum2_valid)
  716. {
  717. struct r5l_io_unit *io = log->current_io;
  718. struct r5l_payload_data_parity *payload;
  719. payload = page_address(io->meta_page) + io->meta_offset;
  720. payload->header.type = cpu_to_le16(type);
  721. payload->header.flags = cpu_to_le16(0);
  722. payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
  723. (PAGE_SHIFT - 9));
  724. payload->location = cpu_to_le64(location);
  725. payload->checksum[0] = cpu_to_le32(checksum1);
  726. if (checksum2_valid)
  727. payload->checksum[1] = cpu_to_le32(checksum2);
  728. io->meta_offset += sizeof(struct r5l_payload_data_parity) +
  729. sizeof(__le32) * (1 + !!checksum2_valid);
  730. }
  731. static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
  732. {
  733. struct r5l_io_unit *io = log->current_io;
  734. if (io->need_split_bio) {
  735. BUG_ON(io->split_bio);
  736. io->split_bio = io->current_bio;
  737. io->current_bio = r5l_bio_alloc(log);
  738. bio_chain(io->current_bio, io->split_bio);
  739. io->need_split_bio = false;
  740. }
  741. if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
  742. BUG();
  743. r5_reserve_log_entry(log, io);
  744. }
  745. static void r5l_append_flush_payload(struct r5l_log *log, sector_t sect)
  746. {
  747. struct mddev *mddev = log->rdev->mddev;
  748. struct r5conf *conf = mddev->private;
  749. struct r5l_io_unit *io;
  750. struct r5l_payload_flush *payload;
  751. int meta_size;
  752. /*
  753. * payload_flush requires extra writes to the journal.
  754. * To avoid handling the extra IO in quiesce, just skip
  755. * flush_payload
  756. */
  757. if (conf->quiesce)
  758. return;
  759. mutex_lock(&log->io_mutex);
  760. meta_size = sizeof(struct r5l_payload_flush) + sizeof(__le64);
  761. if (r5l_get_meta(log, meta_size)) {
  762. mutex_unlock(&log->io_mutex);
  763. return;
  764. }
  765. /* current implementation is one stripe per flush payload */
  766. io = log->current_io;
  767. payload = page_address(io->meta_page) + io->meta_offset;
  768. payload->header.type = cpu_to_le16(R5LOG_PAYLOAD_FLUSH);
  769. payload->header.flags = cpu_to_le16(0);
  770. payload->size = cpu_to_le32(sizeof(__le64));
  771. payload->flush_stripes[0] = cpu_to_le64(sect);
  772. io->meta_offset += meta_size;
  773. /* multiple flush payloads count as one pending_stripe */
  774. if (!io->has_flush_payload) {
  775. io->has_flush_payload = 1;
  776. atomic_inc(&io->pending_stripe);
  777. }
  778. mutex_unlock(&log->io_mutex);
  779. }
  780. static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
  781. int data_pages, int parity_pages)
  782. {
  783. int i;
  784. int meta_size;
  785. int ret;
  786. struct r5l_io_unit *io;
  787. meta_size =
  788. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
  789. * data_pages) +
  790. sizeof(struct r5l_payload_data_parity) +
  791. sizeof(__le32) * parity_pages;
  792. ret = r5l_get_meta(log, meta_size);
  793. if (ret)
  794. return ret;
  795. io = log->current_io;
  796. if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
  797. io->has_flush = 1;
  798. for (i = 0; i < sh->disks; i++) {
  799. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  800. test_bit(R5_InJournal, &sh->dev[i].flags))
  801. continue;
  802. if (i == sh->pd_idx || i == sh->qd_idx)
  803. continue;
  804. if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
  805. log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
  806. io->has_fua = 1;
  807. /*
  808. * we need to flush journal to make sure recovery can
  809. * reach the data with fua flag
  810. */
  811. io->has_flush = 1;
  812. }
  813. r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
  814. raid5_compute_blocknr(sh, i, 0),
  815. sh->dev[i].log_checksum, 0, false);
  816. r5l_append_payload_page(log, sh->dev[i].page);
  817. }
  818. if (parity_pages == 2) {
  819. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  820. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  821. sh->dev[sh->qd_idx].log_checksum, true);
  822. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  823. r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
  824. } else if (parity_pages == 1) {
  825. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  826. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  827. 0, false);
  828. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  829. } else /* Just writing data, not parity, in caching phase */
  830. BUG_ON(parity_pages != 0);
  831. list_add_tail(&sh->log_list, &io->stripe_list);
  832. atomic_inc(&io->pending_stripe);
  833. sh->log_io = io;
  834. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  835. return 0;
  836. if (sh->log_start == MaxSector) {
  837. BUG_ON(!list_empty(&sh->r5c));
  838. sh->log_start = io->log_start;
  839. spin_lock_irq(&log->stripe_in_journal_lock);
  840. list_add_tail(&sh->r5c,
  841. &log->stripe_in_journal_list);
  842. spin_unlock_irq(&log->stripe_in_journal_lock);
  843. atomic_inc(&log->stripe_in_journal_count);
  844. }
  845. return 0;
  846. }
  847. /* add stripe to no_space_stripes, and then wake up reclaim */
  848. static inline void r5l_add_no_space_stripe(struct r5l_log *log,
  849. struct stripe_head *sh)
  850. {
  851. spin_lock(&log->no_space_stripes_lock);
  852. list_add_tail(&sh->log_list, &log->no_space_stripes);
  853. spin_unlock(&log->no_space_stripes_lock);
  854. }
  855. /*
  856. * running in raid5d, where reclaim could wait for raid5d too (when it flushes
  857. * data from log to raid disks), so we shouldn't wait for reclaim here
  858. */
  859. int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
  860. {
  861. struct r5conf *conf = sh->raid_conf;
  862. int write_disks = 0;
  863. int data_pages, parity_pages;
  864. int reserve;
  865. int i;
  866. int ret = 0;
  867. bool wake_reclaim = false;
  868. if (!log)
  869. return -EAGAIN;
  870. /* Don't support stripe batch */
  871. if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
  872. test_bit(STRIPE_SYNCING, &sh->state)) {
  873. /* the stripe is written to log, we start writing it to raid */
  874. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  875. return -EAGAIN;
  876. }
  877. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  878. for (i = 0; i < sh->disks; i++) {
  879. void *addr;
  880. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  881. test_bit(R5_InJournal, &sh->dev[i].flags))
  882. continue;
  883. write_disks++;
  884. /* checksum is already calculated in last run */
  885. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  886. continue;
  887. addr = kmap_atomic(sh->dev[i].page);
  888. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  889. addr, PAGE_SIZE);
  890. kunmap_atomic(addr);
  891. }
  892. parity_pages = 1 + !!(sh->qd_idx >= 0);
  893. data_pages = write_disks - parity_pages;
  894. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  895. /*
  896. * The stripe must enter state machine again to finish the write, so
  897. * don't delay.
  898. */
  899. clear_bit(STRIPE_DELAYED, &sh->state);
  900. atomic_inc(&sh->count);
  901. mutex_lock(&log->io_mutex);
  902. /* meta + data */
  903. reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
  904. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  905. if (!r5l_has_free_space(log, reserve)) {
  906. r5l_add_no_space_stripe(log, sh);
  907. wake_reclaim = true;
  908. } else {
  909. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  910. if (ret) {
  911. spin_lock_irq(&log->io_list_lock);
  912. list_add_tail(&sh->log_list,
  913. &log->no_mem_stripes);
  914. spin_unlock_irq(&log->io_list_lock);
  915. }
  916. }
  917. } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
  918. /*
  919. * log space critical, do not process stripes that are
  920. * not in cache yet (sh->log_start == MaxSector).
  921. */
  922. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  923. sh->log_start == MaxSector) {
  924. r5l_add_no_space_stripe(log, sh);
  925. wake_reclaim = true;
  926. reserve = 0;
  927. } else if (!r5l_has_free_space(log, reserve)) {
  928. if (sh->log_start == log->last_checkpoint)
  929. BUG();
  930. else
  931. r5l_add_no_space_stripe(log, sh);
  932. } else {
  933. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  934. if (ret) {
  935. spin_lock_irq(&log->io_list_lock);
  936. list_add_tail(&sh->log_list,
  937. &log->no_mem_stripes);
  938. spin_unlock_irq(&log->io_list_lock);
  939. }
  940. }
  941. }
  942. mutex_unlock(&log->io_mutex);
  943. if (wake_reclaim)
  944. r5l_wake_reclaim(log, reserve);
  945. return 0;
  946. }
  947. void r5l_write_stripe_run(struct r5l_log *log)
  948. {
  949. if (!log)
  950. return;
  951. mutex_lock(&log->io_mutex);
  952. r5l_submit_current_io(log);
  953. mutex_unlock(&log->io_mutex);
  954. }
  955. int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
  956. {
  957. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  958. /*
  959. * in write through (journal only)
  960. * we flush log disk cache first, then write stripe data to
  961. * raid disks. So if bio is finished, the log disk cache is
  962. * flushed already. The recovery guarantees we can recovery
  963. * the bio from log disk, so we don't need to flush again
  964. */
  965. if (bio->bi_iter.bi_size == 0) {
  966. bio_endio(bio);
  967. return 0;
  968. }
  969. bio->bi_opf &= ~REQ_PREFLUSH;
  970. } else {
  971. /* write back (with cache) */
  972. if (bio->bi_iter.bi_size == 0) {
  973. mutex_lock(&log->io_mutex);
  974. r5l_get_meta(log, 0);
  975. bio_list_add(&log->current_io->flush_barriers, bio);
  976. log->current_io->has_flush = 1;
  977. log->current_io->has_null_flush = 1;
  978. atomic_inc(&log->current_io->pending_stripe);
  979. r5l_submit_current_io(log);
  980. mutex_unlock(&log->io_mutex);
  981. return 0;
  982. }
  983. }
  984. return -EAGAIN;
  985. }
  986. /* This will run after log space is reclaimed */
  987. static void r5l_run_no_space_stripes(struct r5l_log *log)
  988. {
  989. struct stripe_head *sh;
  990. spin_lock(&log->no_space_stripes_lock);
  991. while (!list_empty(&log->no_space_stripes)) {
  992. sh = list_first_entry(&log->no_space_stripes,
  993. struct stripe_head, log_list);
  994. list_del_init(&sh->log_list);
  995. set_bit(STRIPE_HANDLE, &sh->state);
  996. raid5_release_stripe(sh);
  997. }
  998. spin_unlock(&log->no_space_stripes_lock);
  999. }
  1000. /*
  1001. * calculate new last_checkpoint
  1002. * for write through mode, returns log->next_checkpoint
  1003. * for write back, returns log_start of first sh in stripe_in_journal_list
  1004. */
  1005. static sector_t r5c_calculate_new_cp(struct r5conf *conf)
  1006. {
  1007. struct stripe_head *sh;
  1008. struct r5l_log *log = conf->log;
  1009. sector_t new_cp;
  1010. unsigned long flags;
  1011. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  1012. return log->next_checkpoint;
  1013. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  1014. if (list_empty(&conf->log->stripe_in_journal_list)) {
  1015. /* all stripes flushed */
  1016. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1017. return log->next_checkpoint;
  1018. }
  1019. sh = list_first_entry(&conf->log->stripe_in_journal_list,
  1020. struct stripe_head, r5c);
  1021. new_cp = sh->log_start;
  1022. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1023. return new_cp;
  1024. }
  1025. static sector_t r5l_reclaimable_space(struct r5l_log *log)
  1026. {
  1027. struct r5conf *conf = log->rdev->mddev->private;
  1028. return r5l_ring_distance(log, log->last_checkpoint,
  1029. r5c_calculate_new_cp(conf));
  1030. }
  1031. static void r5l_run_no_mem_stripe(struct r5l_log *log)
  1032. {
  1033. struct stripe_head *sh;
  1034. lockdep_assert_held(&log->io_list_lock);
  1035. if (!list_empty(&log->no_mem_stripes)) {
  1036. sh = list_first_entry(&log->no_mem_stripes,
  1037. struct stripe_head, log_list);
  1038. list_del_init(&sh->log_list);
  1039. set_bit(STRIPE_HANDLE, &sh->state);
  1040. raid5_release_stripe(sh);
  1041. }
  1042. }
  1043. static bool r5l_complete_finished_ios(struct r5l_log *log)
  1044. {
  1045. struct r5l_io_unit *io, *next;
  1046. bool found = false;
  1047. lockdep_assert_held(&log->io_list_lock);
  1048. list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
  1049. /* don't change list order */
  1050. if (io->state < IO_UNIT_STRIPE_END)
  1051. break;
  1052. log->next_checkpoint = io->log_start;
  1053. list_del(&io->log_sibling);
  1054. mempool_free(io, log->io_pool);
  1055. r5l_run_no_mem_stripe(log);
  1056. found = true;
  1057. }
  1058. return found;
  1059. }
  1060. static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
  1061. {
  1062. struct r5l_log *log = io->log;
  1063. struct r5conf *conf = log->rdev->mddev->private;
  1064. unsigned long flags;
  1065. spin_lock_irqsave(&log->io_list_lock, flags);
  1066. __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
  1067. if (!r5l_complete_finished_ios(log)) {
  1068. spin_unlock_irqrestore(&log->io_list_lock, flags);
  1069. return;
  1070. }
  1071. if (r5l_reclaimable_space(log) > log->max_free_space ||
  1072. test_bit(R5C_LOG_TIGHT, &conf->cache_state))
  1073. r5l_wake_reclaim(log, 0);
  1074. spin_unlock_irqrestore(&log->io_list_lock, flags);
  1075. wake_up(&log->iounit_wait);
  1076. }
  1077. void r5l_stripe_write_finished(struct stripe_head *sh)
  1078. {
  1079. struct r5l_io_unit *io;
  1080. io = sh->log_io;
  1081. sh->log_io = NULL;
  1082. if (io && atomic_dec_and_test(&io->pending_stripe))
  1083. __r5l_stripe_write_finished(io);
  1084. }
  1085. static void r5l_log_flush_endio(struct bio *bio)
  1086. {
  1087. struct r5l_log *log = container_of(bio, struct r5l_log,
  1088. flush_bio);
  1089. unsigned long flags;
  1090. struct r5l_io_unit *io;
  1091. if (bio->bi_status)
  1092. md_error(log->rdev->mddev, log->rdev);
  1093. spin_lock_irqsave(&log->io_list_lock, flags);
  1094. list_for_each_entry(io, &log->flushing_ios, log_sibling)
  1095. r5l_io_run_stripes(io);
  1096. list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
  1097. spin_unlock_irqrestore(&log->io_list_lock, flags);
  1098. }
  1099. /*
  1100. * Starting dispatch IO to raid.
  1101. * io_unit(meta) consists of a log. There is one situation we want to avoid. A
  1102. * broken meta in the middle of a log causes recovery can't find meta at the
  1103. * head of log. If operations require meta at the head persistent in log, we
  1104. * must make sure meta before it persistent in log too. A case is:
  1105. *
  1106. * stripe data/parity is in log, we start write stripe to raid disks. stripe
  1107. * data/parity must be persistent in log before we do the write to raid disks.
  1108. *
  1109. * The solution is we restrictly maintain io_unit list order. In this case, we
  1110. * only write stripes of an io_unit to raid disks till the io_unit is the first
  1111. * one whose data/parity is in log.
  1112. */
  1113. void r5l_flush_stripe_to_raid(struct r5l_log *log)
  1114. {
  1115. bool do_flush;
  1116. if (!log || !log->need_cache_flush)
  1117. return;
  1118. spin_lock_irq(&log->io_list_lock);
  1119. /* flush bio is running */
  1120. if (!list_empty(&log->flushing_ios)) {
  1121. spin_unlock_irq(&log->io_list_lock);
  1122. return;
  1123. }
  1124. list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
  1125. do_flush = !list_empty(&log->flushing_ios);
  1126. spin_unlock_irq(&log->io_list_lock);
  1127. if (!do_flush)
  1128. return;
  1129. bio_reset(&log->flush_bio);
  1130. bio_set_dev(&log->flush_bio, log->rdev->bdev);
  1131. log->flush_bio.bi_end_io = r5l_log_flush_endio;
  1132. log->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  1133. submit_bio(&log->flush_bio);
  1134. }
  1135. static void r5l_write_super(struct r5l_log *log, sector_t cp);
  1136. static void r5l_write_super_and_discard_space(struct r5l_log *log,
  1137. sector_t end)
  1138. {
  1139. struct block_device *bdev = log->rdev->bdev;
  1140. struct mddev *mddev;
  1141. r5l_write_super(log, end);
  1142. if (!blk_queue_discard(bdev_get_queue(bdev)))
  1143. return;
  1144. mddev = log->rdev->mddev;
  1145. /*
  1146. * Discard could zero data, so before discard we must make sure
  1147. * superblock is updated to new log tail. Updating superblock (either
  1148. * directly call md_update_sb() or depend on md thread) must hold
  1149. * reconfig mutex. On the other hand, raid5_quiesce is called with
  1150. * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
  1151. * for all IO finish, hence waitting for reclaim thread, while reclaim
  1152. * thread is calling this function and waitting for reconfig mutex. So
  1153. * there is a deadlock. We workaround this issue with a trylock.
  1154. * FIXME: we could miss discard if we can't take reconfig mutex
  1155. */
  1156. set_mask_bits(&mddev->sb_flags, 0,
  1157. BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
  1158. if (!mddev_trylock(mddev))
  1159. return;
  1160. md_update_sb(mddev, 1);
  1161. mddev_unlock(mddev);
  1162. /* discard IO error really doesn't matter, ignore it */
  1163. if (log->last_checkpoint < end) {
  1164. blkdev_issue_discard(bdev,
  1165. log->last_checkpoint + log->rdev->data_offset,
  1166. end - log->last_checkpoint, GFP_NOIO, 0);
  1167. } else {
  1168. blkdev_issue_discard(bdev,
  1169. log->last_checkpoint + log->rdev->data_offset,
  1170. log->device_size - log->last_checkpoint,
  1171. GFP_NOIO, 0);
  1172. blkdev_issue_discard(bdev, log->rdev->data_offset, end,
  1173. GFP_NOIO, 0);
  1174. }
  1175. }
  1176. /*
  1177. * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
  1178. * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
  1179. *
  1180. * must hold conf->device_lock
  1181. */
  1182. static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
  1183. {
  1184. BUG_ON(list_empty(&sh->lru));
  1185. BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  1186. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  1187. /*
  1188. * The stripe is not ON_RELEASE_LIST, so it is safe to call
  1189. * raid5_release_stripe() while holding conf->device_lock
  1190. */
  1191. BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
  1192. lockdep_assert_held(&conf->device_lock);
  1193. list_del_init(&sh->lru);
  1194. atomic_inc(&sh->count);
  1195. set_bit(STRIPE_HANDLE, &sh->state);
  1196. atomic_inc(&conf->active_stripes);
  1197. r5c_make_stripe_write_out(sh);
  1198. if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
  1199. atomic_inc(&conf->r5c_flushing_partial_stripes);
  1200. else
  1201. atomic_inc(&conf->r5c_flushing_full_stripes);
  1202. raid5_release_stripe(sh);
  1203. }
  1204. /*
  1205. * if num == 0, flush all full stripes
  1206. * if num > 0, flush all full stripes. If less than num full stripes are
  1207. * flushed, flush some partial stripes until totally num stripes are
  1208. * flushed or there is no more cached stripes.
  1209. */
  1210. void r5c_flush_cache(struct r5conf *conf, int num)
  1211. {
  1212. int count;
  1213. struct stripe_head *sh, *next;
  1214. lockdep_assert_held(&conf->device_lock);
  1215. if (!conf->log)
  1216. return;
  1217. count = 0;
  1218. list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
  1219. r5c_flush_stripe(conf, sh);
  1220. count++;
  1221. }
  1222. if (count >= num)
  1223. return;
  1224. list_for_each_entry_safe(sh, next,
  1225. &conf->r5c_partial_stripe_list, lru) {
  1226. r5c_flush_stripe(conf, sh);
  1227. if (++count >= num)
  1228. break;
  1229. }
  1230. }
  1231. static void r5c_do_reclaim(struct r5conf *conf)
  1232. {
  1233. struct r5l_log *log = conf->log;
  1234. struct stripe_head *sh;
  1235. int count = 0;
  1236. unsigned long flags;
  1237. int total_cached;
  1238. int stripes_to_flush;
  1239. int flushing_partial, flushing_full;
  1240. if (!r5c_is_writeback(log))
  1241. return;
  1242. flushing_partial = atomic_read(&conf->r5c_flushing_partial_stripes);
  1243. flushing_full = atomic_read(&conf->r5c_flushing_full_stripes);
  1244. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  1245. atomic_read(&conf->r5c_cached_full_stripes) -
  1246. flushing_full - flushing_partial;
  1247. if (total_cached > conf->min_nr_stripes * 3 / 4 ||
  1248. atomic_read(&conf->empty_inactive_list_nr) > 0)
  1249. /*
  1250. * if stripe cache pressure high, flush all full stripes and
  1251. * some partial stripes
  1252. */
  1253. stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
  1254. else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  1255. atomic_read(&conf->r5c_cached_full_stripes) - flushing_full >
  1256. R5C_FULL_STRIPE_FLUSH_BATCH(conf))
  1257. /*
  1258. * if stripe cache pressure moderate, or if there is many full
  1259. * stripes,flush all full stripes
  1260. */
  1261. stripes_to_flush = 0;
  1262. else
  1263. /* no need to flush */
  1264. stripes_to_flush = -1;
  1265. if (stripes_to_flush >= 0) {
  1266. spin_lock_irqsave(&conf->device_lock, flags);
  1267. r5c_flush_cache(conf, stripes_to_flush);
  1268. spin_unlock_irqrestore(&conf->device_lock, flags);
  1269. }
  1270. /* if log space is tight, flush stripes on stripe_in_journal_list */
  1271. if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
  1272. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  1273. spin_lock(&conf->device_lock);
  1274. list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
  1275. /*
  1276. * stripes on stripe_in_journal_list could be in any
  1277. * state of the stripe_cache state machine. In this
  1278. * case, we only want to flush stripe on
  1279. * r5c_cached_full/partial_stripes. The following
  1280. * condition makes sure the stripe is on one of the
  1281. * two lists.
  1282. */
  1283. if (!list_empty(&sh->lru) &&
  1284. !test_bit(STRIPE_HANDLE, &sh->state) &&
  1285. atomic_read(&sh->count) == 0) {
  1286. r5c_flush_stripe(conf, sh);
  1287. if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
  1288. break;
  1289. }
  1290. }
  1291. spin_unlock(&conf->device_lock);
  1292. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1293. }
  1294. if (!test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
  1295. r5l_run_no_space_stripes(log);
  1296. md_wakeup_thread(conf->mddev->thread);
  1297. }
  1298. static void r5l_do_reclaim(struct r5l_log *log)
  1299. {
  1300. struct r5conf *conf = log->rdev->mddev->private;
  1301. sector_t reclaim_target = xchg(&log->reclaim_target, 0);
  1302. sector_t reclaimable;
  1303. sector_t next_checkpoint;
  1304. bool write_super;
  1305. spin_lock_irq(&log->io_list_lock);
  1306. write_super = r5l_reclaimable_space(log) > log->max_free_space ||
  1307. reclaim_target != 0 || !list_empty(&log->no_space_stripes);
  1308. /*
  1309. * move proper io_unit to reclaim list. We should not change the order.
  1310. * reclaimable/unreclaimable io_unit can be mixed in the list, we
  1311. * shouldn't reuse space of an unreclaimable io_unit
  1312. */
  1313. while (1) {
  1314. reclaimable = r5l_reclaimable_space(log);
  1315. if (reclaimable >= reclaim_target ||
  1316. (list_empty(&log->running_ios) &&
  1317. list_empty(&log->io_end_ios) &&
  1318. list_empty(&log->flushing_ios) &&
  1319. list_empty(&log->finished_ios)))
  1320. break;
  1321. md_wakeup_thread(log->rdev->mddev->thread);
  1322. wait_event_lock_irq(log->iounit_wait,
  1323. r5l_reclaimable_space(log) > reclaimable,
  1324. log->io_list_lock);
  1325. }
  1326. next_checkpoint = r5c_calculate_new_cp(conf);
  1327. spin_unlock_irq(&log->io_list_lock);
  1328. if (reclaimable == 0 || !write_super)
  1329. return;
  1330. /*
  1331. * write_super will flush cache of each raid disk. We must write super
  1332. * here, because the log area might be reused soon and we don't want to
  1333. * confuse recovery
  1334. */
  1335. r5l_write_super_and_discard_space(log, next_checkpoint);
  1336. mutex_lock(&log->io_mutex);
  1337. log->last_checkpoint = next_checkpoint;
  1338. r5c_update_log_state(log);
  1339. mutex_unlock(&log->io_mutex);
  1340. r5l_run_no_space_stripes(log);
  1341. }
  1342. static void r5l_reclaim_thread(struct md_thread *thread)
  1343. {
  1344. struct mddev *mddev = thread->mddev;
  1345. struct r5conf *conf = mddev->private;
  1346. struct r5l_log *log = conf->log;
  1347. if (!log)
  1348. return;
  1349. r5c_do_reclaim(conf);
  1350. r5l_do_reclaim(log);
  1351. }
  1352. void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
  1353. {
  1354. unsigned long target;
  1355. unsigned long new = (unsigned long)space; /* overflow in theory */
  1356. if (!log)
  1357. return;
  1358. do {
  1359. target = log->reclaim_target;
  1360. if (new < target)
  1361. return;
  1362. } while (cmpxchg(&log->reclaim_target, target, new) != target);
  1363. md_wakeup_thread(log->reclaim_thread);
  1364. }
  1365. void r5l_quiesce(struct r5l_log *log, int quiesce)
  1366. {
  1367. struct mddev *mddev;
  1368. if (quiesce) {
  1369. /* make sure r5l_write_super_and_discard_space exits */
  1370. mddev = log->rdev->mddev;
  1371. wake_up(&mddev->sb_wait);
  1372. kthread_park(log->reclaim_thread->tsk);
  1373. r5l_wake_reclaim(log, MaxSector);
  1374. r5l_do_reclaim(log);
  1375. } else
  1376. kthread_unpark(log->reclaim_thread->tsk);
  1377. }
  1378. bool r5l_log_disk_error(struct r5conf *conf)
  1379. {
  1380. struct r5l_log *log;
  1381. bool ret;
  1382. /* don't allow write if journal disk is missing */
  1383. rcu_read_lock();
  1384. log = rcu_dereference(conf->log);
  1385. if (!log)
  1386. ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  1387. else
  1388. ret = test_bit(Faulty, &log->rdev->flags);
  1389. rcu_read_unlock();
  1390. return ret;
  1391. }
  1392. #define R5L_RECOVERY_PAGE_POOL_SIZE 256
  1393. struct r5l_recovery_ctx {
  1394. struct page *meta_page; /* current meta */
  1395. sector_t meta_total_blocks; /* total size of current meta and data */
  1396. sector_t pos; /* recovery position */
  1397. u64 seq; /* recovery position seq */
  1398. int data_parity_stripes; /* number of data_parity stripes */
  1399. int data_only_stripes; /* number of data_only stripes */
  1400. struct list_head cached_list;
  1401. /*
  1402. * read ahead page pool (ra_pool)
  1403. * in recovery, log is read sequentially. It is not efficient to
  1404. * read every page with sync_page_io(). The read ahead page pool
  1405. * reads multiple pages with one IO, so further log read can
  1406. * just copy data from the pool.
  1407. */
  1408. struct page *ra_pool[R5L_RECOVERY_PAGE_POOL_SIZE];
  1409. sector_t pool_offset; /* offset of first page in the pool */
  1410. int total_pages; /* total allocated pages */
  1411. int valid_pages; /* pages with valid data */
  1412. struct bio *ra_bio; /* bio to do the read ahead */
  1413. };
  1414. static int r5l_recovery_allocate_ra_pool(struct r5l_log *log,
  1415. struct r5l_recovery_ctx *ctx)
  1416. {
  1417. struct page *page;
  1418. ctx->ra_bio = bio_alloc_bioset(GFP_KERNEL, BIO_MAX_PAGES, log->bs);
  1419. if (!ctx->ra_bio)
  1420. return -ENOMEM;
  1421. ctx->valid_pages = 0;
  1422. ctx->total_pages = 0;
  1423. while (ctx->total_pages < R5L_RECOVERY_PAGE_POOL_SIZE) {
  1424. page = alloc_page(GFP_KERNEL);
  1425. if (!page)
  1426. break;
  1427. ctx->ra_pool[ctx->total_pages] = page;
  1428. ctx->total_pages += 1;
  1429. }
  1430. if (ctx->total_pages == 0) {
  1431. bio_put(ctx->ra_bio);
  1432. return -ENOMEM;
  1433. }
  1434. ctx->pool_offset = 0;
  1435. return 0;
  1436. }
  1437. static void r5l_recovery_free_ra_pool(struct r5l_log *log,
  1438. struct r5l_recovery_ctx *ctx)
  1439. {
  1440. int i;
  1441. for (i = 0; i < ctx->total_pages; ++i)
  1442. put_page(ctx->ra_pool[i]);
  1443. bio_put(ctx->ra_bio);
  1444. }
  1445. /*
  1446. * fetch ctx->valid_pages pages from offset
  1447. * In normal cases, ctx->valid_pages == ctx->total_pages after the call.
  1448. * However, if the offset is close to the end of the journal device,
  1449. * ctx->valid_pages could be smaller than ctx->total_pages
  1450. */
  1451. static int r5l_recovery_fetch_ra_pool(struct r5l_log *log,
  1452. struct r5l_recovery_ctx *ctx,
  1453. sector_t offset)
  1454. {
  1455. bio_reset(ctx->ra_bio);
  1456. bio_set_dev(ctx->ra_bio, log->rdev->bdev);
  1457. bio_set_op_attrs(ctx->ra_bio, REQ_OP_READ, 0);
  1458. ctx->ra_bio->bi_iter.bi_sector = log->rdev->data_offset + offset;
  1459. ctx->valid_pages = 0;
  1460. ctx->pool_offset = offset;
  1461. while (ctx->valid_pages < ctx->total_pages) {
  1462. bio_add_page(ctx->ra_bio,
  1463. ctx->ra_pool[ctx->valid_pages], PAGE_SIZE, 0);
  1464. ctx->valid_pages += 1;
  1465. offset = r5l_ring_add(log, offset, BLOCK_SECTORS);
  1466. if (offset == 0) /* reached end of the device */
  1467. break;
  1468. }
  1469. return submit_bio_wait(ctx->ra_bio);
  1470. }
  1471. /*
  1472. * try read a page from the read ahead page pool, if the page is not in the
  1473. * pool, call r5l_recovery_fetch_ra_pool
  1474. */
  1475. static int r5l_recovery_read_page(struct r5l_log *log,
  1476. struct r5l_recovery_ctx *ctx,
  1477. struct page *page,
  1478. sector_t offset)
  1479. {
  1480. int ret;
  1481. if (offset < ctx->pool_offset ||
  1482. offset >= ctx->pool_offset + ctx->valid_pages * BLOCK_SECTORS) {
  1483. ret = r5l_recovery_fetch_ra_pool(log, ctx, offset);
  1484. if (ret)
  1485. return ret;
  1486. }
  1487. BUG_ON(offset < ctx->pool_offset ||
  1488. offset >= ctx->pool_offset + ctx->valid_pages * BLOCK_SECTORS);
  1489. memcpy(page_address(page),
  1490. page_address(ctx->ra_pool[(offset - ctx->pool_offset) >>
  1491. BLOCK_SECTOR_SHIFT]),
  1492. PAGE_SIZE);
  1493. return 0;
  1494. }
  1495. static int r5l_recovery_read_meta_block(struct r5l_log *log,
  1496. struct r5l_recovery_ctx *ctx)
  1497. {
  1498. struct page *page = ctx->meta_page;
  1499. struct r5l_meta_block *mb;
  1500. u32 crc, stored_crc;
  1501. int ret;
  1502. ret = r5l_recovery_read_page(log, ctx, page, ctx->pos);
  1503. if (ret != 0)
  1504. return ret;
  1505. mb = page_address(page);
  1506. stored_crc = le32_to_cpu(mb->checksum);
  1507. mb->checksum = 0;
  1508. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  1509. le64_to_cpu(mb->seq) != ctx->seq ||
  1510. mb->version != R5LOG_VERSION ||
  1511. le64_to_cpu(mb->position) != ctx->pos)
  1512. return -EINVAL;
  1513. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  1514. if (stored_crc != crc)
  1515. return -EINVAL;
  1516. if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
  1517. return -EINVAL;
  1518. ctx->meta_total_blocks = BLOCK_SECTORS;
  1519. return 0;
  1520. }
  1521. static void
  1522. r5l_recovery_create_empty_meta_block(struct r5l_log *log,
  1523. struct page *page,
  1524. sector_t pos, u64 seq)
  1525. {
  1526. struct r5l_meta_block *mb;
  1527. mb = page_address(page);
  1528. clear_page(mb);
  1529. mb->magic = cpu_to_le32(R5LOG_MAGIC);
  1530. mb->version = R5LOG_VERSION;
  1531. mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
  1532. mb->seq = cpu_to_le64(seq);
  1533. mb->position = cpu_to_le64(pos);
  1534. }
  1535. static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
  1536. u64 seq)
  1537. {
  1538. struct page *page;
  1539. struct r5l_meta_block *mb;
  1540. page = alloc_page(GFP_KERNEL);
  1541. if (!page)
  1542. return -ENOMEM;
  1543. r5l_recovery_create_empty_meta_block(log, page, pos, seq);
  1544. mb = page_address(page);
  1545. mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
  1546. mb, PAGE_SIZE));
  1547. if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
  1548. REQ_SYNC | REQ_FUA, false)) {
  1549. __free_page(page);
  1550. return -EIO;
  1551. }
  1552. __free_page(page);
  1553. return 0;
  1554. }
  1555. /*
  1556. * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
  1557. * to mark valid (potentially not flushed) data in the journal.
  1558. *
  1559. * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
  1560. * so there should not be any mismatch here.
  1561. */
  1562. static void r5l_recovery_load_data(struct r5l_log *log,
  1563. struct stripe_head *sh,
  1564. struct r5l_recovery_ctx *ctx,
  1565. struct r5l_payload_data_parity *payload,
  1566. sector_t log_offset)
  1567. {
  1568. struct mddev *mddev = log->rdev->mddev;
  1569. struct r5conf *conf = mddev->private;
  1570. int dd_idx;
  1571. raid5_compute_sector(conf,
  1572. le64_to_cpu(payload->location), 0,
  1573. &dd_idx, sh);
  1574. r5l_recovery_read_page(log, ctx, sh->dev[dd_idx].page, log_offset);
  1575. sh->dev[dd_idx].log_checksum =
  1576. le32_to_cpu(payload->checksum[0]);
  1577. ctx->meta_total_blocks += BLOCK_SECTORS;
  1578. set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
  1579. set_bit(STRIPE_R5C_CACHING, &sh->state);
  1580. }
  1581. static void r5l_recovery_load_parity(struct r5l_log *log,
  1582. struct stripe_head *sh,
  1583. struct r5l_recovery_ctx *ctx,
  1584. struct r5l_payload_data_parity *payload,
  1585. sector_t log_offset)
  1586. {
  1587. struct mddev *mddev = log->rdev->mddev;
  1588. struct r5conf *conf = mddev->private;
  1589. ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
  1590. r5l_recovery_read_page(log, ctx, sh->dev[sh->pd_idx].page, log_offset);
  1591. sh->dev[sh->pd_idx].log_checksum =
  1592. le32_to_cpu(payload->checksum[0]);
  1593. set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
  1594. if (sh->qd_idx >= 0) {
  1595. r5l_recovery_read_page(
  1596. log, ctx, sh->dev[sh->qd_idx].page,
  1597. r5l_ring_add(log, log_offset, BLOCK_SECTORS));
  1598. sh->dev[sh->qd_idx].log_checksum =
  1599. le32_to_cpu(payload->checksum[1]);
  1600. set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
  1601. }
  1602. clear_bit(STRIPE_R5C_CACHING, &sh->state);
  1603. }
  1604. static void r5l_recovery_reset_stripe(struct stripe_head *sh)
  1605. {
  1606. int i;
  1607. sh->state = 0;
  1608. sh->log_start = MaxSector;
  1609. for (i = sh->disks; i--; )
  1610. sh->dev[i].flags = 0;
  1611. }
  1612. static void
  1613. r5l_recovery_replay_one_stripe(struct r5conf *conf,
  1614. struct stripe_head *sh,
  1615. struct r5l_recovery_ctx *ctx)
  1616. {
  1617. struct md_rdev *rdev, *rrdev;
  1618. int disk_index;
  1619. int data_count = 0;
  1620. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1621. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1622. continue;
  1623. if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
  1624. continue;
  1625. data_count++;
  1626. }
  1627. /*
  1628. * stripes that only have parity must have been flushed
  1629. * before the crash that we are now recovering from, so
  1630. * there is nothing more to recovery.
  1631. */
  1632. if (data_count == 0)
  1633. goto out;
  1634. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1635. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1636. continue;
  1637. /* in case device is broken */
  1638. rcu_read_lock();
  1639. rdev = rcu_dereference(conf->disks[disk_index].rdev);
  1640. if (rdev) {
  1641. atomic_inc(&rdev->nr_pending);
  1642. rcu_read_unlock();
  1643. sync_page_io(rdev, sh->sector, PAGE_SIZE,
  1644. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1645. false);
  1646. rdev_dec_pending(rdev, rdev->mddev);
  1647. rcu_read_lock();
  1648. }
  1649. rrdev = rcu_dereference(conf->disks[disk_index].replacement);
  1650. if (rrdev) {
  1651. atomic_inc(&rrdev->nr_pending);
  1652. rcu_read_unlock();
  1653. sync_page_io(rrdev, sh->sector, PAGE_SIZE,
  1654. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1655. false);
  1656. rdev_dec_pending(rrdev, rrdev->mddev);
  1657. rcu_read_lock();
  1658. }
  1659. rcu_read_unlock();
  1660. }
  1661. ctx->data_parity_stripes++;
  1662. out:
  1663. r5l_recovery_reset_stripe(sh);
  1664. }
  1665. static struct stripe_head *
  1666. r5c_recovery_alloc_stripe(struct r5conf *conf,
  1667. sector_t stripe_sect)
  1668. {
  1669. struct stripe_head *sh;
  1670. sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
  1671. if (!sh)
  1672. return NULL; /* no more stripe available */
  1673. r5l_recovery_reset_stripe(sh);
  1674. return sh;
  1675. }
  1676. static struct stripe_head *
  1677. r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
  1678. {
  1679. struct stripe_head *sh;
  1680. list_for_each_entry(sh, list, lru)
  1681. if (sh->sector == sect)
  1682. return sh;
  1683. return NULL;
  1684. }
  1685. static void
  1686. r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
  1687. struct r5l_recovery_ctx *ctx)
  1688. {
  1689. struct stripe_head *sh, *next;
  1690. list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
  1691. r5l_recovery_reset_stripe(sh);
  1692. list_del_init(&sh->lru);
  1693. raid5_release_stripe(sh);
  1694. }
  1695. }
  1696. static void
  1697. r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
  1698. struct r5l_recovery_ctx *ctx)
  1699. {
  1700. struct stripe_head *sh, *next;
  1701. list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
  1702. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  1703. r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
  1704. list_del_init(&sh->lru);
  1705. raid5_release_stripe(sh);
  1706. }
  1707. }
  1708. /* if matches return 0; otherwise return -EINVAL */
  1709. static int
  1710. r5l_recovery_verify_data_checksum(struct r5l_log *log,
  1711. struct r5l_recovery_ctx *ctx,
  1712. struct page *page,
  1713. sector_t log_offset, __le32 log_checksum)
  1714. {
  1715. void *addr;
  1716. u32 checksum;
  1717. r5l_recovery_read_page(log, ctx, page, log_offset);
  1718. addr = kmap_atomic(page);
  1719. checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
  1720. kunmap_atomic(addr);
  1721. return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
  1722. }
  1723. /*
  1724. * before loading data to stripe cache, we need verify checksum for all data,
  1725. * if there is mismatch for any data page, we drop all data in the mata block
  1726. */
  1727. static int
  1728. r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
  1729. struct r5l_recovery_ctx *ctx)
  1730. {
  1731. struct mddev *mddev = log->rdev->mddev;
  1732. struct r5conf *conf = mddev->private;
  1733. struct r5l_meta_block *mb = page_address(ctx->meta_page);
  1734. sector_t mb_offset = sizeof(struct r5l_meta_block);
  1735. sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1736. struct page *page;
  1737. struct r5l_payload_data_parity *payload;
  1738. struct r5l_payload_flush *payload_flush;
  1739. page = alloc_page(GFP_KERNEL);
  1740. if (!page)
  1741. return -ENOMEM;
  1742. while (mb_offset < le32_to_cpu(mb->meta_size)) {
  1743. payload = (void *)mb + mb_offset;
  1744. payload_flush = (void *)mb + mb_offset;
  1745. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
  1746. if (r5l_recovery_verify_data_checksum(
  1747. log, ctx, page, log_offset,
  1748. payload->checksum[0]) < 0)
  1749. goto mismatch;
  1750. } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY) {
  1751. if (r5l_recovery_verify_data_checksum(
  1752. log, ctx, page, log_offset,
  1753. payload->checksum[0]) < 0)
  1754. goto mismatch;
  1755. if (conf->max_degraded == 2 && /* q for RAID 6 */
  1756. r5l_recovery_verify_data_checksum(
  1757. log, ctx, page,
  1758. r5l_ring_add(log, log_offset,
  1759. BLOCK_SECTORS),
  1760. payload->checksum[1]) < 0)
  1761. goto mismatch;
  1762. } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) {
  1763. /* nothing to do for R5LOG_PAYLOAD_FLUSH here */
  1764. } else /* not R5LOG_PAYLOAD_DATA/PARITY/FLUSH */
  1765. goto mismatch;
  1766. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) {
  1767. mb_offset += sizeof(struct r5l_payload_flush) +
  1768. le32_to_cpu(payload_flush->size);
  1769. } else {
  1770. /* DATA or PARITY payload */
  1771. log_offset = r5l_ring_add(log, log_offset,
  1772. le32_to_cpu(payload->size));
  1773. mb_offset += sizeof(struct r5l_payload_data_parity) +
  1774. sizeof(__le32) *
  1775. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  1776. }
  1777. }
  1778. put_page(page);
  1779. return 0;
  1780. mismatch:
  1781. put_page(page);
  1782. return -EINVAL;
  1783. }
  1784. /*
  1785. * Analyze all data/parity pages in one meta block
  1786. * Returns:
  1787. * 0 for success
  1788. * -EINVAL for unknown playload type
  1789. * -EAGAIN for checksum mismatch of data page
  1790. * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
  1791. */
  1792. static int
  1793. r5c_recovery_analyze_meta_block(struct r5l_log *log,
  1794. struct r5l_recovery_ctx *ctx,
  1795. struct list_head *cached_stripe_list)
  1796. {
  1797. struct mddev *mddev = log->rdev->mddev;
  1798. struct r5conf *conf = mddev->private;
  1799. struct r5l_meta_block *mb;
  1800. struct r5l_payload_data_parity *payload;
  1801. struct r5l_payload_flush *payload_flush;
  1802. int mb_offset;
  1803. sector_t log_offset;
  1804. sector_t stripe_sect;
  1805. struct stripe_head *sh;
  1806. int ret;
  1807. /*
  1808. * for mismatch in data blocks, we will drop all data in this mb, but
  1809. * we will still read next mb for other data with FLUSH flag, as
  1810. * io_unit could finish out of order.
  1811. */
  1812. ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
  1813. if (ret == -EINVAL)
  1814. return -EAGAIN;
  1815. else if (ret)
  1816. return ret; /* -ENOMEM duo to alloc_page() failed */
  1817. mb = page_address(ctx->meta_page);
  1818. mb_offset = sizeof(struct r5l_meta_block);
  1819. log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  1820. while (mb_offset < le32_to_cpu(mb->meta_size)) {
  1821. int dd;
  1822. payload = (void *)mb + mb_offset;
  1823. payload_flush = (void *)mb + mb_offset;
  1824. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) {
  1825. int i, count;
  1826. count = le32_to_cpu(payload_flush->size) / sizeof(__le64);
  1827. for (i = 0; i < count; ++i) {
  1828. stripe_sect = le64_to_cpu(payload_flush->flush_stripes[i]);
  1829. sh = r5c_recovery_lookup_stripe(cached_stripe_list,
  1830. stripe_sect);
  1831. if (sh) {
  1832. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  1833. r5l_recovery_reset_stripe(sh);
  1834. list_del_init(&sh->lru);
  1835. raid5_release_stripe(sh);
  1836. }
  1837. }
  1838. mb_offset += sizeof(struct r5l_payload_flush) +
  1839. le32_to_cpu(payload_flush->size);
  1840. continue;
  1841. }
  1842. /* DATA or PARITY payload */
  1843. stripe_sect = (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) ?
  1844. raid5_compute_sector(
  1845. conf, le64_to_cpu(payload->location), 0, &dd,
  1846. NULL)
  1847. : le64_to_cpu(payload->location);
  1848. sh = r5c_recovery_lookup_stripe(cached_stripe_list,
  1849. stripe_sect);
  1850. if (!sh) {
  1851. sh = r5c_recovery_alloc_stripe(conf, stripe_sect);
  1852. /*
  1853. * cannot get stripe from raid5_get_active_stripe
  1854. * try replay some stripes
  1855. */
  1856. if (!sh) {
  1857. r5c_recovery_replay_stripes(
  1858. cached_stripe_list, ctx);
  1859. sh = r5c_recovery_alloc_stripe(
  1860. conf, stripe_sect);
  1861. }
  1862. if (!sh) {
  1863. pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
  1864. mdname(mddev),
  1865. conf->min_nr_stripes * 2);
  1866. raid5_set_cache_size(mddev,
  1867. conf->min_nr_stripes * 2);
  1868. sh = r5c_recovery_alloc_stripe(conf,
  1869. stripe_sect);
  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. pr_debug("md/raid:%s: using device %s as journal\n",
  2649. mdname(conf->mddev), bdevname(rdev->bdev, b));
  2650. if (PAGE_SIZE != 4096)
  2651. return -EINVAL;
  2652. /*
  2653. * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
  2654. * raid_disks r5l_payload_data_parity.
  2655. *
  2656. * Write journal and cache does not work for very big array
  2657. * (raid_disks > 203)
  2658. */
  2659. if (sizeof(struct r5l_meta_block) +
  2660. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
  2661. conf->raid_disks) > PAGE_SIZE) {
  2662. pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
  2663. mdname(conf->mddev), conf->raid_disks);
  2664. return -EINVAL;
  2665. }
  2666. log = kzalloc(sizeof(*log), GFP_KERNEL);
  2667. if (!log)
  2668. return -ENOMEM;
  2669. log->rdev = rdev;
  2670. log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
  2671. log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
  2672. sizeof(rdev->mddev->uuid));
  2673. mutex_init(&log->io_mutex);
  2674. spin_lock_init(&log->io_list_lock);
  2675. INIT_LIST_HEAD(&log->running_ios);
  2676. INIT_LIST_HEAD(&log->io_end_ios);
  2677. INIT_LIST_HEAD(&log->flushing_ios);
  2678. INIT_LIST_HEAD(&log->finished_ios);
  2679. bio_init(&log->flush_bio, NULL, 0);
  2680. log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
  2681. if (!log->io_kc)
  2682. goto io_kc;
  2683. log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
  2684. if (!log->io_pool)
  2685. goto io_pool;
  2686. log->bs = bioset_create(R5L_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  2687. if (!log->bs)
  2688. goto io_bs;
  2689. log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
  2690. if (!log->meta_pool)
  2691. goto out_mempool;
  2692. spin_lock_init(&log->tree_lock);
  2693. INIT_RADIX_TREE(&log->big_stripe_tree, GFP_NOWAIT | __GFP_NOWARN);
  2694. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  2695. log->rdev->mddev, "reclaim");
  2696. if (!log->reclaim_thread)
  2697. goto reclaim_thread;
  2698. log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
  2699. init_waitqueue_head(&log->iounit_wait);
  2700. INIT_LIST_HEAD(&log->no_mem_stripes);
  2701. INIT_LIST_HEAD(&log->no_space_stripes);
  2702. spin_lock_init(&log->no_space_stripes_lock);
  2703. INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
  2704. INIT_WORK(&log->disable_writeback_work, r5c_disable_writeback_async);
  2705. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  2706. INIT_LIST_HEAD(&log->stripe_in_journal_list);
  2707. spin_lock_init(&log->stripe_in_journal_lock);
  2708. atomic_set(&log->stripe_in_journal_count, 0);
  2709. rcu_assign_pointer(conf->log, log);
  2710. set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  2711. return 0;
  2712. rcu_assign_pointer(conf->log, NULL);
  2713. md_unregister_thread(&log->reclaim_thread);
  2714. reclaim_thread:
  2715. mempool_destroy(log->meta_pool);
  2716. out_mempool:
  2717. bioset_free(log->bs);
  2718. io_bs:
  2719. mempool_destroy(log->io_pool);
  2720. io_pool:
  2721. kmem_cache_destroy(log->io_kc);
  2722. io_kc:
  2723. kfree(log);
  2724. return -EINVAL;
  2725. }
  2726. void r5l_exit_log(struct r5conf *conf)
  2727. {
  2728. struct r5l_log *log = conf->log;
  2729. conf->log = NULL;
  2730. synchronize_rcu();
  2731. /* Ensure disable_writeback_work wakes up and exits */
  2732. wake_up(&conf->mddev->sb_wait);
  2733. flush_work(&log->disable_writeback_work);
  2734. md_unregister_thread(&log->reclaim_thread);
  2735. mempool_destroy(log->meta_pool);
  2736. bioset_free(log->bs);
  2737. mempool_destroy(log->io_pool);
  2738. kmem_cache_destroy(log->io_kc);
  2739. kfree(log);
  2740. }