raid5-cache.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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. /*
  24. * metadata/data stored in disk with 4k size unit (a block) regardless
  25. * underneath hardware sector size. only works with PAGE_SIZE == 4096
  26. */
  27. #define BLOCK_SECTORS (8)
  28. /*
  29. * reclaim runs every 1/4 disk size or 10G reclaimable space. This can prevent
  30. * recovery scans a very long log
  31. */
  32. #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
  33. #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
  34. /*
  35. * We only need 2 bios per I/O unit to make progress, but ensure we
  36. * have a few more available to not get too tight.
  37. */
  38. #define R5L_POOL_SIZE 4
  39. struct r5l_log {
  40. struct md_rdev *rdev;
  41. u32 uuid_checksum;
  42. sector_t device_size; /* log device size, round to
  43. * BLOCK_SECTORS */
  44. sector_t max_free_space; /* reclaim run if free space is at
  45. * this size */
  46. sector_t last_checkpoint; /* log tail. where recovery scan
  47. * starts from */
  48. u64 last_cp_seq; /* log tail sequence */
  49. sector_t log_start; /* log head. where new data appends */
  50. u64 seq; /* log head sequence */
  51. sector_t next_checkpoint;
  52. u64 next_cp_seq;
  53. struct mutex io_mutex;
  54. struct r5l_io_unit *current_io; /* current io_unit accepting new data */
  55. spinlock_t io_list_lock;
  56. struct list_head running_ios; /* io_units which are still running,
  57. * and have not yet been completely
  58. * written to the log */
  59. struct list_head io_end_ios; /* io_units which have been completely
  60. * written to the log but not yet written
  61. * to the RAID */
  62. struct list_head flushing_ios; /* io_units which are waiting for log
  63. * cache flush */
  64. struct list_head finished_ios; /* io_units which settle down in log disk */
  65. struct bio flush_bio;
  66. struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
  67. struct kmem_cache *io_kc;
  68. mempool_t *io_pool;
  69. struct bio_set *bs;
  70. mempool_t *meta_pool;
  71. struct md_thread *reclaim_thread;
  72. unsigned long reclaim_target; /* number of space that need to be
  73. * reclaimed. if it's 0, reclaim spaces
  74. * used by io_units which are in
  75. * IO_UNIT_STRIPE_END state (eg, reclaim
  76. * dones't wait for specific io_unit
  77. * switching to IO_UNIT_STRIPE_END
  78. * state) */
  79. wait_queue_head_t iounit_wait;
  80. struct list_head no_space_stripes; /* pending stripes, log has no space */
  81. spinlock_t no_space_stripes_lock;
  82. bool need_cache_flush;
  83. bool in_teardown;
  84. };
  85. /*
  86. * an IO range starts from a meta data block and end at the next meta data
  87. * block. The io unit's the meta data block tracks data/parity followed it. io
  88. * unit is written to log disk with normal write, as we always flush log disk
  89. * first and then start move data to raid disks, there is no requirement to
  90. * write io unit with FLUSH/FUA
  91. */
  92. struct r5l_io_unit {
  93. struct r5l_log *log;
  94. struct page *meta_page; /* store meta block */
  95. int meta_offset; /* current offset in meta_page */
  96. struct bio *current_bio;/* current_bio accepting new data */
  97. atomic_t pending_stripe;/* how many stripes not flushed to raid */
  98. u64 seq; /* seq number of the metablock */
  99. sector_t log_start; /* where the io_unit starts */
  100. sector_t log_end; /* where the io_unit ends */
  101. struct list_head log_sibling; /* log->running_ios */
  102. struct list_head stripe_list; /* stripes added to the io_unit */
  103. int state;
  104. bool need_split_bio;
  105. };
  106. /* r5l_io_unit state */
  107. enum r5l_io_unit_state {
  108. IO_UNIT_RUNNING = 0, /* accepting new IO */
  109. IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
  110. * don't accepting new bio */
  111. IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
  112. IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
  113. };
  114. static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
  115. {
  116. start += inc;
  117. if (start >= log->device_size)
  118. start = start - log->device_size;
  119. return start;
  120. }
  121. static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
  122. sector_t end)
  123. {
  124. if (end >= start)
  125. return end - start;
  126. else
  127. return end + log->device_size - start;
  128. }
  129. static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
  130. {
  131. sector_t used_size;
  132. used_size = r5l_ring_distance(log, log->last_checkpoint,
  133. log->log_start);
  134. return log->device_size > used_size + size;
  135. }
  136. static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
  137. enum r5l_io_unit_state state)
  138. {
  139. if (WARN_ON(io->state >= state))
  140. return;
  141. io->state = state;
  142. }
  143. static void r5l_io_run_stripes(struct r5l_io_unit *io)
  144. {
  145. struct stripe_head *sh, *next;
  146. list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
  147. list_del_init(&sh->log_list);
  148. set_bit(STRIPE_HANDLE, &sh->state);
  149. raid5_release_stripe(sh);
  150. }
  151. }
  152. static void r5l_log_run_stripes(struct r5l_log *log)
  153. {
  154. struct r5l_io_unit *io, *next;
  155. assert_spin_locked(&log->io_list_lock);
  156. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  157. /* don't change list order */
  158. if (io->state < IO_UNIT_IO_END)
  159. break;
  160. list_move_tail(&io->log_sibling, &log->finished_ios);
  161. r5l_io_run_stripes(io);
  162. }
  163. }
  164. static void r5l_move_to_end_ios(struct r5l_log *log)
  165. {
  166. struct r5l_io_unit *io, *next;
  167. assert_spin_locked(&log->io_list_lock);
  168. list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
  169. /* don't change list order */
  170. if (io->state < IO_UNIT_IO_END)
  171. break;
  172. list_move_tail(&io->log_sibling, &log->io_end_ios);
  173. }
  174. }
  175. static void r5l_log_endio(struct bio *bio)
  176. {
  177. struct r5l_io_unit *io = bio->bi_private;
  178. struct r5l_log *log = io->log;
  179. unsigned long flags;
  180. if (bio->bi_error)
  181. md_error(log->rdev->mddev, log->rdev);
  182. bio_put(bio);
  183. mempool_free(io->meta_page, log->meta_pool);
  184. spin_lock_irqsave(&log->io_list_lock, flags);
  185. __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
  186. if (log->need_cache_flush)
  187. r5l_move_to_end_ios(log);
  188. else
  189. r5l_log_run_stripes(log);
  190. spin_unlock_irqrestore(&log->io_list_lock, flags);
  191. if (log->need_cache_flush)
  192. md_wakeup_thread(log->rdev->mddev->thread);
  193. }
  194. static void r5l_submit_current_io(struct r5l_log *log)
  195. {
  196. struct r5l_io_unit *io = log->current_io;
  197. struct r5l_meta_block *block;
  198. unsigned long flags;
  199. u32 crc;
  200. if (!io)
  201. return;
  202. block = page_address(io->meta_page);
  203. block->meta_size = cpu_to_le32(io->meta_offset);
  204. crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
  205. block->checksum = cpu_to_le32(crc);
  206. log->current_io = NULL;
  207. spin_lock_irqsave(&log->io_list_lock, flags);
  208. __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
  209. spin_unlock_irqrestore(&log->io_list_lock, flags);
  210. submit_bio(io->current_bio);
  211. }
  212. static struct bio *r5l_bio_alloc(struct r5l_log *log)
  213. {
  214. struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
  215. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  216. bio->bi_bdev = log->rdev->bdev;
  217. bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
  218. return bio;
  219. }
  220. static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
  221. {
  222. log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
  223. /*
  224. * If we filled up the log device start from the beginning again,
  225. * which will require a new bio.
  226. *
  227. * Note: for this to work properly the log size needs to me a multiple
  228. * of BLOCK_SECTORS.
  229. */
  230. if (log->log_start == 0)
  231. io->need_split_bio = true;
  232. io->log_end = log->log_start;
  233. }
  234. static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
  235. {
  236. struct r5l_io_unit *io;
  237. struct r5l_meta_block *block;
  238. io = mempool_alloc(log->io_pool, GFP_ATOMIC);
  239. if (!io)
  240. return NULL;
  241. memset(io, 0, sizeof(*io));
  242. io->log = log;
  243. INIT_LIST_HEAD(&io->log_sibling);
  244. INIT_LIST_HEAD(&io->stripe_list);
  245. io->state = IO_UNIT_RUNNING;
  246. io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
  247. block = page_address(io->meta_page);
  248. clear_page(block);
  249. block->magic = cpu_to_le32(R5LOG_MAGIC);
  250. block->version = R5LOG_VERSION;
  251. block->seq = cpu_to_le64(log->seq);
  252. block->position = cpu_to_le64(log->log_start);
  253. io->log_start = log->log_start;
  254. io->meta_offset = sizeof(struct r5l_meta_block);
  255. io->seq = log->seq++;
  256. io->current_bio = r5l_bio_alloc(log);
  257. io->current_bio->bi_end_io = r5l_log_endio;
  258. io->current_bio->bi_private = io;
  259. bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
  260. r5_reserve_log_entry(log, io);
  261. spin_lock_irq(&log->io_list_lock);
  262. list_add_tail(&io->log_sibling, &log->running_ios);
  263. spin_unlock_irq(&log->io_list_lock);
  264. return io;
  265. }
  266. static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
  267. {
  268. if (log->current_io &&
  269. log->current_io->meta_offset + payload_size > PAGE_SIZE)
  270. r5l_submit_current_io(log);
  271. if (!log->current_io) {
  272. log->current_io = r5l_new_meta(log);
  273. if (!log->current_io)
  274. return -ENOMEM;
  275. }
  276. return 0;
  277. }
  278. static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
  279. sector_t location,
  280. u32 checksum1, u32 checksum2,
  281. bool checksum2_valid)
  282. {
  283. struct r5l_io_unit *io = log->current_io;
  284. struct r5l_payload_data_parity *payload;
  285. payload = page_address(io->meta_page) + io->meta_offset;
  286. payload->header.type = cpu_to_le16(type);
  287. payload->header.flags = cpu_to_le16(0);
  288. payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
  289. (PAGE_SHIFT - 9));
  290. payload->location = cpu_to_le64(location);
  291. payload->checksum[0] = cpu_to_le32(checksum1);
  292. if (checksum2_valid)
  293. payload->checksum[1] = cpu_to_le32(checksum2);
  294. io->meta_offset += sizeof(struct r5l_payload_data_parity) +
  295. sizeof(__le32) * (1 + !!checksum2_valid);
  296. }
  297. static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
  298. {
  299. struct r5l_io_unit *io = log->current_io;
  300. if (io->need_split_bio) {
  301. struct bio *prev = io->current_bio;
  302. io->current_bio = r5l_bio_alloc(log);
  303. bio_chain(io->current_bio, prev);
  304. submit_bio(prev);
  305. }
  306. if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
  307. BUG();
  308. r5_reserve_log_entry(log, io);
  309. }
  310. static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
  311. int data_pages, int parity_pages)
  312. {
  313. int i;
  314. int meta_size;
  315. int ret;
  316. struct r5l_io_unit *io;
  317. meta_size =
  318. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
  319. * data_pages) +
  320. sizeof(struct r5l_payload_data_parity) +
  321. sizeof(__le32) * parity_pages;
  322. ret = r5l_get_meta(log, meta_size);
  323. if (ret)
  324. return ret;
  325. io = log->current_io;
  326. for (i = 0; i < sh->disks; i++) {
  327. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
  328. continue;
  329. if (i == sh->pd_idx || i == sh->qd_idx)
  330. continue;
  331. r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
  332. raid5_compute_blocknr(sh, i, 0),
  333. sh->dev[i].log_checksum, 0, false);
  334. r5l_append_payload_page(log, sh->dev[i].page);
  335. }
  336. if (sh->qd_idx >= 0) {
  337. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  338. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  339. sh->dev[sh->qd_idx].log_checksum, true);
  340. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  341. r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
  342. } else {
  343. r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
  344. sh->sector, sh->dev[sh->pd_idx].log_checksum,
  345. 0, false);
  346. r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
  347. }
  348. list_add_tail(&sh->log_list, &io->stripe_list);
  349. atomic_inc(&io->pending_stripe);
  350. sh->log_io = io;
  351. return 0;
  352. }
  353. static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
  354. /*
  355. * running in raid5d, where reclaim could wait for raid5d too (when it flushes
  356. * data from log to raid disks), so we shouldn't wait for reclaim here
  357. */
  358. int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
  359. {
  360. int write_disks = 0;
  361. int data_pages, parity_pages;
  362. int meta_size;
  363. int reserve;
  364. int i;
  365. int ret = 0;
  366. if (!log)
  367. return -EAGAIN;
  368. /* Don't support stripe batch */
  369. if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
  370. test_bit(STRIPE_SYNCING, &sh->state)) {
  371. /* the stripe is written to log, we start writing it to raid */
  372. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  373. return -EAGAIN;
  374. }
  375. for (i = 0; i < sh->disks; i++) {
  376. void *addr;
  377. if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
  378. continue;
  379. write_disks++;
  380. /* checksum is already calculated in last run */
  381. if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
  382. continue;
  383. addr = kmap_atomic(sh->dev[i].page);
  384. sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
  385. addr, PAGE_SIZE);
  386. kunmap_atomic(addr);
  387. }
  388. parity_pages = 1 + !!(sh->qd_idx >= 0);
  389. data_pages = write_disks - parity_pages;
  390. meta_size =
  391. ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
  392. * data_pages) +
  393. sizeof(struct r5l_payload_data_parity) +
  394. sizeof(__le32) * parity_pages;
  395. /* Doesn't work with very big raid array */
  396. if (meta_size + sizeof(struct r5l_meta_block) > PAGE_SIZE)
  397. return -EINVAL;
  398. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  399. /*
  400. * The stripe must enter state machine again to finish the write, so
  401. * don't delay.
  402. */
  403. clear_bit(STRIPE_DELAYED, &sh->state);
  404. atomic_inc(&sh->count);
  405. mutex_lock(&log->io_mutex);
  406. /* meta + data */
  407. reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
  408. if (!r5l_has_free_space(log, reserve)) {
  409. spin_lock(&log->no_space_stripes_lock);
  410. list_add_tail(&sh->log_list, &log->no_space_stripes);
  411. spin_unlock(&log->no_space_stripes_lock);
  412. r5l_wake_reclaim(log, reserve);
  413. } else {
  414. ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
  415. if (ret) {
  416. spin_lock_irq(&log->io_list_lock);
  417. list_add_tail(&sh->log_list, &log->no_mem_stripes);
  418. spin_unlock_irq(&log->io_list_lock);
  419. }
  420. }
  421. mutex_unlock(&log->io_mutex);
  422. return 0;
  423. }
  424. void r5l_write_stripe_run(struct r5l_log *log)
  425. {
  426. if (!log)
  427. return;
  428. mutex_lock(&log->io_mutex);
  429. r5l_submit_current_io(log);
  430. mutex_unlock(&log->io_mutex);
  431. }
  432. int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
  433. {
  434. if (!log)
  435. return -ENODEV;
  436. /*
  437. * we flush log disk cache first, then write stripe data to raid disks.
  438. * So if bio is finished, the log disk cache is flushed already. The
  439. * recovery guarantees we can recovery the bio from log disk, so we
  440. * don't need to flush again
  441. */
  442. if (bio->bi_iter.bi_size == 0) {
  443. bio_endio(bio);
  444. return 0;
  445. }
  446. bio->bi_opf &= ~REQ_PREFLUSH;
  447. return -EAGAIN;
  448. }
  449. /* This will run after log space is reclaimed */
  450. static void r5l_run_no_space_stripes(struct r5l_log *log)
  451. {
  452. struct stripe_head *sh;
  453. spin_lock(&log->no_space_stripes_lock);
  454. while (!list_empty(&log->no_space_stripes)) {
  455. sh = list_first_entry(&log->no_space_stripes,
  456. struct stripe_head, log_list);
  457. list_del_init(&sh->log_list);
  458. set_bit(STRIPE_HANDLE, &sh->state);
  459. raid5_release_stripe(sh);
  460. }
  461. spin_unlock(&log->no_space_stripes_lock);
  462. }
  463. static sector_t r5l_reclaimable_space(struct r5l_log *log)
  464. {
  465. return r5l_ring_distance(log, log->last_checkpoint,
  466. log->next_checkpoint);
  467. }
  468. static void r5l_run_no_mem_stripe(struct r5l_log *log)
  469. {
  470. struct stripe_head *sh;
  471. assert_spin_locked(&log->io_list_lock);
  472. if (!list_empty(&log->no_mem_stripes)) {
  473. sh = list_first_entry(&log->no_mem_stripes,
  474. struct stripe_head, log_list);
  475. list_del_init(&sh->log_list);
  476. set_bit(STRIPE_HANDLE, &sh->state);
  477. raid5_release_stripe(sh);
  478. }
  479. }
  480. static bool r5l_complete_finished_ios(struct r5l_log *log)
  481. {
  482. struct r5l_io_unit *io, *next;
  483. bool found = false;
  484. assert_spin_locked(&log->io_list_lock);
  485. list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
  486. /* don't change list order */
  487. if (io->state < IO_UNIT_STRIPE_END)
  488. break;
  489. log->next_checkpoint = io->log_start;
  490. log->next_cp_seq = io->seq;
  491. list_del(&io->log_sibling);
  492. mempool_free(io, log->io_pool);
  493. r5l_run_no_mem_stripe(log);
  494. found = true;
  495. }
  496. return found;
  497. }
  498. static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
  499. {
  500. struct r5l_log *log = io->log;
  501. unsigned long flags;
  502. spin_lock_irqsave(&log->io_list_lock, flags);
  503. __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
  504. if (!r5l_complete_finished_ios(log)) {
  505. spin_unlock_irqrestore(&log->io_list_lock, flags);
  506. return;
  507. }
  508. if (r5l_reclaimable_space(log) > log->max_free_space)
  509. r5l_wake_reclaim(log, 0);
  510. spin_unlock_irqrestore(&log->io_list_lock, flags);
  511. wake_up(&log->iounit_wait);
  512. }
  513. void r5l_stripe_write_finished(struct stripe_head *sh)
  514. {
  515. struct r5l_io_unit *io;
  516. io = sh->log_io;
  517. sh->log_io = NULL;
  518. if (io && atomic_dec_and_test(&io->pending_stripe))
  519. __r5l_stripe_write_finished(io);
  520. }
  521. static void r5l_log_flush_endio(struct bio *bio)
  522. {
  523. struct r5l_log *log = container_of(bio, struct r5l_log,
  524. flush_bio);
  525. unsigned long flags;
  526. struct r5l_io_unit *io;
  527. if (bio->bi_error)
  528. md_error(log->rdev->mddev, log->rdev);
  529. spin_lock_irqsave(&log->io_list_lock, flags);
  530. list_for_each_entry(io, &log->flushing_ios, log_sibling)
  531. r5l_io_run_stripes(io);
  532. list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
  533. spin_unlock_irqrestore(&log->io_list_lock, flags);
  534. }
  535. /*
  536. * Starting dispatch IO to raid.
  537. * io_unit(meta) consists of a log. There is one situation we want to avoid. A
  538. * broken meta in the middle of a log causes recovery can't find meta at the
  539. * head of log. If operations require meta at the head persistent in log, we
  540. * must make sure meta before it persistent in log too. A case is:
  541. *
  542. * stripe data/parity is in log, we start write stripe to raid disks. stripe
  543. * data/parity must be persistent in log before we do the write to raid disks.
  544. *
  545. * The solution is we restrictly maintain io_unit list order. In this case, we
  546. * only write stripes of an io_unit to raid disks till the io_unit is the first
  547. * one whose data/parity is in log.
  548. */
  549. void r5l_flush_stripe_to_raid(struct r5l_log *log)
  550. {
  551. bool do_flush;
  552. if (!log || !log->need_cache_flush)
  553. return;
  554. spin_lock_irq(&log->io_list_lock);
  555. /* flush bio is running */
  556. if (!list_empty(&log->flushing_ios)) {
  557. spin_unlock_irq(&log->io_list_lock);
  558. return;
  559. }
  560. list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
  561. do_flush = !list_empty(&log->flushing_ios);
  562. spin_unlock_irq(&log->io_list_lock);
  563. if (!do_flush)
  564. return;
  565. bio_reset(&log->flush_bio);
  566. log->flush_bio.bi_bdev = log->rdev->bdev;
  567. log->flush_bio.bi_end_io = r5l_log_flush_endio;
  568. bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
  569. submit_bio(&log->flush_bio);
  570. }
  571. static void r5l_write_super(struct r5l_log *log, sector_t cp);
  572. static void r5l_write_super_and_discard_space(struct r5l_log *log,
  573. sector_t end)
  574. {
  575. struct block_device *bdev = log->rdev->bdev;
  576. struct mddev *mddev;
  577. r5l_write_super(log, end);
  578. if (!blk_queue_discard(bdev_get_queue(bdev)))
  579. return;
  580. mddev = log->rdev->mddev;
  581. /*
  582. * This is to avoid a deadlock. r5l_quiesce holds reconfig_mutex and
  583. * wait for this thread to finish. This thread waits for
  584. * MD_CHANGE_PENDING clear, which is supposed to be done in
  585. * md_check_recovery(). md_check_recovery() tries to get
  586. * reconfig_mutex. Since r5l_quiesce already holds the mutex,
  587. * md_check_recovery() fails, so the PENDING never get cleared. The
  588. * in_teardown check workaround this issue.
  589. */
  590. if (!log->in_teardown) {
  591. set_mask_bits(&mddev->flags, 0,
  592. BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
  593. md_wakeup_thread(mddev->thread);
  594. wait_event(mddev->sb_wait,
  595. !test_bit(MD_CHANGE_PENDING, &mddev->flags) ||
  596. log->in_teardown);
  597. /*
  598. * r5l_quiesce could run after in_teardown check and hold
  599. * mutex first. Superblock might get updated twice.
  600. */
  601. if (log->in_teardown)
  602. md_update_sb(mddev, 1);
  603. } else {
  604. WARN_ON(!mddev_is_locked(mddev));
  605. md_update_sb(mddev, 1);
  606. }
  607. /* discard IO error really doesn't matter, ignore it */
  608. if (log->last_checkpoint < end) {
  609. blkdev_issue_discard(bdev,
  610. log->last_checkpoint + log->rdev->data_offset,
  611. end - log->last_checkpoint, GFP_NOIO, 0);
  612. } else {
  613. blkdev_issue_discard(bdev,
  614. log->last_checkpoint + log->rdev->data_offset,
  615. log->device_size - log->last_checkpoint,
  616. GFP_NOIO, 0);
  617. blkdev_issue_discard(bdev, log->rdev->data_offset, end,
  618. GFP_NOIO, 0);
  619. }
  620. }
  621. static void r5l_do_reclaim(struct r5l_log *log)
  622. {
  623. sector_t reclaim_target = xchg(&log->reclaim_target, 0);
  624. sector_t reclaimable;
  625. sector_t next_checkpoint;
  626. u64 next_cp_seq;
  627. spin_lock_irq(&log->io_list_lock);
  628. /*
  629. * move proper io_unit to reclaim list. We should not change the order.
  630. * reclaimable/unreclaimable io_unit can be mixed in the list, we
  631. * shouldn't reuse space of an unreclaimable io_unit
  632. */
  633. while (1) {
  634. reclaimable = r5l_reclaimable_space(log);
  635. if (reclaimable >= reclaim_target ||
  636. (list_empty(&log->running_ios) &&
  637. list_empty(&log->io_end_ios) &&
  638. list_empty(&log->flushing_ios) &&
  639. list_empty(&log->finished_ios)))
  640. break;
  641. md_wakeup_thread(log->rdev->mddev->thread);
  642. wait_event_lock_irq(log->iounit_wait,
  643. r5l_reclaimable_space(log) > reclaimable,
  644. log->io_list_lock);
  645. }
  646. next_checkpoint = log->next_checkpoint;
  647. next_cp_seq = log->next_cp_seq;
  648. spin_unlock_irq(&log->io_list_lock);
  649. BUG_ON(reclaimable < 0);
  650. if (reclaimable == 0)
  651. return;
  652. /*
  653. * write_super will flush cache of each raid disk. We must write super
  654. * here, because the log area might be reused soon and we don't want to
  655. * confuse recovery
  656. */
  657. r5l_write_super_and_discard_space(log, next_checkpoint);
  658. mutex_lock(&log->io_mutex);
  659. log->last_checkpoint = next_checkpoint;
  660. log->last_cp_seq = next_cp_seq;
  661. mutex_unlock(&log->io_mutex);
  662. r5l_run_no_space_stripes(log);
  663. }
  664. static void r5l_reclaim_thread(struct md_thread *thread)
  665. {
  666. struct mddev *mddev = thread->mddev;
  667. struct r5conf *conf = mddev->private;
  668. struct r5l_log *log = conf->log;
  669. if (!log)
  670. return;
  671. r5l_do_reclaim(log);
  672. }
  673. static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
  674. {
  675. unsigned long target;
  676. unsigned long new = (unsigned long)space; /* overflow in theory */
  677. do {
  678. target = log->reclaim_target;
  679. if (new < target)
  680. return;
  681. } while (cmpxchg(&log->reclaim_target, target, new) != target);
  682. md_wakeup_thread(log->reclaim_thread);
  683. }
  684. void r5l_quiesce(struct r5l_log *log, int state)
  685. {
  686. struct mddev *mddev;
  687. if (!log || state == 2)
  688. return;
  689. if (state == 0) {
  690. log->in_teardown = 0;
  691. /*
  692. * This is a special case for hotadd. In suspend, the array has
  693. * no journal. In resume, journal is initialized as well as the
  694. * reclaim thread.
  695. */
  696. if (log->reclaim_thread)
  697. return;
  698. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  699. log->rdev->mddev, "reclaim");
  700. } else if (state == 1) {
  701. /*
  702. * at this point all stripes are finished, so io_unit is at
  703. * least in STRIPE_END state
  704. */
  705. log->in_teardown = 1;
  706. /* make sure r5l_write_super_and_discard_space exits */
  707. mddev = log->rdev->mddev;
  708. wake_up(&mddev->sb_wait);
  709. r5l_wake_reclaim(log, -1L);
  710. md_unregister_thread(&log->reclaim_thread);
  711. r5l_do_reclaim(log);
  712. }
  713. }
  714. bool r5l_log_disk_error(struct r5conf *conf)
  715. {
  716. struct r5l_log *log;
  717. bool ret;
  718. /* don't allow write if journal disk is missing */
  719. rcu_read_lock();
  720. log = rcu_dereference(conf->log);
  721. if (!log)
  722. ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  723. else
  724. ret = test_bit(Faulty, &log->rdev->flags);
  725. rcu_read_unlock();
  726. return ret;
  727. }
  728. struct r5l_recovery_ctx {
  729. struct page *meta_page; /* current meta */
  730. sector_t meta_total_blocks; /* total size of current meta and data */
  731. sector_t pos; /* recovery position */
  732. u64 seq; /* recovery position seq */
  733. };
  734. static int r5l_read_meta_block(struct r5l_log *log,
  735. struct r5l_recovery_ctx *ctx)
  736. {
  737. struct page *page = ctx->meta_page;
  738. struct r5l_meta_block *mb;
  739. u32 crc, stored_crc;
  740. if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
  741. false))
  742. return -EIO;
  743. mb = page_address(page);
  744. stored_crc = le32_to_cpu(mb->checksum);
  745. mb->checksum = 0;
  746. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  747. le64_to_cpu(mb->seq) != ctx->seq ||
  748. mb->version != R5LOG_VERSION ||
  749. le64_to_cpu(mb->position) != ctx->pos)
  750. return -EINVAL;
  751. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  752. if (stored_crc != crc)
  753. return -EINVAL;
  754. if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
  755. return -EINVAL;
  756. ctx->meta_total_blocks = BLOCK_SECTORS;
  757. return 0;
  758. }
  759. static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
  760. struct r5l_recovery_ctx *ctx,
  761. sector_t stripe_sect,
  762. int *offset, sector_t *log_offset)
  763. {
  764. struct r5conf *conf = log->rdev->mddev->private;
  765. struct stripe_head *sh;
  766. struct r5l_payload_data_parity *payload;
  767. int disk_index;
  768. sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
  769. while (1) {
  770. payload = page_address(ctx->meta_page) + *offset;
  771. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
  772. raid5_compute_sector(conf,
  773. le64_to_cpu(payload->location), 0,
  774. &disk_index, sh);
  775. sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
  776. sh->dev[disk_index].page, REQ_OP_READ, 0,
  777. false);
  778. sh->dev[disk_index].log_checksum =
  779. le32_to_cpu(payload->checksum[0]);
  780. set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
  781. ctx->meta_total_blocks += BLOCK_SECTORS;
  782. } else {
  783. disk_index = sh->pd_idx;
  784. sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
  785. sh->dev[disk_index].page, REQ_OP_READ, 0,
  786. false);
  787. sh->dev[disk_index].log_checksum =
  788. le32_to_cpu(payload->checksum[0]);
  789. set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
  790. if (sh->qd_idx >= 0) {
  791. disk_index = sh->qd_idx;
  792. sync_page_io(log->rdev,
  793. r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
  794. PAGE_SIZE, sh->dev[disk_index].page,
  795. REQ_OP_READ, 0, false);
  796. sh->dev[disk_index].log_checksum =
  797. le32_to_cpu(payload->checksum[1]);
  798. set_bit(R5_Wantwrite,
  799. &sh->dev[disk_index].flags);
  800. }
  801. ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
  802. }
  803. *log_offset = r5l_ring_add(log, *log_offset,
  804. le32_to_cpu(payload->size));
  805. *offset += sizeof(struct r5l_payload_data_parity) +
  806. sizeof(__le32) *
  807. (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
  808. if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
  809. break;
  810. }
  811. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  812. void *addr;
  813. u32 checksum;
  814. if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
  815. continue;
  816. addr = kmap_atomic(sh->dev[disk_index].page);
  817. checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
  818. kunmap_atomic(addr);
  819. if (checksum != sh->dev[disk_index].log_checksum)
  820. goto error;
  821. }
  822. for (disk_index = 0; disk_index < sh->disks; disk_index++) {
  823. struct md_rdev *rdev, *rrdev;
  824. if (!test_and_clear_bit(R5_Wantwrite,
  825. &sh->dev[disk_index].flags))
  826. continue;
  827. /* in case device is broken */
  828. rdev = rcu_dereference(conf->disks[disk_index].rdev);
  829. if (rdev)
  830. sync_page_io(rdev, stripe_sect, PAGE_SIZE,
  831. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  832. false);
  833. rrdev = rcu_dereference(conf->disks[disk_index].replacement);
  834. if (rrdev)
  835. sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
  836. sh->dev[disk_index].page, REQ_OP_WRITE, 0,
  837. false);
  838. }
  839. raid5_release_stripe(sh);
  840. return 0;
  841. error:
  842. for (disk_index = 0; disk_index < sh->disks; disk_index++)
  843. sh->dev[disk_index].flags = 0;
  844. raid5_release_stripe(sh);
  845. return -EINVAL;
  846. }
  847. static int r5l_recovery_flush_one_meta(struct r5l_log *log,
  848. struct r5l_recovery_ctx *ctx)
  849. {
  850. struct r5conf *conf = log->rdev->mddev->private;
  851. struct r5l_payload_data_parity *payload;
  852. struct r5l_meta_block *mb;
  853. int offset;
  854. sector_t log_offset;
  855. sector_t stripe_sector;
  856. mb = page_address(ctx->meta_page);
  857. offset = sizeof(struct r5l_meta_block);
  858. log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
  859. while (offset < le32_to_cpu(mb->meta_size)) {
  860. int dd;
  861. payload = (void *)mb + offset;
  862. stripe_sector = raid5_compute_sector(conf,
  863. le64_to_cpu(payload->location), 0, &dd, NULL);
  864. if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
  865. &offset, &log_offset))
  866. return -EINVAL;
  867. }
  868. return 0;
  869. }
  870. /* copy data/parity from log to raid disks */
  871. static void r5l_recovery_flush_log(struct r5l_log *log,
  872. struct r5l_recovery_ctx *ctx)
  873. {
  874. while (1) {
  875. if (r5l_read_meta_block(log, ctx))
  876. return;
  877. if (r5l_recovery_flush_one_meta(log, ctx))
  878. return;
  879. ctx->seq++;
  880. ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
  881. }
  882. }
  883. static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
  884. u64 seq)
  885. {
  886. struct page *page;
  887. struct r5l_meta_block *mb;
  888. u32 crc;
  889. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  890. if (!page)
  891. return -ENOMEM;
  892. mb = page_address(page);
  893. mb->magic = cpu_to_le32(R5LOG_MAGIC);
  894. mb->version = R5LOG_VERSION;
  895. mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
  896. mb->seq = cpu_to_le64(seq);
  897. mb->position = cpu_to_le64(pos);
  898. crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  899. mb->checksum = cpu_to_le32(crc);
  900. if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
  901. WRITE_FUA, false)) {
  902. __free_page(page);
  903. return -EIO;
  904. }
  905. __free_page(page);
  906. return 0;
  907. }
  908. static int r5l_recovery_log(struct r5l_log *log)
  909. {
  910. struct r5l_recovery_ctx ctx;
  911. ctx.pos = log->last_checkpoint;
  912. ctx.seq = log->last_cp_seq;
  913. ctx.meta_page = alloc_page(GFP_KERNEL);
  914. if (!ctx.meta_page)
  915. return -ENOMEM;
  916. r5l_recovery_flush_log(log, &ctx);
  917. __free_page(ctx.meta_page);
  918. /*
  919. * we did a recovery. Now ctx.pos points to an invalid meta block. New
  920. * log will start here. but we can't let superblock point to last valid
  921. * meta block. The log might looks like:
  922. * | meta 1| meta 2| meta 3|
  923. * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
  924. * superblock points to meta 1, we write a new valid meta 2n. if crash
  925. * happens again, new recovery will start from meta 1. Since meta 2n is
  926. * valid now, recovery will think meta 3 is valid, which is wrong.
  927. * The solution is we create a new meta in meta2 with its seq == meta
  928. * 1's seq + 10 and let superblock points to meta2. The same recovery will
  929. * not think meta 3 is a valid meta, because its seq doesn't match
  930. */
  931. if (ctx.seq > log->last_cp_seq + 1) {
  932. int ret;
  933. ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
  934. if (ret)
  935. return ret;
  936. log->seq = ctx.seq + 11;
  937. log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
  938. r5l_write_super(log, ctx.pos);
  939. } else {
  940. log->log_start = ctx.pos;
  941. log->seq = ctx.seq;
  942. }
  943. return 0;
  944. }
  945. static void r5l_write_super(struct r5l_log *log, sector_t cp)
  946. {
  947. struct mddev *mddev = log->rdev->mddev;
  948. log->rdev->journal_tail = cp;
  949. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  950. }
  951. static int r5l_load_log(struct r5l_log *log)
  952. {
  953. struct md_rdev *rdev = log->rdev;
  954. struct page *page;
  955. struct r5l_meta_block *mb;
  956. sector_t cp = log->rdev->journal_tail;
  957. u32 stored_crc, expected_crc;
  958. bool create_super = false;
  959. int ret;
  960. /* Make sure it's valid */
  961. if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
  962. cp = 0;
  963. page = alloc_page(GFP_KERNEL);
  964. if (!page)
  965. return -ENOMEM;
  966. if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
  967. ret = -EIO;
  968. goto ioerr;
  969. }
  970. mb = page_address(page);
  971. if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
  972. mb->version != R5LOG_VERSION) {
  973. create_super = true;
  974. goto create;
  975. }
  976. stored_crc = le32_to_cpu(mb->checksum);
  977. mb->checksum = 0;
  978. expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
  979. if (stored_crc != expected_crc) {
  980. create_super = true;
  981. goto create;
  982. }
  983. if (le64_to_cpu(mb->position) != cp) {
  984. create_super = true;
  985. goto create;
  986. }
  987. create:
  988. if (create_super) {
  989. log->last_cp_seq = prandom_u32();
  990. cp = 0;
  991. /*
  992. * Make sure super points to correct address. Log might have
  993. * data very soon. If super hasn't correct log tail address,
  994. * recovery can't find the log
  995. */
  996. r5l_write_super(log, cp);
  997. } else
  998. log->last_cp_seq = le64_to_cpu(mb->seq);
  999. log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
  1000. log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
  1001. if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
  1002. log->max_free_space = RECLAIM_MAX_FREE_SPACE;
  1003. log->last_checkpoint = cp;
  1004. __free_page(page);
  1005. return r5l_recovery_log(log);
  1006. ioerr:
  1007. __free_page(page);
  1008. return ret;
  1009. }
  1010. int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
  1011. {
  1012. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1013. struct r5l_log *log;
  1014. if (PAGE_SIZE != 4096)
  1015. return -EINVAL;
  1016. log = kzalloc(sizeof(*log), GFP_KERNEL);
  1017. if (!log)
  1018. return -ENOMEM;
  1019. log->rdev = rdev;
  1020. log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
  1021. log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
  1022. sizeof(rdev->mddev->uuid));
  1023. mutex_init(&log->io_mutex);
  1024. spin_lock_init(&log->io_list_lock);
  1025. INIT_LIST_HEAD(&log->running_ios);
  1026. INIT_LIST_HEAD(&log->io_end_ios);
  1027. INIT_LIST_HEAD(&log->flushing_ios);
  1028. INIT_LIST_HEAD(&log->finished_ios);
  1029. bio_init(&log->flush_bio);
  1030. log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
  1031. if (!log->io_kc)
  1032. goto io_kc;
  1033. log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
  1034. if (!log->io_pool)
  1035. goto io_pool;
  1036. log->bs = bioset_create(R5L_POOL_SIZE, 0);
  1037. if (!log->bs)
  1038. goto io_bs;
  1039. log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
  1040. if (!log->meta_pool)
  1041. goto out_mempool;
  1042. log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
  1043. log->rdev->mddev, "reclaim");
  1044. if (!log->reclaim_thread)
  1045. goto reclaim_thread;
  1046. init_waitqueue_head(&log->iounit_wait);
  1047. INIT_LIST_HEAD(&log->no_mem_stripes);
  1048. INIT_LIST_HEAD(&log->no_space_stripes);
  1049. spin_lock_init(&log->no_space_stripes_lock);
  1050. if (r5l_load_log(log))
  1051. goto error;
  1052. rcu_assign_pointer(conf->log, log);
  1053. set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
  1054. return 0;
  1055. error:
  1056. md_unregister_thread(&log->reclaim_thread);
  1057. reclaim_thread:
  1058. mempool_destroy(log->meta_pool);
  1059. out_mempool:
  1060. bioset_free(log->bs);
  1061. io_bs:
  1062. mempool_destroy(log->io_pool);
  1063. io_pool:
  1064. kmem_cache_destroy(log->io_kc);
  1065. io_kc:
  1066. kfree(log);
  1067. return -EINVAL;
  1068. }
  1069. void r5l_exit_log(struct r5l_log *log)
  1070. {
  1071. md_unregister_thread(&log->reclaim_thread);
  1072. mempool_destroy(log->meta_pool);
  1073. bioset_free(log->bs);
  1074. mempool_destroy(log->io_pool);
  1075. kmem_cache_destroy(log->io_kc);
  1076. kfree(log);
  1077. }