dm-raid.c 38 KB

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