raid5-cache.c 86 KB

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