dm-raid.c 40 KB

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