raid5-cache.c 32 KB

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