dm-raid.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. * Possible arguments are...
  293. * <chunk_size> [optional_args]
  294. *
  295. * Argument definitions
  296. * <chunk_size> The number of sectors per disk that
  297. * will form the "stripe"
  298. * [[no]sync] Force or prevent recovery of the
  299. * entire array
  300. * [rebuild <idx>] Rebuild the drive indicated by the index
  301. * [daemon_sleep <ms>] Time between bitmap daemon work to
  302. * clear bits
  303. * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  304. * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  305. * [write_mostly <idx>] Indicate a write mostly drive via index
  306. * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
  307. * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
  308. * [region_size <sectors>] Defines granularity of bitmap
  309. *
  310. * RAID10-only options:
  311. * [raid10_copies <# copies>] Number of copies. (Default: 2)
  312. * [raid10_format <near>] Layout algorithm. (Default: near)
  313. */
  314. static int parse_raid_params(struct raid_set *rs, char **argv,
  315. unsigned num_raid_params)
  316. {
  317. char *raid10_format = "near";
  318. unsigned raid10_copies = 2;
  319. unsigned i, rebuild_cnt = 0;
  320. unsigned long value, region_size = 0;
  321. sector_t sectors_per_dev = rs->ti->len;
  322. sector_t max_io_len;
  323. char *key;
  324. /*
  325. * First, parse the in-order required arguments
  326. * "chunk_size" is the only argument of this type.
  327. */
  328. if ((strict_strtoul(argv[0], 10, &value) < 0)) {
  329. rs->ti->error = "Bad chunk size";
  330. return -EINVAL;
  331. } else if (rs->raid_type->level == 1) {
  332. if (value)
  333. DMERR("Ignoring chunk size parameter for RAID 1");
  334. value = 0;
  335. } else if (!is_power_of_2(value)) {
  336. rs->ti->error = "Chunk size must be a power of 2";
  337. return -EINVAL;
  338. } else if (value < 8) {
  339. rs->ti->error = "Chunk size value is too small";
  340. return -EINVAL;
  341. }
  342. rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
  343. argv++;
  344. num_raid_params--;
  345. /*
  346. * We set each individual device as In_sync with a completed
  347. * 'recovery_offset'. If there has been a device failure or
  348. * replacement then one of the following cases applies:
  349. *
  350. * 1) User specifies 'rebuild'.
  351. * - Device is reset when param is read.
  352. * 2) A new device is supplied.
  353. * - No matching superblock found, resets device.
  354. * 3) Device failure was transient and returns on reload.
  355. * - Failure noticed, resets device for bitmap replay.
  356. * 4) Device hadn't completed recovery after previous failure.
  357. * - Superblock is read and overrides recovery_offset.
  358. *
  359. * What is found in the superblocks of the devices is always
  360. * authoritative, unless 'rebuild' or '[no]sync' was specified.
  361. */
  362. for (i = 0; i < rs->md.raid_disks; i++) {
  363. set_bit(In_sync, &rs->dev[i].rdev.flags);
  364. rs->dev[i].rdev.recovery_offset = MaxSector;
  365. }
  366. /*
  367. * Second, parse the unordered optional arguments
  368. */
  369. for (i = 0; i < num_raid_params; i++) {
  370. if (!strcasecmp(argv[i], "nosync")) {
  371. rs->md.recovery_cp = MaxSector;
  372. rs->print_flags |= DMPF_NOSYNC;
  373. continue;
  374. }
  375. if (!strcasecmp(argv[i], "sync")) {
  376. rs->md.recovery_cp = 0;
  377. rs->print_flags |= DMPF_SYNC;
  378. continue;
  379. }
  380. /* The rest of the optional arguments come in key/value pairs */
  381. if ((i + 1) >= num_raid_params) {
  382. rs->ti->error = "Wrong number of raid parameters given";
  383. return -EINVAL;
  384. }
  385. key = argv[i++];
  386. /* Parameters that take a string value are checked here. */
  387. if (!strcasecmp(key, "raid10_format")) {
  388. if (rs->raid_type->level != 10) {
  389. rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
  390. return -EINVAL;
  391. }
  392. if (strcmp("near", argv[i])) {
  393. rs->ti->error = "Invalid 'raid10_format' value given";
  394. return -EINVAL;
  395. }
  396. raid10_format = argv[i];
  397. rs->print_flags |= DMPF_RAID10_FORMAT;
  398. continue;
  399. }
  400. if (strict_strtoul(argv[i], 10, &value) < 0) {
  401. rs->ti->error = "Bad numerical argument given in raid params";
  402. return -EINVAL;
  403. }
  404. /* Parameters that take a numeric value are checked here */
  405. if (!strcasecmp(key, "rebuild")) {
  406. rebuild_cnt++;
  407. switch (rs->raid_type->level) {
  408. case 1:
  409. if (rebuild_cnt >= rs->md.raid_disks) {
  410. rs->ti->error = "Too many rebuild devices specified";
  411. return -EINVAL;
  412. }
  413. break;
  414. case 4:
  415. case 5:
  416. case 6:
  417. if (rebuild_cnt > rs->raid_type->parity_devs) {
  418. rs->ti->error = "Too many rebuild devices specified for given RAID type";
  419. return -EINVAL;
  420. }
  421. break;
  422. case 10:
  423. default:
  424. DMERR("The rebuild parameter is not supported for %s", rs->raid_type->name);
  425. rs->ti->error = "Rebuild not supported for this RAID type";
  426. return -EINVAL;
  427. }
  428. if (value > rs->md.raid_disks) {
  429. rs->ti->error = "Invalid rebuild index given";
  430. return -EINVAL;
  431. }
  432. clear_bit(In_sync, &rs->dev[value].rdev.flags);
  433. rs->dev[value].rdev.recovery_offset = 0;
  434. rs->print_flags |= DMPF_REBUILD;
  435. } else if (!strcasecmp(key, "write_mostly")) {
  436. if (rs->raid_type->level != 1) {
  437. rs->ti->error = "write_mostly option is only valid for RAID1";
  438. return -EINVAL;
  439. }
  440. if (value >= rs->md.raid_disks) {
  441. rs->ti->error = "Invalid write_mostly drive index given";
  442. return -EINVAL;
  443. }
  444. set_bit(WriteMostly, &rs->dev[value].rdev.flags);
  445. } else if (!strcasecmp(key, "max_write_behind")) {
  446. if (rs->raid_type->level != 1) {
  447. rs->ti->error = "max_write_behind option is only valid for RAID1";
  448. return -EINVAL;
  449. }
  450. rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
  451. /*
  452. * In device-mapper, we specify things in sectors, but
  453. * MD records this value in kB
  454. */
  455. value /= 2;
  456. if (value > COUNTER_MAX) {
  457. rs->ti->error = "Max write-behind limit out of range";
  458. return -EINVAL;
  459. }
  460. rs->md.bitmap_info.max_write_behind = value;
  461. } else if (!strcasecmp(key, "daemon_sleep")) {
  462. rs->print_flags |= DMPF_DAEMON_SLEEP;
  463. if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
  464. rs->ti->error = "daemon sleep period out of range";
  465. return -EINVAL;
  466. }
  467. rs->md.bitmap_info.daemon_sleep = value;
  468. } else if (!strcasecmp(key, "stripe_cache")) {
  469. rs->print_flags |= DMPF_STRIPE_CACHE;
  470. /*
  471. * In device-mapper, we specify things in sectors, but
  472. * MD records this value in kB
  473. */
  474. value /= 2;
  475. if ((rs->raid_type->level != 5) &&
  476. (rs->raid_type->level != 6)) {
  477. rs->ti->error = "Inappropriate argument: stripe_cache";
  478. return -EINVAL;
  479. }
  480. if (raid5_set_cache_size(&rs->md, (int)value)) {
  481. rs->ti->error = "Bad stripe_cache size";
  482. return -EINVAL;
  483. }
  484. } else if (!strcasecmp(key, "min_recovery_rate")) {
  485. rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
  486. if (value > INT_MAX) {
  487. rs->ti->error = "min_recovery_rate out of range";
  488. return -EINVAL;
  489. }
  490. rs->md.sync_speed_min = (int)value;
  491. } else if (!strcasecmp(key, "max_recovery_rate")) {
  492. rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
  493. if (value > INT_MAX) {
  494. rs->ti->error = "max_recovery_rate out of range";
  495. return -EINVAL;
  496. }
  497. rs->md.sync_speed_max = (int)value;
  498. } else if (!strcasecmp(key, "region_size")) {
  499. rs->print_flags |= DMPF_REGION_SIZE;
  500. region_size = value;
  501. } else if (!strcasecmp(key, "raid10_copies") &&
  502. (rs->raid_type->level == 10)) {
  503. if ((value < 2) || (value > 0xFF)) {
  504. rs->ti->error = "Bad value for 'raid10_copies'";
  505. return -EINVAL;
  506. }
  507. rs->print_flags |= DMPF_RAID10_COPIES;
  508. raid10_copies = value;
  509. } else {
  510. DMERR("Unable to parse RAID parameter: %s", key);
  511. rs->ti->error = "Unable to parse RAID parameters";
  512. return -EINVAL;
  513. }
  514. }
  515. if (validate_region_size(rs, region_size))
  516. return -EINVAL;
  517. if (rs->md.chunk_sectors)
  518. max_io_len = rs->md.chunk_sectors;
  519. else
  520. max_io_len = region_size;
  521. if (dm_set_target_max_io_len(rs->ti, max_io_len))
  522. return -EINVAL;
  523. if (rs->raid_type->level == 10) {
  524. if (raid10_copies > rs->md.raid_disks) {
  525. rs->ti->error = "Not enough devices to satisfy specification";
  526. return -EINVAL;
  527. }
  528. /* (Len * #mirrors) / #devices */
  529. sectors_per_dev = rs->ti->len * raid10_copies;
  530. sector_div(sectors_per_dev, rs->md.raid_disks);
  531. rs->md.layout = raid10_format_to_md_layout(raid10_format,
  532. raid10_copies);
  533. rs->md.new_layout = rs->md.layout;
  534. } else if ((rs->raid_type->level > 1) &&
  535. sector_div(sectors_per_dev,
  536. (rs->md.raid_disks - rs->raid_type->parity_devs))) {
  537. rs->ti->error = "Target length not divisible by number of data devices";
  538. return -EINVAL;
  539. }
  540. rs->md.dev_sectors = sectors_per_dev;
  541. /* Assume there are no metadata devices until the drives are parsed */
  542. rs->md.persistent = 0;
  543. rs->md.external = 1;
  544. return 0;
  545. }
  546. static void do_table_event(struct work_struct *ws)
  547. {
  548. struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
  549. dm_table_event(rs->ti->table);
  550. }
  551. static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
  552. {
  553. struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
  554. if (rs->raid_type->level == 1)
  555. return md_raid1_congested(&rs->md, bits);
  556. if (rs->raid_type->level == 10)
  557. return md_raid10_congested(&rs->md, bits);
  558. return md_raid5_congested(&rs->md, bits);
  559. }
  560. /*
  561. * This structure is never routinely used by userspace, unlike md superblocks.
  562. * Devices with this superblock should only ever be accessed via device-mapper.
  563. */
  564. #define DM_RAID_MAGIC 0x64526D44
  565. struct dm_raid_superblock {
  566. __le32 magic; /* "DmRd" */
  567. __le32 features; /* Used to indicate possible future changes */
  568. __le32 num_devices; /* Number of devices in this array. (Max 64) */
  569. __le32 array_position; /* The position of this drive in the array */
  570. __le64 events; /* Incremented by md when superblock updated */
  571. __le64 failed_devices; /* Bit field of devices to indicate failures */
  572. /*
  573. * This offset tracks the progress of the repair or replacement of
  574. * an individual drive.
  575. */
  576. __le64 disk_recovery_offset;
  577. /*
  578. * This offset tracks the progress of the initial array
  579. * synchronisation/parity calculation.
  580. */
  581. __le64 array_resync_offset;
  582. /*
  583. * RAID characteristics
  584. */
  585. __le32 level;
  586. __le32 layout;
  587. __le32 stripe_sectors;
  588. __u8 pad[452]; /* Round struct to 512 bytes. */
  589. /* Always set to 0 when writing. */
  590. } __packed;
  591. static int read_disk_sb(struct md_rdev *rdev, int size)
  592. {
  593. BUG_ON(!rdev->sb_page);
  594. if (rdev->sb_loaded)
  595. return 0;
  596. if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
  597. DMERR("Failed to read superblock of device at position %d",
  598. rdev->raid_disk);
  599. md_error(rdev->mddev, rdev);
  600. return -EINVAL;
  601. }
  602. rdev->sb_loaded = 1;
  603. return 0;
  604. }
  605. static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
  606. {
  607. int i;
  608. uint64_t failed_devices;
  609. struct dm_raid_superblock *sb;
  610. struct raid_set *rs = container_of(mddev, struct raid_set, md);
  611. sb = page_address(rdev->sb_page);
  612. failed_devices = le64_to_cpu(sb->failed_devices);
  613. for (i = 0; i < mddev->raid_disks; i++)
  614. if (!rs->dev[i].data_dev ||
  615. test_bit(Faulty, &(rs->dev[i].rdev.flags)))
  616. failed_devices |= (1ULL << i);
  617. memset(sb, 0, sizeof(*sb));
  618. sb->magic = cpu_to_le32(DM_RAID_MAGIC);
  619. sb->features = cpu_to_le32(0); /* No features yet */
  620. sb->num_devices = cpu_to_le32(mddev->raid_disks);
  621. sb->array_position = cpu_to_le32(rdev->raid_disk);
  622. sb->events = cpu_to_le64(mddev->events);
  623. sb->failed_devices = cpu_to_le64(failed_devices);
  624. sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
  625. sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
  626. sb->level = cpu_to_le32(mddev->level);
  627. sb->layout = cpu_to_le32(mddev->layout);
  628. sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
  629. }
  630. /*
  631. * super_load
  632. *
  633. * This function creates a superblock if one is not found on the device
  634. * and will decide which superblock to use if there's a choice.
  635. *
  636. * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
  637. */
  638. static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
  639. {
  640. int ret;
  641. struct dm_raid_superblock *sb;
  642. struct dm_raid_superblock *refsb;
  643. uint64_t events_sb, events_refsb;
  644. rdev->sb_start = 0;
  645. rdev->sb_size = sizeof(*sb);
  646. ret = read_disk_sb(rdev, rdev->sb_size);
  647. if (ret)
  648. return ret;
  649. sb = page_address(rdev->sb_page);
  650. /*
  651. * Two cases that we want to write new superblocks and rebuild:
  652. * 1) New device (no matching magic number)
  653. * 2) Device specified for rebuild (!In_sync w/ offset == 0)
  654. */
  655. if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
  656. (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
  657. super_sync(rdev->mddev, rdev);
  658. set_bit(FirstUse, &rdev->flags);
  659. /* Force writing of superblocks to disk */
  660. set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
  661. /* Any superblock is better than none, choose that if given */
  662. return refdev ? 0 : 1;
  663. }
  664. if (!refdev)
  665. return 1;
  666. events_sb = le64_to_cpu(sb->events);
  667. refsb = page_address(refdev->sb_page);
  668. events_refsb = le64_to_cpu(refsb->events);
  669. return (events_sb > events_refsb) ? 1 : 0;
  670. }
  671. static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
  672. {
  673. int role;
  674. struct raid_set *rs = container_of(mddev, struct raid_set, md);
  675. uint64_t events_sb;
  676. uint64_t failed_devices;
  677. struct dm_raid_superblock *sb;
  678. uint32_t new_devs = 0;
  679. uint32_t rebuilds = 0;
  680. struct md_rdev *r;
  681. struct dm_raid_superblock *sb2;
  682. sb = page_address(rdev->sb_page);
  683. events_sb = le64_to_cpu(sb->events);
  684. failed_devices = le64_to_cpu(sb->failed_devices);
  685. /*
  686. * Initialise to 1 if this is a new superblock.
  687. */
  688. mddev->events = events_sb ? : 1;
  689. /*
  690. * Reshaping is not currently allowed
  691. */
  692. if ((le32_to_cpu(sb->level) != mddev->level) ||
  693. (le32_to_cpu(sb->layout) != mddev->layout) ||
  694. (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors)) {
  695. DMERR("Reshaping arrays not yet supported.");
  696. return -EINVAL;
  697. }
  698. /* We can only change the number of devices in RAID1 right now */
  699. if ((rs->raid_type->level != 1) &&
  700. (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
  701. DMERR("Reshaping arrays not yet supported.");
  702. return -EINVAL;
  703. }
  704. if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
  705. mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
  706. /*
  707. * During load, we set FirstUse if a new superblock was written.
  708. * There are two reasons we might not have a superblock:
  709. * 1) The array is brand new - in which case, all of the
  710. * devices must have their In_sync bit set. Also,
  711. * recovery_cp must be 0, unless forced.
  712. * 2) This is a new device being added to an old array
  713. * and the new device needs to be rebuilt - in which
  714. * case the In_sync bit will /not/ be set and
  715. * recovery_cp must be MaxSector.
  716. */
  717. rdev_for_each(r, mddev) {
  718. if (!test_bit(In_sync, &r->flags)) {
  719. DMINFO("Device %d specified for rebuild: "
  720. "Clearing superblock", r->raid_disk);
  721. rebuilds++;
  722. } else if (test_bit(FirstUse, &r->flags))
  723. new_devs++;
  724. }
  725. if (!rebuilds) {
  726. if (new_devs == mddev->raid_disks) {
  727. DMINFO("Superblocks created for new array");
  728. set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
  729. } else if (new_devs) {
  730. DMERR("New device injected "
  731. "into existing array without 'rebuild' "
  732. "parameter specified");
  733. return -EINVAL;
  734. }
  735. } else if (new_devs) {
  736. DMERR("'rebuild' devices cannot be "
  737. "injected into an array with other first-time devices");
  738. return -EINVAL;
  739. } else if (mddev->recovery_cp != MaxSector) {
  740. DMERR("'rebuild' specified while array is not in-sync");
  741. return -EINVAL;
  742. }
  743. /*
  744. * Now we set the Faulty bit for those devices that are
  745. * recorded in the superblock as failed.
  746. */
  747. rdev_for_each(r, mddev) {
  748. if (!r->sb_page)
  749. continue;
  750. sb2 = page_address(r->sb_page);
  751. sb2->failed_devices = 0;
  752. /*
  753. * Check for any device re-ordering.
  754. */
  755. if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
  756. role = le32_to_cpu(sb2->array_position);
  757. if (role != r->raid_disk) {
  758. if (rs->raid_type->level != 1) {
  759. rs->ti->error = "Cannot change device "
  760. "positions in RAID array";
  761. return -EINVAL;
  762. }
  763. DMINFO("RAID1 device #%d now at position #%d",
  764. role, r->raid_disk);
  765. }
  766. /*
  767. * Partial recovery is performed on
  768. * returning failed devices.
  769. */
  770. if (failed_devices & (1 << role))
  771. set_bit(Faulty, &r->flags);
  772. }
  773. }
  774. return 0;
  775. }
  776. static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
  777. {
  778. struct dm_raid_superblock *sb = page_address(rdev->sb_page);
  779. /*
  780. * If mddev->events is not set, we know we have not yet initialized
  781. * the array.
  782. */
  783. if (!mddev->events && super_init_validation(mddev, rdev))
  784. return -EINVAL;
  785. mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
  786. rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
  787. if (!test_bit(FirstUse, &rdev->flags)) {
  788. rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
  789. if (rdev->recovery_offset != MaxSector)
  790. clear_bit(In_sync, &rdev->flags);
  791. }
  792. /*
  793. * If a device comes back, set it as not In_sync and no longer faulty.
  794. */
  795. if (test_bit(Faulty, &rdev->flags)) {
  796. clear_bit(Faulty, &rdev->flags);
  797. clear_bit(In_sync, &rdev->flags);
  798. rdev->saved_raid_disk = rdev->raid_disk;
  799. rdev->recovery_offset = 0;
  800. }
  801. clear_bit(FirstUse, &rdev->flags);
  802. return 0;
  803. }
  804. /*
  805. * Analyse superblocks and select the freshest.
  806. */
  807. static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
  808. {
  809. int ret;
  810. unsigned redundancy = 0;
  811. struct raid_dev *dev;
  812. struct md_rdev *rdev, *tmp, *freshest;
  813. struct mddev *mddev = &rs->md;
  814. switch (rs->raid_type->level) {
  815. case 1:
  816. redundancy = rs->md.raid_disks - 1;
  817. break;
  818. case 4:
  819. case 5:
  820. case 6:
  821. redundancy = rs->raid_type->parity_devs;
  822. break;
  823. case 10:
  824. redundancy = raid10_md_layout_to_copies(mddev->layout) - 1;
  825. break;
  826. default:
  827. ti->error = "Unknown RAID type";
  828. return -EINVAL;
  829. }
  830. freshest = NULL;
  831. rdev_for_each_safe(rdev, tmp, mddev) {
  832. if (!rdev->meta_bdev)
  833. continue;
  834. ret = super_load(rdev, freshest);
  835. switch (ret) {
  836. case 1:
  837. freshest = rdev;
  838. break;
  839. case 0:
  840. break;
  841. default:
  842. dev = container_of(rdev, struct raid_dev, rdev);
  843. if (redundancy--) {
  844. if (dev->meta_dev)
  845. dm_put_device(ti, dev->meta_dev);
  846. dev->meta_dev = NULL;
  847. rdev->meta_bdev = NULL;
  848. if (rdev->sb_page)
  849. put_page(rdev->sb_page);
  850. rdev->sb_page = NULL;
  851. rdev->sb_loaded = 0;
  852. /*
  853. * We might be able to salvage the data device
  854. * even though the meta device has failed. For
  855. * now, we behave as though '- -' had been
  856. * set for this device in the table.
  857. */
  858. if (dev->data_dev)
  859. dm_put_device(ti, dev->data_dev);
  860. dev->data_dev = NULL;
  861. rdev->bdev = NULL;
  862. list_del(&rdev->same_set);
  863. continue;
  864. }
  865. ti->error = "Failed to load superblock";
  866. return ret;
  867. }
  868. }
  869. if (!freshest)
  870. return 0;
  871. /*
  872. * Validation of the freshest device provides the source of
  873. * validation for the remaining devices.
  874. */
  875. ti->error = "Unable to assemble array: Invalid superblocks";
  876. if (super_validate(mddev, freshest))
  877. return -EINVAL;
  878. rdev_for_each(rdev, mddev)
  879. if ((rdev != freshest) && super_validate(mddev, rdev))
  880. return -EINVAL;
  881. return 0;
  882. }
  883. /*
  884. * Construct a RAID4/5/6 mapping:
  885. * Args:
  886. * <raid_type> <#raid_params> <raid_params> \
  887. * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
  888. *
  889. * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
  890. * details on possible <raid_params>.
  891. */
  892. static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
  893. {
  894. int ret;
  895. struct raid_type *rt;
  896. unsigned long num_raid_params, num_raid_devs;
  897. struct raid_set *rs = NULL;
  898. /* Must have at least <raid_type> <#raid_params> */
  899. if (argc < 2) {
  900. ti->error = "Too few arguments";
  901. return -EINVAL;
  902. }
  903. /* raid type */
  904. rt = get_raid_type(argv[0]);
  905. if (!rt) {
  906. ti->error = "Unrecognised raid_type";
  907. return -EINVAL;
  908. }
  909. argc--;
  910. argv++;
  911. /* number of RAID parameters */
  912. if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) {
  913. ti->error = "Cannot understand number of RAID parameters";
  914. return -EINVAL;
  915. }
  916. argc--;
  917. argv++;
  918. /* Skip over RAID params for now and find out # of devices */
  919. if (num_raid_params + 1 > argc) {
  920. ti->error = "Arguments do not agree with counts given";
  921. return -EINVAL;
  922. }
  923. if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
  924. (num_raid_devs >= INT_MAX)) {
  925. ti->error = "Cannot understand number of raid devices";
  926. return -EINVAL;
  927. }
  928. rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
  929. if (IS_ERR(rs))
  930. return PTR_ERR(rs);
  931. ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
  932. if (ret)
  933. goto bad;
  934. ret = -EINVAL;
  935. argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
  936. argv += num_raid_params + 1;
  937. if (argc != (num_raid_devs * 2)) {
  938. ti->error = "Supplied RAID devices does not match the count given";
  939. goto bad;
  940. }
  941. ret = dev_parms(rs, argv);
  942. if (ret)
  943. goto bad;
  944. rs->md.sync_super = super_sync;
  945. ret = analyse_superblocks(ti, rs);
  946. if (ret)
  947. goto bad;
  948. INIT_WORK(&rs->md.event_work, do_table_event);
  949. ti->private = rs;
  950. ti->num_flush_requests = 1;
  951. mutex_lock(&rs->md.reconfig_mutex);
  952. ret = md_run(&rs->md);
  953. rs->md.in_sync = 0; /* Assume already marked dirty */
  954. mutex_unlock(&rs->md.reconfig_mutex);
  955. if (ret) {
  956. ti->error = "Fail to run raid array";
  957. goto bad;
  958. }
  959. if (ti->len != rs->md.array_sectors) {
  960. ti->error = "Array size does not match requested target length";
  961. ret = -EINVAL;
  962. goto size_mismatch;
  963. }
  964. rs->callbacks.congested_fn = raid_is_congested;
  965. dm_table_add_target_callbacks(ti->table, &rs->callbacks);
  966. mddev_suspend(&rs->md);
  967. return 0;
  968. size_mismatch:
  969. md_stop(&rs->md);
  970. bad:
  971. context_free(rs);
  972. return ret;
  973. }
  974. static void raid_dtr(struct dm_target *ti)
  975. {
  976. struct raid_set *rs = ti->private;
  977. list_del_init(&rs->callbacks.list);
  978. md_stop(&rs->md);
  979. context_free(rs);
  980. }
  981. static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context)
  982. {
  983. struct raid_set *rs = ti->private;
  984. struct mddev *mddev = &rs->md;
  985. mddev->pers->make_request(mddev, bio);
  986. return DM_MAPIO_SUBMITTED;
  987. }
  988. static int raid_status(struct dm_target *ti, status_type_t type,
  989. unsigned status_flags, char *result, unsigned maxlen)
  990. {
  991. struct raid_set *rs = ti->private;
  992. unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
  993. unsigned sz = 0;
  994. int i, array_in_sync = 0;
  995. sector_t sync;
  996. switch (type) {
  997. case STATUSTYPE_INFO:
  998. DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
  999. if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
  1000. sync = rs->md.curr_resync_completed;
  1001. else
  1002. sync = rs->md.recovery_cp;
  1003. if (sync >= rs->md.resync_max_sectors) {
  1004. array_in_sync = 1;
  1005. sync = rs->md.resync_max_sectors;
  1006. } else {
  1007. /*
  1008. * The array may be doing an initial sync, or it may
  1009. * be rebuilding individual components. If all the
  1010. * devices are In_sync, then it is the array that is
  1011. * being initialized.
  1012. */
  1013. for (i = 0; i < rs->md.raid_disks; i++)
  1014. if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
  1015. array_in_sync = 1;
  1016. }
  1017. /*
  1018. * Status characters:
  1019. * 'D' = Dead/Failed device
  1020. * 'a' = Alive but not in-sync
  1021. * 'A' = Alive and in-sync
  1022. */
  1023. for (i = 0; i < rs->md.raid_disks; i++) {
  1024. if (test_bit(Faulty, &rs->dev[i].rdev.flags))
  1025. DMEMIT("D");
  1026. else if (!array_in_sync ||
  1027. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  1028. DMEMIT("a");
  1029. else
  1030. DMEMIT("A");
  1031. }
  1032. /*
  1033. * In-sync ratio:
  1034. * The in-sync ratio shows the progress of:
  1035. * - Initializing the array
  1036. * - Rebuilding a subset of devices of the array
  1037. * The user can distinguish between the two by referring
  1038. * to the status characters.
  1039. */
  1040. DMEMIT(" %llu/%llu",
  1041. (unsigned long long) sync,
  1042. (unsigned long long) rs->md.resync_max_sectors);
  1043. break;
  1044. case STATUSTYPE_TABLE:
  1045. /* The string you would use to construct this array */
  1046. for (i = 0; i < rs->md.raid_disks; i++) {
  1047. if ((rs->print_flags & DMPF_REBUILD) &&
  1048. rs->dev[i].data_dev &&
  1049. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  1050. raid_param_cnt += 2; /* for rebuilds */
  1051. if (rs->dev[i].data_dev &&
  1052. test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  1053. raid_param_cnt += 2;
  1054. }
  1055. raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
  1056. if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
  1057. raid_param_cnt--;
  1058. DMEMIT("%s %u %u", rs->raid_type->name,
  1059. raid_param_cnt, rs->md.chunk_sectors);
  1060. if ((rs->print_flags & DMPF_SYNC) &&
  1061. (rs->md.recovery_cp == MaxSector))
  1062. DMEMIT(" sync");
  1063. if (rs->print_flags & DMPF_NOSYNC)
  1064. DMEMIT(" nosync");
  1065. for (i = 0; i < rs->md.raid_disks; i++)
  1066. if ((rs->print_flags & DMPF_REBUILD) &&
  1067. rs->dev[i].data_dev &&
  1068. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  1069. DMEMIT(" rebuild %u", i);
  1070. if (rs->print_flags & DMPF_DAEMON_SLEEP)
  1071. DMEMIT(" daemon_sleep %lu",
  1072. rs->md.bitmap_info.daemon_sleep);
  1073. if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
  1074. DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
  1075. if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
  1076. DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
  1077. for (i = 0; i < rs->md.raid_disks; i++)
  1078. if (rs->dev[i].data_dev &&
  1079. test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  1080. DMEMIT(" write_mostly %u", i);
  1081. if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
  1082. DMEMIT(" max_write_behind %lu",
  1083. rs->md.bitmap_info.max_write_behind);
  1084. if (rs->print_flags & DMPF_STRIPE_CACHE) {
  1085. struct r5conf *conf = rs->md.private;
  1086. /* convert from kiB to sectors */
  1087. DMEMIT(" stripe_cache %d",
  1088. conf ? conf->max_nr_stripes * 2 : 0);
  1089. }
  1090. if (rs->print_flags & DMPF_REGION_SIZE)
  1091. DMEMIT(" region_size %lu",
  1092. rs->md.bitmap_info.chunksize >> 9);
  1093. if (rs->print_flags & DMPF_RAID10_COPIES)
  1094. DMEMIT(" raid10_copies %u",
  1095. raid10_md_layout_to_copies(rs->md.layout));
  1096. if (rs->print_flags & DMPF_RAID10_FORMAT)
  1097. DMEMIT(" raid10_format near");
  1098. DMEMIT(" %d", rs->md.raid_disks);
  1099. for (i = 0; i < rs->md.raid_disks; i++) {
  1100. if (rs->dev[i].meta_dev)
  1101. DMEMIT(" %s", rs->dev[i].meta_dev->name);
  1102. else
  1103. DMEMIT(" -");
  1104. if (rs->dev[i].data_dev)
  1105. DMEMIT(" %s", rs->dev[i].data_dev->name);
  1106. else
  1107. DMEMIT(" -");
  1108. }
  1109. }
  1110. return 0;
  1111. }
  1112. static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  1113. {
  1114. struct raid_set *rs = ti->private;
  1115. unsigned i;
  1116. int ret = 0;
  1117. for (i = 0; !ret && i < rs->md.raid_disks; i++)
  1118. if (rs->dev[i].data_dev)
  1119. ret = fn(ti,
  1120. rs->dev[i].data_dev,
  1121. 0, /* No offset on data devs */
  1122. rs->md.dev_sectors,
  1123. data);
  1124. return ret;
  1125. }
  1126. static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1127. {
  1128. struct raid_set *rs = ti->private;
  1129. unsigned chunk_size = rs->md.chunk_sectors << 9;
  1130. struct r5conf *conf = rs->md.private;
  1131. blk_limits_io_min(limits, chunk_size);
  1132. blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
  1133. }
  1134. static void raid_presuspend(struct dm_target *ti)
  1135. {
  1136. struct raid_set *rs = ti->private;
  1137. md_stop_writes(&rs->md);
  1138. }
  1139. static void raid_postsuspend(struct dm_target *ti)
  1140. {
  1141. struct raid_set *rs = ti->private;
  1142. mddev_suspend(&rs->md);
  1143. }
  1144. static void raid_resume(struct dm_target *ti)
  1145. {
  1146. struct raid_set *rs = ti->private;
  1147. set_bit(MD_CHANGE_DEVS, &rs->md.flags);
  1148. if (!rs->bitmap_loaded) {
  1149. bitmap_load(&rs->md);
  1150. rs->bitmap_loaded = 1;
  1151. }
  1152. clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
  1153. mddev_resume(&rs->md);
  1154. }
  1155. static struct target_type raid_target = {
  1156. .name = "raid",
  1157. .version = {1, 3, 0},
  1158. .module = THIS_MODULE,
  1159. .ctr = raid_ctr,
  1160. .dtr = raid_dtr,
  1161. .map = raid_map,
  1162. .status = raid_status,
  1163. .iterate_devices = raid_iterate_devices,
  1164. .io_hints = raid_io_hints,
  1165. .presuspend = raid_presuspend,
  1166. .postsuspend = raid_postsuspend,
  1167. .resume = raid_resume,
  1168. };
  1169. static int __init dm_raid_init(void)
  1170. {
  1171. return dm_register_target(&raid_target);
  1172. }
  1173. static void __exit dm_raid_exit(void)
  1174. {
  1175. dm_unregister_target(&raid_target);
  1176. }
  1177. module_init(dm_raid_init);
  1178. module_exit(dm_raid_exit);
  1179. MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
  1180. MODULE_ALIAS("dm-raid1");
  1181. MODULE_ALIAS("dm-raid10");
  1182. MODULE_ALIAS("dm-raid4");
  1183. MODULE_ALIAS("dm-raid5");
  1184. MODULE_ALIAS("dm-raid6");
  1185. MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
  1186. MODULE_LICENSE("GPL");