raid5-ppl.c 42 KB

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