raid5-cache.c 86 KB

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