raid5-cache.c 73 KB

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