raid5-cache.c 72 KB

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