dm-raid.c 38 KB

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