raid5-ppl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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) {
  633. pr_debug("%s:%*s data member disk %d missing\n",
  634. __func__, indent, "", dd_idx);
  635. update_parity = false;
  636. break;
  637. }
  638. pr_debug("%s:%*s reading data member disk %s sector %llu\n",
  639. __func__, indent, "", bdevname(rdev->bdev, b),
  640. (unsigned long long)sector);
  641. if (!sync_page_io(rdev, sector, block_size, page2,
  642. REQ_OP_READ, 0, false)) {
  643. md_error(mddev, rdev);
  644. pr_debug("%s:%*s read failed!\n", __func__,
  645. indent, "");
  646. ret = -EIO;
  647. goto out;
  648. }
  649. ppl_xor(block_size, page1, page2);
  650. indent -= 2;
  651. }
  652. if (!update_parity)
  653. continue;
  654. if (pp_size > 0) {
  655. pr_debug("%s:%*s reading pp disk sector %llu\n",
  656. __func__, indent, "",
  657. (unsigned long long)(ppl_sector + i));
  658. if (!sync_page_io(log->rdev,
  659. ppl_sector - log->rdev->data_offset + i,
  660. block_size, page2, REQ_OP_READ, 0,
  661. false)) {
  662. pr_debug("%s:%*s read failed!\n", __func__,
  663. indent, "");
  664. md_error(mddev, log->rdev);
  665. ret = -EIO;
  666. goto out;
  667. }
  668. ppl_xor(block_size, page1, page2);
  669. }
  670. /* map raid sector to parity disk */
  671. parity_sector = raid5_compute_sector(conf, r_sector_first + i,
  672. 0, &disk, &sh);
  673. BUG_ON(sh.pd_idx != le32_to_cpu(e->parity_disk));
  674. parity_rdev = conf->disks[sh.pd_idx].rdev;
  675. BUG_ON(parity_rdev->bdev->bd_dev != log->rdev->bdev->bd_dev);
  676. pr_debug("%s:%*s write parity at sector %llu, disk %s\n",
  677. __func__, indent, "",
  678. (unsigned long long)parity_sector,
  679. bdevname(parity_rdev->bdev, b));
  680. if (!sync_page_io(parity_rdev, parity_sector, block_size,
  681. page1, REQ_OP_WRITE, 0, false)) {
  682. pr_debug("%s:%*s parity write error!\n", __func__,
  683. indent, "");
  684. md_error(mddev, parity_rdev);
  685. ret = -EIO;
  686. goto out;
  687. }
  688. }
  689. out:
  690. if (page1)
  691. __free_page(page1);
  692. if (page2)
  693. __free_page(page2);
  694. return ret;
  695. }
  696. static int ppl_recover(struct ppl_log *log, struct ppl_header *pplhdr,
  697. sector_t offset)
  698. {
  699. struct ppl_conf *ppl_conf = log->ppl_conf;
  700. struct md_rdev *rdev = log->rdev;
  701. struct mddev *mddev = rdev->mddev;
  702. sector_t ppl_sector = rdev->ppl.sector + offset +
  703. (PPL_HEADER_SIZE >> 9);
  704. struct page *page;
  705. int i;
  706. int ret = 0;
  707. page = alloc_page(GFP_KERNEL);
  708. if (!page)
  709. return -ENOMEM;
  710. /* iterate through all PPL entries saved */
  711. for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++) {
  712. struct ppl_header_entry *e = &pplhdr->entries[i];
  713. u32 pp_size = le32_to_cpu(e->pp_size);
  714. sector_t sector = ppl_sector;
  715. int ppl_entry_sectors = pp_size >> 9;
  716. u32 crc, crc_stored;
  717. pr_debug("%s: disk: %d entry: %d ppl_sector: %llu pp_size: %u\n",
  718. __func__, rdev->raid_disk, i,
  719. (unsigned long long)ppl_sector, pp_size);
  720. crc = ~0;
  721. crc_stored = le32_to_cpu(e->checksum);
  722. /* read parial parity for this entry and calculate its checksum */
  723. while (pp_size) {
  724. int s = pp_size > PAGE_SIZE ? PAGE_SIZE : pp_size;
  725. if (!sync_page_io(rdev, sector - rdev->data_offset,
  726. s, page, REQ_OP_READ, 0, false)) {
  727. md_error(mddev, rdev);
  728. ret = -EIO;
  729. goto out;
  730. }
  731. crc = crc32c_le(crc, page_address(page), s);
  732. pp_size -= s;
  733. sector += s >> 9;
  734. }
  735. crc = ~crc;
  736. if (crc != crc_stored) {
  737. /*
  738. * Don't recover this entry if the checksum does not
  739. * match, but keep going and try to recover other
  740. * entries.
  741. */
  742. pr_debug("%s: ppl entry crc does not match: stored: 0x%x calculated: 0x%x\n",
  743. __func__, crc_stored, crc);
  744. ppl_conf->mismatch_count++;
  745. } else {
  746. ret = ppl_recover_entry(log, e, ppl_sector);
  747. if (ret)
  748. goto out;
  749. ppl_conf->recovered_entries++;
  750. }
  751. ppl_sector += ppl_entry_sectors;
  752. }
  753. /* flush the disk cache after recovery if necessary */
  754. ret = blkdev_issue_flush(rdev->bdev, GFP_KERNEL, NULL);
  755. out:
  756. __free_page(page);
  757. return ret;
  758. }
  759. static int ppl_write_empty_header(struct ppl_log *log)
  760. {
  761. struct page *page;
  762. struct ppl_header *pplhdr;
  763. struct md_rdev *rdev = log->rdev;
  764. int ret = 0;
  765. pr_debug("%s: disk: %d ppl_sector: %llu\n", __func__,
  766. rdev->raid_disk, (unsigned long long)rdev->ppl.sector);
  767. page = alloc_page(GFP_NOIO | __GFP_ZERO);
  768. if (!page)
  769. return -ENOMEM;
  770. pplhdr = page_address(page);
  771. /* zero out PPL space to avoid collision with old PPLs */
  772. blkdev_issue_zeroout(rdev->bdev, rdev->ppl.sector,
  773. log->rdev->ppl.size, GFP_NOIO, 0);
  774. memset(pplhdr->reserved, 0xff, PPL_HDR_RESERVED);
  775. pplhdr->signature = cpu_to_le32(log->ppl_conf->signature);
  776. pplhdr->checksum = cpu_to_le32(~crc32c_le(~0, pplhdr, PAGE_SIZE));
  777. if (!sync_page_io(rdev, rdev->ppl.sector - rdev->data_offset,
  778. PPL_HEADER_SIZE, page, REQ_OP_WRITE | REQ_SYNC |
  779. REQ_FUA, 0, false)) {
  780. md_error(rdev->mddev, rdev);
  781. ret = -EIO;
  782. }
  783. __free_page(page);
  784. return ret;
  785. }
  786. static int ppl_load_distributed(struct ppl_log *log)
  787. {
  788. struct ppl_conf *ppl_conf = log->ppl_conf;
  789. struct md_rdev *rdev = log->rdev;
  790. struct mddev *mddev = rdev->mddev;
  791. struct page *page, *page2, *tmp;
  792. struct ppl_header *pplhdr = NULL, *prev_pplhdr = NULL;
  793. u32 crc, crc_stored;
  794. u32 signature;
  795. int ret = 0, i;
  796. sector_t pplhdr_offset = 0, prev_pplhdr_offset = 0;
  797. pr_debug("%s: disk: %d\n", __func__, rdev->raid_disk);
  798. /* read PPL headers, find the recent one */
  799. page = alloc_page(GFP_KERNEL);
  800. if (!page)
  801. return -ENOMEM;
  802. page2 = alloc_page(GFP_KERNEL);
  803. if (!page2) {
  804. __free_page(page);
  805. return -ENOMEM;
  806. }
  807. /* searching ppl area for latest ppl */
  808. while (pplhdr_offset < rdev->ppl.size - (PPL_HEADER_SIZE >> 9)) {
  809. if (!sync_page_io(rdev,
  810. rdev->ppl.sector - rdev->data_offset +
  811. pplhdr_offset, PAGE_SIZE, page, REQ_OP_READ,
  812. 0, false)) {
  813. md_error(mddev, rdev);
  814. ret = -EIO;
  815. /* if not able to read - don't recover any PPL */
  816. pplhdr = NULL;
  817. break;
  818. }
  819. pplhdr = page_address(page);
  820. /* check header validity */
  821. crc_stored = le32_to_cpu(pplhdr->checksum);
  822. pplhdr->checksum = 0;
  823. crc = ~crc32c_le(~0, pplhdr, PAGE_SIZE);
  824. if (crc_stored != crc) {
  825. pr_debug("%s: ppl header crc does not match: stored: 0x%x calculated: 0x%x (offset: %llu)\n",
  826. __func__, crc_stored, crc,
  827. (unsigned long long)pplhdr_offset);
  828. pplhdr = prev_pplhdr;
  829. pplhdr_offset = prev_pplhdr_offset;
  830. break;
  831. }
  832. signature = le32_to_cpu(pplhdr->signature);
  833. if (mddev->external) {
  834. /*
  835. * For external metadata the header signature is set and
  836. * validated in userspace.
  837. */
  838. ppl_conf->signature = signature;
  839. } else if (ppl_conf->signature != signature) {
  840. pr_debug("%s: ppl header signature does not match: stored: 0x%x configured: 0x%x (offset: %llu)\n",
  841. __func__, signature, ppl_conf->signature,
  842. (unsigned long long)pplhdr_offset);
  843. pplhdr = prev_pplhdr;
  844. pplhdr_offset = prev_pplhdr_offset;
  845. break;
  846. }
  847. if (prev_pplhdr && le64_to_cpu(prev_pplhdr->generation) >
  848. le64_to_cpu(pplhdr->generation)) {
  849. /* previous was newest */
  850. pplhdr = prev_pplhdr;
  851. pplhdr_offset = prev_pplhdr_offset;
  852. break;
  853. }
  854. prev_pplhdr_offset = pplhdr_offset;
  855. prev_pplhdr = pplhdr;
  856. tmp = page;
  857. page = page2;
  858. page2 = tmp;
  859. /* calculate next potential ppl offset */
  860. for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++)
  861. pplhdr_offset +=
  862. le32_to_cpu(pplhdr->entries[i].pp_size) >> 9;
  863. pplhdr_offset += PPL_HEADER_SIZE >> 9;
  864. }
  865. /* no valid ppl found */
  866. if (!pplhdr)
  867. ppl_conf->mismatch_count++;
  868. else
  869. pr_debug("%s: latest PPL found at offset: %llu, with generation: %llu\n",
  870. __func__, (unsigned long long)pplhdr_offset,
  871. le64_to_cpu(pplhdr->generation));
  872. /* attempt to recover from log if we are starting a dirty array */
  873. if (pplhdr && !mddev->pers && mddev->recovery_cp != MaxSector)
  874. ret = ppl_recover(log, pplhdr, pplhdr_offset);
  875. /* write empty header if we are starting the array */
  876. if (!ret && !mddev->pers)
  877. ret = ppl_write_empty_header(log);
  878. __free_page(page);
  879. __free_page(page2);
  880. pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
  881. __func__, ret, ppl_conf->mismatch_count,
  882. ppl_conf->recovered_entries);
  883. return ret;
  884. }
  885. static int ppl_load(struct ppl_conf *ppl_conf)
  886. {
  887. int ret = 0;
  888. u32 signature = 0;
  889. bool signature_set = false;
  890. int i;
  891. for (i = 0; i < ppl_conf->count; i++) {
  892. struct ppl_log *log = &ppl_conf->child_logs[i];
  893. /* skip missing drive */
  894. if (!log->rdev)
  895. continue;
  896. ret = ppl_load_distributed(log);
  897. if (ret)
  898. break;
  899. /*
  900. * For external metadata we can't check if the signature is
  901. * correct on a single drive, but we can check if it is the same
  902. * on all drives.
  903. */
  904. if (ppl_conf->mddev->external) {
  905. if (!signature_set) {
  906. signature = ppl_conf->signature;
  907. signature_set = true;
  908. } else if (signature != ppl_conf->signature) {
  909. pr_warn("md/raid:%s: PPL header signature does not match on all member drives\n",
  910. mdname(ppl_conf->mddev));
  911. ret = -EINVAL;
  912. break;
  913. }
  914. }
  915. }
  916. pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
  917. __func__, ret, ppl_conf->mismatch_count,
  918. ppl_conf->recovered_entries);
  919. return ret;
  920. }
  921. static void __ppl_exit_log(struct ppl_conf *ppl_conf)
  922. {
  923. clear_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  924. clear_bit(MD_HAS_MULTIPLE_PPLS, &ppl_conf->mddev->flags);
  925. kfree(ppl_conf->child_logs);
  926. if (ppl_conf->bs)
  927. bioset_free(ppl_conf->bs);
  928. mempool_destroy(ppl_conf->io_pool);
  929. kmem_cache_destroy(ppl_conf->io_kc);
  930. kfree(ppl_conf);
  931. }
  932. void ppl_exit_log(struct r5conf *conf)
  933. {
  934. struct ppl_conf *ppl_conf = conf->log_private;
  935. if (ppl_conf) {
  936. __ppl_exit_log(ppl_conf);
  937. conf->log_private = NULL;
  938. }
  939. }
  940. static int ppl_validate_rdev(struct md_rdev *rdev)
  941. {
  942. char b[BDEVNAME_SIZE];
  943. int ppl_data_sectors;
  944. int ppl_size_new;
  945. /*
  946. * The configured PPL size must be enough to store
  947. * the header and (at the very least) partial parity
  948. * for one stripe. Round it down to ensure the data
  949. * space is cleanly divisible by stripe size.
  950. */
  951. ppl_data_sectors = rdev->ppl.size - (PPL_HEADER_SIZE >> 9);
  952. if (ppl_data_sectors > 0)
  953. ppl_data_sectors = rounddown(ppl_data_sectors, STRIPE_SECTORS);
  954. if (ppl_data_sectors <= 0) {
  955. pr_warn("md/raid:%s: PPL space too small on %s\n",
  956. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  957. return -ENOSPC;
  958. }
  959. ppl_size_new = ppl_data_sectors + (PPL_HEADER_SIZE >> 9);
  960. if ((rdev->ppl.sector < rdev->data_offset &&
  961. rdev->ppl.sector + ppl_size_new > rdev->data_offset) ||
  962. (rdev->ppl.sector >= rdev->data_offset &&
  963. rdev->data_offset + rdev->sectors > rdev->ppl.sector)) {
  964. pr_warn("md/raid:%s: PPL space overlaps with data on %s\n",
  965. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  966. return -EINVAL;
  967. }
  968. if (!rdev->mddev->external &&
  969. ((rdev->ppl.offset > 0 && rdev->ppl.offset < (rdev->sb_size >> 9)) ||
  970. (rdev->ppl.offset <= 0 && rdev->ppl.offset + ppl_size_new > 0))) {
  971. pr_warn("md/raid:%s: PPL space overlaps with superblock on %s\n",
  972. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  973. return -EINVAL;
  974. }
  975. rdev->ppl.size = ppl_size_new;
  976. return 0;
  977. }
  978. static void ppl_init_child_log(struct ppl_log *log, struct md_rdev *rdev)
  979. {
  980. if ((rdev->ppl.size << 9) >= (PPL_SPACE_SIZE +
  981. PPL_HEADER_SIZE) * 2) {
  982. log->use_multippl = true;
  983. set_bit(MD_HAS_MULTIPLE_PPLS,
  984. &log->ppl_conf->mddev->flags);
  985. log->entry_space = PPL_SPACE_SIZE;
  986. } else {
  987. log->use_multippl = false;
  988. log->entry_space = (log->rdev->ppl.size << 9) -
  989. PPL_HEADER_SIZE;
  990. }
  991. log->next_io_sector = rdev->ppl.sector;
  992. }
  993. int ppl_init_log(struct r5conf *conf)
  994. {
  995. struct ppl_conf *ppl_conf;
  996. struct mddev *mddev = conf->mddev;
  997. int ret = 0;
  998. int i;
  999. bool need_cache_flush = false;
  1000. pr_debug("md/raid:%s: enabling distributed Partial Parity Log\n",
  1001. mdname(conf->mddev));
  1002. if (PAGE_SIZE != 4096)
  1003. return -EINVAL;
  1004. if (mddev->level != 5) {
  1005. pr_warn("md/raid:%s PPL is not compatible with raid level %d\n",
  1006. mdname(mddev), mddev->level);
  1007. return -EINVAL;
  1008. }
  1009. if (mddev->bitmap_info.file || mddev->bitmap_info.offset) {
  1010. pr_warn("md/raid:%s PPL is not compatible with bitmap\n",
  1011. mdname(mddev));
  1012. return -EINVAL;
  1013. }
  1014. if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
  1015. pr_warn("md/raid:%s PPL is not compatible with journal\n",
  1016. mdname(mddev));
  1017. return -EINVAL;
  1018. }
  1019. ppl_conf = kzalloc(sizeof(struct ppl_conf), GFP_KERNEL);
  1020. if (!ppl_conf)
  1021. return -ENOMEM;
  1022. ppl_conf->mddev = mddev;
  1023. ppl_conf->io_kc = KMEM_CACHE(ppl_io_unit, 0);
  1024. if (!ppl_conf->io_kc) {
  1025. ret = -ENOMEM;
  1026. goto err;
  1027. }
  1028. ppl_conf->io_pool = mempool_create(conf->raid_disks, ppl_io_pool_alloc,
  1029. ppl_io_pool_free, ppl_conf->io_kc);
  1030. if (!ppl_conf->io_pool) {
  1031. ret = -ENOMEM;
  1032. goto err;
  1033. }
  1034. ppl_conf->bs = bioset_create(conf->raid_disks, 0, BIOSET_NEED_BVECS);
  1035. if (!ppl_conf->bs) {
  1036. ret = -ENOMEM;
  1037. goto err;
  1038. }
  1039. ppl_conf->count = conf->raid_disks;
  1040. ppl_conf->child_logs = kcalloc(ppl_conf->count, sizeof(struct ppl_log),
  1041. GFP_KERNEL);
  1042. if (!ppl_conf->child_logs) {
  1043. ret = -ENOMEM;
  1044. goto err;
  1045. }
  1046. atomic64_set(&ppl_conf->seq, 0);
  1047. INIT_LIST_HEAD(&ppl_conf->no_mem_stripes);
  1048. spin_lock_init(&ppl_conf->no_mem_stripes_lock);
  1049. if (!mddev->external) {
  1050. ppl_conf->signature = ~crc32c_le(~0, mddev->uuid, sizeof(mddev->uuid));
  1051. ppl_conf->block_size = 512;
  1052. } else {
  1053. ppl_conf->block_size = queue_logical_block_size(mddev->queue);
  1054. }
  1055. for (i = 0; i < ppl_conf->count; i++) {
  1056. struct ppl_log *log = &ppl_conf->child_logs[i];
  1057. struct md_rdev *rdev = conf->disks[i].rdev;
  1058. mutex_init(&log->io_mutex);
  1059. spin_lock_init(&log->io_list_lock);
  1060. INIT_LIST_HEAD(&log->io_list);
  1061. log->ppl_conf = ppl_conf;
  1062. log->rdev = rdev;
  1063. if (rdev) {
  1064. struct request_queue *q;
  1065. ret = ppl_validate_rdev(rdev);
  1066. if (ret)
  1067. goto err;
  1068. q = bdev_get_queue(rdev->bdev);
  1069. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  1070. need_cache_flush = true;
  1071. ppl_init_child_log(log, rdev);
  1072. }
  1073. }
  1074. if (need_cache_flush)
  1075. pr_warn("md/raid:%s: Volatile write-back cache should be disabled on all member drives when using PPL!\n",
  1076. mdname(mddev));
  1077. /* load and possibly recover the logs from the member disks */
  1078. ret = ppl_load(ppl_conf);
  1079. if (ret) {
  1080. goto err;
  1081. } else if (!mddev->pers &&
  1082. mddev->recovery_cp == 0 && !mddev->degraded &&
  1083. ppl_conf->recovered_entries > 0 &&
  1084. ppl_conf->mismatch_count == 0) {
  1085. /*
  1086. * If we are starting a dirty array and the recovery succeeds
  1087. * without any issues, set the array as clean.
  1088. */
  1089. mddev->recovery_cp = MaxSector;
  1090. set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
  1091. } else if (mddev->pers && ppl_conf->mismatch_count > 0) {
  1092. /* no mismatch allowed when enabling PPL for a running array */
  1093. ret = -EINVAL;
  1094. goto err;
  1095. }
  1096. conf->log_private = ppl_conf;
  1097. set_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  1098. return 0;
  1099. err:
  1100. __ppl_exit_log(ppl_conf);
  1101. return ret;
  1102. }
  1103. int ppl_modify_log(struct r5conf *conf, struct md_rdev *rdev, bool add)
  1104. {
  1105. struct ppl_conf *ppl_conf = conf->log_private;
  1106. struct ppl_log *log;
  1107. int ret = 0;
  1108. char b[BDEVNAME_SIZE];
  1109. if (!rdev)
  1110. return -EINVAL;
  1111. pr_debug("%s: disk: %d operation: %s dev: %s\n",
  1112. __func__, rdev->raid_disk, add ? "add" : "remove",
  1113. bdevname(rdev->bdev, b));
  1114. if (rdev->raid_disk < 0)
  1115. return 0;
  1116. if (rdev->raid_disk >= ppl_conf->count)
  1117. return -ENODEV;
  1118. log = &ppl_conf->child_logs[rdev->raid_disk];
  1119. mutex_lock(&log->io_mutex);
  1120. if (add) {
  1121. ret = ppl_validate_rdev(rdev);
  1122. if (!ret) {
  1123. log->rdev = rdev;
  1124. ret = ppl_write_empty_header(log);
  1125. ppl_init_child_log(log, rdev);
  1126. }
  1127. } else {
  1128. log->rdev = NULL;
  1129. }
  1130. mutex_unlock(&log->io_mutex);
  1131. return ret;
  1132. }