raid5-cache.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. /*
  2. * Copyright (C) 2015 Shaohua Li <shli@fb.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/wait.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/slab.h>
  18. #include <linux/raid/md_p.h>
  19. #include <linux/crc32c.h>
  20. #include <linux/random.h>
  21. #include "md.h"
  22. #include "raid5.h"
  23. #include "bitmap.h"
  24. /*
  25. * metadata/data stored in disk with 4k size unit (a block) regardless
  26. * underneath hardware sector size. only works with PAGE_SIZE == 4096
  27. */
  28. #define BLOCK_SECTORS (8)
  29. /*
  30. * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
  31. *
  32. * In write through mode, the reclaim runs every log->max_free_space.
  33. * This can prevent the recovery scans for too long
  34. */
  35. #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
  36. #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
  37. /* wake up reclaim thread periodically */
  38. #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
  39. /* start flush with these full stripes */
  40. #define R5C_FULL_STRIPE_FLUSH_BATCH 256
  41. /* reclaim stripes in groups */
  42. #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
  43. /*
  44. * We only need 2 bios per I/O unit to make progress, but ensure we
  45. * have a few more available to not get too tight.
  46. */
  47. #define R5L_POOL_SIZE 4
  48. /*
  49. * r5c journal modes of the array: write-back or write-through.
  50. * write-through mode has identical behavior as existing log only
  51. * implementation.
  52. */
  53. enum r5c_journal_mode {
  54. R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
  55. R5C_JOURNAL_MODE_WRITE_BACK = 1,
  56. };
  57. static char *r5c_journal_mode_str[] = {"write-through",
  58. "write-back"};
  59. /*
  60. * raid5 cache state machine
  61. *
  62. * With rhe RAID cache, each stripe works in two phases:
  63. * - caching phase
  64. * - writing-out phase
  65. *
  66. * These two phases are controlled by bit STRIPE_R5C_CACHING:
  67. * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
  68. * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
  69. *
  70. * When there is no journal, or the journal is in write-through mode,
  71. * the stripe is always in writing-out phase.
  72. *
  73. * For write-back journal, the stripe is sent to caching phase on write
  74. * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
  75. * the write-out phase by clearing STRIPE_R5C_CACHING.
  76. *
  77. * Stripes in caching phase do not write the raid disks. Instead, all
  78. * writes are committed from the log device. Therefore, a stripe in
  79. * caching phase handles writes as:
  80. * - write to log device
  81. * - return IO
  82. *
  83. * Stripes in writing-out phase handle writes as:
  84. * - calculate parity
  85. * - write pending data and parity to journal
  86. * - write data and parity to raid disks
  87. * - return IO for pending writes
  88. */
  89. struct r5l_log {
  90. struct md_rdev *rdev;
  91. u32 uuid_checksum;
  92. sector_t device_size; /* log device size, round to
  93. * BLOCK_SECTORS */
  94. sector_t max_free_space; /* reclaim run if free space is at
  95. * this size */
  96. sector_t last_checkpoint; /* log tail. where recovery scan
  97. * starts from */
  98. u64 last_cp_seq; /* log tail sequence */
  99. sector_t log_start; /* log head. where new data appends */
  100. u64 seq; /* log head sequence */
  101. sector_t next_checkpoint;
  102. u64 next_cp_seq;
  103. struct mutex io_mutex;
  104. struct r5l_io_unit *current_io; /* current io_unit accepting new data */
  105. spinlock_t io_list_lock;
  106. struct list_head running_ios; /* io_units which are still running,
  107. * and have not yet been completely
  108. * written to the log */
  109. struct list_head io_end_ios; /* io_units which have been completely
  110. * written to the log but not yet written
  111. * to the RAID */
  112. struct list_head flushing_ios; /* io_units which are waiting for log
  113. * cache flush */
  114. struct list_head finished_ios; /* io_units which settle down in log disk */
  115. struct bio flush_bio;
  116. struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
  117. struct kmem_cache *io_kc;
  118. mempool_t *io_pool;
  119. struct bio_set *bs;
  120. mempool_t *meta_pool;
  121. struct md_thread *reclaim_thread;
  122. unsigned long reclaim_target; /* number of space that need to be
  123. * reclaimed. if it's 0, reclaim spaces
  124. * used by io_units which are in
  125. * IO_UNIT_STRIPE_END state (eg, reclaim
  126. * dones't wait for specific io_unit
  127. * switching to IO_UNIT_STRIPE_END
  128. * state) */
  129. wait_queue_head_t iounit_wait;
  130. struct list_head no_space_stripes; /* pending stripes, log has no space */
  131. spinlock_t no_space_stripes_lock;
  132. bool need_cache_flush;
  133. /* for r5c_cache */
  134. enum r5c_journal_mode r5c_journal_mode;
  135. /* all stripes in r5cache, in the order of seq at sh->log_start */
  136. struct list_head stripe_in_journal_list;
  137. spinlock_t stripe_in_journal_lock;
  138. atomic_t stripe_in_journal_count;
  139. };
  140. /*
  141. * an IO range starts from a meta data block and end at the next meta data
  142. * block. The io unit's the meta data block tracks data/parity followed it. io
  143. * unit is written to log disk with normal write, as we always flush log disk
  144. * first and then start move data to raid disks, there is no requirement to
  145. * write io unit with FLUSH/FUA
  146. */
  147. struct r5l_io_unit {
  148. struct r5l_log *log;
  149. struct page *meta_page; /* store meta block */
  150. int meta_offset; /* current offset in meta_page */
  151. struct bio *current_bio;/* current_bio accepting new data */
  152. atomic_t pending_stripe;/* how many stripes not flushed to raid */
  153. u64 seq; /* seq number of the metablock */
  154. sector_t log_start; /* where the io_unit starts */
  155. sector_t log_end; /* where the io_unit ends */
  156. struct list_head log_sibling; /* log->running_ios */
  157. struct list_head stripe_list; /* stripes added to the io_unit */
  158. int state;
  159. bool need_split_bio;
  160. };
  161. /* r5l_io_unit state */
  162. enum r5l_io_unit_state {
  163. IO_UNIT_RUNNING = 0, /* accepting new IO */
  164. IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
  165. * don't accepting new bio */
  166. IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
  167. IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
  168. };
  169. bool r5c_is_writeback(struct r5l_log *log)
  170. {
  171. return (log != NULL &&
  172. log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
  173. }
  174. static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
  175. {
  176. start += inc;
  177. if (start >= log->device_size)
  178. start = start - log->device_size;
  179. return start;
  180. }
  181. static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
  182. sector_t end)
  183. {
  184. if (end >= start)
  185. return end - start;
  186. else
  187. return end + log->device_size - start;
  188. }
  189. static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
  190. {
  191. sector_t used_size;
  192. used_size = r5l_ring_distance(log, log->last_checkpoint,
  193. log->log_start);
  194. return log->device_size > used_size + size;
  195. }
  196. static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
  197. enum r5l_io_unit_state state)
  198. {
  199. if (WARN_ON(io->state >= state))
  200. return;
  201. io->state = state;
  202. }
  203. static void
  204. r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
  205. struct bio_list *return_bi)
  206. {
  207. struct bio *wbi, *wbi2;
  208. wbi = dev->written;
  209. dev->written = NULL;
  210. while (wbi && wbi->bi_iter.bi_sector <
  211. dev->sector + STRIPE_SECTORS) {
  212. wbi2 = r5_next_bio(wbi, dev->sector);
  213. if (!raid5_dec_bi_active_stripes(wbi)) {
  214. md_write_end(conf->mddev);
  215. bio_list_add(return_bi, wbi);
  216. }
  217. wbi = wbi2;
  218. }
  219. }
  220. void r5c_handle_cached_data_endio(struct r5conf *conf,
  221. struct stripe_head *sh, int disks, struct bio_list *return_bi)
  222. {
  223. int i;
  224. for (i = sh->disks; i--; ) {
  225. if (sh->dev[i].written) {
  226. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  227. r5c_return_dev_pending_writes(conf, &sh->dev[i],
  228. return_bi);
  229. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  230. STRIPE_SECTORS,
  231. !test_bit(STRIPE_DEGRADED, &sh->state),
  232. 0);
  233. }
  234. }
  235. }
  236. /* Check whether we should flush some stripes to free up stripe cache */
  237. void r5c_check_stripe_cache_usage(struct r5conf *conf)
  238. {
  239. int total_cached;
  240. if (!r5c_is_writeback(conf->log))
  241. return;
  242. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  243. atomic_read(&conf->r5c_cached_full_stripes);
  244. /*
  245. * The following condition is true for either of the following:
  246. * - stripe cache pressure high:
  247. * total_cached > 3/4 min_nr_stripes ||
  248. * empty_inactive_list_nr > 0
  249. * - stripe cache pressure moderate:
  250. * total_cached > 1/2 min_nr_stripes
  251. */
  252. if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  253. atomic_read(&conf->empty_inactive_list_nr) > 0)
  254. r5l_wake_reclaim(conf->log, 0);
  255. }
  256. /*
  257. * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
  258. * stripes in the cache
  259. */
  260. void r5c_check_cached_full_stripe(struct r5conf *conf)
  261. {
  262. if (!r5c_is_writeback(conf->log))
  263. return;
  264. /*
  265. * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
  266. * or a full stripe (chunk size / 4k stripes).
  267. */
  268. if (atomic_read(&conf->r5c_cached_full_stripes) >=
  269. min(R5C_FULL_STRIPE_FLUSH_BATCH,
  270. conf->chunk_sectors >> STRIPE_SHIFT))
  271. r5l_wake_reclaim(conf->log, 0);
  272. }
  273. /*
  274. * Total log space (in sectors) needed to flush all data in cache
  275. *
  276. * Currently, writing-out phase automatically includes all pending writes
  277. * to the same sector. So the reclaim of each stripe takes up to
  278. * (conf->raid_disks + 1) pages of log space.
  279. *
  280. * To totally avoid deadlock due to log space, the code reserves
  281. * (conf->raid_disks + 1) pages for each stripe in cache, which is not
  282. * necessary in most cases.
  283. *
  284. * To improve this, we will need writing-out phase to be able to NOT include
  285. * pending writes, which will reduce the requirement to
  286. * (conf->max_degraded + 1) pages per stripe in cache.
  287. */
  288. static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
  289. {
  290. struct r5l_log *log = conf->log;
  291. if (!r5c_is_writeback(log))
  292. return 0;
  293. return BLOCK_SECTORS * (conf->raid_disks + 1) *
  294. atomic_read(&log->stripe_in_journal_count);
  295. }
  296. /*
  297. * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
  298. *
  299. * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
  300. * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
  301. * device is less than 2x of reclaim_required_space.
  302. */
  303. static inline void r5c_update_log_state(struct r5l_log *log)
  304. {
  305. struct r5conf *conf = log->rdev->mddev->private;
  306. sector_t free_space;
  307. sector_t reclaim_space;
  308. if (!r5c_is_writeback(log))
  309. return;
  310. free_space = r5l_ring_distance(log, log->log_start,
  311. log->last_checkpoint);
  312. reclaim_space = r5c_log_required_to_flush_cache(conf);
  313. if (free_space < 2 * reclaim_space)
  314. set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
  315. else
  316. clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
  317. if (free_space < 3 * reclaim_space)
  318. set_bit(R5C_LOG_TIGHT, &conf->cache_state);
  319. else
  320. clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
  321. }
  322. /*
  323. * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
  324. * This function should only be called in write-back mode.
  325. */
  326. void r5c_make_stripe_write_out(struct stripe_head *sh)
  327. {
  328. struct r5conf *conf = sh->raid_conf;
  329. struct r5l_log *log = conf->log;
  330. BUG_ON(!r5c_is_writeback(log));
  331. WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  332. clear_bit(STRIPE_R5C_CACHING, &sh->state);
  333. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  334. atomic_inc(&conf->preread_active_stripes);
  335. if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
  336. BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
  337. atomic_dec(&conf->r5c_cached_partial_stripes);
  338. }
  339. if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
  340. BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
  341. atomic_dec(&conf->r5c_cached_full_stripes);
  342. }
  343. }
  344. static void r5c_handle_data_cached(struct stripe_head *sh)
  345. {
  346. int i;
  347. for (i = sh->disks; i--; )
  348. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
  349. set_bit(R5_InJournal, &sh->dev[i].flags);
  350. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  351. }
  352. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  353. }
  354. /*
  355. * this journal write must contain full parity,
  356. * it may also contain some data pages
  357. */
  358. static void r5c_handle_parity_cached(struct stripe_head *sh)
  359. {
  360. int i;
  361. for (i = sh->disks; i--; )
  362. if (test_bit(R5_InJournal, &sh->dev[i].flags))
  363. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  364. }
  365. /*
  366. * Setting proper flags after writing (or flushing) data and/or parity to the
  367. * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
  368. */
  369. static void r5c_finish_cache_stripe(struct stripe_head *sh)
  370. {
  371. struct r5l_log *log = sh->raid_conf->log;
  372. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  373. BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  374. /*
  375. * Set R5_InJournal for parity dev[pd_idx]. This means
  376. * all data AND parity in the journal. For RAID 6, it is
  377. * NOT necessary to set the flag for dev[qd_idx], as the
  378. * two parities are written out together.
  379. */
  380. set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  381. } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  382. r5c_handle_data_cached(sh);
  383. } else {
  384. r5c_handle_parity_cached(sh);
  385. set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  386. }
  387. }
  388. static void r5l_io_run_stripes(struct r5l_io_unit *io)
  389. {
  390. struct stripe_head *sh, *next;
  391. list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
  392. list_del_init(&sh->log_list);
  393. r5c_finish_cache_stripe(sh);
  394. set_bit(STRIPE_HANDLE, &sh->state);
  395. raid5_release_stripe(sh);
  396. }
  397. }
  398. static void r5l_log_run_stripes(struct r5l_log *log)
  399. {
  400. struct r5l_io_unit *io, *next;
  401. assert_spin_locked(&log->io_list_lock);
  402. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  403. /* don't change list order */
  404. if (io->state < IO_UNIT_IO_END)
  405. break;
  406. list_move_tail(&io->log_sibling, &log->finished_ios);
  407. r5l_io_run_stripes(io);
  408. }
  409. }
  410. static void r5l_move_to_end_ios(struct r5l_log *log)
  411. {
  412. struct r5l_io_unit *io, *next;
  413. assert_spin_locked(&log->io_list_lock);
  414. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  415. /* don't change list order */
  416. if (io->state < IO_UNIT_IO_END)
  417. break;
  418. list_move_tail(&io->log_sibling, &log->io_end_ios);
  419. }
  420. }
  421. static void r5l_log_endio(struct bio *bio)
  422. {
  423. struct r5l_io_unit *io = bio->bi_private;
  424. struct r5l_log *log = io->log;
  425. unsigned long flags;
  426. if (bio->bi_error)
  427. md_error(log->rdev->mddev, log->rdev);
  428. bio_put(bio);
  429. mempool_free(io->meta_page, log->meta_pool);
  430. spin_lock_irqsave(&log->io_list_lock, flags);
  431. __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
  432. if (log->need_cache_flush)
  433. r5l_move_to_end_ios(log);
  434. else
  435. r5l_log_run_stripes(log);
  436. spin_unlock_irqrestore(&log->io_list_lock, flags);
  437. if (log->need_cache_flush)
  438. md_wakeup_thread(log->rdev->mddev->thread);
  439. }
  440. static void r5l_submit_current_io(struct r5l_log *log)
  441. {
  442. struct r5l_io_unit *io = log->current_io;
  443. struct r5l_meta_block *block;
  444. unsigned long flags;
  445. u32 crc;
  446. if (!io)
  447. return;
  448. block = page_address(io->meta_page);
  449. block->meta_size = cpu_to_le32(io->meta_offset);
  450. crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
  451. block->checksum = cpu_to_le32(crc);
  452. log->current_io = NULL;
  453. spin_lock_irqsave(&log->io_list_lock, flags);
  454. __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
  455. spin_unlock_irqrestore(&log->io_list_lock, flags);
  456. submit_bio(io->current_bio);
  457. }
  458. static struct bio *r5l_bio_alloc(struct r5l_log *log)
  459. {
  460. struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
  461. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  462. bio->bi_bdev = log->rdev->bdev;
  463. bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
  464. return bio;
  465. }
  466. static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
  467. {
  468. log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
  469. r5c_update_log_state(log);
  470. /*
  471. * If we filled up the log device start from the beginning again,
  472. * which will require a new bio.
  473. *
  474. * Note: for this to work properly the log size needs to me a multiple
  475. * of BLOCK_SECTORS.
  476. */
  477. if (log->log_start == 0)
  478. io->need_split_bio = true;
  479. io->log_end = log->log_start;
  480. }
  481. static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
  482. {
  483. struct r5l_io_unit *io;
  484. struct r5l_meta_block *block;
  485. io = mempool_alloc(log->io_pool, GFP_ATOMIC);
  486. if (!io)
  487. return NULL;
  488. memset(io, 0, sizeof(*io));
  489. io->log = log;
  490. INIT_LIST_HEAD(&io->log_sibling);
  491. INIT_LIST_HEAD(&io->stripe_list);
  492. io->state = IO_UNIT_RUNNING;
  493. io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
  494. block = page_address(io->meta_page);
  495. clear_page(block);
  496. block->magic = cpu_to_le32(R5LOG_MAGIC);
  497. block->version = R5LOG_VERSION;
  498. block->seq = cpu_to_le64(log->seq);
  499. block->position = cpu_to_le64(log->log_start);
  500. io->log_start = log->log_start;
  501. io->meta_offset = sizeof(struct r5l_meta_block);
  502. io->seq = log->seq++;
  503. io->current_bio = r5l_bio_alloc(log);
  504. io->current_bio->bi_end_io = r5l_log_endio;
  505. io->current_bio->bi_private = io;
  506. bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
  507. r5_reserve_log_entry(log, io);
  508. spin_lock_irq(&log->io_list_lock);
  509. list_add_tail(&io->log_sibling, &log->running_ios);
  510. spin_unlock_irq(&log->io_list_lock);
  511. return io;
  512. }
  513. static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
  514. {
  515. if (log->current_io &&
  516. log->current_io->meta_offset + payload_size > PAGE_SIZE)
  517. r5l_submit_current_io(log);
  518. if (!log->current_io) {
  519. log->current_io = r5l_new_meta(log);
  520. if (!log->current_io)
  521. return -ENOMEM;
  522. }
  523. return 0;
  524. }
  525. static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
  526. sector_t location,
  527. u32 checksum1, u32 checksum2,
  528. bool checksum2_valid)
  529. {
  530. struct r5l_io_unit *io = log->current_io;
  531. struct r5l_payload_data_parity *payload;
  532. payload = page_address(io->meta_page) + io->meta_offset;
  533. payload->header.type = cpu_to_le16(type);
  534. payload->header.flags = cpu_to_le16(0);
  535. payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
  536. (PAGE_SHIFT - 9));
  537. payload->location = cpu_to_le64(location);
  538. payload->checksum[0] = cpu_to_le32(checksum1);
  539. if (checksum2_valid)
  540. payload->checksum[1] = cpu_to_le32(checksum2);
  541. io->meta_offset += sizeof(struct r5l_payload_data_parity) +
  542. sizeof(__le32) * (1 + !!checksum2_valid);
  543. }
  544. static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
  545. {
  546. struct r5l_io_unit *io = log->current_io;
  547. if (io->need_split_bio) {
  548. struct bio *prev = io->current_bio;
  549. io->current_bio = r5l_bio_alloc(log);
  550. bio_chain(io->current_bio, prev);
  551. submit_bio(prev);
  552. }
  553. if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
  554. BUG();
  555. r5_reserve_log_entry(log, io);
  556. }
  557. static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
  558. int data_pages, int parity_pages)
  559. {
  560. int i;
  561. int meta_size;
  562. int ret;
  563. struct r5l_io_unit *io;
  564. meta_size =
  565. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
  566. * data_pages) +
  567. sizeof(struct r5l_payload_data_parity) +
  568. sizeof(__le32) * parity_pages;
  569. ret = r5l_get_meta(log, meta_size);
  570. if (ret)
  571. return ret;
  572. io = log->current_io;
  573. for (i = 0; i < sh->disks; i++) {
  574. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  575. test_bit(R5_InJournal, &sh->dev[i].flags))
  576. continue;
  577. if (i == sh->pd_idx || i == sh->qd_idx)
  578. continue;
  579. r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
  580. raid5_compute_blocknr(sh, i, 0),
  581. sh->dev[i].log_checksum, 0, false);
  582. r5l_append_payload_page(log, sh->dev[i].page);
  583. }
  584. if (parity_pages == 2) {
  585. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  586. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  587. sh->dev[sh->qd_idx].log_checksum, true);
  588. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  589. r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
  590. } else if (parity_pages == 1) {
  591. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  592. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  593. 0, false);
  594. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  595. } else /* Just writing data, not parity, in caching phase */
  596. BUG_ON(parity_pages != 0);
  597. list_add_tail(&sh->log_list, &io->stripe_list);
  598. atomic_inc(&io->pending_stripe);
  599. sh->log_io = io;
  600. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  601. return 0;
  602. if (sh->log_start == MaxSector) {
  603. BUG_ON(!list_empty(&sh->r5c));
  604. sh->log_start = io->log_start;
  605. spin_lock_irq(&log->stripe_in_journal_lock);
  606. list_add_tail(&sh->r5c,
  607. &log->stripe_in_journal_list);
  608. spin_unlock_irq(&log->stripe_in_journal_lock);
  609. atomic_inc(&log->stripe_in_journal_count);
  610. }
  611. return 0;
  612. }
  613. /* add stripe to no_space_stripes, and then wake up reclaim */
  614. static inline void r5l_add_no_space_stripe(struct r5l_log *log,
  615. struct stripe_head *sh)
  616. {
  617. spin_lock(&log->no_space_stripes_lock);
  618. list_add_tail(&sh->log_list, &log->no_space_stripes);
  619. spin_unlock(&log->no_space_stripes_lock);
  620. }
  621. /*
  622. * running in raid5d, where reclaim could wait for raid5d too (when it flushes
  623. * data from log to raid disks), so we shouldn't wait for reclaim here
  624. */
  625. int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
  626. {
  627. struct r5conf *conf = sh->raid_conf;
  628. int write_disks = 0;
  629. int data_pages, parity_pages;
  630. int reserve;
  631. int i;
  632. int ret = 0;
  633. bool wake_reclaim = false;
  634. if (!log)
  635. return -EAGAIN;
  636. /* Don't support stripe batch */
  637. if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
  638. test_bit(STRIPE_SYNCING, &sh->state)) {
  639. /* the stripe is written to log, we start writing it to raid */
  640. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  641. return -EAGAIN;
  642. }
  643. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  644. for (i = 0; i < sh->disks; i++) {
  645. void *addr;
  646. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
  647. test_bit(R5_InJournal, &sh->dev[i].flags))
  648. continue;
  649. write_disks++;
  650. /* checksum is already calculated in last run */
  651. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  652. continue;
  653. addr = kmap_atomic(sh->dev[i].page);
  654. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  655. addr, PAGE_SIZE);
  656. kunmap_atomic(addr);
  657. }
  658. parity_pages = 1 + !!(sh->qd_idx >= 0);
  659. data_pages = write_disks - parity_pages;
  660. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  661. /*
  662. * The stripe must enter state machine again to finish the write, so
  663. * don't delay.
  664. */
  665. clear_bit(STRIPE_DELAYED, &sh->state);
  666. atomic_inc(&sh->count);
  667. mutex_lock(&log->io_mutex);
  668. /* meta + data */
  669. reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
  670. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
  671. if (!r5l_has_free_space(log, reserve)) {
  672. r5l_add_no_space_stripe(log, sh);
  673. wake_reclaim = true;
  674. } else {
  675. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  676. if (ret) {
  677. spin_lock_irq(&log->io_list_lock);
  678. list_add_tail(&sh->log_list,
  679. &log->no_mem_stripes);
  680. spin_unlock_irq(&log->io_list_lock);
  681. }
  682. }
  683. } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
  684. /*
  685. * log space critical, do not process stripes that are
  686. * not in cache yet (sh->log_start == MaxSector).
  687. */
  688. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  689. sh->log_start == MaxSector) {
  690. r5l_add_no_space_stripe(log, sh);
  691. wake_reclaim = true;
  692. reserve = 0;
  693. } else if (!r5l_has_free_space(log, reserve)) {
  694. if (sh->log_start == log->last_checkpoint)
  695. BUG();
  696. else
  697. r5l_add_no_space_stripe(log, sh);
  698. } else {
  699. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  700. if (ret) {
  701. spin_lock_irq(&log->io_list_lock);
  702. list_add_tail(&sh->log_list,
  703. &log->no_mem_stripes);
  704. spin_unlock_irq(&log->io_list_lock);
  705. }
  706. }
  707. }
  708. mutex_unlock(&log->io_mutex);
  709. if (wake_reclaim)
  710. r5l_wake_reclaim(log, reserve);
  711. return 0;
  712. }
  713. void r5l_write_stripe_run(struct r5l_log *log)
  714. {
  715. if (!log)
  716. return;
  717. mutex_lock(&log->io_mutex);
  718. r5l_submit_current_io(log);
  719. mutex_unlock(&log->io_mutex);
  720. }
  721. int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
  722. {
  723. if (!log)
  724. return -ENODEV;
  725. /*
  726. * we flush log disk cache first, then write stripe data to raid disks.
  727. * So if bio is finished, the log disk cache is flushed already. The
  728. * recovery guarantees we can recovery the bio from log disk, so we
  729. * don't need to flush again
  730. */
  731. if (bio->bi_iter.bi_size == 0) {
  732. bio_endio(bio);
  733. return 0;
  734. }
  735. bio->bi_opf &= ~REQ_PREFLUSH;
  736. return -EAGAIN;
  737. }
  738. /* This will run after log space is reclaimed */
  739. static void r5l_run_no_space_stripes(struct r5l_log *log)
  740. {
  741. struct stripe_head *sh;
  742. spin_lock(&log->no_space_stripes_lock);
  743. while (!list_empty(&log->no_space_stripes)) {
  744. sh = list_first_entry(&log->no_space_stripes,
  745. struct stripe_head, log_list);
  746. list_del_init(&sh->log_list);
  747. set_bit(STRIPE_HANDLE, &sh->state);
  748. raid5_release_stripe(sh);
  749. }
  750. spin_unlock(&log->no_space_stripes_lock);
  751. }
  752. /*
  753. * calculate new last_checkpoint
  754. * for write through mode, returns log->next_checkpoint
  755. * for write back, returns log_start of first sh in stripe_in_journal_list
  756. */
  757. static sector_t r5c_calculate_new_cp(struct r5conf *conf)
  758. {
  759. struct stripe_head *sh;
  760. struct r5l_log *log = conf->log;
  761. sector_t new_cp;
  762. unsigned long flags;
  763. if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  764. return log->next_checkpoint;
  765. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  766. if (list_empty(&conf->log->stripe_in_journal_list)) {
  767. /* all stripes flushed */
  768. spin_unlock(&log->stripe_in_journal_lock);
  769. return log->next_checkpoint;
  770. }
  771. sh = list_first_entry(&conf->log->stripe_in_journal_list,
  772. struct stripe_head, r5c);
  773. new_cp = sh->log_start;
  774. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  775. return new_cp;
  776. }
  777. static sector_t r5l_reclaimable_space(struct r5l_log *log)
  778. {
  779. struct r5conf *conf = log->rdev->mddev->private;
  780. return r5l_ring_distance(log, log->last_checkpoint,
  781. r5c_calculate_new_cp(conf));
  782. }
  783. static void r5l_run_no_mem_stripe(struct r5l_log *log)
  784. {
  785. struct stripe_head *sh;
  786. assert_spin_locked(&log->io_list_lock);
  787. if (!list_empty(&log->no_mem_stripes)) {
  788. sh = list_first_entry(&log->no_mem_stripes,
  789. struct stripe_head, log_list);
  790. list_del_init(&sh->log_list);
  791. set_bit(STRIPE_HANDLE, &sh->state);
  792. raid5_release_stripe(sh);
  793. }
  794. }
  795. static bool r5l_complete_finished_ios(struct r5l_log *log)
  796. {
  797. struct r5l_io_unit *io, *next;
  798. bool found = false;
  799. assert_spin_locked(&log->io_list_lock);
  800. list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
  801. /* don't change list order */
  802. if (io->state < IO_UNIT_STRIPE_END)
  803. break;
  804. log->next_checkpoint = io->log_start;
  805. log->next_cp_seq = io->seq;
  806. list_del(&io->log_sibling);
  807. mempool_free(io, log->io_pool);
  808. r5l_run_no_mem_stripe(log);
  809. found = true;
  810. }
  811. return found;
  812. }
  813. static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
  814. {
  815. struct r5l_log *log = io->log;
  816. struct r5conf *conf = log->rdev->mddev->private;
  817. unsigned long flags;
  818. spin_lock_irqsave(&log->io_list_lock, flags);
  819. __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
  820. if (!r5l_complete_finished_ios(log)) {
  821. spin_unlock_irqrestore(&log->io_list_lock, flags);
  822. return;
  823. }
  824. if (r5l_reclaimable_space(log) > log->max_free_space ||
  825. test_bit(R5C_LOG_TIGHT, &conf->cache_state))
  826. r5l_wake_reclaim(log, 0);
  827. spin_unlock_irqrestore(&log->io_list_lock, flags);
  828. wake_up(&log->iounit_wait);
  829. }
  830. void r5l_stripe_write_finished(struct stripe_head *sh)
  831. {
  832. struct r5l_io_unit *io;
  833. io = sh->log_io;
  834. sh->log_io = NULL;
  835. if (io && atomic_dec_and_test(&io->pending_stripe))
  836. __r5l_stripe_write_finished(io);
  837. }
  838. static void r5l_log_flush_endio(struct bio *bio)
  839. {
  840. struct r5l_log *log = container_of(bio, struct r5l_log,
  841. flush_bio);
  842. unsigned long flags;
  843. struct r5l_io_unit *io;
  844. if (bio->bi_error)
  845. md_error(log->rdev->mddev, log->rdev);
  846. spin_lock_irqsave(&log->io_list_lock, flags);
  847. list_for_each_entry(io, &log->flushing_ios, log_sibling)
  848. r5l_io_run_stripes(io);
  849. list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
  850. spin_unlock_irqrestore(&log->io_list_lock, flags);
  851. }
  852. /*
  853. * Starting dispatch IO to raid.
  854. * io_unit(meta) consists of a log. There is one situation we want to avoid. A
  855. * broken meta in the middle of a log causes recovery can't find meta at the
  856. * head of log. If operations require meta at the head persistent in log, we
  857. * must make sure meta before it persistent in log too. A case is:
  858. *
  859. * stripe data/parity is in log, we start write stripe to raid disks. stripe
  860. * data/parity must be persistent in log before we do the write to raid disks.
  861. *
  862. * The solution is we restrictly maintain io_unit list order. In this case, we
  863. * only write stripes of an io_unit to raid disks till the io_unit is the first
  864. * one whose data/parity is in log.
  865. */
  866. void r5l_flush_stripe_to_raid(struct r5l_log *log)
  867. {
  868. bool do_flush;
  869. if (!log || !log->need_cache_flush)
  870. return;
  871. spin_lock_irq(&log->io_list_lock);
  872. /* flush bio is running */
  873. if (!list_empty(&log->flushing_ios)) {
  874. spin_unlock_irq(&log->io_list_lock);
  875. return;
  876. }
  877. list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
  878. do_flush = !list_empty(&log->flushing_ios);
  879. spin_unlock_irq(&log->io_list_lock);
  880. if (!do_flush)
  881. return;
  882. bio_reset(&log->flush_bio);
  883. log->flush_bio.bi_bdev = log->rdev->bdev;
  884. log->flush_bio.bi_end_io = r5l_log_flush_endio;
  885. bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
  886. submit_bio(&log->flush_bio);
  887. }
  888. static void r5l_write_super(struct r5l_log *log, sector_t cp);
  889. static void r5l_write_super_and_discard_space(struct r5l_log *log,
  890. sector_t end)
  891. {
  892. struct block_device *bdev = log->rdev->bdev;
  893. struct mddev *mddev;
  894. r5l_write_super(log, end);
  895. if (!blk_queue_discard(bdev_get_queue(bdev)))
  896. return;
  897. mddev = log->rdev->mddev;
  898. /*
  899. * Discard could zero data, so before discard we must make sure
  900. * superblock is updated to new log tail. Updating superblock (either
  901. * directly call md_update_sb() or depend on md thread) must hold
  902. * reconfig mutex. On the other hand, raid5_quiesce is called with
  903. * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
  904. * for all IO finish, hence waitting for reclaim thread, while reclaim
  905. * thread is calling this function and waitting for reconfig mutex. So
  906. * there is a deadlock. We workaround this issue with a trylock.
  907. * FIXME: we could miss discard if we can't take reconfig mutex
  908. */
  909. set_mask_bits(&mddev->flags, 0,
  910. BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
  911. if (!mddev_trylock(mddev))
  912. return;
  913. md_update_sb(mddev, 1);
  914. mddev_unlock(mddev);
  915. /* discard IO error really doesn't matter, ignore it */
  916. if (log->last_checkpoint < end) {
  917. blkdev_issue_discard(bdev,
  918. log->last_checkpoint + log->rdev->data_offset,
  919. end - log->last_checkpoint, GFP_NOIO, 0);
  920. } else {
  921. blkdev_issue_discard(bdev,
  922. log->last_checkpoint + log->rdev->data_offset,
  923. log->device_size - log->last_checkpoint,
  924. GFP_NOIO, 0);
  925. blkdev_issue_discard(bdev, log->rdev->data_offset, end,
  926. GFP_NOIO, 0);
  927. }
  928. }
  929. /*
  930. * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
  931. * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
  932. *
  933. * must hold conf->device_lock
  934. */
  935. static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
  936. {
  937. BUG_ON(list_empty(&sh->lru));
  938. BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
  939. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  940. /*
  941. * The stripe is not ON_RELEASE_LIST, so it is safe to call
  942. * raid5_release_stripe() while holding conf->device_lock
  943. */
  944. BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
  945. assert_spin_locked(&conf->device_lock);
  946. list_del_init(&sh->lru);
  947. atomic_inc(&sh->count);
  948. set_bit(STRIPE_HANDLE, &sh->state);
  949. atomic_inc(&conf->active_stripes);
  950. r5c_make_stripe_write_out(sh);
  951. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  952. atomic_inc(&conf->preread_active_stripes);
  953. raid5_release_stripe(sh);
  954. }
  955. /*
  956. * if num == 0, flush all full stripes
  957. * if num > 0, flush all full stripes. If less than num full stripes are
  958. * flushed, flush some partial stripes until totally num stripes are
  959. * flushed or there is no more cached stripes.
  960. */
  961. void r5c_flush_cache(struct r5conf *conf, int num)
  962. {
  963. int count;
  964. struct stripe_head *sh, *next;
  965. assert_spin_locked(&conf->device_lock);
  966. if (!conf->log)
  967. return;
  968. count = 0;
  969. list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
  970. r5c_flush_stripe(conf, sh);
  971. count++;
  972. }
  973. if (count >= num)
  974. return;
  975. list_for_each_entry_safe(sh, next,
  976. &conf->r5c_partial_stripe_list, lru) {
  977. r5c_flush_stripe(conf, sh);
  978. if (++count >= num)
  979. break;
  980. }
  981. }
  982. static void r5c_do_reclaim(struct r5conf *conf)
  983. {
  984. struct r5l_log *log = conf->log;
  985. struct stripe_head *sh;
  986. int count = 0;
  987. unsigned long flags;
  988. int total_cached;
  989. int stripes_to_flush;
  990. if (!r5c_is_writeback(log))
  991. return;
  992. total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
  993. atomic_read(&conf->r5c_cached_full_stripes);
  994. if (total_cached > conf->min_nr_stripes * 3 / 4 ||
  995. atomic_read(&conf->empty_inactive_list_nr) > 0)
  996. /*
  997. * if stripe cache pressure high, flush all full stripes and
  998. * some partial stripes
  999. */
  1000. stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
  1001. else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
  1002. atomic_read(&conf->r5c_cached_full_stripes) >
  1003. R5C_FULL_STRIPE_FLUSH_BATCH)
  1004. /*
  1005. * if stripe cache pressure moderate, or if there is many full
  1006. * stripes,flush all full stripes
  1007. */
  1008. stripes_to_flush = 0;
  1009. else
  1010. /* no need to flush */
  1011. stripes_to_flush = -1;
  1012. if (stripes_to_flush >= 0) {
  1013. spin_lock_irqsave(&conf->device_lock, flags);
  1014. r5c_flush_cache(conf, stripes_to_flush);
  1015. spin_unlock_irqrestore(&conf->device_lock, flags);
  1016. }
  1017. /* if log space is tight, flush stripes on stripe_in_journal_list */
  1018. if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
  1019. spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
  1020. spin_lock(&conf->device_lock);
  1021. list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
  1022. /*
  1023. * stripes on stripe_in_journal_list could be in any
  1024. * state of the stripe_cache state machine. In this
  1025. * case, we only want to flush stripe on
  1026. * r5c_cached_full/partial_stripes. The following
  1027. * condition makes sure the stripe is on one of the
  1028. * two lists.
  1029. */
  1030. if (!list_empty(&sh->lru) &&
  1031. !test_bit(STRIPE_HANDLE, &sh->state) &&
  1032. atomic_read(&sh->count) == 0) {
  1033. r5c_flush_stripe(conf, sh);
  1034. }
  1035. if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
  1036. break;
  1037. }
  1038. spin_unlock(&conf->device_lock);
  1039. spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
  1040. }
  1041. md_wakeup_thread(conf->mddev->thread);
  1042. }
  1043. static void r5l_do_reclaim(struct r5l_log *log)
  1044. {
  1045. struct r5conf *conf = log->rdev->mddev->private;
  1046. sector_t reclaim_target = xchg(&log->reclaim_target, 0);
  1047. sector_t reclaimable;
  1048. sector_t next_checkpoint;
  1049. bool write_super;
  1050. spin_lock_irq(&log->io_list_lock);
  1051. write_super = r5l_reclaimable_space(log) > log->max_free_space ||
  1052. reclaim_target != 0 || !list_empty(&log->no_space_stripes);
  1053. /*
  1054. * move proper io_unit to reclaim list. We should not change the order.
  1055. * reclaimable/unreclaimable io_unit can be mixed in the list, we
  1056. * shouldn't reuse space of an unreclaimable io_unit
  1057. */
  1058. while (1) {
  1059. reclaimable = r5l_reclaimable_space(log);
  1060. if (reclaimable >= reclaim_target ||
  1061. (list_empty(&log->running_ios) &&
  1062. list_empty(&log->io_end_ios) &&
  1063. list_empty(&log->flushing_ios) &&
  1064. list_empty(&log->finished_ios)))
  1065. break;
  1066. md_wakeup_thread(log->rdev->mddev->thread);
  1067. wait_event_lock_irq(log->iounit_wait,
  1068. r5l_reclaimable_space(log) > reclaimable,
  1069. log->io_list_lock);
  1070. }
  1071. next_checkpoint = r5c_calculate_new_cp(conf);
  1072. spin_unlock_irq(&log->io_list_lock);
  1073. BUG_ON(reclaimable < 0);
  1074. if (reclaimable == 0 || !write_super)
  1075. return;
  1076. /*
  1077. * write_super will flush cache of each raid disk. We must write super
  1078. * here, because the log area might be reused soon and we don't want to
  1079. * confuse recovery
  1080. */
  1081. r5l_write_super_and_discard_space(log, next_checkpoint);
  1082. mutex_lock(&log->io_mutex);
  1083. log->last_checkpoint = next_checkpoint;
  1084. r5c_update_log_state(log);
  1085. mutex_unlock(&log->io_mutex);
  1086. r5l_run_no_space_stripes(log);
  1087. }
  1088. static void r5l_reclaim_thread(struct md_thread *thread)
  1089. {
  1090. struct mddev *mddev = thread->mddev;
  1091. struct r5conf *conf = mddev->private;
  1092. struct r5l_log *log = conf->log;
  1093. if (!log)
  1094. return;
  1095. r5c_do_reclaim(conf);
  1096. r5l_do_reclaim(log);
  1097. }
  1098. void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
  1099. {
  1100. unsigned long target;
  1101. unsigned long new = (unsigned long)space; /* overflow in theory */
  1102. if (!log)
  1103. return;
  1104. do {
  1105. target = log->reclaim_target;
  1106. if (new < target)
  1107. return;
  1108. } while (cmpxchg(&log->reclaim_target, target, new) != target);
  1109. md_wakeup_thread(log->reclaim_thread);
  1110. }
  1111. void r5l_quiesce(struct r5l_log *log, int state)
  1112. {
  1113. struct mddev *mddev;
  1114. if (!log || state == 2)
  1115. return;
  1116. if (state == 0) {
  1117. /*
  1118. * This is a special case for hotadd. In suspend, the array has
  1119. * no journal. In resume, journal is initialized as well as the
  1120. * reclaim thread.
  1121. */
  1122. if (log->reclaim_thread)
  1123. return;
  1124. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  1125. log->rdev->mddev, "reclaim");
  1126. log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
  1127. } else if (state == 1) {
  1128. /* make sure r5l_write_super_and_discard_space exits */
  1129. mddev = log->rdev->mddev;
  1130. wake_up(&mddev->sb_wait);
  1131. r5l_wake_reclaim(log, MaxSector);
  1132. md_unregister_thread(&log->reclaim_thread);
  1133. r5l_do_reclaim(log);
  1134. }
  1135. }
  1136. bool r5l_log_disk_error(struct r5conf *conf)
  1137. {
  1138. struct r5l_log *log;
  1139. bool ret;
  1140. /* don't allow write if journal disk is missing */
  1141. rcu_read_lock();
  1142. log = rcu_dereference(conf->log);
  1143. if (!log)
  1144. ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  1145. else
  1146. ret = test_bit(Faulty, &log->rdev->flags);
  1147. rcu_read_unlock();
  1148. return ret;
  1149. }
  1150. struct r5l_recovery_ctx {
  1151. struct page *meta_page; /* current meta */
  1152. sector_t meta_total_blocks; /* total size of current meta and data */
  1153. sector_t pos; /* recovery position */
  1154. u64 seq; /* recovery position seq */
  1155. };
  1156. static int r5l_read_meta_block(struct r5l_log *log,
  1157. struct r5l_recovery_ctx *ctx)
  1158. {
  1159. struct page *page = ctx->meta_page;
  1160. struct r5l_meta_block *mb;
  1161. u32 crc, stored_crc;
  1162. if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
  1163. false))
  1164. return -EIO;
  1165. mb = page_address(page);
  1166. stored_crc = le32_to_cpu(mb->checksum);
  1167. mb->checksum = 0;
  1168. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  1169. le64_to_cpu(mb->seq) != ctx->seq ||
  1170. mb->version != R5LOG_VERSION ||
  1171. le64_to_cpu(mb->position) != ctx->pos)
  1172. return -EINVAL;
  1173. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  1174. if (stored_crc != crc)
  1175. return -EINVAL;
  1176. if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
  1177. return -EINVAL;
  1178. ctx->meta_total_blocks = BLOCK_SECTORS;
  1179. return 0;
  1180. }
  1181. static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
  1182. struct r5l_recovery_ctx *ctx,
  1183. sector_t stripe_sect,
  1184. int *offset)
  1185. {
  1186. struct r5conf *conf = log->rdev->mddev->private;
  1187. struct stripe_head *sh;
  1188. struct r5l_payload_data_parity *payload;
  1189. int disk_index;
  1190. sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
  1191. while (1) {
  1192. sector_t log_offset = r5l_ring_add(log, ctx->pos,
  1193. ctx->meta_total_blocks);
  1194. payload = page_address(ctx->meta_page) + *offset;
  1195. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
  1196. raid5_compute_sector(conf,
  1197. le64_to_cpu(payload->location), 0,
  1198. &disk_index, sh);
  1199. sync_page_io(log->rdev, log_offset, PAGE_SIZE,
  1200. sh->dev[disk_index].page, REQ_OP_READ, 0,
  1201. false);
  1202. sh->dev[disk_index].log_checksum =
  1203. le32_to_cpu(payload->checksum[0]);
  1204. set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
  1205. } else {
  1206. disk_index = sh->pd_idx;
  1207. sync_page_io(log->rdev, log_offset, PAGE_SIZE,
  1208. sh->dev[disk_index].page, REQ_OP_READ, 0,
  1209. false);
  1210. sh->dev[disk_index].log_checksum =
  1211. le32_to_cpu(payload->checksum[0]);
  1212. set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
  1213. if (sh->qd_idx >= 0) {
  1214. disk_index = sh->qd_idx;
  1215. sync_page_io(log->rdev,
  1216. r5l_ring_add(log, log_offset, BLOCK_SECTORS),
  1217. PAGE_SIZE, sh->dev[disk_index].page,
  1218. REQ_OP_READ, 0, false);
  1219. sh->dev[disk_index].log_checksum =
  1220. le32_to_cpu(payload->checksum[1]);
  1221. set_bit(R5_Wantwrite,
  1222. &sh->dev[disk_index].flags);
  1223. }
  1224. }
  1225. ctx->meta_total_blocks += le32_to_cpu(payload->size);
  1226. *offset += sizeof(struct r5l_payload_data_parity) +
  1227. sizeof(__le32) *
  1228. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  1229. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
  1230. break;
  1231. }
  1232. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1233. void *addr;
  1234. u32 checksum;
  1235. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  1236. continue;
  1237. addr = kmap_atomic(sh->dev[disk_index].page);
  1238. checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
  1239. kunmap_atomic(addr);
  1240. if (checksum != sh->dev[disk_index].log_checksum)
  1241. goto error;
  1242. }
  1243. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  1244. struct md_rdev *rdev, *rrdev;
  1245. if (!test_and_clear_bit(R5_Wantwrite,
  1246. &sh->dev[disk_index].flags))
  1247. continue;
  1248. /* in case device is broken */
  1249. rcu_read_lock();
  1250. rdev = rcu_dereference(conf->disks[disk_index].rdev);
  1251. if (rdev) {
  1252. atomic_inc(&rdev->nr_pending);
  1253. rcu_read_unlock();
  1254. sync_page_io(rdev, stripe_sect, PAGE_SIZE,
  1255. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1256. false);
  1257. rdev_dec_pending(rdev, rdev->mddev);
  1258. rcu_read_lock();
  1259. }
  1260. rrdev = rcu_dereference(conf->disks[disk_index].replacement);
  1261. if (rrdev) {
  1262. atomic_inc(&rrdev->nr_pending);
  1263. rcu_read_unlock();
  1264. sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
  1265. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  1266. false);
  1267. rdev_dec_pending(rrdev, rrdev->mddev);
  1268. rcu_read_lock();
  1269. }
  1270. rcu_read_unlock();
  1271. }
  1272. raid5_release_stripe(sh);
  1273. return 0;
  1274. error:
  1275. for (disk_index = 0; disk_index < sh->disks; disk_index++)
  1276. sh->dev[disk_index].flags = 0;
  1277. raid5_release_stripe(sh);
  1278. return -EINVAL;
  1279. }
  1280. static int r5l_recovery_flush_one_meta(struct r5l_log *log,
  1281. struct r5l_recovery_ctx *ctx)
  1282. {
  1283. struct r5conf *conf = log->rdev->mddev->private;
  1284. struct r5l_payload_data_parity *payload;
  1285. struct r5l_meta_block *mb;
  1286. int offset;
  1287. sector_t stripe_sector;
  1288. mb = page_address(ctx->meta_page);
  1289. offset = sizeof(struct r5l_meta_block);
  1290. while (offset < le32_to_cpu(mb->meta_size)) {
  1291. int dd;
  1292. payload = (void *)mb + offset;
  1293. stripe_sector = raid5_compute_sector(conf,
  1294. le64_to_cpu(payload->location), 0, &dd, NULL);
  1295. if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
  1296. &offset))
  1297. return -EINVAL;
  1298. }
  1299. return 0;
  1300. }
  1301. /* copy data/parity from log to raid disks */
  1302. static void r5l_recovery_flush_log(struct r5l_log *log,
  1303. struct r5l_recovery_ctx *ctx)
  1304. {
  1305. while (1) {
  1306. if (r5l_read_meta_block(log, ctx))
  1307. return;
  1308. if (r5l_recovery_flush_one_meta(log, ctx))
  1309. return;
  1310. ctx->seq++;
  1311. ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
  1312. }
  1313. }
  1314. static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
  1315. u64 seq)
  1316. {
  1317. struct page *page;
  1318. struct r5l_meta_block *mb;
  1319. u32 crc;
  1320. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1321. if (!page)
  1322. return -ENOMEM;
  1323. mb = page_address(page);
  1324. mb->magic = cpu_to_le32(R5LOG_MAGIC);
  1325. mb->version = R5LOG_VERSION;
  1326. mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
  1327. mb->seq = cpu_to_le64(seq);
  1328. mb->position = cpu_to_le64(pos);
  1329. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  1330. mb->checksum = cpu_to_le32(crc);
  1331. if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
  1332. WRITE_FUA, false)) {
  1333. __free_page(page);
  1334. return -EIO;
  1335. }
  1336. __free_page(page);
  1337. return 0;
  1338. }
  1339. static int r5l_recovery_log(struct r5l_log *log)
  1340. {
  1341. struct r5l_recovery_ctx ctx;
  1342. ctx.pos = log->last_checkpoint;
  1343. ctx.seq = log->last_cp_seq;
  1344. ctx.meta_page = alloc_page(GFP_KERNEL);
  1345. if (!ctx.meta_page)
  1346. return -ENOMEM;
  1347. r5l_recovery_flush_log(log, &ctx);
  1348. __free_page(ctx.meta_page);
  1349. /*
  1350. * we did a recovery. Now ctx.pos points to an invalid meta block. New
  1351. * log will start here. but we can't let superblock point to last valid
  1352. * meta block. The log might looks like:
  1353. * | meta 1| meta 2| meta 3|
  1354. * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
  1355. * superblock points to meta 1, we write a new valid meta 2n. if crash
  1356. * happens again, new recovery will start from meta 1. Since meta 2n is
  1357. * valid now, recovery will think meta 3 is valid, which is wrong.
  1358. * The solution is we create a new meta in meta2 with its seq == meta
  1359. * 1's seq + 10 and let superblock points to meta2. The same recovery will
  1360. * not think meta 3 is a valid meta, because its seq doesn't match
  1361. */
  1362. if (ctx.seq > log->last_cp_seq) {
  1363. int ret;
  1364. ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
  1365. if (ret)
  1366. return ret;
  1367. log->seq = ctx.seq + 11;
  1368. log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
  1369. r5l_write_super(log, ctx.pos);
  1370. log->last_checkpoint = ctx.pos;
  1371. log->next_checkpoint = ctx.pos;
  1372. } else {
  1373. log->log_start = ctx.pos;
  1374. log->seq = ctx.seq;
  1375. }
  1376. return 0;
  1377. }
  1378. static void r5l_write_super(struct r5l_log *log, sector_t cp)
  1379. {
  1380. struct mddev *mddev = log->rdev->mddev;
  1381. log->rdev->journal_tail = cp;
  1382. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1383. }
  1384. static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
  1385. {
  1386. struct r5conf *conf = mddev->private;
  1387. int ret;
  1388. if (!conf->log)
  1389. return 0;
  1390. switch (conf->log->r5c_journal_mode) {
  1391. case R5C_JOURNAL_MODE_WRITE_THROUGH:
  1392. ret = snprintf(
  1393. page, PAGE_SIZE, "[%s] %s\n",
  1394. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
  1395. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
  1396. break;
  1397. case R5C_JOURNAL_MODE_WRITE_BACK:
  1398. ret = snprintf(
  1399. page, PAGE_SIZE, "%s [%s]\n",
  1400. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
  1401. r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
  1402. break;
  1403. default:
  1404. ret = 0;
  1405. }
  1406. return ret;
  1407. }
  1408. static ssize_t r5c_journal_mode_store(struct mddev *mddev,
  1409. const char *page, size_t length)
  1410. {
  1411. struct r5conf *conf = mddev->private;
  1412. struct r5l_log *log = conf->log;
  1413. int val = -1, i;
  1414. int len = length;
  1415. if (!log)
  1416. return -ENODEV;
  1417. if (len && page[len - 1] == '\n')
  1418. len -= 1;
  1419. for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
  1420. if (strlen(r5c_journal_mode_str[i]) == len &&
  1421. strncmp(page, r5c_journal_mode_str[i], len) == 0) {
  1422. val = i;
  1423. break;
  1424. }
  1425. if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
  1426. val > R5C_JOURNAL_MODE_WRITE_BACK)
  1427. return -EINVAL;
  1428. mddev_suspend(mddev);
  1429. conf->log->r5c_journal_mode = val;
  1430. mddev_resume(mddev);
  1431. pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
  1432. mdname(mddev), val, r5c_journal_mode_str[val]);
  1433. return length;
  1434. }
  1435. struct md_sysfs_entry
  1436. r5c_journal_mode = __ATTR(journal_mode, 0644,
  1437. r5c_journal_mode_show, r5c_journal_mode_store);
  1438. /*
  1439. * Try handle write operation in caching phase. This function should only
  1440. * be called in write-back mode.
  1441. *
  1442. * If all outstanding writes can be handled in caching phase, returns 0
  1443. * If writes requires write-out phase, call r5c_make_stripe_write_out()
  1444. * and returns -EAGAIN
  1445. */
  1446. int r5c_try_caching_write(struct r5conf *conf,
  1447. struct stripe_head *sh,
  1448. struct stripe_head_state *s,
  1449. int disks)
  1450. {
  1451. struct r5l_log *log = conf->log;
  1452. int i;
  1453. struct r5dev *dev;
  1454. int to_cache = 0;
  1455. BUG_ON(!r5c_is_writeback(log));
  1456. if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
  1457. /*
  1458. * There are two different scenarios here:
  1459. * 1. The stripe has some data cached, and it is sent to
  1460. * write-out phase for reclaim
  1461. * 2. The stripe is clean, and this is the first write
  1462. *
  1463. * For 1, return -EAGAIN, so we continue with
  1464. * handle_stripe_dirtying().
  1465. *
  1466. * For 2, set STRIPE_R5C_CACHING and continue with caching
  1467. * write.
  1468. */
  1469. /* case 1: anything injournal or anything in written */
  1470. if (s->injournal > 0 || s->written > 0)
  1471. return -EAGAIN;
  1472. /* case 2 */
  1473. set_bit(STRIPE_R5C_CACHING, &sh->state);
  1474. }
  1475. for (i = disks; i--; ) {
  1476. dev = &sh->dev[i];
  1477. /* if non-overwrite, use writing-out phase */
  1478. if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
  1479. !test_bit(R5_InJournal, &dev->flags)) {
  1480. r5c_make_stripe_write_out(sh);
  1481. return -EAGAIN;
  1482. }
  1483. }
  1484. for (i = disks; i--; ) {
  1485. dev = &sh->dev[i];
  1486. if (dev->towrite) {
  1487. set_bit(R5_Wantwrite, &dev->flags);
  1488. set_bit(R5_Wantdrain, &dev->flags);
  1489. set_bit(R5_LOCKED, &dev->flags);
  1490. to_cache++;
  1491. }
  1492. }
  1493. if (to_cache) {
  1494. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1495. /*
  1496. * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
  1497. * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
  1498. * r5c_handle_data_cached()
  1499. */
  1500. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  1501. }
  1502. return 0;
  1503. }
  1504. /*
  1505. * free extra pages (orig_page) we allocated for prexor
  1506. */
  1507. void r5c_release_extra_page(struct stripe_head *sh)
  1508. {
  1509. int i;
  1510. for (i = sh->disks; i--; )
  1511. if (sh->dev[i].page != sh->dev[i].orig_page) {
  1512. struct page *p = sh->dev[i].orig_page;
  1513. sh->dev[i].orig_page = sh->dev[i].page;
  1514. put_page(p);
  1515. }
  1516. }
  1517. /*
  1518. * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
  1519. * stripe is committed to RAID disks.
  1520. */
  1521. void r5c_finish_stripe_write_out(struct r5conf *conf,
  1522. struct stripe_head *sh,
  1523. struct stripe_head_state *s)
  1524. {
  1525. int i;
  1526. int do_wakeup = 0;
  1527. if (!conf->log ||
  1528. !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
  1529. return;
  1530. WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
  1531. clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
  1532. if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  1533. return;
  1534. for (i = sh->disks; i--; ) {
  1535. clear_bit(R5_InJournal, &sh->dev[i].flags);
  1536. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1537. do_wakeup = 1;
  1538. }
  1539. /*
  1540. * analyse_stripe() runs before r5c_finish_stripe_write_out(),
  1541. * We updated R5_InJournal, so we also update s->injournal.
  1542. */
  1543. s->injournal = 0;
  1544. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  1545. if (atomic_dec_and_test(&conf->pending_full_writes))
  1546. md_wakeup_thread(conf->mddev->thread);
  1547. if (do_wakeup)
  1548. wake_up(&conf->wait_for_overlap);
  1549. if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
  1550. return;
  1551. spin_lock_irq(&conf->log->stripe_in_journal_lock);
  1552. list_del_init(&sh->r5c);
  1553. spin_unlock_irq(&conf->log->stripe_in_journal_lock);
  1554. sh->log_start = MaxSector;
  1555. atomic_dec(&conf->log->stripe_in_journal_count);
  1556. }
  1557. int
  1558. r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
  1559. struct stripe_head_state *s)
  1560. {
  1561. struct r5conf *conf = sh->raid_conf;
  1562. int pages = 0;
  1563. int reserve;
  1564. int i;
  1565. int ret = 0;
  1566. BUG_ON(!log);
  1567. for (i = 0; i < sh->disks; i++) {
  1568. void *addr;
  1569. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
  1570. continue;
  1571. addr = kmap_atomic(sh->dev[i].page);
  1572. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  1573. addr, PAGE_SIZE);
  1574. kunmap_atomic(addr);
  1575. pages++;
  1576. }
  1577. WARN_ON(pages == 0);
  1578. /*
  1579. * The stripe must enter state machine again to call endio, so
  1580. * don't delay.
  1581. */
  1582. clear_bit(STRIPE_DELAYED, &sh->state);
  1583. atomic_inc(&sh->count);
  1584. mutex_lock(&log->io_mutex);
  1585. /* meta + data */
  1586. reserve = (1 + pages) << (PAGE_SHIFT - 9);
  1587. if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
  1588. sh->log_start == MaxSector)
  1589. r5l_add_no_space_stripe(log, sh);
  1590. else if (!r5l_has_free_space(log, reserve)) {
  1591. if (sh->log_start == log->last_checkpoint)
  1592. BUG();
  1593. else
  1594. r5l_add_no_space_stripe(log, sh);
  1595. } else {
  1596. ret = r5l_log_stripe(log, sh, pages, 0);
  1597. if (ret) {
  1598. spin_lock_irq(&log->io_list_lock);
  1599. list_add_tail(&sh->log_list, &log->no_mem_stripes);
  1600. spin_unlock_irq(&log->io_list_lock);
  1601. }
  1602. }
  1603. mutex_unlock(&log->io_mutex);
  1604. return 0;
  1605. }
  1606. static int r5l_load_log(struct r5l_log *log)
  1607. {
  1608. struct md_rdev *rdev = log->rdev;
  1609. struct page *page;
  1610. struct r5l_meta_block *mb;
  1611. sector_t cp = log->rdev->journal_tail;
  1612. u32 stored_crc, expected_crc;
  1613. bool create_super = false;
  1614. int ret;
  1615. /* Make sure it's valid */
  1616. if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
  1617. cp = 0;
  1618. page = alloc_page(GFP_KERNEL);
  1619. if (!page)
  1620. return -ENOMEM;
  1621. if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
  1622. ret = -EIO;
  1623. goto ioerr;
  1624. }
  1625. mb = page_address(page);
  1626. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  1627. mb->version != R5LOG_VERSION) {
  1628. create_super = true;
  1629. goto create;
  1630. }
  1631. stored_crc = le32_to_cpu(mb->checksum);
  1632. mb->checksum = 0;
  1633. expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  1634. if (stored_crc != expected_crc) {
  1635. create_super = true;
  1636. goto create;
  1637. }
  1638. if (le64_to_cpu(mb->position) != cp) {
  1639. create_super = true;
  1640. goto create;
  1641. }
  1642. create:
  1643. if (create_super) {
  1644. log->last_cp_seq = prandom_u32();
  1645. cp = 0;
  1646. r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
  1647. /*
  1648. * Make sure super points to correct address. Log might have
  1649. * data very soon. If super hasn't correct log tail address,
  1650. * recovery can't find the log
  1651. */
  1652. r5l_write_super(log, cp);
  1653. } else
  1654. log->last_cp_seq = le64_to_cpu(mb->seq);
  1655. log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
  1656. log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
  1657. if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
  1658. log->max_free_space = RECLAIM_MAX_FREE_SPACE;
  1659. log->last_checkpoint = cp;
  1660. log->next_checkpoint = cp;
  1661. mutex_lock(&log->io_mutex);
  1662. r5c_update_log_state(log);
  1663. mutex_unlock(&log->io_mutex);
  1664. __free_page(page);
  1665. return r5l_recovery_log(log);
  1666. ioerr:
  1667. __free_page(page);
  1668. return ret;
  1669. }
  1670. int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
  1671. {
  1672. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1673. struct r5l_log *log;
  1674. if (PAGE_SIZE != 4096)
  1675. return -EINVAL;
  1676. /*
  1677. * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
  1678. * raid_disks r5l_payload_data_parity.
  1679. *
  1680. * Write journal and cache does not work for very big array
  1681. * (raid_disks > 203)
  1682. */
  1683. if (sizeof(struct r5l_meta_block) +
  1684. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
  1685. conf->raid_disks) > PAGE_SIZE) {
  1686. pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
  1687. mdname(conf->mddev), conf->raid_disks);
  1688. return -EINVAL;
  1689. }
  1690. log = kzalloc(sizeof(*log), GFP_KERNEL);
  1691. if (!log)
  1692. return -ENOMEM;
  1693. log->rdev = rdev;
  1694. log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
  1695. log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
  1696. sizeof(rdev->mddev->uuid));
  1697. mutex_init(&log->io_mutex);
  1698. spin_lock_init(&log->io_list_lock);
  1699. INIT_LIST_HEAD(&log->running_ios);
  1700. INIT_LIST_HEAD(&log->io_end_ios);
  1701. INIT_LIST_HEAD(&log->flushing_ios);
  1702. INIT_LIST_HEAD(&log->finished_ios);
  1703. bio_init(&log->flush_bio);
  1704. log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
  1705. if (!log->io_kc)
  1706. goto io_kc;
  1707. log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
  1708. if (!log->io_pool)
  1709. goto io_pool;
  1710. log->bs = bioset_create(R5L_POOL_SIZE, 0);
  1711. if (!log->bs)
  1712. goto io_bs;
  1713. log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
  1714. if (!log->meta_pool)
  1715. goto out_mempool;
  1716. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  1717. log->rdev->mddev, "reclaim");
  1718. if (!log->reclaim_thread)
  1719. goto reclaim_thread;
  1720. log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
  1721. init_waitqueue_head(&log->iounit_wait);
  1722. INIT_LIST_HEAD(&log->no_mem_stripes);
  1723. INIT_LIST_HEAD(&log->no_space_stripes);
  1724. spin_lock_init(&log->no_space_stripes_lock);
  1725. log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  1726. INIT_LIST_HEAD(&log->stripe_in_journal_list);
  1727. spin_lock_init(&log->stripe_in_journal_lock);
  1728. atomic_set(&log->stripe_in_journal_count, 0);
  1729. if (r5l_load_log(log))
  1730. goto error;
  1731. rcu_assign_pointer(conf->log, log);
  1732. set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  1733. return 0;
  1734. error:
  1735. md_unregister_thread(&log->reclaim_thread);
  1736. reclaim_thread:
  1737. mempool_destroy(log->meta_pool);
  1738. out_mempool:
  1739. bioset_free(log->bs);
  1740. io_bs:
  1741. mempool_destroy(log->io_pool);
  1742. io_pool:
  1743. kmem_cache_destroy(log->io_kc);
  1744. io_kc:
  1745. kfree(log);
  1746. return -EINVAL;
  1747. }
  1748. void r5l_exit_log(struct r5l_log *log)
  1749. {
  1750. md_unregister_thread(&log->reclaim_thread);
  1751. mempool_destroy(log->meta_pool);
  1752. bioset_free(log->bs);
  1753. mempool_destroy(log->io_pool);
  1754. kmem_cache_destroy(log->io_kc);
  1755. kfree(log);
  1756. }