dm-raid.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /*
  2. * Copyright (C) 2010-2011 Neil Brown
  3. * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include "md.h"
  10. #include "raid1.h"
  11. #include "raid5.h"
  12. #include "raid10.h"
  13. #include "bitmap.h"
  14. #include <linux/device-mapper.h>
  15. #define DM_MSG_PREFIX "raid"
  16. #define MAX_RAID_DEVICES 253 /* raid4/5/6 limit */
  17. static bool devices_handle_discard_safely = false;
  18. /*
  19. * The following flags are used by dm-raid.c to set up the array state.
  20. * They must be cleared before md_run is called.
  21. */
  22. #define FirstUse 10 /* rdev flag */
  23. struct raid_dev {
  24. /*
  25. * Two DM devices, one to hold metadata and one to hold the
  26. * actual data/parity. The reason for this is to not confuse
  27. * ti->len and give more flexibility in altering size and
  28. * characteristics.
  29. *
  30. * While it is possible for this device to be associated
  31. * with a different physical device than the data_dev, it
  32. * is intended for it to be the same.
  33. * |--------- Physical Device ---------|
  34. * |- meta_dev -|------ data_dev ------|
  35. */
  36. struct dm_dev *meta_dev;
  37. struct dm_dev *data_dev;
  38. struct md_rdev rdev;
  39. };
  40. /*
  41. * Flags for rs->ctr_flags field.
  42. */
  43. #define CTR_FLAG_SYNC 0x1
  44. #define CTR_FLAG_NOSYNC 0x2
  45. #define CTR_FLAG_REBUILD 0x4
  46. #define CTR_FLAG_DAEMON_SLEEP 0x8
  47. #define CTR_FLAG_MIN_RECOVERY_RATE 0x10
  48. #define CTR_FLAG_MAX_RECOVERY_RATE 0x20
  49. #define CTR_FLAG_MAX_WRITE_BEHIND 0x40
  50. #define CTR_FLAG_STRIPE_CACHE 0x80
  51. #define CTR_FLAG_REGION_SIZE 0x100
  52. #define CTR_FLAG_RAID10_COPIES 0x200
  53. #define CTR_FLAG_RAID10_FORMAT 0x400
  54. struct raid_set {
  55. struct dm_target *ti;
  56. uint32_t bitmap_loaded;
  57. uint32_t ctr_flags;
  58. struct mddev md;
  59. struct raid_type *raid_type;
  60. struct dm_target_callbacks callbacks;
  61. struct raid_dev dev[0];
  62. };
  63. /* Supported raid types and properties. */
  64. static struct raid_type {
  65. const char *name; /* RAID algorithm. */
  66. const char *descr; /* Descriptor text for logging. */
  67. const unsigned parity_devs; /* # of parity devices. */
  68. const unsigned minimal_devs; /* minimal # of devices in set. */
  69. const unsigned level; /* RAID level. */
  70. const unsigned algorithm; /* RAID algorithm. */
  71. } raid_types[] = {
  72. {"raid0", "RAID0 (striping)", 0, 2, 0, 0 /* NONE */},
  73. {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
  74. {"raid10", "RAID10 (striped mirrors)", 0, 2, 10, UINT_MAX /* Varies */},
  75. {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
  76. {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
  77. {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
  78. {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
  79. {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
  80. {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
  81. {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
  82. {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
  83. };
  84. static char *raid10_md_layout_to_format(int layout)
  85. {
  86. /*
  87. * Bit 16 and 17 stand for "offset" and "use_far_sets"
  88. * Refer to MD's raid10.c for details
  89. */
  90. if ((layout & 0x10000) && (layout & 0x20000))
  91. return "offset";
  92. if ((layout & 0xFF) > 1)
  93. return "near";
  94. return "far";
  95. }
  96. static unsigned raid10_md_layout_to_copies(int layout)
  97. {
  98. if ((layout & 0xFF) > 1)
  99. return layout & 0xFF;
  100. return (layout >> 8) & 0xFF;
  101. }
  102. static int raid10_format_to_md_layout(char *format, unsigned copies)
  103. {
  104. unsigned n = 1, f = 1;
  105. if (!strcasecmp("near", format))
  106. n = copies;
  107. else
  108. f = copies;
  109. if (!strcasecmp("offset", format))
  110. return 0x30000 | (f << 8) | n;
  111. if (!strcasecmp("far", format))
  112. return 0x20000 | (f << 8) | n;
  113. return (f << 8) | n;
  114. }
  115. static struct raid_type *get_raid_type(char *name)
  116. {
  117. int i;
  118. for (i = 0; i < ARRAY_SIZE(raid_types); i++)
  119. if (!strcmp(raid_types[i].name, name))
  120. return &raid_types[i];
  121. return NULL;
  122. }
  123. static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
  124. {
  125. unsigned i;
  126. struct raid_set *rs;
  127. if (raid_devs <= raid_type->parity_devs) {
  128. ti->error = "Insufficient number of devices";
  129. return ERR_PTR(-EINVAL);
  130. }
  131. rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
  132. if (!rs) {
  133. ti->error = "Cannot allocate raid context";
  134. return ERR_PTR(-ENOMEM);
  135. }
  136. mddev_init(&rs->md);
  137. rs->ti = ti;
  138. rs->raid_type = raid_type;
  139. rs->md.raid_disks = raid_devs;
  140. rs->md.level = raid_type->level;
  141. rs->md.new_level = rs->md.level;
  142. rs->md.layout = raid_type->algorithm;
  143. rs->md.new_layout = rs->md.layout;
  144. rs->md.delta_disks = 0;
  145. rs->md.recovery_cp = 0;
  146. for (i = 0; i < raid_devs; i++)
  147. md_rdev_init(&rs->dev[i].rdev);
  148. /*
  149. * Remaining items to be initialized by further RAID params:
  150. * rs->md.persistent
  151. * rs->md.external
  152. * rs->md.chunk_sectors
  153. * rs->md.new_chunk_sectors
  154. * rs->md.dev_sectors
  155. */
  156. return rs;
  157. }
  158. static void context_free(struct raid_set *rs)
  159. {
  160. int i;
  161. for (i = 0; i < rs->md.raid_disks; i++) {
  162. if (rs->dev[i].meta_dev)
  163. dm_put_device(rs->ti, rs->dev[i].meta_dev);
  164. md_rdev_clear(&rs->dev[i].rdev);
  165. if (rs->dev[i].data_dev)
  166. dm_put_device(rs->ti, rs->dev[i].data_dev);
  167. }
  168. kfree(rs);
  169. }
  170. /*
  171. * For every device we have two words
  172. * <meta_dev>: meta device name or '-' if missing
  173. * <data_dev>: data device name or '-' if missing
  174. *
  175. * The following are permitted:
  176. * - -
  177. * - <data_dev>
  178. * <meta_dev> <data_dev>
  179. *
  180. * The following is not allowed:
  181. * <meta_dev> -
  182. *
  183. * This code parses those words. If there is a failure,
  184. * the caller must use context_free to unwind the operations.
  185. */
  186. static int dev_parms(struct raid_set *rs, char **argv)
  187. {
  188. int i;
  189. int rebuild = 0;
  190. int metadata_available = 0;
  191. int ret = 0;
  192. for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
  193. rs->dev[i].rdev.raid_disk = i;
  194. rs->dev[i].meta_dev = NULL;
  195. rs->dev[i].data_dev = NULL;
  196. /*
  197. * There are no offsets, since there is a separate device
  198. * for data and metadata.
  199. */
  200. rs->dev[i].rdev.data_offset = 0;
  201. rs->dev[i].rdev.mddev = &rs->md;
  202. if (strcmp(argv[0], "-")) {
  203. ret = dm_get_device(rs->ti, argv[0],
  204. dm_table_get_mode(rs->ti->table),
  205. &rs->dev[i].meta_dev);
  206. rs->ti->error = "RAID metadata device lookup failure";
  207. if (ret)
  208. return ret;
  209. rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
  210. if (!rs->dev[i].rdev.sb_page)
  211. return -ENOMEM;
  212. }
  213. if (!strcmp(argv[1], "-")) {
  214. if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
  215. (!rs->dev[i].rdev.recovery_offset)) {
  216. rs->ti->error = "Drive designated for rebuild not specified";
  217. return -EINVAL;
  218. }
  219. rs->ti->error = "No data device supplied with metadata device";
  220. if (rs->dev[i].meta_dev)
  221. return -EINVAL;
  222. continue;
  223. }
  224. ret = dm_get_device(rs->ti, argv[1],
  225. dm_table_get_mode(rs->ti->table),
  226. &rs->dev[i].data_dev);
  227. if (ret) {
  228. rs->ti->error = "RAID device lookup failure";
  229. return ret;
  230. }
  231. if (rs->dev[i].meta_dev) {
  232. metadata_available = 1;
  233. rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
  234. }
  235. rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
  236. list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
  237. if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
  238. rebuild++;
  239. }
  240. if (metadata_available) {
  241. rs->md.external = 0;
  242. rs->md.persistent = 1;
  243. rs->md.major_version = 2;
  244. } else if (rebuild && !rs->md.recovery_cp) {
  245. /*
  246. * Without metadata, we will not be able to tell if the array
  247. * is in-sync or not - we must assume it is not. Therefore,
  248. * it is impossible to rebuild a drive.
  249. *
  250. * Even if there is metadata, the on-disk information may
  251. * indicate that the array is not in-sync and it will then
  252. * fail at that time.
  253. *
  254. * User could specify 'nosync' option if desperate.
  255. */
  256. DMERR("Unable to rebuild drive while array is not in-sync");
  257. rs->ti->error = "RAID device lookup failure";
  258. return -EINVAL;
  259. }
  260. return 0;
  261. }
  262. /*
  263. * validate_region_size
  264. * @rs
  265. * @region_size: region size in sectors. If 0, pick a size (4MiB default).
  266. *
  267. * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
  268. * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
  269. *
  270. * Returns: 0 on success, -EINVAL on failure.
  271. */
  272. static int validate_region_size(struct raid_set *rs, unsigned long region_size)
  273. {
  274. unsigned long min_region_size = rs->ti->len / (1 << 21);
  275. if (!region_size) {
  276. /*
  277. * Choose a reasonable default. All figures in sectors.
  278. */
  279. if (min_region_size > (1 << 13)) {
  280. /* If not a power of 2, make it the next power of 2 */
  281. if (min_region_size & (min_region_size - 1))
  282. region_size = 1 << fls(region_size);
  283. DMINFO("Choosing default region size of %lu sectors",
  284. region_size);
  285. } else {
  286. DMINFO("Choosing default region size of 4MiB");
  287. region_size = 1 << 13; /* sectors */
  288. }
  289. } else {
  290. /*
  291. * Validate user-supplied value.
  292. */
  293. if (region_size > rs->ti->len) {
  294. rs->ti->error = "Supplied region size is too large";
  295. return -EINVAL;
  296. }
  297. if (region_size < min_region_size) {
  298. DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
  299. region_size, min_region_size);
  300. rs->ti->error = "Supplied region size is too small";
  301. return -EINVAL;
  302. }
  303. if (!is_power_of_2(region_size)) {
  304. rs->ti->error = "Region size is not a power of 2";
  305. return -EINVAL;
  306. }
  307. if (region_size < rs->md.chunk_sectors) {
  308. rs->ti->error = "Region size is smaller than the chunk size";
  309. return -EINVAL;
  310. }
  311. }
  312. /*
  313. * Convert sectors to bytes.
  314. */
  315. rs->md.bitmap_info.chunksize = (region_size << 9);
  316. return 0;
  317. }
  318. /*
  319. * validate_raid_redundancy
  320. * @rs
  321. *
  322. * Determine if there are enough devices in the array that haven't
  323. * failed (or are being rebuilt) to form a usable array.
  324. *
  325. * Returns: 0 on success, -EINVAL on failure.
  326. */
  327. static int validate_raid_redundancy(struct raid_set *rs)
  328. {
  329. unsigned i, rebuild_cnt = 0;
  330. unsigned rebuilds_per_group = 0, copies, d;
  331. unsigned group_size, last_group_start;
  332. for (i = 0; i < rs->md.raid_disks; i++)
  333. if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
  334. !rs->dev[i].rdev.sb_page)
  335. rebuild_cnt++;
  336. switch (rs->raid_type->level) {
  337. case 1:
  338. if (rebuild_cnt >= rs->md.raid_disks)
  339. goto too_many;
  340. break;
  341. case 4:
  342. case 5:
  343. case 6:
  344. if (rebuild_cnt > rs->raid_type->parity_devs)
  345. goto too_many;
  346. break;
  347. case 10:
  348. copies = raid10_md_layout_to_copies(rs->md.layout);
  349. if (rebuild_cnt < copies)
  350. break;
  351. /*
  352. * It is possible to have a higher rebuild count for RAID10,
  353. * as long as the failed devices occur in different mirror
  354. * groups (i.e. different stripes).
  355. *
  356. * When checking "near" format, make sure no adjacent devices
  357. * have failed beyond what can be handled. In addition to the
  358. * simple case where the number of devices is a multiple of the
  359. * number of copies, we must also handle cases where the number
  360. * of devices is not a multiple of the number of copies.
  361. * E.g. dev1 dev2 dev3 dev4 dev5
  362. * A A B B C
  363. * C D D E E
  364. */
  365. if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
  366. for (i = 0; i < rs->md.raid_disks * copies; i++) {
  367. if (!(i % copies))
  368. rebuilds_per_group = 0;
  369. d = i % rs->md.raid_disks;
  370. if ((!rs->dev[d].rdev.sb_page ||
  371. !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
  372. (++rebuilds_per_group >= copies))
  373. goto too_many;
  374. }
  375. break;
  376. }
  377. /*
  378. * When checking "far" and "offset" formats, we need to ensure
  379. * that the device that holds its copy is not also dead or
  380. * being rebuilt. (Note that "far" and "offset" formats only
  381. * support two copies right now. These formats also only ever
  382. * use the 'use_far_sets' variant.)
  383. *
  384. * This check is somewhat complicated by the need to account
  385. * for arrays that are not a multiple of (far) copies. This
  386. * results in the need to treat the last (potentially larger)
  387. * set differently.
  388. */
  389. group_size = (rs->md.raid_disks / copies);
  390. last_group_start = (rs->md.raid_disks / group_size) - 1;
  391. last_group_start *= group_size;
  392. for (i = 0; i < rs->md.raid_disks; i++) {
  393. if (!(i % copies) && !(i > last_group_start))
  394. rebuilds_per_group = 0;
  395. if ((!rs->dev[i].rdev.sb_page ||
  396. !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
  397. (++rebuilds_per_group >= copies))
  398. goto too_many;
  399. }
  400. break;
  401. default:
  402. if (rebuild_cnt)
  403. return -EINVAL;
  404. }
  405. return 0;
  406. too_many:
  407. return -EINVAL;
  408. }
  409. /*
  410. * Possible arguments are...
  411. * <chunk_size> [optional_args]
  412. *
  413. * Argument definitions
  414. * <chunk_size> The number of sectors per disk that
  415. * will form the "stripe"
  416. * [[no]sync] Force or prevent recovery of the
  417. * entire array
  418. * [rebuild <idx>] Rebuild the drive indicated by the index
  419. * [daemon_sleep <ms>] Time between bitmap daemon work to
  420. * clear bits
  421. * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  422. * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  423. * [write_mostly <idx>] Indicate a write mostly drive via index
  424. * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
  425. * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
  426. * [region_size <sectors>] Defines granularity of bitmap
  427. *
  428. * RAID10-only options:
  429. * [raid10_copies <# copies>] Number of copies. (Default: 2)
  430. * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
  431. */
  432. static int parse_raid_params(struct raid_set *rs, char **argv,
  433. unsigned num_raid_params)
  434. {
  435. char *raid10_format = "near";
  436. unsigned raid10_copies = 2;
  437. unsigned i;
  438. unsigned long value, region_size = 0;
  439. sector_t sectors_per_dev = rs->ti->len;
  440. sector_t max_io_len;
  441. char *key;
  442. /*
  443. * First, parse the in-order required arguments
  444. * "chunk_size" is the only argument of this type.
  445. */
  446. if ((kstrtoul(argv[0], 10, &value) < 0)) {
  447. rs->ti->error = "Bad chunk size";
  448. return -EINVAL;
  449. } else if (rs->raid_type->level == 1) {
  450. if (value)
  451. DMERR("Ignoring chunk size parameter for RAID 1");
  452. value = 0;
  453. } else if (!is_power_of_2(value)) {
  454. rs->ti->error = "Chunk size must be a power of 2";
  455. return -EINVAL;
  456. } else if (value < 8) {
  457. rs->ti->error = "Chunk size value is too small";
  458. return -EINVAL;
  459. }
  460. rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
  461. argv++;
  462. num_raid_params--;
  463. /*
  464. * We set each individual device as In_sync with a completed
  465. * 'recovery_offset'. If there has been a device failure or
  466. * replacement then one of the following cases applies:
  467. *
  468. * 1) User specifies 'rebuild'.
  469. * - Device is reset when param is read.
  470. * 2) A new device is supplied.
  471. * - No matching superblock found, resets device.
  472. * 3) Device failure was transient and returns on reload.
  473. * - Failure noticed, resets device for bitmap replay.
  474. * 4) Device hadn't completed recovery after previous failure.
  475. * - Superblock is read and overrides recovery_offset.
  476. *
  477. * What is found in the superblocks of the devices is always
  478. * authoritative, unless 'rebuild' or '[no]sync' was specified.
  479. */
  480. for (i = 0; i < rs->md.raid_disks; i++) {
  481. set_bit(In_sync, &rs->dev[i].rdev.flags);
  482. rs->dev[i].rdev.recovery_offset = MaxSector;
  483. }
  484. /*
  485. * Second, parse the unordered optional arguments
  486. */
  487. for (i = 0; i < num_raid_params; i++) {
  488. if (!strcasecmp(argv[i], "nosync")) {
  489. rs->md.recovery_cp = MaxSector;
  490. rs->ctr_flags |= CTR_FLAG_NOSYNC;
  491. continue;
  492. }
  493. if (!strcasecmp(argv[i], "sync")) {
  494. rs->md.recovery_cp = 0;
  495. rs->ctr_flags |= CTR_FLAG_SYNC;
  496. continue;
  497. }
  498. /* The rest of the optional arguments come in key/value pairs */
  499. if ((i + 1) >= num_raid_params) {
  500. rs->ti->error = "Wrong number of raid parameters given";
  501. return -EINVAL;
  502. }
  503. key = argv[i++];
  504. /* Parameters that take a string value are checked here. */
  505. if (!strcasecmp(key, "raid10_format")) {
  506. if (rs->raid_type->level != 10) {
  507. rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
  508. return -EINVAL;
  509. }
  510. if (strcmp("near", argv[i]) &&
  511. strcmp("far", argv[i]) &&
  512. strcmp("offset", argv[i])) {
  513. rs->ti->error = "Invalid 'raid10_format' value given";
  514. return -EINVAL;
  515. }
  516. raid10_format = argv[i];
  517. rs->ctr_flags |= CTR_FLAG_RAID10_FORMAT;
  518. continue;
  519. }
  520. if (kstrtoul(argv[i], 10, &value) < 0) {
  521. rs->ti->error = "Bad numerical argument given in raid params";
  522. return -EINVAL;
  523. }
  524. /* Parameters that take a numeric value are checked here */
  525. if (!strcasecmp(key, "rebuild")) {
  526. if (value >= rs->md.raid_disks) {
  527. rs->ti->error = "Invalid rebuild index given";
  528. return -EINVAL;
  529. }
  530. clear_bit(In_sync, &rs->dev[value].rdev.flags);
  531. rs->dev[value].rdev.recovery_offset = 0;
  532. rs->ctr_flags |= CTR_FLAG_REBUILD;
  533. } else if (!strcasecmp(key, "write_mostly")) {
  534. if (rs->raid_type->level != 1) {
  535. rs->ti->error = "write_mostly option is only valid for RAID1";
  536. return -EINVAL;
  537. }
  538. if (value >= rs->md.raid_disks) {
  539. rs->ti->error = "Invalid write_mostly drive index given";
  540. return -EINVAL;
  541. }
  542. set_bit(WriteMostly, &rs->dev[value].rdev.flags);
  543. } else if (!strcasecmp(key, "max_write_behind")) {
  544. if (rs->raid_type->level != 1) {
  545. rs->ti->error = "max_write_behind option is only valid for RAID1";
  546. return -EINVAL;
  547. }
  548. rs->ctr_flags |= CTR_FLAG_MAX_WRITE_BEHIND;
  549. /*
  550. * In device-mapper, we specify things in sectors, but
  551. * MD records this value in kB
  552. */
  553. value /= 2;
  554. if (value > COUNTER_MAX) {
  555. rs->ti->error = "Max write-behind limit out of range";
  556. return -EINVAL;
  557. }
  558. rs->md.bitmap_info.max_write_behind = value;
  559. } else if (!strcasecmp(key, "daemon_sleep")) {
  560. rs->ctr_flags |= CTR_FLAG_DAEMON_SLEEP;
  561. if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
  562. rs->ti->error = "daemon sleep period out of range";
  563. return -EINVAL;
  564. }
  565. rs->md.bitmap_info.daemon_sleep = value;
  566. } else if (!strcasecmp(key, "stripe_cache")) {
  567. rs->ctr_flags |= CTR_FLAG_STRIPE_CACHE;
  568. /*
  569. * In device-mapper, we specify things in sectors, but
  570. * MD records this value in kB
  571. */
  572. value /= 2;
  573. if ((rs->raid_type->level != 5) &&
  574. (rs->raid_type->level != 6)) {
  575. rs->ti->error = "Inappropriate argument: stripe_cache";
  576. return -EINVAL;
  577. }
  578. if (raid5_set_cache_size(&rs->md, (int)value)) {
  579. rs->ti->error = "Bad stripe_cache size";
  580. return -EINVAL;
  581. }
  582. } else if (!strcasecmp(key, "min_recovery_rate")) {
  583. rs->ctr_flags |= CTR_FLAG_MIN_RECOVERY_RATE;
  584. if (value > INT_MAX) {
  585. rs->ti->error = "min_recovery_rate out of range";
  586. return -EINVAL;
  587. }
  588. rs->md.sync_speed_min = (int)value;
  589. } else if (!strcasecmp(key, "max_recovery_rate")) {
  590. rs->ctr_flags |= CTR_FLAG_MAX_RECOVERY_RATE;
  591. if (value > INT_MAX) {
  592. rs->ti->error = "max_recovery_rate out of range";
  593. return -EINVAL;
  594. }
  595. rs->md.sync_speed_max = (int)value;
  596. } else if (!strcasecmp(key, "region_size")) {
  597. rs->ctr_flags |= CTR_FLAG_REGION_SIZE;
  598. region_size = value;
  599. } else if (!strcasecmp(key, "raid10_copies") &&
  600. (rs->raid_type->level == 10)) {
  601. if ((value < 2) || (value > 0xFF)) {
  602. rs->ti->error = "Bad value for 'raid10_copies'";
  603. return -EINVAL;
  604. }
  605. rs->ctr_flags |= CTR_FLAG_RAID10_COPIES;
  606. raid10_copies = value;
  607. } else {
  608. DMERR("Unable to parse RAID parameter: %s", key);
  609. rs->ti->error = "Unable to parse RAID parameters";
  610. return -EINVAL;
  611. }
  612. }
  613. if (validate_region_size(rs, region_size))
  614. return -EINVAL;
  615. if (rs->md.chunk_sectors)
  616. max_io_len = rs->md.chunk_sectors;
  617. else
  618. max_io_len = region_size;
  619. if (dm_set_target_max_io_len(rs->ti, max_io_len))
  620. return -EINVAL;
  621. if (rs->raid_type->level == 10) {
  622. if (raid10_copies > rs->md.raid_disks) {
  623. rs->ti->error = "Not enough devices to satisfy specification";
  624. return -EINVAL;
  625. }
  626. /*
  627. * If the format is not "near", we only support
  628. * two copies at the moment.
  629. */
  630. if (strcmp("near", raid10_format) && (raid10_copies > 2)) {
  631. rs->ti->error = "Too many copies for given RAID10 format.";
  632. return -EINVAL;
  633. }
  634. /* (Len * #mirrors) / #devices */
  635. sectors_per_dev = rs->ti->len * raid10_copies;
  636. sector_div(sectors_per_dev, rs->md.raid_disks);
  637. rs->md.layout = raid10_format_to_md_layout(raid10_format,
  638. raid10_copies);
  639. rs->md.new_layout = rs->md.layout;
  640. } else if ((!rs->raid_type->level || rs->raid_type->level > 1) &&
  641. sector_div(sectors_per_dev,
  642. (rs->md.raid_disks - rs->raid_type->parity_devs))) {
  643. rs->ti->error = "Target length not divisible by number of data devices";
  644. return -EINVAL;
  645. }
  646. rs->md.dev_sectors = sectors_per_dev;
  647. /* Assume there are no metadata devices until the drives are parsed */
  648. rs->md.persistent = 0;
  649. rs->md.external = 1;
  650. return 0;
  651. }
  652. static void do_table_event(struct work_struct *ws)
  653. {
  654. struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
  655. dm_table_event(rs->ti->table);
  656. }
  657. static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
  658. {
  659. struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
  660. return mddev_congested(&rs->md, bits);
  661. }
  662. /*
  663. * This structure is never routinely used by userspace, unlike md superblocks.
  664. * Devices with this superblock should only ever be accessed via device-mapper.
  665. */
  666. #define DM_RAID_MAGIC 0x64526D44
  667. struct dm_raid_superblock {
  668. __le32 magic; /* "DmRd" */
  669. __le32 features; /* Used to indicate possible future changes */
  670. __le32 num_devices; /* Number of devices in this array. (Max 64) */
  671. __le32 array_position; /* The position of this drive in the array */
  672. __le64 events; /* Incremented by md when superblock updated */
  673. __le64 failed_devices; /* Bit field of devices to indicate failures */
  674. /*
  675. * This offset tracks the progress of the repair or replacement of
  676. * an individual drive.
  677. */
  678. __le64 disk_recovery_offset;
  679. /*
  680. * This offset tracks the progress of the initial array
  681. * synchronisation/parity calculation.
  682. */
  683. __le64 array_resync_offset;
  684. /*
  685. * RAID characteristics
  686. */
  687. __le32 level;
  688. __le32 layout;
  689. __le32 stripe_sectors;
  690. /* Remainder of a logical block is zero-filled when writing (see super_sync()). */
  691. } __packed;
  692. static int read_disk_sb(struct md_rdev *rdev, int size)
  693. {
  694. BUG_ON(!rdev->sb_page);
  695. if (rdev->sb_loaded)
  696. return 0;
  697. if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
  698. DMERR("Failed to read superblock of device at position %d",
  699. rdev->raid_disk);
  700. md_error(rdev->mddev, rdev);
  701. return -EINVAL;
  702. }
  703. rdev->sb_loaded = 1;
  704. return 0;
  705. }
  706. static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
  707. {
  708. int i;
  709. uint64_t failed_devices;
  710. struct dm_raid_superblock *sb;
  711. struct raid_set *rs = container_of(mddev, struct raid_set, md);
  712. sb = page_address(rdev->sb_page);
  713. failed_devices = le64_to_cpu(sb->failed_devices);
  714. for (i = 0; i < mddev->raid_disks; i++)
  715. if (!rs->dev[i].data_dev ||
  716. test_bit(Faulty, &(rs->dev[i].rdev.flags)))
  717. failed_devices |= (1ULL << i);
  718. memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
  719. sb->magic = cpu_to_le32(DM_RAID_MAGIC);
  720. sb->features = cpu_to_le32(0); /* No features yet */
  721. sb->num_devices = cpu_to_le32(mddev->raid_disks);
  722. sb->array_position = cpu_to_le32(rdev->raid_disk);
  723. sb->events = cpu_to_le64(mddev->events);
  724. sb->failed_devices = cpu_to_le64(failed_devices);
  725. sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
  726. sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
  727. sb->level = cpu_to_le32(mddev->level);
  728. sb->layout = cpu_to_le32(mddev->layout);
  729. sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
  730. }
  731. /*
  732. * super_load
  733. *
  734. * This function creates a superblock if one is not found on the device
  735. * and will decide which superblock to use if there's a choice.
  736. *
  737. * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
  738. */
  739. static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
  740. {
  741. int ret;
  742. struct dm_raid_superblock *sb;
  743. struct dm_raid_superblock *refsb;
  744. uint64_t events_sb, events_refsb;
  745. rdev->sb_start = 0;
  746. rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
  747. if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
  748. DMERR("superblock size of a logical block is no longer valid");
  749. return -EINVAL;
  750. }
  751. ret = read_disk_sb(rdev, rdev->sb_size);
  752. if (ret)
  753. return ret;
  754. sb = page_address(rdev->sb_page);
  755. /*
  756. * Two cases that we want to write new superblocks and rebuild:
  757. * 1) New device (no matching magic number)
  758. * 2) Device specified for rebuild (!In_sync w/ offset == 0)
  759. */
  760. if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
  761. (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
  762. super_sync(rdev->mddev, rdev);
  763. set_bit(FirstUse, &rdev->flags);
  764. /* Force writing of superblocks to disk */
  765. set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
  766. /* Any superblock is better than none, choose that if given */
  767. return refdev ? 0 : 1;
  768. }
  769. if (!refdev)
  770. return 1;
  771. events_sb = le64_to_cpu(sb->events);
  772. refsb = page_address(refdev->sb_page);
  773. events_refsb = le64_to_cpu(refsb->events);
  774. return (events_sb > events_refsb) ? 1 : 0;
  775. }
  776. static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
  777. {
  778. int role;
  779. struct raid_set *rs = container_of(mddev, struct raid_set, md);
  780. uint64_t events_sb;
  781. uint64_t failed_devices;
  782. struct dm_raid_superblock *sb;
  783. uint32_t new_devs = 0;
  784. uint32_t rebuilds = 0;
  785. struct md_rdev *r;
  786. struct dm_raid_superblock *sb2;
  787. sb = page_address(rdev->sb_page);
  788. events_sb = le64_to_cpu(sb->events);
  789. failed_devices = le64_to_cpu(sb->failed_devices);
  790. /*
  791. * Initialise to 1 if this is a new superblock.
  792. */
  793. mddev->events = events_sb ? : 1;
  794. /*
  795. * Reshaping is not currently allowed
  796. */
  797. if (le32_to_cpu(sb->level) != mddev->level) {
  798. DMERR("Reshaping arrays not yet supported. (RAID level change)");
  799. return -EINVAL;
  800. }
  801. if (le32_to_cpu(sb->layout) != mddev->layout) {
  802. DMERR("Reshaping arrays not yet supported. (RAID layout change)");
  803. DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
  804. DMERR(" Old layout: %s w/ %d copies",
  805. raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
  806. raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
  807. DMERR(" New layout: %s w/ %d copies",
  808. raid10_md_layout_to_format(mddev->layout),
  809. raid10_md_layout_to_copies(mddev->layout));
  810. return -EINVAL;
  811. }
  812. if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
  813. DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
  814. return -EINVAL;
  815. }
  816. /* We can only change the number of devices in RAID1 right now */
  817. if ((rs->raid_type->level != 1) &&
  818. (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
  819. DMERR("Reshaping arrays not yet supported. (device count change)");
  820. return -EINVAL;
  821. }
  822. if (!(rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)))
  823. mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
  824. /*
  825. * During load, we set FirstUse if a new superblock was written.
  826. * There are two reasons we might not have a superblock:
  827. * 1) The array is brand new - in which case, all of the
  828. * devices must have their In_sync bit set. Also,
  829. * recovery_cp must be 0, unless forced.
  830. * 2) This is a new device being added to an old array
  831. * and the new device needs to be rebuilt - in which
  832. * case the In_sync bit will /not/ be set and
  833. * recovery_cp must be MaxSector.
  834. */
  835. rdev_for_each(r, mddev) {
  836. if (!test_bit(In_sync, &r->flags)) {
  837. DMINFO("Device %d specified for rebuild: "
  838. "Clearing superblock", r->raid_disk);
  839. rebuilds++;
  840. } else if (test_bit(FirstUse, &r->flags))
  841. new_devs++;
  842. }
  843. if (!rebuilds) {
  844. if (new_devs == mddev->raid_disks) {
  845. DMINFO("Superblocks created for new array");
  846. set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
  847. } else if (new_devs) {
  848. DMERR("New device injected "
  849. "into existing array without 'rebuild' "
  850. "parameter specified");
  851. return -EINVAL;
  852. }
  853. } else if (new_devs) {
  854. DMERR("'rebuild' devices cannot be "
  855. "injected into an array with other first-time devices");
  856. return -EINVAL;
  857. } else if (mddev->recovery_cp != MaxSector) {
  858. DMERR("'rebuild' specified while array is not in-sync");
  859. return -EINVAL;
  860. }
  861. /*
  862. * Now we set the Faulty bit for those devices that are
  863. * recorded in the superblock as failed.
  864. */
  865. rdev_for_each(r, mddev) {
  866. if (!r->sb_page)
  867. continue;
  868. sb2 = page_address(r->sb_page);
  869. sb2->failed_devices = 0;
  870. /*
  871. * Check for any device re-ordering.
  872. */
  873. if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
  874. role = le32_to_cpu(sb2->array_position);
  875. if (role != r->raid_disk) {
  876. if (rs->raid_type->level != 1) {
  877. rs->ti->error = "Cannot change device "
  878. "positions in RAID array";
  879. return -EINVAL;
  880. }
  881. DMINFO("RAID1 device #%d now at position #%d",
  882. role, r->raid_disk);
  883. }
  884. /*
  885. * Partial recovery is performed on
  886. * returning failed devices.
  887. */
  888. if (failed_devices & (1 << role))
  889. set_bit(Faulty, &r->flags);
  890. }
  891. }
  892. return 0;
  893. }
  894. static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
  895. {
  896. struct mddev *mddev = &rs->md;
  897. struct dm_raid_superblock *sb = page_address(rdev->sb_page);
  898. /*
  899. * If mddev->events is not set, we know we have not yet initialized
  900. * the array.
  901. */
  902. if (!mddev->events && super_init_validation(mddev, rdev))
  903. return -EINVAL;
  904. /* Enable bitmap creation for RAID levels != 0 */
  905. mddev->bitmap_info.offset = (rs->raid_type->level) ? to_sector(4096) : 0;
  906. rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
  907. if (!test_bit(FirstUse, &rdev->flags)) {
  908. rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
  909. if (rdev->recovery_offset != MaxSector)
  910. clear_bit(In_sync, &rdev->flags);
  911. }
  912. /*
  913. * If a device comes back, set it as not In_sync and no longer faulty.
  914. */
  915. if (test_bit(Faulty, &rdev->flags)) {
  916. clear_bit(Faulty, &rdev->flags);
  917. clear_bit(In_sync, &rdev->flags);
  918. rdev->saved_raid_disk = rdev->raid_disk;
  919. rdev->recovery_offset = 0;
  920. }
  921. clear_bit(FirstUse, &rdev->flags);
  922. return 0;
  923. }
  924. /*
  925. * Analyse superblocks and select the freshest.
  926. */
  927. static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
  928. {
  929. int ret;
  930. struct raid_dev *dev;
  931. struct md_rdev *rdev, *tmp, *freshest;
  932. struct mddev *mddev = &rs->md;
  933. freshest = NULL;
  934. rdev_for_each_safe(rdev, tmp, mddev) {
  935. /*
  936. * Skipping super_load due to CTR_FLAG_SYNC will cause
  937. * the array to undergo initialization again as
  938. * though it were new. This is the intended effect
  939. * of the "sync" directive.
  940. *
  941. * When reshaping capability is added, we must ensure
  942. * that the "sync" directive is disallowed during the
  943. * reshape.
  944. */
  945. rdev->sectors = to_sector(i_size_read(rdev->bdev->bd_inode));
  946. if (rs->ctr_flags & CTR_FLAG_SYNC)
  947. continue;
  948. if (!rdev->meta_bdev)
  949. continue;
  950. ret = super_load(rdev, freshest);
  951. switch (ret) {
  952. case 1:
  953. freshest = rdev;
  954. break;
  955. case 0:
  956. break;
  957. default:
  958. dev = container_of(rdev, struct raid_dev, rdev);
  959. if (dev->meta_dev)
  960. dm_put_device(ti, dev->meta_dev);
  961. dev->meta_dev = NULL;
  962. rdev->meta_bdev = NULL;
  963. if (rdev->sb_page)
  964. put_page(rdev->sb_page);
  965. rdev->sb_page = NULL;
  966. rdev->sb_loaded = 0;
  967. /*
  968. * We might be able to salvage the data device
  969. * even though the meta device has failed. For
  970. * now, we behave as though '- -' had been
  971. * set for this device in the table.
  972. */
  973. if (dev->data_dev)
  974. dm_put_device(ti, dev->data_dev);
  975. dev->data_dev = NULL;
  976. rdev->bdev = NULL;
  977. list_del(&rdev->same_set);
  978. }
  979. }
  980. if (!freshest)
  981. return 0;
  982. if (validate_raid_redundancy(rs)) {
  983. rs->ti->error = "Insufficient redundancy to activate array";
  984. return -EINVAL;
  985. }
  986. /*
  987. * Validation of the freshest device provides the source of
  988. * validation for the remaining devices.
  989. */
  990. ti->error = "Unable to assemble array: Invalid superblocks";
  991. if (super_validate(rs, freshest))
  992. return -EINVAL;
  993. rdev_for_each(rdev, mddev)
  994. if ((rdev != freshest) && super_validate(rs, rdev))
  995. return -EINVAL;
  996. return 0;
  997. }
  998. /*
  999. * Enable/disable discard support on RAID set depending on
  1000. * RAID level and discard properties of underlying RAID members.
  1001. */
  1002. static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
  1003. {
  1004. int i;
  1005. bool raid456;
  1006. /* Assume discards not supported until after checks below. */
  1007. ti->discards_supported = false;
  1008. /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
  1009. raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
  1010. for (i = 0; i < rs->md.raid_disks; i++) {
  1011. struct request_queue *q;
  1012. if (!rs->dev[i].rdev.bdev)
  1013. continue;
  1014. q = bdev_get_queue(rs->dev[i].rdev.bdev);
  1015. if (!q || !blk_queue_discard(q))
  1016. return;
  1017. if (raid456) {
  1018. if (!q->limits.discard_zeroes_data)
  1019. return;
  1020. if (!devices_handle_discard_safely) {
  1021. DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
  1022. DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
  1023. return;
  1024. }
  1025. }
  1026. }
  1027. /* All RAID members properly support discards */
  1028. ti->discards_supported = true;
  1029. /*
  1030. * RAID1 and RAID10 personalities require bio splitting,
  1031. * RAID0/4/5/6 don't and process large discard bios properly.
  1032. */
  1033. ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
  1034. ti->num_discard_bios = 1;
  1035. }
  1036. /*
  1037. * Construct a RAID4/5/6 mapping:
  1038. * Args:
  1039. * <raid_type> <#raid_params> <raid_params> \
  1040. * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
  1041. *
  1042. * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
  1043. * details on possible <raid_params>.
  1044. */
  1045. static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1046. {
  1047. int ret;
  1048. struct raid_type *rt;
  1049. unsigned long num_raid_params, num_raid_devs;
  1050. struct raid_set *rs = NULL;
  1051. /* Must have at least <raid_type> <#raid_params> */
  1052. if (argc < 2) {
  1053. ti->error = "Too few arguments";
  1054. return -EINVAL;
  1055. }
  1056. /* raid type */
  1057. rt = get_raid_type(argv[0]);
  1058. if (!rt) {
  1059. ti->error = "Unrecognised raid_type";
  1060. return -EINVAL;
  1061. }
  1062. argc--;
  1063. argv++;
  1064. /* number of RAID parameters */
  1065. if (kstrtoul(argv[0], 10, &num_raid_params) < 0) {
  1066. ti->error = "Cannot understand number of RAID parameters";
  1067. return -EINVAL;
  1068. }
  1069. argc--;
  1070. argv++;
  1071. /* Skip over RAID params for now and find out # of devices */
  1072. if (num_raid_params >= argc) {
  1073. ti->error = "Arguments do not agree with counts given";
  1074. return -EINVAL;
  1075. }
  1076. if ((kstrtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
  1077. (num_raid_devs > MAX_RAID_DEVICES)) {
  1078. ti->error = "Cannot understand number of raid devices";
  1079. return -EINVAL;
  1080. }
  1081. argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
  1082. if (argc != (num_raid_devs * 2)) {
  1083. ti->error = "Supplied RAID devices does not match the count given";
  1084. return -EINVAL;
  1085. }
  1086. rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
  1087. if (IS_ERR(rs))
  1088. return PTR_ERR(rs);
  1089. ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
  1090. if (ret)
  1091. goto bad;
  1092. argv += num_raid_params + 1;
  1093. ret = dev_parms(rs, argv);
  1094. if (ret)
  1095. goto bad;
  1096. rs->md.sync_super = super_sync;
  1097. ret = analyse_superblocks(ti, rs);
  1098. if (ret)
  1099. goto bad;
  1100. INIT_WORK(&rs->md.event_work, do_table_event);
  1101. ti->private = rs;
  1102. ti->num_flush_bios = 1;
  1103. /*
  1104. * Disable/enable discard support on RAID set.
  1105. */
  1106. configure_discard_support(ti, rs);
  1107. /* Has to be held on running the array */
  1108. mddev_lock_nointr(&rs->md);
  1109. ret = md_run(&rs->md);
  1110. rs->md.in_sync = 0; /* Assume already marked dirty */
  1111. mddev_unlock(&rs->md);
  1112. if (ret) {
  1113. ti->error = "Fail to run raid array";
  1114. goto bad;
  1115. }
  1116. if (ti->len != rs->md.array_sectors) {
  1117. ti->error = "Array size does not match requested target length";
  1118. ret = -EINVAL;
  1119. goto size_mismatch;
  1120. }
  1121. rs->callbacks.congested_fn = raid_is_congested;
  1122. dm_table_add_target_callbacks(ti->table, &rs->callbacks);
  1123. mddev_suspend(&rs->md);
  1124. return 0;
  1125. size_mismatch:
  1126. md_stop(&rs->md);
  1127. bad:
  1128. context_free(rs);
  1129. return ret;
  1130. }
  1131. static void raid_dtr(struct dm_target *ti)
  1132. {
  1133. struct raid_set *rs = ti->private;
  1134. list_del_init(&rs->callbacks.list);
  1135. md_stop(&rs->md);
  1136. context_free(rs);
  1137. }
  1138. static int raid_map(struct dm_target *ti, struct bio *bio)
  1139. {
  1140. struct raid_set *rs = ti->private;
  1141. struct mddev *mddev = &rs->md;
  1142. mddev->pers->make_request(mddev, bio);
  1143. return DM_MAPIO_SUBMITTED;
  1144. }
  1145. static const char *decipher_sync_action(struct mddev *mddev)
  1146. {
  1147. if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
  1148. return "frozen";
  1149. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
  1150. (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
  1151. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  1152. return "reshape";
  1153. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1154. if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1155. return "resync";
  1156. else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1157. return "check";
  1158. return "repair";
  1159. }
  1160. if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
  1161. return "recover";
  1162. }
  1163. return "idle";
  1164. }
  1165. static void raid_status(struct dm_target *ti, status_type_t type,
  1166. unsigned status_flags, char *result, unsigned maxlen)
  1167. {
  1168. struct raid_set *rs = ti->private;
  1169. unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
  1170. unsigned sz = 0;
  1171. int i, array_in_sync = 0;
  1172. sector_t sync;
  1173. switch (type) {
  1174. case STATUSTYPE_INFO:
  1175. DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
  1176. if (rs->raid_type->level) {
  1177. if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
  1178. sync = rs->md.curr_resync_completed;
  1179. else
  1180. sync = rs->md.recovery_cp;
  1181. if (sync >= rs->md.resync_max_sectors) {
  1182. /*
  1183. * Sync complete.
  1184. */
  1185. array_in_sync = 1;
  1186. sync = rs->md.resync_max_sectors;
  1187. } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
  1188. /*
  1189. * If "check" or "repair" is occurring, the array has
  1190. * undergone and initial sync and the health characters
  1191. * should not be 'a' anymore.
  1192. */
  1193. array_in_sync = 1;
  1194. } else {
  1195. /*
  1196. * The array may be doing an initial sync, or it may
  1197. * be rebuilding individual components. If all the
  1198. * devices are In_sync, then it is the array that is
  1199. * being initialized.
  1200. */
  1201. for (i = 0; i < rs->md.raid_disks; i++)
  1202. if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
  1203. array_in_sync = 1;
  1204. }
  1205. } else {
  1206. /* RAID0 */
  1207. array_in_sync = 1;
  1208. sync = rs->md.resync_max_sectors;
  1209. }
  1210. /*
  1211. * Status characters:
  1212. * 'D' = Dead/Failed device
  1213. * 'a' = Alive but not in-sync
  1214. * 'A' = Alive and in-sync
  1215. */
  1216. for (i = 0; i < rs->md.raid_disks; i++) {
  1217. if (test_bit(Faulty, &rs->dev[i].rdev.flags))
  1218. DMEMIT("D");
  1219. else if (!array_in_sync ||
  1220. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  1221. DMEMIT("a");
  1222. else
  1223. DMEMIT("A");
  1224. }
  1225. /*
  1226. * In-sync ratio:
  1227. * The in-sync ratio shows the progress of:
  1228. * - Initializing the array
  1229. * - Rebuilding a subset of devices of the array
  1230. * The user can distinguish between the two by referring
  1231. * to the status characters.
  1232. */
  1233. DMEMIT(" %llu/%llu",
  1234. (unsigned long long) sync,
  1235. (unsigned long long) rs->md.resync_max_sectors);
  1236. /*
  1237. * Sync action:
  1238. * See Documentation/device-mapper/dm-raid.c for
  1239. * information on each of these states.
  1240. */
  1241. DMEMIT(" %s", decipher_sync_action(&rs->md));
  1242. /*
  1243. * resync_mismatches/mismatch_cnt
  1244. * This field shows the number of discrepancies found when
  1245. * performing a "check" of the array.
  1246. */
  1247. DMEMIT(" %llu",
  1248. (strcmp(rs->md.last_sync_action, "check")) ? 0 :
  1249. (unsigned long long)
  1250. atomic64_read(&rs->md.resync_mismatches));
  1251. break;
  1252. case STATUSTYPE_TABLE:
  1253. /* The string you would use to construct this array */
  1254. for (i = 0; i < rs->md.raid_disks; i++) {
  1255. if ((rs->ctr_flags & CTR_FLAG_REBUILD) &&
  1256. rs->dev[i].data_dev &&
  1257. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  1258. raid_param_cnt += 2; /* for rebuilds */
  1259. if (rs->dev[i].data_dev &&
  1260. test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  1261. raid_param_cnt += 2;
  1262. }
  1263. raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
  1264. if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
  1265. raid_param_cnt--;
  1266. DMEMIT("%s %u %u", rs->raid_type->name,
  1267. raid_param_cnt, rs->md.chunk_sectors);
  1268. if ((rs->ctr_flags & CTR_FLAG_SYNC) &&
  1269. (rs->md.recovery_cp == MaxSector))
  1270. DMEMIT(" sync");
  1271. if (rs->ctr_flags & CTR_FLAG_NOSYNC)
  1272. DMEMIT(" nosync");
  1273. for (i = 0; i < rs->md.raid_disks; i++)
  1274. if ((rs->ctr_flags & CTR_FLAG_REBUILD) &&
  1275. rs->dev[i].data_dev &&
  1276. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  1277. DMEMIT(" rebuild %u", i);
  1278. if (rs->ctr_flags & CTR_FLAG_DAEMON_SLEEP)
  1279. DMEMIT(" daemon_sleep %lu",
  1280. rs->md.bitmap_info.daemon_sleep);
  1281. if (rs->ctr_flags & CTR_FLAG_MIN_RECOVERY_RATE)
  1282. DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
  1283. if (rs->ctr_flags & CTR_FLAG_MAX_RECOVERY_RATE)
  1284. DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
  1285. for (i = 0; i < rs->md.raid_disks; i++)
  1286. if (rs->dev[i].data_dev &&
  1287. test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  1288. DMEMIT(" write_mostly %u", i);
  1289. if (rs->ctr_flags & CTR_FLAG_MAX_WRITE_BEHIND)
  1290. DMEMIT(" max_write_behind %lu",
  1291. rs->md.bitmap_info.max_write_behind);
  1292. if (rs->ctr_flags & CTR_FLAG_STRIPE_CACHE) {
  1293. struct r5conf *conf = rs->md.private;
  1294. /* convert from kiB to sectors */
  1295. DMEMIT(" stripe_cache %d",
  1296. conf ? conf->max_nr_stripes * 2 : 0);
  1297. }
  1298. if (rs->ctr_flags & CTR_FLAG_REGION_SIZE)
  1299. DMEMIT(" region_size %lu",
  1300. rs->md.bitmap_info.chunksize >> 9);
  1301. if (rs->ctr_flags & CTR_FLAG_RAID10_COPIES)
  1302. DMEMIT(" raid10_copies %u",
  1303. raid10_md_layout_to_copies(rs->md.layout));
  1304. if (rs->ctr_flags & CTR_FLAG_RAID10_FORMAT)
  1305. DMEMIT(" raid10_format %s",
  1306. raid10_md_layout_to_format(rs->md.layout));
  1307. DMEMIT(" %d", rs->md.raid_disks);
  1308. for (i = 0; i < rs->md.raid_disks; i++) {
  1309. if (rs->dev[i].meta_dev)
  1310. DMEMIT(" %s", rs->dev[i].meta_dev->name);
  1311. else
  1312. DMEMIT(" -");
  1313. if (rs->dev[i].data_dev)
  1314. DMEMIT(" %s", rs->dev[i].data_dev->name);
  1315. else
  1316. DMEMIT(" -");
  1317. }
  1318. }
  1319. }
  1320. static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
  1321. {
  1322. struct raid_set *rs = ti->private;
  1323. struct mddev *mddev = &rs->md;
  1324. if (!strcasecmp(argv[0], "reshape")) {
  1325. DMERR("Reshape not supported.");
  1326. return -EINVAL;
  1327. }
  1328. if (!mddev->pers || !mddev->pers->sync_request)
  1329. return -EINVAL;
  1330. if (!strcasecmp(argv[0], "frozen"))
  1331. set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  1332. else
  1333. clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  1334. if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
  1335. if (mddev->sync_thread) {
  1336. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1337. md_reap_sync_thread(mddev);
  1338. }
  1339. } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
  1340. test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
  1341. return -EBUSY;
  1342. else if (!strcasecmp(argv[0], "resync"))
  1343. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1344. else if (!strcasecmp(argv[0], "recover")) {
  1345. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  1346. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1347. } else {
  1348. if (!strcasecmp(argv[0], "check"))
  1349. set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  1350. else if (!!strcasecmp(argv[0], "repair"))
  1351. return -EINVAL;
  1352. set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  1353. set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  1354. }
  1355. if (mddev->ro == 2) {
  1356. /* A write to sync_action is enough to justify
  1357. * canceling read-auto mode
  1358. */
  1359. mddev->ro = 0;
  1360. if (!mddev->suspended)
  1361. md_wakeup_thread(mddev->sync_thread);
  1362. }
  1363. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1364. if (!mddev->suspended)
  1365. md_wakeup_thread(mddev->thread);
  1366. return 0;
  1367. }
  1368. static int raid_iterate_devices(struct dm_target *ti,
  1369. iterate_devices_callout_fn fn, void *data)
  1370. {
  1371. struct raid_set *rs = ti->private;
  1372. unsigned i;
  1373. int ret = 0;
  1374. for (i = 0; !ret && i < rs->md.raid_disks; i++)
  1375. if (rs->dev[i].data_dev)
  1376. ret = fn(ti,
  1377. rs->dev[i].data_dev,
  1378. 0, /* No offset on data devs */
  1379. rs->md.dev_sectors,
  1380. data);
  1381. return ret;
  1382. }
  1383. static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1384. {
  1385. struct raid_set *rs = ti->private;
  1386. unsigned chunk_size = rs->md.chunk_sectors << 9;
  1387. struct r5conf *conf = rs->md.private;
  1388. blk_limits_io_min(limits, chunk_size);
  1389. blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
  1390. }
  1391. static void raid_presuspend(struct dm_target *ti)
  1392. {
  1393. struct raid_set *rs = ti->private;
  1394. md_stop_writes(&rs->md);
  1395. }
  1396. static void raid_postsuspend(struct dm_target *ti)
  1397. {
  1398. struct raid_set *rs = ti->private;
  1399. mddev_suspend(&rs->md);
  1400. }
  1401. static void attempt_restore_of_faulty_devices(struct raid_set *rs)
  1402. {
  1403. int i;
  1404. uint64_t failed_devices, cleared_failed_devices = 0;
  1405. unsigned long flags;
  1406. struct dm_raid_superblock *sb;
  1407. struct md_rdev *r;
  1408. for (i = 0; i < rs->md.raid_disks; i++) {
  1409. r = &rs->dev[i].rdev;
  1410. if (test_bit(Faulty, &r->flags) && r->sb_page &&
  1411. sync_page_io(r, 0, r->sb_size, r->sb_page, READ, 1)) {
  1412. DMINFO("Faulty %s device #%d has readable super block."
  1413. " Attempting to revive it.",
  1414. rs->raid_type->name, i);
  1415. /*
  1416. * Faulty bit may be set, but sometimes the array can
  1417. * be suspended before the personalities can respond
  1418. * by removing the device from the array (i.e. calling
  1419. * 'hot_remove_disk'). If they haven't yet removed
  1420. * the failed device, its 'raid_disk' number will be
  1421. * '>= 0' - meaning we must call this function
  1422. * ourselves.
  1423. */
  1424. if ((r->raid_disk >= 0) &&
  1425. (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
  1426. /* Failed to revive this device, try next */
  1427. continue;
  1428. r->raid_disk = i;
  1429. r->saved_raid_disk = i;
  1430. flags = r->flags;
  1431. clear_bit(Faulty, &r->flags);
  1432. clear_bit(WriteErrorSeen, &r->flags);
  1433. clear_bit(In_sync, &r->flags);
  1434. if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
  1435. r->raid_disk = -1;
  1436. r->saved_raid_disk = -1;
  1437. r->flags = flags;
  1438. } else {
  1439. r->recovery_offset = 0;
  1440. cleared_failed_devices |= 1 << i;
  1441. }
  1442. }
  1443. }
  1444. if (cleared_failed_devices) {
  1445. rdev_for_each(r, &rs->md) {
  1446. sb = page_address(r->sb_page);
  1447. failed_devices = le64_to_cpu(sb->failed_devices);
  1448. failed_devices &= ~cleared_failed_devices;
  1449. sb->failed_devices = cpu_to_le64(failed_devices);
  1450. }
  1451. }
  1452. }
  1453. static void raid_resume(struct dm_target *ti)
  1454. {
  1455. struct raid_set *rs = ti->private;
  1456. if (rs->raid_type->level) {
  1457. set_bit(MD_CHANGE_DEVS, &rs->md.flags);
  1458. if (!rs->bitmap_loaded) {
  1459. bitmap_load(&rs->md);
  1460. rs->bitmap_loaded = 1;
  1461. } else {
  1462. /*
  1463. * A secondary resume while the device is active.
  1464. * Take this opportunity to check whether any failed
  1465. * devices are reachable again.
  1466. */
  1467. attempt_restore_of_faulty_devices(rs);
  1468. }
  1469. clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
  1470. }
  1471. mddev_resume(&rs->md);
  1472. }
  1473. static int raid_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1474. struct bio_vec *biovec, int max_size)
  1475. {
  1476. struct raid_set *rs = ti->private;
  1477. struct md_personality *pers = rs->md.pers;
  1478. if (pers && pers->mergeable_bvec)
  1479. return min(max_size, pers->mergeable_bvec(&rs->md, bvm, biovec));
  1480. /*
  1481. * In case we can't request the personality because
  1482. * the raid set is not running yet
  1483. *
  1484. * -> return safe minimum
  1485. */
  1486. return rs->md.chunk_sectors;
  1487. }
  1488. static struct target_type raid_target = {
  1489. .name = "raid",
  1490. .version = {1, 7, 0},
  1491. .module = THIS_MODULE,
  1492. .ctr = raid_ctr,
  1493. .dtr = raid_dtr,
  1494. .map = raid_map,
  1495. .status = raid_status,
  1496. .message = raid_message,
  1497. .iterate_devices = raid_iterate_devices,
  1498. .io_hints = raid_io_hints,
  1499. .presuspend = raid_presuspend,
  1500. .postsuspend = raid_postsuspend,
  1501. .resume = raid_resume,
  1502. .merge = raid_merge,
  1503. };
  1504. static int __init dm_raid_init(void)
  1505. {
  1506. DMINFO("Loading target version %u.%u.%u",
  1507. raid_target.version[0],
  1508. raid_target.version[1],
  1509. raid_target.version[2]);
  1510. return dm_register_target(&raid_target);
  1511. }
  1512. static void __exit dm_raid_exit(void)
  1513. {
  1514. dm_unregister_target(&raid_target);
  1515. }
  1516. module_init(dm_raid_init);
  1517. module_exit(dm_raid_exit);
  1518. module_param(devices_handle_discard_safely, bool, 0644);
  1519. MODULE_PARM_DESC(devices_handle_discard_safely,
  1520. "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
  1521. MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
  1522. MODULE_ALIAS("dm-raid1");
  1523. MODULE_ALIAS("dm-raid10");
  1524. MODULE_ALIAS("dm-raid4");
  1525. MODULE_ALIAS("dm-raid5");
  1526. MODULE_ALIAS("dm-raid6");
  1527. MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
  1528. MODULE_LICENSE("GPL");