raid5-ppl.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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. if (ppl_conf->bs)
  1041. bioset_free(ppl_conf->bs);
  1042. if (ppl_conf->flush_bs)
  1043. bioset_free(ppl_conf->flush_bs);
  1044. mempool_destroy(ppl_conf->io_pool);
  1045. kmem_cache_destroy(ppl_conf->io_kc);
  1046. kfree(ppl_conf);
  1047. }
  1048. void ppl_exit_log(struct r5conf *conf)
  1049. {
  1050. struct ppl_conf *ppl_conf = conf->log_private;
  1051. if (ppl_conf) {
  1052. __ppl_exit_log(ppl_conf);
  1053. conf->log_private = NULL;
  1054. }
  1055. }
  1056. static int ppl_validate_rdev(struct md_rdev *rdev)
  1057. {
  1058. char b[BDEVNAME_SIZE];
  1059. int ppl_data_sectors;
  1060. int ppl_size_new;
  1061. /*
  1062. * The configured PPL size must be enough to store
  1063. * the header and (at the very least) partial parity
  1064. * for one stripe. Round it down to ensure the data
  1065. * space is cleanly divisible by stripe size.
  1066. */
  1067. ppl_data_sectors = rdev->ppl.size - (PPL_HEADER_SIZE >> 9);
  1068. if (ppl_data_sectors > 0)
  1069. ppl_data_sectors = rounddown(ppl_data_sectors, STRIPE_SECTORS);
  1070. if (ppl_data_sectors <= 0) {
  1071. pr_warn("md/raid:%s: PPL space too small on %s\n",
  1072. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  1073. return -ENOSPC;
  1074. }
  1075. ppl_size_new = ppl_data_sectors + (PPL_HEADER_SIZE >> 9);
  1076. if ((rdev->ppl.sector < rdev->data_offset &&
  1077. rdev->ppl.sector + ppl_size_new > rdev->data_offset) ||
  1078. (rdev->ppl.sector >= rdev->data_offset &&
  1079. rdev->data_offset + rdev->sectors > rdev->ppl.sector)) {
  1080. pr_warn("md/raid:%s: PPL space overlaps with data on %s\n",
  1081. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  1082. return -EINVAL;
  1083. }
  1084. if (!rdev->mddev->external &&
  1085. ((rdev->ppl.offset > 0 && rdev->ppl.offset < (rdev->sb_size >> 9)) ||
  1086. (rdev->ppl.offset <= 0 && rdev->ppl.offset + ppl_size_new > 0))) {
  1087. pr_warn("md/raid:%s: PPL space overlaps with superblock on %s\n",
  1088. mdname(rdev->mddev), bdevname(rdev->bdev, b));
  1089. return -EINVAL;
  1090. }
  1091. rdev->ppl.size = ppl_size_new;
  1092. return 0;
  1093. }
  1094. static void ppl_init_child_log(struct ppl_log *log, struct md_rdev *rdev)
  1095. {
  1096. struct request_queue *q;
  1097. if ((rdev->ppl.size << 9) >= (PPL_SPACE_SIZE +
  1098. PPL_HEADER_SIZE) * 2) {
  1099. log->use_multippl = true;
  1100. set_bit(MD_HAS_MULTIPLE_PPLS,
  1101. &log->ppl_conf->mddev->flags);
  1102. log->entry_space = PPL_SPACE_SIZE;
  1103. } else {
  1104. log->use_multippl = false;
  1105. log->entry_space = (log->rdev->ppl.size << 9) -
  1106. PPL_HEADER_SIZE;
  1107. }
  1108. log->next_io_sector = rdev->ppl.sector;
  1109. q = bdev_get_queue(rdev->bdev);
  1110. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  1111. log->wb_cache_on = true;
  1112. }
  1113. int ppl_init_log(struct r5conf *conf)
  1114. {
  1115. struct ppl_conf *ppl_conf;
  1116. struct mddev *mddev = conf->mddev;
  1117. int ret = 0;
  1118. int max_disks;
  1119. int i;
  1120. pr_debug("md/raid:%s: enabling distributed Partial Parity Log\n",
  1121. mdname(conf->mddev));
  1122. if (PAGE_SIZE != 4096)
  1123. return -EINVAL;
  1124. if (mddev->level != 5) {
  1125. pr_warn("md/raid:%s PPL is not compatible with raid level %d\n",
  1126. mdname(mddev), mddev->level);
  1127. return -EINVAL;
  1128. }
  1129. if (mddev->bitmap_info.file || mddev->bitmap_info.offset) {
  1130. pr_warn("md/raid:%s PPL is not compatible with bitmap\n",
  1131. mdname(mddev));
  1132. return -EINVAL;
  1133. }
  1134. if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
  1135. pr_warn("md/raid:%s PPL is not compatible with journal\n",
  1136. mdname(mddev));
  1137. return -EINVAL;
  1138. }
  1139. max_disks = FIELD_SIZEOF(struct ppl_log, disk_flush_bitmap) *
  1140. BITS_PER_BYTE;
  1141. if (conf->raid_disks > max_disks) {
  1142. pr_warn("md/raid:%s PPL doesn't support over %d disks in the array\n",
  1143. mdname(mddev), max_disks);
  1144. return -EINVAL;
  1145. }
  1146. ppl_conf = kzalloc(sizeof(struct ppl_conf), GFP_KERNEL);
  1147. if (!ppl_conf)
  1148. return -ENOMEM;
  1149. ppl_conf->mddev = mddev;
  1150. ppl_conf->io_kc = KMEM_CACHE(ppl_io_unit, 0);
  1151. if (!ppl_conf->io_kc) {
  1152. ret = -ENOMEM;
  1153. goto err;
  1154. }
  1155. ppl_conf->io_pool = mempool_create(conf->raid_disks, ppl_io_pool_alloc,
  1156. ppl_io_pool_free, ppl_conf->io_kc);
  1157. if (!ppl_conf->io_pool) {
  1158. ret = -ENOMEM;
  1159. goto err;
  1160. }
  1161. ppl_conf->bs = bioset_create(conf->raid_disks, 0, BIOSET_NEED_BVECS);
  1162. if (!ppl_conf->bs) {
  1163. ret = -ENOMEM;
  1164. goto err;
  1165. }
  1166. ppl_conf->flush_bs = bioset_create(conf->raid_disks, 0, 0);
  1167. if (!ppl_conf->flush_bs) {
  1168. ret = -ENOMEM;
  1169. goto err;
  1170. }
  1171. ppl_conf->count = conf->raid_disks;
  1172. ppl_conf->child_logs = kcalloc(ppl_conf->count, sizeof(struct ppl_log),
  1173. GFP_KERNEL);
  1174. if (!ppl_conf->child_logs) {
  1175. ret = -ENOMEM;
  1176. goto err;
  1177. }
  1178. atomic64_set(&ppl_conf->seq, 0);
  1179. INIT_LIST_HEAD(&ppl_conf->no_mem_stripes);
  1180. spin_lock_init(&ppl_conf->no_mem_stripes_lock);
  1181. if (!mddev->external) {
  1182. ppl_conf->signature = ~crc32c_le(~0, mddev->uuid, sizeof(mddev->uuid));
  1183. ppl_conf->block_size = 512;
  1184. } else {
  1185. ppl_conf->block_size = queue_logical_block_size(mddev->queue);
  1186. }
  1187. for (i = 0; i < ppl_conf->count; i++) {
  1188. struct ppl_log *log = &ppl_conf->child_logs[i];
  1189. struct md_rdev *rdev = conf->disks[i].rdev;
  1190. mutex_init(&log->io_mutex);
  1191. spin_lock_init(&log->io_list_lock);
  1192. INIT_LIST_HEAD(&log->io_list);
  1193. log->ppl_conf = ppl_conf;
  1194. log->rdev = rdev;
  1195. if (rdev) {
  1196. ret = ppl_validate_rdev(rdev);
  1197. if (ret)
  1198. goto err;
  1199. ppl_init_child_log(log, rdev);
  1200. }
  1201. }
  1202. /* load and possibly recover the logs from the member disks */
  1203. ret = ppl_load(ppl_conf);
  1204. if (ret) {
  1205. goto err;
  1206. } else if (!mddev->pers && mddev->recovery_cp == 0 &&
  1207. ppl_conf->recovered_entries > 0 &&
  1208. ppl_conf->mismatch_count == 0) {
  1209. /*
  1210. * If we are starting a dirty array and the recovery succeeds
  1211. * without any issues, set the array as clean.
  1212. */
  1213. mddev->recovery_cp = MaxSector;
  1214. set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
  1215. } else if (mddev->pers && ppl_conf->mismatch_count > 0) {
  1216. /* no mismatch allowed when enabling PPL for a running array */
  1217. ret = -EINVAL;
  1218. goto err;
  1219. }
  1220. conf->log_private = ppl_conf;
  1221. set_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  1222. return 0;
  1223. err:
  1224. __ppl_exit_log(ppl_conf);
  1225. return ret;
  1226. }
  1227. int ppl_modify_log(struct r5conf *conf, struct md_rdev *rdev, bool add)
  1228. {
  1229. struct ppl_conf *ppl_conf = conf->log_private;
  1230. struct ppl_log *log;
  1231. int ret = 0;
  1232. char b[BDEVNAME_SIZE];
  1233. if (!rdev)
  1234. return -EINVAL;
  1235. pr_debug("%s: disk: %d operation: %s dev: %s\n",
  1236. __func__, rdev->raid_disk, add ? "add" : "remove",
  1237. bdevname(rdev->bdev, b));
  1238. if (rdev->raid_disk < 0)
  1239. return 0;
  1240. if (rdev->raid_disk >= ppl_conf->count)
  1241. return -ENODEV;
  1242. log = &ppl_conf->child_logs[rdev->raid_disk];
  1243. mutex_lock(&log->io_mutex);
  1244. if (add) {
  1245. ret = ppl_validate_rdev(rdev);
  1246. if (!ret) {
  1247. log->rdev = rdev;
  1248. ret = ppl_write_empty_header(log);
  1249. ppl_init_child_log(log, rdev);
  1250. }
  1251. } else {
  1252. log->rdev = NULL;
  1253. }
  1254. mutex_unlock(&log->io_mutex);
  1255. return ret;
  1256. }