raid5-cache.c 80 KB

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