raid5-cache.c 72 KB

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