dm-raid.c 38 KB

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