raid5-ppl.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * Partial Parity Log for closing the RAID5 write hole
  3. * Copyright (c) 2017, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/slab.h>
  17. #include <linux/crc32c.h>
  18. #include <linux/flex_array.h>
  19. #include <linux/async_tx.h>
  20. #include <linux/raid/md_p.h>
  21. #include "md.h"
  22. #include "raid5.h"
  23. /*
  24. * PPL consists of a 4KB header (struct ppl_header) and at least 128KB for
  25. * partial parity data. The header contains an array of entries
  26. * (struct ppl_header_entry) which describe the logged write requests.
  27. * Partial parity for the entries comes after the header, written in the same
  28. * sequence as the entries:
  29. *
  30. * Header
  31. * entry0
  32. * ...
  33. * entryN
  34. * PP data
  35. * PP for entry0
  36. * ...
  37. * PP for entryN
  38. *
  39. * An entry describes one or more consecutive stripe_heads, up to a full
  40. * stripe. The modifed raid data chunks form an m-by-n matrix, where m is the
  41. * number of stripe_heads in the entry and n is the number of modified data
  42. * disks. Every stripe_head in the entry must write to the same data disks.
  43. * An example of a valid case described by a single entry (writes to the first
  44. * stripe of a 4 disk array, 16k chunk size):
  45. *
  46. * sh->sector dd0 dd1 dd2 ppl
  47. * +-----+-----+-----+
  48. * 0 | --- | --- | --- | +----+
  49. * 8 | -W- | -W- | --- | | pp | data_sector = 8
  50. * 16 | -W- | -W- | --- | | pp | data_size = 3 * 2 * 4k
  51. * 24 | -W- | -W- | --- | | pp | pp_size = 3 * 4k
  52. * +-----+-----+-----+ +----+
  53. *
  54. * data_sector is the first raid sector of the modified data, data_size is the
  55. * total size of modified data and pp_size is the size of partial parity for
  56. * this entry. Entries for full stripe writes contain no partial parity
  57. * (pp_size = 0), they only mark the stripes for which parity should be
  58. * recalculated after an unclean shutdown. Every entry holds a checksum of its
  59. * partial parity, the header also has a checksum of the header itself.
  60. *
  61. * A write request is always logged to the PPL instance stored on the parity
  62. * disk of the corresponding stripe. For each member disk there is one ppl_log
  63. * used to handle logging for this disk, independently from others. They are
  64. * grouped in child_logs array in struct ppl_conf, which is assigned to
  65. * r5conf->log_private.
  66. *
  67. * ppl_io_unit represents a full PPL write, header_page contains the ppl_header.
  68. * PPL entries for logged stripes are added in ppl_log_stripe(). A stripe_head
  69. * can be appended to the last entry if it meets the conditions for a valid
  70. * entry described above, otherwise a new entry is added. Checksums of entries
  71. * are calculated incrementally as stripes containing partial parity are being
  72. * added. ppl_submit_iounit() calculates the checksum of the header and submits
  73. * a bio containing the header page and partial parity pages (sh->ppl_page) for
  74. * all stripes of the io_unit. When the PPL write completes, the stripes
  75. * associated with the io_unit are released and raid5d starts writing their data
  76. * and parity. When all stripes are written, the io_unit is freed and the next
  77. * can be submitted.
  78. *
  79. * An io_unit is used to gather stripes until it is submitted or becomes full
  80. * (if the maximum number of entries or size of PPL is reached). Another io_unit
  81. * can't be submitted until the previous has completed (PPL and stripe
  82. * data+parity is written). The log->io_list tracks all io_units of a log
  83. * (for a single member disk). New io_units are added to the end of the list
  84. * and the first io_unit is submitted, if it is not submitted already.
  85. * The current io_unit accepting new stripes is always at the end of the list.
  86. */
  87. #define PPL_SPACE_SIZE (128 * 1024)
  88. struct ppl_conf {
  89. struct mddev *mddev;
  90. /* array of child logs, one for each raid disk */
  91. struct ppl_log *child_logs;
  92. int count;
  93. int block_size; /* the logical block size used for data_sector
  94. * in ppl_header_entry */
  95. u32 signature; /* raid array identifier */
  96. atomic64_t seq; /* current log write sequence number */
  97. struct kmem_cache *io_kc;
  98. mempool_t *io_pool;
  99. struct bio_set *bs;
  100. /* used only for recovery */
  101. int recovered_entries;
  102. int mismatch_count;
  103. /* stripes to retry if failed to allocate io_unit */
  104. struct list_head no_mem_stripes;
  105. spinlock_t no_mem_stripes_lock;
  106. };
  107. struct ppl_log {
  108. struct ppl_conf *ppl_conf; /* shared between all log instances */
  109. struct md_rdev *rdev; /* array member disk associated with
  110. * this log instance */
  111. struct mutex io_mutex;
  112. struct ppl_io_unit *current_io; /* current io_unit accepting new data
  113. * always at the end of io_list */
  114. spinlock_t io_list_lock;
  115. struct list_head io_list; /* all io_units of this log */
  116. sector_t next_io_sector;
  117. unsigned int entry_space;
  118. bool use_multippl;
  119. };
  120. #define PPL_IO_INLINE_BVECS 32
  121. struct ppl_io_unit {
  122. struct ppl_log *log;
  123. struct page *header_page; /* for ppl_header */
  124. unsigned int entries_count; /* number of entries in ppl_header */
  125. unsigned int pp_size; /* total size current of partial parity */
  126. u64 seq; /* sequence number of this log write */
  127. struct list_head log_sibling; /* log->io_list */
  128. struct list_head stripe_list; /* stripes added to the io_unit */
  129. atomic_t pending_stripes; /* how many stripes not written to raid */
  130. bool submitted; /* true if write to log started */
  131. /* inline bio and its biovec for submitting the iounit */
  132. struct bio bio;
  133. struct bio_vec biovec[PPL_IO_INLINE_BVECS];
  134. };
  135. struct dma_async_tx_descriptor *
  136. ops_run_partial_parity(struct stripe_head *sh, struct raid5_percpu *percpu,
  137. struct dma_async_tx_descriptor *tx)
  138. {
  139. int disks = sh->disks;
  140. struct page **srcs = flex_array_get(percpu->scribble, 0);
  141. int count = 0, pd_idx = sh->pd_idx, i;
  142. struct async_submit_ctl submit;
  143. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  144. /*
  145. * Partial parity is the XOR of stripe data chunks that are not changed
  146. * during the write request. Depending on available data
  147. * (read-modify-write vs. reconstruct-write case) we calculate it
  148. * differently.
  149. */
  150. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  151. /*
  152. * rmw: xor old data and parity from updated disks
  153. * This is calculated earlier by ops_run_prexor5() so just copy
  154. * the parity dev page.
  155. */
  156. srcs[count++] = sh->dev[pd_idx].page;
  157. } else if (sh->reconstruct_state == reconstruct_state_drain_run) {
  158. /* rcw: xor data from all not updated disks */
  159. for (i = disks; i--;) {
  160. struct r5dev *dev = &sh->dev[i];
  161. if (test_bit(R5_UPTODATE, &dev->flags))
  162. srcs[count++] = dev->page;
  163. }
  164. } else {
  165. return tx;
  166. }
  167. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, tx,
  168. NULL, sh, flex_array_get(percpu->scribble, 0)
  169. + sizeof(struct page *) * (sh->disks + 2));
  170. if (count == 1)
  171. tx = async_memcpy(sh->ppl_page, srcs[0], 0, 0, PAGE_SIZE,
  172. &submit);
  173. else
  174. tx = async_xor(sh->ppl_page, srcs, 0, count, PAGE_SIZE,
  175. &submit);
  176. return tx;
  177. }
  178. static void *ppl_io_pool_alloc(gfp_t gfp_mask, void *pool_data)
  179. {
  180. struct kmem_cache *kc = pool_data;
  181. struct ppl_io_unit *io;
  182. io = kmem_cache_alloc(kc, gfp_mask);
  183. if (!io)
  184. return NULL;
  185. io->header_page = alloc_page(gfp_mask);
  186. if (!io->header_page) {
  187. kmem_cache_free(kc, io);
  188. return NULL;
  189. }
  190. return io;
  191. }
  192. static void ppl_io_pool_free(void *element, void *pool_data)
  193. {
  194. struct kmem_cache *kc = pool_data;
  195. struct ppl_io_unit *io = element;
  196. __free_page(io->header_page);
  197. kmem_cache_free(kc, io);
  198. }
  199. static struct ppl_io_unit *ppl_new_iounit(struct ppl_log *log,
  200. struct stripe_head *sh)
  201. {
  202. struct ppl_conf *ppl_conf = log->ppl_conf;
  203. struct ppl_io_unit *io;
  204. struct ppl_header *pplhdr;
  205. struct page *header_page;
  206. io = mempool_alloc(ppl_conf->io_pool, GFP_NOWAIT);
  207. if (!io)
  208. return NULL;
  209. header_page = io->header_page;
  210. memset(io, 0, sizeof(*io));
  211. io->header_page = header_page;
  212. io->log = log;
  213. INIT_LIST_HEAD(&io->log_sibling);
  214. INIT_LIST_HEAD(&io->stripe_list);
  215. atomic_set(&io->pending_stripes, 0);
  216. bio_init(&io->bio, io->biovec, PPL_IO_INLINE_BVECS);
  217. pplhdr = page_address(io->header_page);
  218. clear_page(pplhdr);
  219. memset(pplhdr->reserved, 0xff, PPL_HDR_RESERVED);
  220. pplhdr->signature = cpu_to_le32(ppl_conf->signature);
  221. io->seq = atomic64_add_return(1, &ppl_conf->seq);
  222. pplhdr->generation = cpu_to_le64(io->seq);
  223. return io;
  224. }
  225. static int ppl_log_stripe(struct ppl_log *log, struct stripe_head *sh)
  226. {
  227. struct ppl_io_unit *io = log->current_io;
  228. struct ppl_header_entry *e = NULL;
  229. struct ppl_header *pplhdr;
  230. int i;
  231. sector_t data_sector = 0;
  232. int data_disks = 0;
  233. struct r5conf *conf = sh->raid_conf;
  234. pr_debug("%s: stripe: %llu\n", __func__, (unsigned long long)sh->sector);
  235. /* check if current io_unit is full */
  236. if (io && (io->pp_size == log->entry_space ||
  237. io->entries_count == PPL_HDR_MAX_ENTRIES)) {
  238. pr_debug("%s: add io_unit blocked by seq: %llu\n",
  239. __func__, io->seq);
  240. io = NULL;
  241. }
  242. /* add a new unit if there is none or the current is full */
  243. if (!io) {
  244. io = ppl_new_iounit(log, sh);
  245. if (!io)
  246. return -ENOMEM;
  247. spin_lock_irq(&log->io_list_lock);
  248. list_add_tail(&io->log_sibling, &log->io_list);
  249. spin_unlock_irq(&log->io_list_lock);
  250. log->current_io = io;
  251. }
  252. for (i = 0; i < sh->disks; i++) {
  253. struct r5dev *dev = &sh->dev[i];
  254. if (i != sh->pd_idx && test_bit(R5_Wantwrite, &dev->flags)) {
  255. if (!data_disks || dev->sector < data_sector)
  256. data_sector = dev->sector;
  257. data_disks++;
  258. }
  259. }
  260. BUG_ON(!data_disks);
  261. pr_debug("%s: seq: %llu data_sector: %llu data_disks: %d\n", __func__,
  262. io->seq, (unsigned long long)data_sector, data_disks);
  263. pplhdr = page_address(io->header_page);
  264. if (io->entries_count > 0) {
  265. struct ppl_header_entry *last =
  266. &pplhdr->entries[io->entries_count - 1];
  267. struct stripe_head *sh_last = list_last_entry(
  268. &io->stripe_list, struct stripe_head, log_list);
  269. u64 data_sector_last = le64_to_cpu(last->data_sector);
  270. u32 data_size_last = le32_to_cpu(last->data_size);
  271. /*
  272. * Check if we can append the stripe to the last entry. It must
  273. * be just after the last logged stripe and write to the same
  274. * disks. Use bit shift and logarithm to avoid 64-bit division.
  275. */
  276. if ((sh->sector == sh_last->sector + STRIPE_SECTORS) &&
  277. (data_sector >> ilog2(conf->chunk_sectors) ==
  278. data_sector_last >> ilog2(conf->chunk_sectors)) &&
  279. ((data_sector - data_sector_last) * data_disks ==
  280. data_size_last >> 9))
  281. e = last;
  282. }
  283. if (!e) {
  284. e = &pplhdr->entries[io->entries_count++];
  285. e->data_sector = cpu_to_le64(data_sector);
  286. e->parity_disk = cpu_to_le32(sh->pd_idx);
  287. e->checksum = cpu_to_le32(~0);
  288. }
  289. le32_add_cpu(&e->data_size, data_disks << PAGE_SHIFT);
  290. /* don't write any PP if full stripe write */
  291. if (!test_bit(STRIPE_FULL_WRITE, &sh->state)) {
  292. le32_add_cpu(&e->pp_size, PAGE_SIZE);
  293. io->pp_size += PAGE_SIZE;
  294. e->checksum = cpu_to_le32(crc32c_le(le32_to_cpu(e->checksum),
  295. page_address(sh->ppl_page),
  296. PAGE_SIZE));
  297. }
  298. list_add_tail(&sh->log_list, &io->stripe_list);
  299. atomic_inc(&io->pending_stripes);
  300. sh->ppl_io = io;
  301. return 0;
  302. }
  303. int ppl_write_stripe(struct r5conf *conf, struct stripe_head *sh)
  304. {
  305. struct ppl_conf *ppl_conf = conf->log_private;
  306. struct ppl_io_unit *io = sh->ppl_io;
  307. struct ppl_log *log;
  308. if (io || test_bit(STRIPE_SYNCING, &sh->state) || !sh->ppl_page ||
  309. !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
  310. !test_bit(R5_Insync, &sh->dev[sh->pd_idx].flags)) {
  311. clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
  312. return -EAGAIN;
  313. }
  314. log = &ppl_conf->child_logs[sh->pd_idx];
  315. mutex_lock(&log->io_mutex);
  316. if (!log->rdev || test_bit(Faulty, &log->rdev->flags)) {
  317. mutex_unlock(&log->io_mutex);
  318. return -EAGAIN;
  319. }
  320. set_bit(STRIPE_LOG_TRAPPED, &sh->state);
  321. clear_bit(STRIPE_DELAYED, &sh->state);
  322. atomic_inc(&sh->count);
  323. if (ppl_log_stripe(log, sh)) {
  324. spin_lock_irq(&ppl_conf->no_mem_stripes_lock);
  325. list_add_tail(&sh->log_list, &ppl_conf->no_mem_stripes);
  326. spin_unlock_irq(&ppl_conf->no_mem_stripes_lock);
  327. }
  328. mutex_unlock(&log->io_mutex);
  329. return 0;
  330. }
  331. static void ppl_log_endio(struct bio *bio)
  332. {
  333. struct ppl_io_unit *io = bio->bi_private;
  334. struct ppl_log *log = io->log;
  335. struct ppl_conf *ppl_conf = log->ppl_conf;
  336. struct stripe_head *sh, *next;
  337. pr_debug("%s: seq: %llu\n", __func__, io->seq);
  338. if (bio->bi_status)
  339. md_error(ppl_conf->mddev, log->rdev);
  340. list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
  341. list_del_init(&sh->log_list);
  342. set_bit(STRIPE_HANDLE, &sh->state);
  343. raid5_release_stripe(sh);
  344. }
  345. }
  346. static void ppl_submit_iounit_bio(struct ppl_io_unit *io, struct bio *bio)
  347. {
  348. char b[BDEVNAME_SIZE];
  349. pr_debug("%s: seq: %llu size: %u sector: %llu dev: %s\n",
  350. __func__, io->seq, bio->bi_iter.bi_size,
  351. (unsigned long long)bio->bi_iter.bi_sector,
  352. bio_devname(bio, b));
  353. submit_bio(bio);
  354. }
  355. static void ppl_submit_iounit(struct ppl_io_unit *io)
  356. {
  357. struct ppl_log *log = io->log;
  358. struct ppl_conf *ppl_conf = log->ppl_conf;
  359. struct ppl_header *pplhdr = page_address(io->header_page);
  360. struct bio *bio = &io->bio;
  361. struct stripe_head *sh;
  362. int i;
  363. bio->bi_private = io;
  364. if (!log->rdev || test_bit(Faulty, &log->rdev->flags)) {
  365. ppl_log_endio(bio);
  366. return;
  367. }
  368. for (i = 0; i < io->entries_count; i++) {
  369. struct ppl_header_entry *e = &pplhdr->entries[i];
  370. pr_debug("%s: seq: %llu entry: %d data_sector: %llu pp_size: %u data_size: %u\n",
  371. __func__, io->seq, i, le64_to_cpu(e->data_sector),
  372. le32_to_cpu(e->pp_size), le32_to_cpu(e->data_size));
  373. e->data_sector = cpu_to_le64(le64_to_cpu(e->data_sector) >>
  374. ilog2(ppl_conf->block_size >> 9));
  375. e->checksum = cpu_to_le32(~le32_to_cpu(e->checksum));
  376. }
  377. pplhdr->entries_count = cpu_to_le32(io->entries_count);
  378. pplhdr->checksum = cpu_to_le32(~crc32c_le(~0, pplhdr, PPL_HEADER_SIZE));
  379. /* Rewind the buffer if current PPL is larger then remaining space */
  380. if (log->use_multippl &&
  381. log->rdev->ppl.sector + log->rdev->ppl.size - log->next_io_sector <
  382. (PPL_HEADER_SIZE + io->pp_size) >> 9)
  383. log->next_io_sector = log->rdev->ppl.sector;
  384. bio->bi_end_io = ppl_log_endio;
  385. bio->bi_opf = REQ_OP_WRITE | REQ_FUA;
  386. bio_set_dev(bio, log->rdev->bdev);
  387. bio->bi_iter.bi_sector = log->next_io_sector;
  388. bio_add_page(bio, io->header_page, PAGE_SIZE, 0);
  389. pr_debug("%s: log->current_io_sector: %llu\n", __func__,
  390. (unsigned long long)log->next_io_sector);
  391. if (log->use_multippl)
  392. log->next_io_sector += (PPL_HEADER_SIZE + io->pp_size) >> 9;
  393. list_for_each_entry(sh, &io->stripe_list, log_list) {
  394. /* entries for full stripe writes have no partial parity */
  395. if (test_bit(STRIPE_FULL_WRITE, &sh->state))
  396. continue;
  397. if (!bio_add_page(bio, sh->ppl_page, PAGE_SIZE, 0)) {
  398. struct bio *prev = bio;
  399. bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES,
  400. ppl_conf->bs);
  401. bio->bi_opf = prev->bi_opf;
  402. bio_copy_dev(bio, prev);
  403. bio->bi_iter.bi_sector = bio_end_sector(prev);
  404. bio_add_page(bio, sh->ppl_page, PAGE_SIZE, 0);
  405. bio_chain(bio, prev);
  406. ppl_submit_iounit_bio(io, prev);
  407. }
  408. }
  409. ppl_submit_iounit_bio(io, bio);
  410. }
  411. static void ppl_submit_current_io(struct ppl_log *log)
  412. {
  413. struct ppl_io_unit *io;
  414. spin_lock_irq(&log->io_list_lock);
  415. io = list_first_entry_or_null(&log->io_list, struct ppl_io_unit,
  416. log_sibling);
  417. if (io && io->submitted)
  418. io = NULL;
  419. spin_unlock_irq(&log->io_list_lock);
  420. if (io) {
  421. io->submitted = true;
  422. if (io == log->current_io)
  423. log->current_io = NULL;
  424. ppl_submit_iounit(io);
  425. }
  426. }
  427. void ppl_write_stripe_run(struct r5conf *conf)
  428. {
  429. struct ppl_conf *ppl_conf = conf->log_private;
  430. struct ppl_log *log;
  431. int i;
  432. for (i = 0; i < ppl_conf->count; i++) {
  433. log = &ppl_conf->child_logs[i];
  434. mutex_lock(&log->io_mutex);
  435. ppl_submit_current_io(log);
  436. mutex_unlock(&log->io_mutex);
  437. }
  438. }
  439. static void ppl_io_unit_finished(struct ppl_io_unit *io)
  440. {
  441. struct ppl_log *log = io->log;
  442. struct ppl_conf *ppl_conf = log->ppl_conf;
  443. unsigned long flags;
  444. pr_debug("%s: seq: %llu\n", __func__, io->seq);
  445. local_irq_save(flags);
  446. spin_lock(&log->io_list_lock);
  447. list_del(&io->log_sibling);
  448. spin_unlock(&log->io_list_lock);
  449. mempool_free(io, ppl_conf->io_pool);
  450. spin_lock(&ppl_conf->no_mem_stripes_lock);
  451. if (!list_empty(&ppl_conf->no_mem_stripes)) {
  452. struct stripe_head *sh;
  453. sh = list_first_entry(&ppl_conf->no_mem_stripes,
  454. struct stripe_head, log_list);
  455. list_del_init(&sh->log_list);
  456. set_bit(STRIPE_HANDLE, &sh->state);
  457. raid5_release_stripe(sh);
  458. }
  459. spin_unlock(&ppl_conf->no_mem_stripes_lock);
  460. local_irq_restore(flags);
  461. }
  462. void ppl_stripe_write_finished(struct stripe_head *sh)
  463. {
  464. struct ppl_io_unit *io;
  465. io = sh->ppl_io;
  466. sh->ppl_io = NULL;
  467. if (io && atomic_dec_and_test(&io->pending_stripes))
  468. ppl_io_unit_finished(io);
  469. }
  470. static void ppl_xor(int size, struct page *page1, struct page *page2)
  471. {
  472. struct async_submit_ctl submit;
  473. struct dma_async_tx_descriptor *tx;
  474. struct page *xor_srcs[] = { page1, page2 };
  475. init_async_submit(&submit, ASYNC_TX_ACK|ASYNC_TX_XOR_DROP_DST,
  476. NULL, NULL, NULL, NULL);
  477. tx = async_xor(page1, xor_srcs, 0, 2, size, &submit);
  478. async_tx_quiesce(&tx);
  479. }
  480. /*
  481. * PPL recovery strategy: xor partial parity and data from all modified data
  482. * disks within a stripe and write the result as the new stripe parity. If all
  483. * stripe data disks are modified (full stripe write), no partial parity is
  484. * available, so just xor the data disks.
  485. *
  486. * Recovery of a PPL entry shall occur only if all modified data disks are
  487. * available and read from all of them succeeds.
  488. *
  489. * A PPL entry applies to a stripe, partial parity size for an entry is at most
  490. * the size of the chunk. Examples of possible cases for a single entry:
  491. *
  492. * case 0: single data disk write:
  493. * data0 data1 data2 ppl parity
  494. * +--------+--------+--------+ +--------------------+
  495. * | ------ | ------ | ------ | +----+ | (no change) |
  496. * | ------ | -data- | ------ | | pp | -> | data1 ^ pp |
  497. * | ------ | -data- | ------ | | pp | -> | data1 ^ pp |
  498. * | ------ | ------ | ------ | +----+ | (no change) |
  499. * +--------+--------+--------+ +--------------------+
  500. * pp_size = data_size
  501. *
  502. * case 1: more than one data disk write:
  503. * data0 data1 data2 ppl parity
  504. * +--------+--------+--------+ +--------------------+
  505. * | ------ | ------ | ------ | +----+ | (no change) |
  506. * | -data- | -data- | ------ | | pp | -> | data0 ^ data1 ^ pp |
  507. * | -data- | -data- | ------ | | pp | -> | data0 ^ data1 ^ pp |
  508. * | ------ | ------ | ------ | +----+ | (no change) |
  509. * +--------+--------+--------+ +--------------------+
  510. * pp_size = data_size / modified_data_disks
  511. *
  512. * case 2: write to all data disks (also full stripe write):
  513. * data0 data1 data2 parity
  514. * +--------+--------+--------+ +--------------------+
  515. * | ------ | ------ | ------ | | (no change) |
  516. * | -data- | -data- | -data- | --------> | xor all data |
  517. * | ------ | ------ | ------ | --------> | (no change) |
  518. * | ------ | ------ | ------ | | (no change) |
  519. * +--------+--------+--------+ +--------------------+
  520. * pp_size = 0
  521. *
  522. * The following cases are possible only in other implementations. The recovery
  523. * code can handle them, but they are not generated at runtime because they can
  524. * be reduced to cases 0, 1 and 2:
  525. *
  526. * case 3:
  527. * data0 data1 data2 ppl parity
  528. * +--------+--------+--------+ +----+ +--------------------+
  529. * | ------ | -data- | -data- | | pp | | data1 ^ data2 ^ pp |
  530. * | ------ | -data- | -data- | | pp | -> | data1 ^ data2 ^ pp |
  531. * | -data- | -data- | -data- | | -- | -> | xor all data |
  532. * | -data- | -data- | ------ | | pp | | data0 ^ data1 ^ pp |
  533. * +--------+--------+--------+ +----+ +--------------------+
  534. * pp_size = chunk_size
  535. *
  536. * case 4:
  537. * data0 data1 data2 ppl parity
  538. * +--------+--------+--------+ +----+ +--------------------+
  539. * | ------ | -data- | ------ | | pp | | data1 ^ pp |
  540. * | ------ | ------ | ------ | | -- | -> | (no change) |
  541. * | ------ | ------ | ------ | | -- | -> | (no change) |
  542. * | -data- | ------ | ------ | | pp | | data0 ^ pp |
  543. * +--------+--------+--------+ +----+ +--------------------+
  544. * pp_size = chunk_size
  545. */
  546. static int ppl_recover_entry(struct ppl_log *log, struct ppl_header_entry *e,
  547. sector_t ppl_sector)
  548. {
  549. struct ppl_conf *ppl_conf = log->ppl_conf;
  550. struct mddev *mddev = ppl_conf->mddev;
  551. struct r5conf *conf = mddev->private;
  552. int block_size = ppl_conf->block_size;
  553. struct page *page1;
  554. struct page *page2;
  555. sector_t r_sector_first;
  556. sector_t r_sector_last;
  557. int strip_sectors;
  558. int data_disks;
  559. int i;
  560. int ret = 0;
  561. char b[BDEVNAME_SIZE];
  562. unsigned int pp_size = le32_to_cpu(e->pp_size);
  563. unsigned int data_size = le32_to_cpu(e->data_size);
  564. page1 = alloc_page(GFP_KERNEL);
  565. page2 = alloc_page(GFP_KERNEL);
  566. if (!page1 || !page2) {
  567. ret = -ENOMEM;
  568. goto out;
  569. }
  570. r_sector_first = le64_to_cpu(e->data_sector) * (block_size >> 9);
  571. if ((pp_size >> 9) < conf->chunk_sectors) {
  572. if (pp_size > 0) {
  573. data_disks = data_size / pp_size;
  574. strip_sectors = pp_size >> 9;
  575. } else {
  576. data_disks = conf->raid_disks - conf->max_degraded;
  577. strip_sectors = (data_size >> 9) / data_disks;
  578. }
  579. r_sector_last = r_sector_first +
  580. (data_disks - 1) * conf->chunk_sectors +
  581. strip_sectors;
  582. } else {
  583. data_disks = conf->raid_disks - conf->max_degraded;
  584. strip_sectors = conf->chunk_sectors;
  585. r_sector_last = r_sector_first + (data_size >> 9);
  586. }
  587. pr_debug("%s: array sector first: %llu last: %llu\n", __func__,
  588. (unsigned long long)r_sector_first,
  589. (unsigned long long)r_sector_last);
  590. /* if start and end is 4k aligned, use a 4k block */
  591. if (block_size == 512 &&
  592. (r_sector_first & (STRIPE_SECTORS - 1)) == 0 &&
  593. (r_sector_last & (STRIPE_SECTORS - 1)) == 0)
  594. block_size = STRIPE_SIZE;
  595. /* iterate through blocks in strip */
  596. for (i = 0; i < strip_sectors; i += (block_size >> 9)) {
  597. bool update_parity = false;
  598. sector_t parity_sector;
  599. struct md_rdev *parity_rdev;
  600. struct stripe_head sh;
  601. int disk;
  602. int indent = 0;
  603. pr_debug("%s:%*s iter %d start\n", __func__, indent, "", i);
  604. indent += 2;
  605. memset(page_address(page1), 0, PAGE_SIZE);
  606. /* iterate through data member disks */
  607. for (disk = 0; disk < data_disks; disk++) {
  608. int dd_idx;
  609. struct md_rdev *rdev;
  610. sector_t sector;
  611. sector_t r_sector = r_sector_first + i +
  612. (disk * conf->chunk_sectors);
  613. pr_debug("%s:%*s data member disk %d start\n",
  614. __func__, indent, "", disk);
  615. indent += 2;
  616. if (r_sector >= r_sector_last) {
  617. pr_debug("%s:%*s array sector %llu doesn't need parity update\n",
  618. __func__, indent, "",
  619. (unsigned long long)r_sector);
  620. indent -= 2;
  621. continue;
  622. }
  623. update_parity = true;
  624. /* map raid sector to member disk */
  625. sector = raid5_compute_sector(conf, r_sector, 0,
  626. &dd_idx, NULL);
  627. pr_debug("%s:%*s processing array sector %llu => data member disk %d, sector %llu\n",
  628. __func__, indent, "",
  629. (unsigned long long)r_sector, dd_idx,
  630. (unsigned long long)sector);
  631. rdev = conf->disks[dd_idx].rdev;
  632. if (!rdev || (!test_bit(In_sync, &rdev->flags) &&
  633. sector >= rdev->recovery_offset)) {
  634. pr_debug("%s:%*s data member disk %d missing\n",
  635. __func__, indent, "", dd_idx);
  636. update_parity = false;
  637. break;
  638. }
  639. pr_debug("%s:%*s reading data member disk %s sector %llu\n",
  640. __func__, indent, "", bdevname(rdev->bdev, b),
  641. (unsigned long long)sector);
  642. if (!sync_page_io(rdev, sector, block_size, page2,
  643. REQ_OP_READ, 0, false)) {
  644. md_error(mddev, rdev);
  645. pr_debug("%s:%*s read failed!\n", __func__,
  646. indent, "");
  647. ret = -EIO;
  648. goto out;
  649. }
  650. ppl_xor(block_size, page1, page2);
  651. indent -= 2;
  652. }
  653. if (!update_parity)
  654. continue;
  655. if (pp_size > 0) {
  656. pr_debug("%s:%*s reading pp disk sector %llu\n",
  657. __func__, indent, "",
  658. (unsigned long long)(ppl_sector + i));
  659. if (!sync_page_io(log->rdev,
  660. ppl_sector - log->rdev->data_offset + i,
  661. block_size, page2, REQ_OP_READ, 0,
  662. false)) {
  663. pr_debug("%s:%*s read failed!\n", __func__,
  664. indent, "");
  665. md_error(mddev, log->rdev);
  666. ret = -EIO;
  667. goto out;
  668. }
  669. ppl_xor(block_size, page1, page2);
  670. }
  671. /* map raid sector to parity disk */
  672. parity_sector = raid5_compute_sector(conf, r_sector_first + i,
  673. 0, &disk, &sh);
  674. BUG_ON(sh.pd_idx != le32_to_cpu(e->parity_disk));
  675. parity_rdev = conf->disks[sh.pd_idx].rdev;
  676. BUG_ON(parity_rdev->bdev->bd_dev != log->rdev->bdev->bd_dev);
  677. pr_debug("%s:%*s write parity at sector %llu, disk %s\n",
  678. __func__, indent, "",
  679. (unsigned long long)parity_sector,
  680. bdevname(parity_rdev->bdev, b));
  681. if (!sync_page_io(parity_rdev, parity_sector, block_size,
  682. page1, REQ_OP_WRITE, 0, false)) {
  683. pr_debug("%s:%*s parity write error!\n", __func__,
  684. indent, "");
  685. md_error(mddev, parity_rdev);
  686. ret = -EIO;
  687. goto out;
  688. }
  689. }
  690. out:
  691. if (page1)
  692. __free_page(page1);
  693. if (page2)
  694. __free_page(page2);
  695. return ret;
  696. }
  697. static int ppl_recover(struct ppl_log *log, struct ppl_header *pplhdr,
  698. sector_t offset)
  699. {
  700. struct ppl_conf *ppl_conf = log->ppl_conf;
  701. struct md_rdev *rdev = log->rdev;
  702. struct mddev *mddev = rdev->mddev;
  703. sector_t ppl_sector = rdev->ppl.sector + offset +
  704. (PPL_HEADER_SIZE >> 9);
  705. struct page *page;
  706. int i;
  707. int ret = 0;
  708. page = alloc_page(GFP_KERNEL);
  709. if (!page)
  710. return -ENOMEM;
  711. /* iterate through all PPL entries saved */
  712. for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++) {
  713. struct ppl_header_entry *e = &pplhdr->entries[i];
  714. u32 pp_size = le32_to_cpu(e->pp_size);
  715. sector_t sector = ppl_sector;
  716. int ppl_entry_sectors = pp_size >> 9;
  717. u32 crc, crc_stored;
  718. pr_debug("%s: disk: %d entry: %d ppl_sector: %llu pp_size: %u\n",
  719. __func__, rdev->raid_disk, i,
  720. (unsigned long long)ppl_sector, pp_size);
  721. crc = ~0;
  722. crc_stored = le32_to_cpu(e->checksum);
  723. /* read parial parity for this entry and calculate its checksum */
  724. while (pp_size) {
  725. int s = pp_size > PAGE_SIZE ? PAGE_SIZE : pp_size;
  726. if (!sync_page_io(rdev, sector - rdev->data_offset,
  727. s, page, REQ_OP_READ, 0, false)) {
  728. md_error(mddev, rdev);
  729. ret = -EIO;
  730. goto out;
  731. }
  732. crc = crc32c_le(crc, page_address(page), s);
  733. pp_size -= s;
  734. sector += s >> 9;
  735. }
  736. crc = ~crc;
  737. if (crc != crc_stored) {
  738. /*
  739. * Don't recover this entry if the checksum does not
  740. * match, but keep going and try to recover other
  741. * entries.
  742. */
  743. pr_debug("%s: ppl entry crc does not match: stored: 0x%x calculated: 0x%x\n",
  744. __func__, crc_stored, crc);
  745. ppl_conf->mismatch_count++;
  746. } else {
  747. ret = ppl_recover_entry(log, e, ppl_sector);
  748. if (ret)
  749. goto out;
  750. ppl_conf->recovered_entries++;
  751. }
  752. ppl_sector += ppl_entry_sectors;
  753. }
  754. /* flush the disk cache after recovery if necessary */
  755. ret = blkdev_issue_flush(rdev->bdev, GFP_KERNEL, NULL);
  756. out:
  757. __free_page(page);
  758. return ret;
  759. }
  760. static int ppl_write_empty_header(struct ppl_log *log)
  761. {
  762. struct page *page;
  763. struct ppl_header *pplhdr;
  764. struct md_rdev *rdev = log->rdev;
  765. int ret = 0;
  766. pr_debug("%s: disk: %d ppl_sector: %llu\n", __func__,
  767. rdev->raid_disk, (unsigned long long)rdev->ppl.sector);
  768. page = alloc_page(GFP_NOIO | __GFP_ZERO);
  769. if (!page)
  770. return -ENOMEM;
  771. pplhdr = page_address(page);
  772. /* zero out PPL space to avoid collision with old PPLs */
  773. blkdev_issue_zeroout(rdev->bdev, rdev->ppl.sector,
  774. log->rdev->ppl.size, GFP_NOIO, 0);
  775. memset(pplhdr->reserved, 0xff, PPL_HDR_RESERVED);
  776. pplhdr->signature = cpu_to_le32(log->ppl_conf->signature);
  777. pplhdr->checksum = cpu_to_le32(~crc32c_le(~0, pplhdr, PAGE_SIZE));
  778. if (!sync_page_io(rdev, rdev->ppl.sector - rdev->data_offset,
  779. PPL_HEADER_SIZE, page, REQ_OP_WRITE | REQ_SYNC |
  780. REQ_FUA, 0, false)) {
  781. md_error(rdev->mddev, rdev);
  782. ret = -EIO;
  783. }
  784. __free_page(page);
  785. return ret;
  786. }
  787. static int ppl_load_distributed(struct ppl_log *log)
  788. {
  789. struct ppl_conf *ppl_conf = log->ppl_conf;
  790. struct md_rdev *rdev = log->rdev;
  791. struct mddev *mddev = rdev->mddev;
  792. struct page *page, *page2, *tmp;
  793. struct ppl_header *pplhdr = NULL, *prev_pplhdr = NULL;
  794. u32 crc, crc_stored;
  795. u32 signature;
  796. int ret = 0, i;
  797. sector_t pplhdr_offset = 0, prev_pplhdr_offset = 0;
  798. pr_debug("%s: disk: %d\n", __func__, rdev->raid_disk);
  799. /* read PPL headers, find the recent one */
  800. page = alloc_page(GFP_KERNEL);
  801. if (!page)
  802. return -ENOMEM;
  803. page2 = alloc_page(GFP_KERNEL);
  804. if (!page2) {
  805. __free_page(page);
  806. return -ENOMEM;
  807. }
  808. /* searching ppl area for latest ppl */
  809. while (pplhdr_offset < rdev->ppl.size - (PPL_HEADER_SIZE >> 9)) {
  810. if (!sync_page_io(rdev,
  811. rdev->ppl.sector - rdev->data_offset +
  812. pplhdr_offset, PAGE_SIZE, page, REQ_OP_READ,
  813. 0, false)) {
  814. md_error(mddev, rdev);
  815. ret = -EIO;
  816. /* if not able to read - don't recover any PPL */
  817. pplhdr = NULL;
  818. break;
  819. }
  820. pplhdr = page_address(page);
  821. /* check header validity */
  822. crc_stored = le32_to_cpu(pplhdr->checksum);
  823. pplhdr->checksum = 0;
  824. crc = ~crc32c_le(~0, pplhdr, PAGE_SIZE);
  825. if (crc_stored != crc) {
  826. pr_debug("%s: ppl header crc does not match: stored: 0x%x calculated: 0x%x (offset: %llu)\n",
  827. __func__, crc_stored, crc,
  828. (unsigned long long)pplhdr_offset);
  829. pplhdr = prev_pplhdr;
  830. pplhdr_offset = prev_pplhdr_offset;
  831. break;
  832. }
  833. signature = le32_to_cpu(pplhdr->signature);
  834. if (mddev->external) {
  835. /*
  836. * For external metadata the header signature is set and
  837. * validated in userspace.
  838. */
  839. ppl_conf->signature = signature;
  840. } else if (ppl_conf->signature != signature) {
  841. pr_debug("%s: ppl header signature does not match: stored: 0x%x configured: 0x%x (offset: %llu)\n",
  842. __func__, signature, ppl_conf->signature,
  843. (unsigned long long)pplhdr_offset);
  844. pplhdr = prev_pplhdr;
  845. pplhdr_offset = prev_pplhdr_offset;
  846. break;
  847. }
  848. if (prev_pplhdr && le64_to_cpu(prev_pplhdr->generation) >
  849. le64_to_cpu(pplhdr->generation)) {
  850. /* previous was newest */
  851. pplhdr = prev_pplhdr;
  852. pplhdr_offset = prev_pplhdr_offset;
  853. break;
  854. }
  855. prev_pplhdr_offset = pplhdr_offset;
  856. prev_pplhdr = pplhdr;
  857. tmp = page;
  858. page = page2;
  859. page2 = tmp;
  860. /* calculate next potential ppl offset */
  861. for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++)
  862. pplhdr_offset +=
  863. le32_to_cpu(pplhdr->entries[i].pp_size) >> 9;
  864. pplhdr_offset += PPL_HEADER_SIZE >> 9;
  865. }
  866. /* no valid ppl found */
  867. if (!pplhdr)
  868. ppl_conf->mismatch_count++;
  869. else
  870. pr_debug("%s: latest PPL found at offset: %llu, with generation: %llu\n",
  871. __func__, (unsigned long long)pplhdr_offset,
  872. le64_to_cpu(pplhdr->generation));
  873. /* attempt to recover from log if we are starting a dirty array */
  874. if (pplhdr && !mddev->pers && mddev->recovery_cp != MaxSector)
  875. ret = ppl_recover(log, pplhdr, pplhdr_offset);
  876. /* write empty header if we are starting the array */
  877. if (!ret && !mddev->pers)
  878. ret = ppl_write_empty_header(log);
  879. __free_page(page);
  880. __free_page(page2);
  881. pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
  882. __func__, ret, ppl_conf->mismatch_count,
  883. ppl_conf->recovered_entries);
  884. return ret;
  885. }
  886. static int ppl_load(struct ppl_conf *ppl_conf)
  887. {
  888. int ret = 0;
  889. u32 signature = 0;
  890. bool signature_set = false;
  891. int i;
  892. for (i = 0; i < ppl_conf->count; i++) {
  893. struct ppl_log *log = &ppl_conf->child_logs[i];
  894. /* skip missing drive */
  895. if (!log->rdev)
  896. continue;
  897. ret = ppl_load_distributed(log);
  898. if (ret)
  899. break;
  900. /*
  901. * For external metadata we can't check if the signature is
  902. * correct on a single drive, but we can check if it is the same
  903. * on all drives.
  904. */
  905. if (ppl_conf->mddev->external) {
  906. if (!signature_set) {
  907. signature = ppl_conf->signature;
  908. signature_set = true;
  909. } else if (signature != ppl_conf->signature) {
  910. pr_warn("md/raid:%s: PPL header signature does not match on all member drives\n",
  911. mdname(ppl_conf->mddev));
  912. ret = -EINVAL;
  913. break;
  914. }
  915. }
  916. }
  917. pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
  918. __func__, ret, ppl_conf->mismatch_count,
  919. ppl_conf->recovered_entries);
  920. return ret;
  921. }
  922. static void __ppl_exit_log(struct ppl_conf *ppl_conf)
  923. {
  924. clear_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  925. clear_bit(MD_HAS_MULTIPLE_PPLS, &ppl_conf->mddev->flags);
  926. kfree(ppl_conf->child_logs);
  927. if (ppl_conf->bs)
  928. bioset_free(ppl_conf->bs);
  929. mempool_destroy(ppl_conf->io_pool);
  930. kmem_cache_destroy(ppl_conf->io_kc);
  931. kfree(ppl_conf);
  932. }
  933. void ppl_exit_log(struct r5conf *conf)
  934. {
  935. struct ppl_conf *ppl_conf = conf->log_private;
  936. if (ppl_conf) {
  937. __ppl_exit_log(ppl_conf);
  938. conf->log_private = NULL;
  939. }
  940. }
  941. static int ppl_validate_rdev(struct md_rdev *rdev)
  942. {
  943. char b[BDEVNAME_SIZE];
  944. int ppl_data_sectors;
  945. int ppl_size_new;
  946. /*
  947. * The configured PPL size must be enough to store
  948. * the header and (at the very least) partial parity
  949. * for one stripe. Round it down to ensure the data
  950. * space is cleanly divisible by stripe size.
  951. */
  952. ppl_data_sectors = rdev->ppl.size - (PPL_HEADER_SIZE >> 9);
  953. if (ppl_data_sectors > 0)
  954. ppl_data_sectors = rounddown(ppl_data_sectors, STRIPE_SECTORS);
  955. if (ppl_data_sectors <= 0) {
  956. pr_warn("md/raid:%s: PPL space too small on %s\n",
  957. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  958. return -ENOSPC;
  959. }
  960. ppl_size_new = ppl_data_sectors + (PPL_HEADER_SIZE >> 9);
  961. if ((rdev->ppl.sector < rdev->data_offset &&
  962. rdev->ppl.sector + ppl_size_new > rdev->data_offset) ||
  963. (rdev->ppl.sector >= rdev->data_offset &&
  964. rdev->data_offset + rdev->sectors > rdev->ppl.sector)) {
  965. pr_warn("md/raid:%s: PPL space overlaps with data on %s\n",
  966. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  967. return -EINVAL;
  968. }
  969. if (!rdev->mddev->external &&
  970. ((rdev->ppl.offset > 0 && rdev->ppl.offset < (rdev->sb_size >> 9)) ||
  971. (rdev->ppl.offset <= 0 && rdev->ppl.offset + ppl_size_new > 0))) {
  972. pr_warn("md/raid:%s: PPL space overlaps with superblock on %s\n",
  973. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  974. return -EINVAL;
  975. }
  976. rdev->ppl.size = ppl_size_new;
  977. return 0;
  978. }
  979. static void ppl_init_child_log(struct ppl_log *log, struct md_rdev *rdev)
  980. {
  981. if ((rdev->ppl.size << 9) >= (PPL_SPACE_SIZE +
  982. PPL_HEADER_SIZE) * 2) {
  983. log->use_multippl = true;
  984. set_bit(MD_HAS_MULTIPLE_PPLS,
  985. &log->ppl_conf->mddev->flags);
  986. log->entry_space = PPL_SPACE_SIZE;
  987. } else {
  988. log->use_multippl = false;
  989. log->entry_space = (log->rdev->ppl.size << 9) -
  990. PPL_HEADER_SIZE;
  991. }
  992. log->next_io_sector = rdev->ppl.sector;
  993. }
  994. int ppl_init_log(struct r5conf *conf)
  995. {
  996. struct ppl_conf *ppl_conf;
  997. struct mddev *mddev = conf->mddev;
  998. int ret = 0;
  999. int i;
  1000. bool need_cache_flush = false;
  1001. pr_debug("md/raid:%s: enabling distributed Partial Parity Log\n",
  1002. mdname(conf->mddev));
  1003. if (PAGE_SIZE != 4096)
  1004. return -EINVAL;
  1005. if (mddev->level != 5) {
  1006. pr_warn("md/raid:%s PPL is not compatible with raid level %d\n",
  1007. mdname(mddev), mddev->level);
  1008. return -EINVAL;
  1009. }
  1010. if (mddev->bitmap_info.file || mddev->bitmap_info.offset) {
  1011. pr_warn("md/raid:%s PPL is not compatible with bitmap\n",
  1012. mdname(mddev));
  1013. return -EINVAL;
  1014. }
  1015. if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
  1016. pr_warn("md/raid:%s PPL is not compatible with journal\n",
  1017. mdname(mddev));
  1018. return -EINVAL;
  1019. }
  1020. ppl_conf = kzalloc(sizeof(struct ppl_conf), GFP_KERNEL);
  1021. if (!ppl_conf)
  1022. return -ENOMEM;
  1023. ppl_conf->mddev = mddev;
  1024. ppl_conf->io_kc = KMEM_CACHE(ppl_io_unit, 0);
  1025. if (!ppl_conf->io_kc) {
  1026. ret = -ENOMEM;
  1027. goto err;
  1028. }
  1029. ppl_conf->io_pool = mempool_create(conf->raid_disks, ppl_io_pool_alloc,
  1030. ppl_io_pool_free, ppl_conf->io_kc);
  1031. if (!ppl_conf->io_pool) {
  1032. ret = -ENOMEM;
  1033. goto err;
  1034. }
  1035. ppl_conf->bs = bioset_create(conf->raid_disks, 0, BIOSET_NEED_BVECS);
  1036. if (!ppl_conf->bs) {
  1037. ret = -ENOMEM;
  1038. goto err;
  1039. }
  1040. ppl_conf->count = conf->raid_disks;
  1041. ppl_conf->child_logs = kcalloc(ppl_conf->count, sizeof(struct ppl_log),
  1042. GFP_KERNEL);
  1043. if (!ppl_conf->child_logs) {
  1044. ret = -ENOMEM;
  1045. goto err;
  1046. }
  1047. atomic64_set(&ppl_conf->seq, 0);
  1048. INIT_LIST_HEAD(&ppl_conf->no_mem_stripes);
  1049. spin_lock_init(&ppl_conf->no_mem_stripes_lock);
  1050. if (!mddev->external) {
  1051. ppl_conf->signature = ~crc32c_le(~0, mddev->uuid, sizeof(mddev->uuid));
  1052. ppl_conf->block_size = 512;
  1053. } else {
  1054. ppl_conf->block_size = queue_logical_block_size(mddev->queue);
  1055. }
  1056. for (i = 0; i < ppl_conf->count; i++) {
  1057. struct ppl_log *log = &ppl_conf->child_logs[i];
  1058. struct md_rdev *rdev = conf->disks[i].rdev;
  1059. mutex_init(&log->io_mutex);
  1060. spin_lock_init(&log->io_list_lock);
  1061. INIT_LIST_HEAD(&log->io_list);
  1062. log->ppl_conf = ppl_conf;
  1063. log->rdev = rdev;
  1064. if (rdev) {
  1065. struct request_queue *q;
  1066. ret = ppl_validate_rdev(rdev);
  1067. if (ret)
  1068. goto err;
  1069. q = bdev_get_queue(rdev->bdev);
  1070. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  1071. need_cache_flush = true;
  1072. ppl_init_child_log(log, rdev);
  1073. }
  1074. }
  1075. if (need_cache_flush)
  1076. pr_warn("md/raid:%s: Volatile write-back cache should be disabled on all member drives when using PPL!\n",
  1077. mdname(mddev));
  1078. /* load and possibly recover the logs from the member disks */
  1079. ret = ppl_load(ppl_conf);
  1080. if (ret) {
  1081. goto err;
  1082. } else if (!mddev->pers &&
  1083. mddev->recovery_cp == 0 && !mddev->degraded &&
  1084. ppl_conf->recovered_entries > 0 &&
  1085. ppl_conf->mismatch_count == 0) {
  1086. /*
  1087. * If we are starting a dirty array and the recovery succeeds
  1088. * without any issues, set the array as clean.
  1089. */
  1090. mddev->recovery_cp = MaxSector;
  1091. set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
  1092. } else if (mddev->pers && ppl_conf->mismatch_count > 0) {
  1093. /* no mismatch allowed when enabling PPL for a running array */
  1094. ret = -EINVAL;
  1095. goto err;
  1096. }
  1097. conf->log_private = ppl_conf;
  1098. set_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  1099. return 0;
  1100. err:
  1101. __ppl_exit_log(ppl_conf);
  1102. return ret;
  1103. }
  1104. int ppl_modify_log(struct r5conf *conf, struct md_rdev *rdev, bool add)
  1105. {
  1106. struct ppl_conf *ppl_conf = conf->log_private;
  1107. struct ppl_log *log;
  1108. int ret = 0;
  1109. char b[BDEVNAME_SIZE];
  1110. if (!rdev)
  1111. return -EINVAL;
  1112. pr_debug("%s: disk: %d operation: %s dev: %s\n",
  1113. __func__, rdev->raid_disk, add ? "add" : "remove",
  1114. bdevname(rdev->bdev, b));
  1115. if (rdev->raid_disk < 0)
  1116. return 0;
  1117. if (rdev->raid_disk >= ppl_conf->count)
  1118. return -ENODEV;
  1119. log = &ppl_conf->child_logs[rdev->raid_disk];
  1120. mutex_lock(&log->io_mutex);
  1121. if (add) {
  1122. ret = ppl_validate_rdev(rdev);
  1123. if (!ret) {
  1124. log->rdev = rdev;
  1125. ret = ppl_write_empty_header(log);
  1126. ppl_init_child_log(log, rdev);
  1127. }
  1128. } else {
  1129. log->rdev = NULL;
  1130. }
  1131. mutex_unlock(&log->io_mutex);
  1132. return ret;
  1133. }