raid5-ppl.c 41 KB

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