raid5-ppl.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. rcu_read_lock();
  503. rdev = rcu_dereference(conf->disks[i].rdev);
  504. if (rdev && !test_bit(Faulty, &rdev->flags))
  505. bdev = rdev->bdev;
  506. rcu_read_unlock();
  507. if (bdev) {
  508. struct bio *bio;
  509. bio = bio_alloc_bioset(bdev, 0,
  510. REQ_OP_WRITE | REQ_PREFLUSH,
  511. GFP_NOIO, &ppl_conf->flush_bs);
  512. bio->bi_private = io;
  513. bio->bi_end_io = ppl_flush_endio;
  514. pr_debug("%s: dev: %ps\n", __func__, bio->bi_bdev);
  515. submit_bio(bio);
  516. flushed_disks++;
  517. }
  518. }
  519. log->disk_flush_bitmap = 0;
  520. for (i = flushed_disks ; i < raid_disks; i++) {
  521. if (atomic_dec_and_test(&io->pending_flushes))
  522. ppl_io_unit_finished(io);
  523. }
  524. }
  525. static inline bool ppl_no_io_unit_submitted(struct r5conf *conf,
  526. struct ppl_log *log)
  527. {
  528. struct ppl_io_unit *io;
  529. io = list_first_entry_or_null(&log->io_list, struct ppl_io_unit,
  530. log_sibling);
  531. return !io || !io->submitted;
  532. }
  533. void ppl_quiesce(struct r5conf *conf, int quiesce)
  534. {
  535. struct ppl_conf *ppl_conf = conf->log_private;
  536. int i;
  537. if (quiesce) {
  538. for (i = 0; i < ppl_conf->count; i++) {
  539. struct ppl_log *log = &ppl_conf->child_logs[i];
  540. spin_lock_irq(&log->io_list_lock);
  541. wait_event_lock_irq(conf->wait_for_quiescent,
  542. ppl_no_io_unit_submitted(conf, log),
  543. log->io_list_lock);
  544. spin_unlock_irq(&log->io_list_lock);
  545. }
  546. }
  547. }
  548. int ppl_handle_flush_request(struct bio *bio)
  549. {
  550. if (bio->bi_iter.bi_size == 0) {
  551. bio_endio(bio);
  552. return 0;
  553. }
  554. bio->bi_opf &= ~REQ_PREFLUSH;
  555. return -EAGAIN;
  556. }
  557. void ppl_stripe_write_finished(struct stripe_head *sh)
  558. {
  559. struct ppl_io_unit *io;
  560. io = sh->ppl_io;
  561. sh->ppl_io = NULL;
  562. if (io && atomic_dec_and_test(&io->pending_stripes)) {
  563. if (io->log->disk_flush_bitmap)
  564. ppl_do_flush(io);
  565. else
  566. ppl_io_unit_finished(io);
  567. }
  568. }
  569. static void ppl_xor(int size, struct page *page1, struct page *page2)
  570. {
  571. struct async_submit_ctl submit;
  572. struct dma_async_tx_descriptor *tx;
  573. struct page *xor_srcs[] = { page1, page2 };
  574. init_async_submit(&submit, ASYNC_TX_ACK|ASYNC_TX_XOR_DROP_DST,
  575. NULL, NULL, NULL, NULL);
  576. tx = async_xor(page1, xor_srcs, 0, 2, size, &submit);
  577. async_tx_quiesce(&tx);
  578. }
  579. /*
  580. * PPL recovery strategy: xor partial parity and data from all modified data
  581. * disks within a stripe and write the result as the new stripe parity. If all
  582. * stripe data disks are modified (full stripe write), no partial parity is
  583. * available, so just xor the data disks.
  584. *
  585. * Recovery of a PPL entry shall occur only if all modified data disks are
  586. * available and read from all of them succeeds.
  587. *
  588. * A PPL entry applies to a stripe, partial parity size for an entry is at most
  589. * the size of the chunk. Examples of possible cases for a single entry:
  590. *
  591. * case 0: single data disk write:
  592. * data0 data1 data2 ppl parity
  593. * +--------+--------+--------+ +--------------------+
  594. * | ------ | ------ | ------ | +----+ | (no change) |
  595. * | ------ | -data- | ------ | | pp | -> | data1 ^ pp |
  596. * | ------ | -data- | ------ | | pp | -> | data1 ^ pp |
  597. * | ------ | ------ | ------ | +----+ | (no change) |
  598. * +--------+--------+--------+ +--------------------+
  599. * pp_size = data_size
  600. *
  601. * case 1: more than one data disk write:
  602. * data0 data1 data2 ppl parity
  603. * +--------+--------+--------+ +--------------------+
  604. * | ------ | ------ | ------ | +----+ | (no change) |
  605. * | -data- | -data- | ------ | | pp | -> | data0 ^ data1 ^ pp |
  606. * | -data- | -data- | ------ | | pp | -> | data0 ^ data1 ^ pp |
  607. * | ------ | ------ | ------ | +----+ | (no change) |
  608. * +--------+--------+--------+ +--------------------+
  609. * pp_size = data_size / modified_data_disks
  610. *
  611. * case 2: write to all data disks (also full stripe write):
  612. * data0 data1 data2 parity
  613. * +--------+--------+--------+ +--------------------+
  614. * | ------ | ------ | ------ | | (no change) |
  615. * | -data- | -data- | -data- | --------> | xor all data |
  616. * | ------ | ------ | ------ | --------> | (no change) |
  617. * | ------ | ------ | ------ | | (no change) |
  618. * +--------+--------+--------+ +--------------------+
  619. * pp_size = 0
  620. *
  621. * The following cases are possible only in other implementations. The recovery
  622. * code can handle them, but they are not generated at runtime because they can
  623. * be reduced to cases 0, 1 and 2:
  624. *
  625. * case 3:
  626. * data0 data1 data2 ppl parity
  627. * +--------+--------+--------+ +----+ +--------------------+
  628. * | ------ | -data- | -data- | | pp | | data1 ^ data2 ^ pp |
  629. * | ------ | -data- | -data- | | pp | -> | data1 ^ data2 ^ pp |
  630. * | -data- | -data- | -data- | | -- | -> | xor all data |
  631. * | -data- | -data- | ------ | | pp | | data0 ^ data1 ^ pp |
  632. * +--------+--------+--------+ +----+ +--------------------+
  633. * pp_size = chunk_size
  634. *
  635. * case 4:
  636. * data0 data1 data2 ppl parity
  637. * +--------+--------+--------+ +----+ +--------------------+
  638. * | ------ | -data- | ------ | | pp | | data1 ^ pp |
  639. * | ------ | ------ | ------ | | -- | -> | (no change) |
  640. * | ------ | ------ | ------ | | -- | -> | (no change) |
  641. * | -data- | ------ | ------ | | pp | | data0 ^ pp |
  642. * +--------+--------+--------+ +----+ +--------------------+
  643. * pp_size = chunk_size
  644. */
  645. static int ppl_recover_entry(struct ppl_log *log, struct ppl_header_entry *e,
  646. sector_t ppl_sector)
  647. {
  648. struct ppl_conf *ppl_conf = log->ppl_conf;
  649. struct mddev *mddev = ppl_conf->mddev;
  650. struct r5conf *conf = mddev->private;
  651. int block_size = ppl_conf->block_size;
  652. struct page *page1;
  653. struct page *page2;
  654. sector_t r_sector_first;
  655. sector_t r_sector_last;
  656. int strip_sectors;
  657. int data_disks;
  658. int i;
  659. int ret = 0;
  660. unsigned int pp_size = le32_to_cpu(e->pp_size);
  661. unsigned int data_size = le32_to_cpu(e->data_size);
  662. page1 = alloc_page(GFP_KERNEL);
  663. page2 = alloc_page(GFP_KERNEL);
  664. if (!page1 || !page2) {
  665. ret = -ENOMEM;
  666. goto out;
  667. }
  668. r_sector_first = le64_to_cpu(e->data_sector) * (block_size >> 9);
  669. if ((pp_size >> 9) < conf->chunk_sectors) {
  670. if (pp_size > 0) {
  671. data_disks = data_size / pp_size;
  672. strip_sectors = pp_size >> 9;
  673. } else {
  674. data_disks = conf->raid_disks - conf->max_degraded;
  675. strip_sectors = (data_size >> 9) / data_disks;
  676. }
  677. r_sector_last = r_sector_first +
  678. (data_disks - 1) * conf->chunk_sectors +
  679. strip_sectors;
  680. } else {
  681. data_disks = conf->raid_disks - conf->max_degraded;
  682. strip_sectors = conf->chunk_sectors;
  683. r_sector_last = r_sector_first + (data_size >> 9);
  684. }
  685. pr_debug("%s: array sector first: %llu last: %llu\n", __func__,
  686. (unsigned long long)r_sector_first,
  687. (unsigned long long)r_sector_last);
  688. /* if start and end is 4k aligned, use a 4k block */
  689. if (block_size == 512 &&
  690. (r_sector_first & (RAID5_STRIPE_SECTORS(conf) - 1)) == 0 &&
  691. (r_sector_last & (RAID5_STRIPE_SECTORS(conf) - 1)) == 0)
  692. block_size = RAID5_STRIPE_SIZE(conf);
  693. /* iterate through blocks in strip */
  694. for (i = 0; i < strip_sectors; i += (block_size >> 9)) {
  695. bool update_parity = false;
  696. sector_t parity_sector;
  697. struct md_rdev *parity_rdev;
  698. struct stripe_head sh;
  699. int disk;
  700. int indent = 0;
  701. pr_debug("%s:%*s iter %d start\n", __func__, indent, "", i);
  702. indent += 2;
  703. memset(page_address(page1), 0, PAGE_SIZE);
  704. /* iterate through data member disks */
  705. for (disk = 0; disk < data_disks; disk++) {
  706. int dd_idx;
  707. struct md_rdev *rdev;
  708. sector_t sector;
  709. sector_t r_sector = r_sector_first + i +
  710. (disk * conf->chunk_sectors);
  711. pr_debug("%s:%*s data member disk %d start\n",
  712. __func__, indent, "", disk);
  713. indent += 2;
  714. if (r_sector >= r_sector_last) {
  715. pr_debug("%s:%*s array sector %llu doesn't need parity update\n",
  716. __func__, indent, "",
  717. (unsigned long long)r_sector);
  718. indent -= 2;
  719. continue;
  720. }
  721. update_parity = true;
  722. /* map raid sector to member disk */
  723. sector = raid5_compute_sector(conf, r_sector, 0,
  724. &dd_idx, NULL);
  725. pr_debug("%s:%*s processing array sector %llu => data member disk %d, sector %llu\n",
  726. __func__, indent, "",
  727. (unsigned long long)r_sector, dd_idx,
  728. (unsigned long long)sector);
  729. /* Array has not started so rcu dereference is safe */
  730. rdev = rcu_dereference_protected(
  731. conf->disks[dd_idx].rdev, 1);
  732. if (!rdev || (!test_bit(In_sync, &rdev->flags) &&
  733. sector >= rdev->recovery_offset)) {
  734. pr_debug("%s:%*s data member disk %d missing\n",
  735. __func__, indent, "", dd_idx);
  736. update_parity = false;
  737. break;
  738. }
  739. pr_debug("%s:%*s reading data member disk %pg sector %llu\n",
  740. __func__, indent, "", rdev->bdev,
  741. (unsigned long long)sector);
  742. if (!sync_page_io(rdev, sector, block_size, page2,
  743. REQ_OP_READ, false)) {
  744. md_error(mddev, rdev);
  745. pr_debug("%s:%*s read failed!\n", __func__,
  746. indent, "");
  747. ret = -EIO;
  748. goto out;
  749. }
  750. ppl_xor(block_size, page1, page2);
  751. indent -= 2;
  752. }
  753. if (!update_parity)
  754. continue;
  755. if (pp_size > 0) {
  756. pr_debug("%s:%*s reading pp disk sector %llu\n",
  757. __func__, indent, "",
  758. (unsigned long long)(ppl_sector + i));
  759. if (!sync_page_io(log->rdev,
  760. ppl_sector - log->rdev->data_offset + i,
  761. block_size, page2, REQ_OP_READ,
  762. false)) {
  763. pr_debug("%s:%*s read failed!\n", __func__,
  764. indent, "");
  765. md_error(mddev, log->rdev);
  766. ret = -EIO;
  767. goto out;
  768. }
  769. ppl_xor(block_size, page1, page2);
  770. }
  771. /* map raid sector to parity disk */
  772. parity_sector = raid5_compute_sector(conf, r_sector_first + i,
  773. 0, &disk, &sh);
  774. BUG_ON(sh.pd_idx != le32_to_cpu(e->parity_disk));
  775. /* Array has not started so rcu dereference is safe */
  776. parity_rdev = rcu_dereference_protected(
  777. conf->disks[sh.pd_idx].rdev, 1);
  778. BUG_ON(parity_rdev->bdev->bd_dev != log->rdev->bdev->bd_dev);
  779. pr_debug("%s:%*s write parity at sector %llu, disk %pg\n",
  780. __func__, indent, "",
  781. (unsigned long long)parity_sector,
  782. parity_rdev->bdev);
  783. if (!sync_page_io(parity_rdev, parity_sector, block_size,
  784. page1, REQ_OP_WRITE, false)) {
  785. pr_debug("%s:%*s parity write error!\n", __func__,
  786. indent, "");
  787. md_error(mddev, parity_rdev);
  788. ret = -EIO;
  789. goto out;
  790. }
  791. }
  792. out:
  793. if (page1)
  794. __free_page(page1);
  795. if (page2)
  796. __free_page(page2);
  797. return ret;
  798. }
  799. static int ppl_recover(struct ppl_log *log, struct ppl_header *pplhdr,
  800. sector_t offset)
  801. {
  802. struct ppl_conf *ppl_conf = log->ppl_conf;
  803. struct md_rdev *rdev = log->rdev;
  804. struct mddev *mddev = rdev->mddev;
  805. sector_t ppl_sector = rdev->ppl.sector + offset +
  806. (PPL_HEADER_SIZE >> 9);
  807. struct page *page;
  808. int i;
  809. int ret = 0;
  810. page = alloc_page(GFP_KERNEL);
  811. if (!page)
  812. return -ENOMEM;
  813. /* iterate through all PPL entries saved */
  814. for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++) {
  815. struct ppl_header_entry *e = &pplhdr->entries[i];
  816. u32 pp_size = le32_to_cpu(e->pp_size);
  817. sector_t sector = ppl_sector;
  818. int ppl_entry_sectors = pp_size >> 9;
  819. u32 crc, crc_stored;
  820. pr_debug("%s: disk: %d entry: %d ppl_sector: %llu pp_size: %u\n",
  821. __func__, rdev->raid_disk, i,
  822. (unsigned long long)ppl_sector, pp_size);
  823. crc = ~0;
  824. crc_stored = le32_to_cpu(e->checksum);
  825. /* read parial parity for this entry and calculate its checksum */
  826. while (pp_size) {
  827. int s = pp_size > PAGE_SIZE ? PAGE_SIZE : pp_size;
  828. if (!sync_page_io(rdev, sector - rdev->data_offset,
  829. s, page, REQ_OP_READ, false)) {
  830. md_error(mddev, rdev);
  831. ret = -EIO;
  832. goto out;
  833. }
  834. crc = crc32c_le(crc, page_address(page), s);
  835. pp_size -= s;
  836. sector += s >> 9;
  837. }
  838. crc = ~crc;
  839. if (crc != crc_stored) {
  840. /*
  841. * Don't recover this entry if the checksum does not
  842. * match, but keep going and try to recover other
  843. * entries.
  844. */
  845. pr_debug("%s: ppl entry crc does not match: stored: 0x%x calculated: 0x%x\n",
  846. __func__, crc_stored, crc);
  847. ppl_conf->mismatch_count++;
  848. } else {
  849. ret = ppl_recover_entry(log, e, ppl_sector);
  850. if (ret)
  851. goto out;
  852. ppl_conf->recovered_entries++;
  853. }
  854. ppl_sector += ppl_entry_sectors;
  855. }
  856. /* flush the disk cache after recovery if necessary */
  857. ret = blkdev_issue_flush(rdev->bdev);
  858. out:
  859. __free_page(page);
  860. return ret;
  861. }
  862. static int ppl_write_empty_header(struct ppl_log *log)
  863. {
  864. struct page *page;
  865. struct ppl_header *pplhdr;
  866. struct md_rdev *rdev = log->rdev;
  867. int ret = 0;
  868. pr_debug("%s: disk: %d ppl_sector: %llu\n", __func__,
  869. rdev->raid_disk, (unsigned long long)rdev->ppl.sector);
  870. page = alloc_page(GFP_NOIO | __GFP_ZERO);
  871. if (!page)
  872. return -ENOMEM;
  873. pplhdr = page_address(page);
  874. /* zero out PPL space to avoid collision with old PPLs */
  875. blkdev_issue_zeroout(rdev->bdev, rdev->ppl.sector,
  876. log->rdev->ppl.size, GFP_NOIO, 0);
  877. memset(pplhdr->reserved, 0xff, PPL_HDR_RESERVED);
  878. pplhdr->signature = cpu_to_le32(log->ppl_conf->signature);
  879. pplhdr->checksum = cpu_to_le32(~crc32c_le(~0, pplhdr, PAGE_SIZE));
  880. if (!sync_page_io(rdev, rdev->ppl.sector - rdev->data_offset,
  881. PPL_HEADER_SIZE, page, REQ_OP_WRITE | REQ_SYNC |
  882. REQ_FUA, false)) {
  883. md_error(rdev->mddev, rdev);
  884. ret = -EIO;
  885. }
  886. __free_page(page);
  887. return ret;
  888. }
  889. static int ppl_load_distributed(struct ppl_log *log)
  890. {
  891. struct ppl_conf *ppl_conf = log->ppl_conf;
  892. struct md_rdev *rdev = log->rdev;
  893. struct mddev *mddev = rdev->mddev;
  894. struct page *page, *page2;
  895. struct ppl_header *pplhdr = NULL, *prev_pplhdr = NULL;
  896. u32 crc, crc_stored;
  897. u32 signature;
  898. int ret = 0, i;
  899. sector_t pplhdr_offset = 0, prev_pplhdr_offset = 0;
  900. pr_debug("%s: disk: %d\n", __func__, rdev->raid_disk);
  901. /* read PPL headers, find the recent one */
  902. page = alloc_page(GFP_KERNEL);
  903. if (!page)
  904. return -ENOMEM;
  905. page2 = alloc_page(GFP_KERNEL);
  906. if (!page2) {
  907. __free_page(page);
  908. return -ENOMEM;
  909. }
  910. /* searching ppl area for latest ppl */
  911. while (pplhdr_offset < rdev->ppl.size - (PPL_HEADER_SIZE >> 9)) {
  912. if (!sync_page_io(rdev,
  913. rdev->ppl.sector - rdev->data_offset +
  914. pplhdr_offset, PAGE_SIZE, page, REQ_OP_READ,
  915. false)) {
  916. md_error(mddev, rdev);
  917. ret = -EIO;
  918. /* if not able to read - don't recover any PPL */
  919. pplhdr = NULL;
  920. break;
  921. }
  922. pplhdr = page_address(page);
  923. /* check header validity */
  924. crc_stored = le32_to_cpu(pplhdr->checksum);
  925. pplhdr->checksum = 0;
  926. crc = ~crc32c_le(~0, pplhdr, PAGE_SIZE);
  927. if (crc_stored != crc) {
  928. pr_debug("%s: ppl header crc does not match: stored: 0x%x calculated: 0x%x (offset: %llu)\n",
  929. __func__, crc_stored, crc,
  930. (unsigned long long)pplhdr_offset);
  931. pplhdr = prev_pplhdr;
  932. pplhdr_offset = prev_pplhdr_offset;
  933. break;
  934. }
  935. signature = le32_to_cpu(pplhdr->signature);
  936. if (mddev->external) {
  937. /*
  938. * For external metadata the header signature is set and
  939. * validated in userspace.
  940. */
  941. ppl_conf->signature = signature;
  942. } else if (ppl_conf->signature != signature) {
  943. pr_debug("%s: ppl header signature does not match: stored: 0x%x configured: 0x%x (offset: %llu)\n",
  944. __func__, signature, ppl_conf->signature,
  945. (unsigned long long)pplhdr_offset);
  946. pplhdr = prev_pplhdr;
  947. pplhdr_offset = prev_pplhdr_offset;
  948. break;
  949. }
  950. if (prev_pplhdr && le64_to_cpu(prev_pplhdr->generation) >
  951. le64_to_cpu(pplhdr->generation)) {
  952. /* previous was newest */
  953. pplhdr = prev_pplhdr;
  954. pplhdr_offset = prev_pplhdr_offset;
  955. break;
  956. }
  957. prev_pplhdr_offset = pplhdr_offset;
  958. prev_pplhdr = pplhdr;
  959. swap(page, page2);
  960. /* calculate next potential ppl offset */
  961. for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++)
  962. pplhdr_offset +=
  963. le32_to_cpu(pplhdr->entries[i].pp_size) >> 9;
  964. pplhdr_offset += PPL_HEADER_SIZE >> 9;
  965. }
  966. /* no valid ppl found */
  967. if (!pplhdr)
  968. ppl_conf->mismatch_count++;
  969. else
  970. pr_debug("%s: latest PPL found at offset: %llu, with generation: %llu\n",
  971. __func__, (unsigned long long)pplhdr_offset,
  972. le64_to_cpu(pplhdr->generation));
  973. /* attempt to recover from log if we are starting a dirty array */
  974. if (pplhdr && !mddev->pers && mddev->recovery_cp != MaxSector)
  975. ret = ppl_recover(log, pplhdr, pplhdr_offset);
  976. /* write empty header if we are starting the array */
  977. if (!ret && !mddev->pers)
  978. ret = ppl_write_empty_header(log);
  979. __free_page(page);
  980. __free_page(page2);
  981. pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
  982. __func__, ret, ppl_conf->mismatch_count,
  983. ppl_conf->recovered_entries);
  984. return ret;
  985. }
  986. static int ppl_load(struct ppl_conf *ppl_conf)
  987. {
  988. int ret = 0;
  989. u32 signature = 0;
  990. bool signature_set = false;
  991. int i;
  992. for (i = 0; i < ppl_conf->count; i++) {
  993. struct ppl_log *log = &ppl_conf->child_logs[i];
  994. /* skip missing drive */
  995. if (!log->rdev)
  996. continue;
  997. ret = ppl_load_distributed(log);
  998. if (ret)
  999. break;
  1000. /*
  1001. * For external metadata we can't check if the signature is
  1002. * correct on a single drive, but we can check if it is the same
  1003. * on all drives.
  1004. */
  1005. if (ppl_conf->mddev->external) {
  1006. if (!signature_set) {
  1007. signature = ppl_conf->signature;
  1008. signature_set = true;
  1009. } else if (signature != ppl_conf->signature) {
  1010. pr_warn("md/raid:%s: PPL header signature does not match on all member drives\n",
  1011. mdname(ppl_conf->mddev));
  1012. ret = -EINVAL;
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
  1018. __func__, ret, ppl_conf->mismatch_count,
  1019. ppl_conf->recovered_entries);
  1020. return ret;
  1021. }
  1022. static void __ppl_exit_log(struct ppl_conf *ppl_conf)
  1023. {
  1024. clear_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  1025. clear_bit(MD_HAS_MULTIPLE_PPLS, &ppl_conf->mddev->flags);
  1026. kfree(ppl_conf->child_logs);
  1027. bioset_exit(&ppl_conf->bs);
  1028. bioset_exit(&ppl_conf->flush_bs);
  1029. mempool_exit(&ppl_conf->io_pool);
  1030. kmem_cache_destroy(ppl_conf->io_kc);
  1031. kfree(ppl_conf);
  1032. }
  1033. void ppl_exit_log(struct r5conf *conf)
  1034. {
  1035. struct ppl_conf *ppl_conf = conf->log_private;
  1036. if (ppl_conf) {
  1037. __ppl_exit_log(ppl_conf);
  1038. conf->log_private = NULL;
  1039. }
  1040. }
  1041. static int ppl_validate_rdev(struct md_rdev *rdev)
  1042. {
  1043. int ppl_data_sectors;
  1044. int ppl_size_new;
  1045. /*
  1046. * The configured PPL size must be enough to store
  1047. * the header and (at the very least) partial parity
  1048. * for one stripe. Round it down to ensure the data
  1049. * space is cleanly divisible by stripe size.
  1050. */
  1051. ppl_data_sectors = rdev->ppl.size - (PPL_HEADER_SIZE >> 9);
  1052. if (ppl_data_sectors > 0)
  1053. ppl_data_sectors = rounddown(ppl_data_sectors,
  1054. RAID5_STRIPE_SECTORS((struct r5conf *)rdev->mddev->private));
  1055. if (ppl_data_sectors <= 0) {
  1056. pr_warn("md/raid:%s: PPL space too small on %pg\n",
  1057. mdname(rdev->mddev), rdev->bdev);
  1058. return -ENOSPC;
  1059. }
  1060. ppl_size_new = ppl_data_sectors + (PPL_HEADER_SIZE >> 9);
  1061. if ((rdev->ppl.sector < rdev->data_offset &&
  1062. rdev->ppl.sector + ppl_size_new > rdev->data_offset) ||
  1063. (rdev->ppl.sector >= rdev->data_offset &&
  1064. rdev->data_offset + rdev->sectors > rdev->ppl.sector)) {
  1065. pr_warn("md/raid:%s: PPL space overlaps with data on %pg\n",
  1066. mdname(rdev->mddev), rdev->bdev);
  1067. return -EINVAL;
  1068. }
  1069. if (!rdev->mddev->external &&
  1070. ((rdev->ppl.offset > 0 && rdev->ppl.offset < (rdev->sb_size >> 9)) ||
  1071. (rdev->ppl.offset <= 0 && rdev->ppl.offset + ppl_size_new > 0))) {
  1072. pr_warn("md/raid:%s: PPL space overlaps with superblock on %pg\n",
  1073. mdname(rdev->mddev), rdev->bdev);
  1074. return -EINVAL;
  1075. }
  1076. rdev->ppl.size = ppl_size_new;
  1077. return 0;
  1078. }
  1079. static void ppl_init_child_log(struct ppl_log *log, struct md_rdev *rdev)
  1080. {
  1081. struct request_queue *q;
  1082. if ((rdev->ppl.size << 9) >= (PPL_SPACE_SIZE +
  1083. PPL_HEADER_SIZE) * 2) {
  1084. log->use_multippl = true;
  1085. set_bit(MD_HAS_MULTIPLE_PPLS,
  1086. &log->ppl_conf->mddev->flags);
  1087. log->entry_space = PPL_SPACE_SIZE;
  1088. } else {
  1089. log->use_multippl = false;
  1090. log->entry_space = (log->rdev->ppl.size << 9) -
  1091. PPL_HEADER_SIZE;
  1092. }
  1093. log->next_io_sector = rdev->ppl.sector;
  1094. q = bdev_get_queue(rdev->bdev);
  1095. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  1096. log->wb_cache_on = true;
  1097. }
  1098. int ppl_init_log(struct r5conf *conf)
  1099. {
  1100. struct ppl_conf *ppl_conf;
  1101. struct mddev *mddev = conf->mddev;
  1102. int ret = 0;
  1103. int max_disks;
  1104. int i;
  1105. pr_debug("md/raid:%s: enabling distributed Partial Parity Log\n",
  1106. mdname(conf->mddev));
  1107. if (PAGE_SIZE != 4096)
  1108. return -EINVAL;
  1109. if (mddev->level != 5) {
  1110. pr_warn("md/raid:%s PPL is not compatible with raid level %d\n",
  1111. mdname(mddev), mddev->level);
  1112. return -EINVAL;
  1113. }
  1114. if (mddev->bitmap_info.file || mddev->bitmap_info.offset) {
  1115. pr_warn("md/raid:%s PPL is not compatible with bitmap\n",
  1116. mdname(mddev));
  1117. return -EINVAL;
  1118. }
  1119. if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
  1120. pr_warn("md/raid:%s PPL is not compatible with journal\n",
  1121. mdname(mddev));
  1122. return -EINVAL;
  1123. }
  1124. max_disks = sizeof_field(struct ppl_log, disk_flush_bitmap) *
  1125. BITS_PER_BYTE;
  1126. if (conf->raid_disks > max_disks) {
  1127. pr_warn("md/raid:%s PPL doesn't support over %d disks in the array\n",
  1128. mdname(mddev), max_disks);
  1129. return -EINVAL;
  1130. }
  1131. ppl_conf = kzalloc(sizeof(struct ppl_conf), GFP_KERNEL);
  1132. if (!ppl_conf)
  1133. return -ENOMEM;
  1134. ppl_conf->mddev = mddev;
  1135. ppl_conf->io_kc = KMEM_CACHE(ppl_io_unit, 0);
  1136. if (!ppl_conf->io_kc) {
  1137. ret = -ENOMEM;
  1138. goto err;
  1139. }
  1140. ret = mempool_init(&ppl_conf->io_pool, conf->raid_disks, ppl_io_pool_alloc,
  1141. ppl_io_pool_free, ppl_conf->io_kc);
  1142. if (ret)
  1143. goto err;
  1144. ret = bioset_init(&ppl_conf->bs, conf->raid_disks, 0, BIOSET_NEED_BVECS);
  1145. if (ret)
  1146. goto err;
  1147. ret = bioset_init(&ppl_conf->flush_bs, conf->raid_disks, 0, 0);
  1148. if (ret)
  1149. goto err;
  1150. ppl_conf->count = conf->raid_disks;
  1151. ppl_conf->child_logs = kcalloc(ppl_conf->count, sizeof(struct ppl_log),
  1152. GFP_KERNEL);
  1153. if (!ppl_conf->child_logs) {
  1154. ret = -ENOMEM;
  1155. goto err;
  1156. }
  1157. atomic64_set(&ppl_conf->seq, 0);
  1158. INIT_LIST_HEAD(&ppl_conf->no_mem_stripes);
  1159. spin_lock_init(&ppl_conf->no_mem_stripes_lock);
  1160. if (!mddev->external) {
  1161. ppl_conf->signature = ~crc32c_le(~0, mddev->uuid, sizeof(mddev->uuid));
  1162. ppl_conf->block_size = 512;
  1163. } else {
  1164. ppl_conf->block_size = queue_logical_block_size(mddev->queue);
  1165. }
  1166. for (i = 0; i < ppl_conf->count; i++) {
  1167. struct ppl_log *log = &ppl_conf->child_logs[i];
  1168. /* Array has not started so rcu dereference is safe */
  1169. struct md_rdev *rdev =
  1170. rcu_dereference_protected(conf->disks[i].rdev, 1);
  1171. mutex_init(&log->io_mutex);
  1172. spin_lock_init(&log->io_list_lock);
  1173. INIT_LIST_HEAD(&log->io_list);
  1174. log->ppl_conf = ppl_conf;
  1175. log->rdev = rdev;
  1176. if (rdev) {
  1177. ret = ppl_validate_rdev(rdev);
  1178. if (ret)
  1179. goto err;
  1180. ppl_init_child_log(log, rdev);
  1181. }
  1182. }
  1183. /* load and possibly recover the logs from the member disks */
  1184. ret = ppl_load(ppl_conf);
  1185. if (ret) {
  1186. goto err;
  1187. } else if (!mddev->pers && mddev->recovery_cp == 0 &&
  1188. ppl_conf->recovered_entries > 0 &&
  1189. ppl_conf->mismatch_count == 0) {
  1190. /*
  1191. * If we are starting a dirty array and the recovery succeeds
  1192. * without any issues, set the array as clean.
  1193. */
  1194. mddev->recovery_cp = MaxSector;
  1195. set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
  1196. } else if (mddev->pers && ppl_conf->mismatch_count > 0) {
  1197. /* no mismatch allowed when enabling PPL for a running array */
  1198. ret = -EINVAL;
  1199. goto err;
  1200. }
  1201. conf->log_private = ppl_conf;
  1202. set_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
  1203. return 0;
  1204. err:
  1205. __ppl_exit_log(ppl_conf);
  1206. return ret;
  1207. }
  1208. int ppl_modify_log(struct r5conf *conf, struct md_rdev *rdev, bool add)
  1209. {
  1210. struct ppl_conf *ppl_conf = conf->log_private;
  1211. struct ppl_log *log;
  1212. int ret = 0;
  1213. if (!rdev)
  1214. return -EINVAL;
  1215. pr_debug("%s: disk: %d operation: %s dev: %pg\n",
  1216. __func__, rdev->raid_disk, add ? "add" : "remove",
  1217. rdev->bdev);
  1218. if (rdev->raid_disk < 0)
  1219. return 0;
  1220. if (rdev->raid_disk >= ppl_conf->count)
  1221. return -ENODEV;
  1222. log = &ppl_conf->child_logs[rdev->raid_disk];
  1223. mutex_lock(&log->io_mutex);
  1224. if (add) {
  1225. ret = ppl_validate_rdev(rdev);
  1226. if (!ret) {
  1227. log->rdev = rdev;
  1228. ret = ppl_write_empty_header(log);
  1229. ppl_init_child_log(log, rdev);
  1230. }
  1231. } else {
  1232. log->rdev = NULL;
  1233. }
  1234. mutex_unlock(&log->io_mutex);
  1235. return ret;
  1236. }
  1237. static ssize_t
  1238. ppl_write_hint_show(struct mddev *mddev, char *buf)
  1239. {
  1240. return sprintf(buf, "%d\n", 0);
  1241. }
  1242. static ssize_t
  1243. ppl_write_hint_store(struct mddev *mddev, const char *page, size_t len)
  1244. {
  1245. struct r5conf *conf;
  1246. int err = 0;
  1247. unsigned short new;
  1248. if (len >= PAGE_SIZE)
  1249. return -EINVAL;
  1250. if (kstrtou16(page, 10, &new))
  1251. return -EINVAL;
  1252. err = mddev_lock(mddev);
  1253. if (err)
  1254. return err;
  1255. conf = mddev->private;
  1256. if (!conf)
  1257. err = -ENODEV;
  1258. else if (!raid5_has_ppl(conf) || !conf->log_private)
  1259. err = -EINVAL;
  1260. mddev_unlock(mddev);
  1261. return err ?: len;
  1262. }
  1263. struct md_sysfs_entry
  1264. ppl_write_hint = __ATTR(ppl_write_hint, S_IRUGO | S_IWUSR,
  1265. ppl_write_hint_show,
  1266. ppl_write_hint_store);