dm-raid.c 47 KB

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