dm-raid.c 43 KB

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