raid5-ppl.c 42 KB

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