raid5-cache.c 87 KB

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