dm-raid.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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 "bitmap.h"
  13. #include <linux/device-mapper.h>
  14. #define DM_MSG_PREFIX "raid"
  15. /*
  16. * The following flags are used by dm-raid.c to set up the array state.
  17. * They must be cleared before md_run is called.
  18. */
  19. #define FirstUse 10 /* rdev flag */
  20. struct raid_dev {
  21. /*
  22. * Two DM devices, one to hold metadata and one to hold the
  23. * actual data/parity. The reason for this is to not confuse
  24. * ti->len and give more flexibility in altering size and
  25. * characteristics.
  26. *
  27. * While it is possible for this device to be associated
  28. * with a different physical device than the data_dev, it
  29. * is intended for it to be the same.
  30. * |--------- Physical Device ---------|
  31. * |- meta_dev -|------ data_dev ------|
  32. */
  33. struct dm_dev *meta_dev;
  34. struct dm_dev *data_dev;
  35. struct md_rdev rdev;
  36. };
  37. /*
  38. * Flags for rs->print_flags field.
  39. */
  40. #define DMPF_SYNC 0x1
  41. #define DMPF_NOSYNC 0x2
  42. #define DMPF_REBUILD 0x4
  43. #define DMPF_DAEMON_SLEEP 0x8
  44. #define DMPF_MIN_RECOVERY_RATE 0x10
  45. #define DMPF_MAX_RECOVERY_RATE 0x20
  46. #define DMPF_MAX_WRITE_BEHIND 0x40
  47. #define DMPF_STRIPE_CACHE 0x80
  48. #define DMPF_REGION_SIZE 0X100
  49. struct raid_set {
  50. struct dm_target *ti;
  51. uint32_t bitmap_loaded;
  52. uint32_t print_flags;
  53. struct mddev md;
  54. struct raid_type *raid_type;
  55. struct dm_target_callbacks callbacks;
  56. struct raid_dev dev[0];
  57. };
  58. /* Supported raid types and properties. */
  59. static struct raid_type {
  60. const char *name; /* RAID algorithm. */
  61. const char *descr; /* Descriptor text for logging. */
  62. const unsigned parity_devs; /* # of parity devices. */
  63. const unsigned minimal_devs; /* minimal # of devices in set. */
  64. const unsigned level; /* RAID level. */
  65. const unsigned algorithm; /* RAID algorithm. */
  66. } raid_types[] = {
  67. {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
  68. {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
  69. {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
  70. {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
  71. {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
  72. {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
  73. {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
  74. {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
  75. {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
  76. };
  77. static struct raid_type *get_raid_type(char *name)
  78. {
  79. int i;
  80. for (i = 0; i < ARRAY_SIZE(raid_types); i++)
  81. if (!strcmp(raid_types[i].name, name))
  82. return &raid_types[i];
  83. return NULL;
  84. }
  85. static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
  86. {
  87. unsigned i;
  88. struct raid_set *rs;
  89. sector_t sectors_per_dev;
  90. if (raid_devs <= raid_type->parity_devs) {
  91. ti->error = "Insufficient number of devices";
  92. return ERR_PTR(-EINVAL);
  93. }
  94. sectors_per_dev = ti->len;
  95. if ((raid_type->level > 1) &&
  96. sector_div(sectors_per_dev, (raid_devs - raid_type->parity_devs))) {
  97. ti->error = "Target length not divisible by number of data devices";
  98. return ERR_PTR(-EINVAL);
  99. }
  100. rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
  101. if (!rs) {
  102. ti->error = "Cannot allocate raid context";
  103. return ERR_PTR(-ENOMEM);
  104. }
  105. mddev_init(&rs->md);
  106. rs->ti = ti;
  107. rs->raid_type = raid_type;
  108. rs->md.raid_disks = raid_devs;
  109. rs->md.level = raid_type->level;
  110. rs->md.new_level = rs->md.level;
  111. rs->md.dev_sectors = sectors_per_dev;
  112. rs->md.layout = raid_type->algorithm;
  113. rs->md.new_layout = rs->md.layout;
  114. rs->md.delta_disks = 0;
  115. rs->md.recovery_cp = 0;
  116. for (i = 0; i < raid_devs; i++)
  117. md_rdev_init(&rs->dev[i].rdev);
  118. /*
  119. * Remaining items to be initialized by further RAID params:
  120. * rs->md.persistent
  121. * rs->md.external
  122. * rs->md.chunk_sectors
  123. * rs->md.new_chunk_sectors
  124. */
  125. return rs;
  126. }
  127. static void context_free(struct raid_set *rs)
  128. {
  129. int i;
  130. for (i = 0; i < rs->md.raid_disks; i++) {
  131. if (rs->dev[i].meta_dev)
  132. dm_put_device(rs->ti, rs->dev[i].meta_dev);
  133. if (rs->dev[i].rdev.sb_page)
  134. put_page(rs->dev[i].rdev.sb_page);
  135. rs->dev[i].rdev.sb_page = NULL;
  136. rs->dev[i].rdev.sb_loaded = 0;
  137. if (rs->dev[i].data_dev)
  138. dm_put_device(rs->ti, rs->dev[i].data_dev);
  139. }
  140. kfree(rs);
  141. }
  142. /*
  143. * For every device we have two words
  144. * <meta_dev>: meta device name or '-' if missing
  145. * <data_dev>: data device name or '-' if missing
  146. *
  147. * The following are permitted:
  148. * - -
  149. * - <data_dev>
  150. * <meta_dev> <data_dev>
  151. *
  152. * The following is not allowed:
  153. * <meta_dev> -
  154. *
  155. * This code parses those words. If there is a failure,
  156. * the caller must use context_free to unwind the operations.
  157. */
  158. static int dev_parms(struct raid_set *rs, char **argv)
  159. {
  160. int i;
  161. int rebuild = 0;
  162. int metadata_available = 0;
  163. int ret = 0;
  164. for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
  165. rs->dev[i].rdev.raid_disk = i;
  166. rs->dev[i].meta_dev = NULL;
  167. rs->dev[i].data_dev = NULL;
  168. /*
  169. * There are no offsets, since there is a separate device
  170. * for data and metadata.
  171. */
  172. rs->dev[i].rdev.data_offset = 0;
  173. rs->dev[i].rdev.mddev = &rs->md;
  174. if (strcmp(argv[0], "-")) {
  175. ret = dm_get_device(rs->ti, argv[0],
  176. dm_table_get_mode(rs->ti->table),
  177. &rs->dev[i].meta_dev);
  178. rs->ti->error = "RAID metadata device lookup failure";
  179. if (ret)
  180. return ret;
  181. rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
  182. if (!rs->dev[i].rdev.sb_page)
  183. return -ENOMEM;
  184. }
  185. if (!strcmp(argv[1], "-")) {
  186. if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
  187. (!rs->dev[i].rdev.recovery_offset)) {
  188. rs->ti->error = "Drive designated for rebuild not specified";
  189. return -EINVAL;
  190. }
  191. rs->ti->error = "No data device supplied with metadata device";
  192. if (rs->dev[i].meta_dev)
  193. return -EINVAL;
  194. continue;
  195. }
  196. ret = dm_get_device(rs->ti, argv[1],
  197. dm_table_get_mode(rs->ti->table),
  198. &rs->dev[i].data_dev);
  199. if (ret) {
  200. rs->ti->error = "RAID device lookup failure";
  201. return ret;
  202. }
  203. if (rs->dev[i].meta_dev) {
  204. metadata_available = 1;
  205. rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
  206. }
  207. rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
  208. list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
  209. if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
  210. rebuild++;
  211. }
  212. if (metadata_available) {
  213. rs->md.external = 0;
  214. rs->md.persistent = 1;
  215. rs->md.major_version = 2;
  216. } else if (rebuild && !rs->md.recovery_cp) {
  217. /*
  218. * Without metadata, we will not be able to tell if the array
  219. * is in-sync or not - we must assume it is not. Therefore,
  220. * it is impossible to rebuild a drive.
  221. *
  222. * Even if there is metadata, the on-disk information may
  223. * indicate that the array is not in-sync and it will then
  224. * fail at that time.
  225. *
  226. * User could specify 'nosync' option if desperate.
  227. */
  228. DMERR("Unable to rebuild drive while array is not in-sync");
  229. rs->ti->error = "RAID device lookup failure";
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. /*
  235. * validate_region_size
  236. * @rs
  237. * @region_size: region size in sectors. If 0, pick a size (4MiB default).
  238. *
  239. * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
  240. * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
  241. *
  242. * Returns: 0 on success, -EINVAL on failure.
  243. */
  244. static int validate_region_size(struct raid_set *rs, unsigned long region_size)
  245. {
  246. unsigned long min_region_size = rs->ti->len / (1 << 21);
  247. if (!region_size) {
  248. /*
  249. * Choose a reasonable default. All figures in sectors.
  250. */
  251. if (min_region_size > (1 << 13)) {
  252. DMINFO("Choosing default region size of %lu sectors",
  253. region_size);
  254. region_size = min_region_size;
  255. } else {
  256. DMINFO("Choosing default region size of 4MiB");
  257. region_size = 1 << 13; /* sectors */
  258. }
  259. } else {
  260. /*
  261. * Validate user-supplied value.
  262. */
  263. if (region_size > rs->ti->len) {
  264. rs->ti->error = "Supplied region size is too large";
  265. return -EINVAL;
  266. }
  267. if (region_size < min_region_size) {
  268. DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
  269. region_size, min_region_size);
  270. rs->ti->error = "Supplied region size is too small";
  271. return -EINVAL;
  272. }
  273. if (!is_power_of_2(region_size)) {
  274. rs->ti->error = "Region size is not a power of 2";
  275. return -EINVAL;
  276. }
  277. if (region_size < rs->md.chunk_sectors) {
  278. rs->ti->error = "Region size is smaller than the chunk size";
  279. return -EINVAL;
  280. }
  281. }
  282. /*
  283. * Convert sectors to bytes.
  284. */
  285. rs->md.bitmap_info.chunksize = (region_size << 9);
  286. return 0;
  287. }
  288. /*
  289. * Possible arguments are...
  290. * <chunk_size> [optional_args]
  291. *
  292. * Argument definitions
  293. * <chunk_size> The number of sectors per disk that
  294. * will form the "stripe"
  295. * [[no]sync] Force or prevent recovery of the
  296. * entire array
  297. * [rebuild <idx>] Rebuild the drive indicated by the index
  298. * [daemon_sleep <ms>] Time between bitmap daemon work to
  299. * clear bits
  300. * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  301. * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  302. * [write_mostly <idx>] Indicate a write mostly drive via index
  303. * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
  304. * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
  305. * [region_size <sectors>] Defines granularity of bitmap
  306. */
  307. static int parse_raid_params(struct raid_set *rs, char **argv,
  308. unsigned num_raid_params)
  309. {
  310. unsigned i, rebuild_cnt = 0;
  311. unsigned long value, region_size = 0;
  312. char *key;
  313. /*
  314. * First, parse the in-order required arguments
  315. * "chunk_size" is the only argument of this type.
  316. */
  317. if ((strict_strtoul(argv[0], 10, &value) < 0)) {
  318. rs->ti->error = "Bad chunk size";
  319. return -EINVAL;
  320. } else if (rs->raid_type->level == 1) {
  321. if (value)
  322. DMERR("Ignoring chunk size parameter for RAID 1");
  323. value = 0;
  324. } else if (!is_power_of_2(value)) {
  325. rs->ti->error = "Chunk size must be a power of 2";
  326. return -EINVAL;
  327. } else if (value < 8) {
  328. rs->ti->error = "Chunk size value is too small";
  329. return -EINVAL;
  330. }
  331. rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
  332. argv++;
  333. num_raid_params--;
  334. /*
  335. * We set each individual device as In_sync with a completed
  336. * 'recovery_offset'. If there has been a device failure or
  337. * replacement then one of the following cases applies:
  338. *
  339. * 1) User specifies 'rebuild'.
  340. * - Device is reset when param is read.
  341. * 2) A new device is supplied.
  342. * - No matching superblock found, resets device.
  343. * 3) Device failure was transient and returns on reload.
  344. * - Failure noticed, resets device for bitmap replay.
  345. * 4) Device hadn't completed recovery after previous failure.
  346. * - Superblock is read and overrides recovery_offset.
  347. *
  348. * What is found in the superblocks of the devices is always
  349. * authoritative, unless 'rebuild' or '[no]sync' was specified.
  350. */
  351. for (i = 0; i < rs->md.raid_disks; i++) {
  352. set_bit(In_sync, &rs->dev[i].rdev.flags);
  353. rs->dev[i].rdev.recovery_offset = MaxSector;
  354. }
  355. /*
  356. * Second, parse the unordered optional arguments
  357. */
  358. for (i = 0; i < num_raid_params; i++) {
  359. if (!strcasecmp(argv[i], "nosync")) {
  360. rs->md.recovery_cp = MaxSector;
  361. rs->print_flags |= DMPF_NOSYNC;
  362. continue;
  363. }
  364. if (!strcasecmp(argv[i], "sync")) {
  365. rs->md.recovery_cp = 0;
  366. rs->print_flags |= DMPF_SYNC;
  367. continue;
  368. }
  369. /* The rest of the optional arguments come in key/value pairs */
  370. if ((i + 1) >= num_raid_params) {
  371. rs->ti->error = "Wrong number of raid parameters given";
  372. return -EINVAL;
  373. }
  374. key = argv[i++];
  375. if (strict_strtoul(argv[i], 10, &value) < 0) {
  376. rs->ti->error = "Bad numerical argument given in raid params";
  377. return -EINVAL;
  378. }
  379. if (!strcasecmp(key, "rebuild")) {
  380. rebuild_cnt++;
  381. if (((rs->raid_type->level != 1) &&
  382. (rebuild_cnt > rs->raid_type->parity_devs)) ||
  383. ((rs->raid_type->level == 1) &&
  384. (rebuild_cnt > (rs->md.raid_disks - 1)))) {
  385. rs->ti->error = "Too many rebuild devices specified for given RAID type";
  386. return -EINVAL;
  387. }
  388. if (value > rs->md.raid_disks) {
  389. rs->ti->error = "Invalid rebuild index given";
  390. return -EINVAL;
  391. }
  392. clear_bit(In_sync, &rs->dev[value].rdev.flags);
  393. rs->dev[value].rdev.recovery_offset = 0;
  394. rs->print_flags |= DMPF_REBUILD;
  395. } else if (!strcasecmp(key, "write_mostly")) {
  396. if (rs->raid_type->level != 1) {
  397. rs->ti->error = "write_mostly option is only valid for RAID1";
  398. return -EINVAL;
  399. }
  400. if (value >= rs->md.raid_disks) {
  401. rs->ti->error = "Invalid write_mostly drive index given";
  402. return -EINVAL;
  403. }
  404. set_bit(WriteMostly, &rs->dev[value].rdev.flags);
  405. } else if (!strcasecmp(key, "max_write_behind")) {
  406. if (rs->raid_type->level != 1) {
  407. rs->ti->error = "max_write_behind option is only valid for RAID1";
  408. return -EINVAL;
  409. }
  410. rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
  411. /*
  412. * In device-mapper, we specify things in sectors, but
  413. * MD records this value in kB
  414. */
  415. value /= 2;
  416. if (value > COUNTER_MAX) {
  417. rs->ti->error = "Max write-behind limit out of range";
  418. return -EINVAL;
  419. }
  420. rs->md.bitmap_info.max_write_behind = value;
  421. } else if (!strcasecmp(key, "daemon_sleep")) {
  422. rs->print_flags |= DMPF_DAEMON_SLEEP;
  423. if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
  424. rs->ti->error = "daemon sleep period out of range";
  425. return -EINVAL;
  426. }
  427. rs->md.bitmap_info.daemon_sleep = value;
  428. } else if (!strcasecmp(key, "stripe_cache")) {
  429. rs->print_flags |= DMPF_STRIPE_CACHE;
  430. /*
  431. * In device-mapper, we specify things in sectors, but
  432. * MD records this value in kB
  433. */
  434. value /= 2;
  435. if (rs->raid_type->level < 5) {
  436. rs->ti->error = "Inappropriate argument: stripe_cache";
  437. return -EINVAL;
  438. }
  439. if (raid5_set_cache_size(&rs->md, (int)value)) {
  440. rs->ti->error = "Bad stripe_cache size";
  441. return -EINVAL;
  442. }
  443. } else if (!strcasecmp(key, "min_recovery_rate")) {
  444. rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
  445. if (value > INT_MAX) {
  446. rs->ti->error = "min_recovery_rate out of range";
  447. return -EINVAL;
  448. }
  449. rs->md.sync_speed_min = (int)value;
  450. } else if (!strcasecmp(key, "max_recovery_rate")) {
  451. rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
  452. if (value > INT_MAX) {
  453. rs->ti->error = "max_recovery_rate out of range";
  454. return -EINVAL;
  455. }
  456. rs->md.sync_speed_max = (int)value;
  457. } else if (!strcasecmp(key, "region_size")) {
  458. rs->print_flags |= DMPF_REGION_SIZE;
  459. region_size = value;
  460. } else {
  461. DMERR("Unable to parse RAID parameter: %s", key);
  462. rs->ti->error = "Unable to parse RAID parameters";
  463. return -EINVAL;
  464. }
  465. }
  466. if (validate_region_size(rs, region_size))
  467. return -EINVAL;
  468. if (rs->md.chunk_sectors)
  469. rs->ti->split_io = rs->md.chunk_sectors;
  470. else
  471. rs->ti->split_io = region_size;
  472. if (rs->md.chunk_sectors)
  473. rs->ti->split_io = rs->md.chunk_sectors;
  474. else
  475. rs->ti->split_io = region_size;
  476. /* Assume there are no metadata devices until the drives are parsed */
  477. rs->md.persistent = 0;
  478. rs->md.external = 1;
  479. return 0;
  480. }
  481. static void do_table_event(struct work_struct *ws)
  482. {
  483. struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
  484. dm_table_event(rs->ti->table);
  485. }
  486. static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
  487. {
  488. struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
  489. if (rs->raid_type->level == 1)
  490. return md_raid1_congested(&rs->md, bits);
  491. return md_raid5_congested(&rs->md, bits);
  492. }
  493. /*
  494. * This structure is never routinely used by userspace, unlike md superblocks.
  495. * Devices with this superblock should only ever be accessed via device-mapper.
  496. */
  497. #define DM_RAID_MAGIC 0x64526D44
  498. struct dm_raid_superblock {
  499. __le32 magic; /* "DmRd" */
  500. __le32 features; /* Used to indicate possible future changes */
  501. __le32 num_devices; /* Number of devices in this array. (Max 64) */
  502. __le32 array_position; /* The position of this drive in the array */
  503. __le64 events; /* Incremented by md when superblock updated */
  504. __le64 failed_devices; /* Bit field of devices to indicate failures */
  505. /*
  506. * This offset tracks the progress of the repair or replacement of
  507. * an individual drive.
  508. */
  509. __le64 disk_recovery_offset;
  510. /*
  511. * This offset tracks the progress of the initial array
  512. * synchronisation/parity calculation.
  513. */
  514. __le64 array_resync_offset;
  515. /*
  516. * RAID characteristics
  517. */
  518. __le32 level;
  519. __le32 layout;
  520. __le32 stripe_sectors;
  521. __u8 pad[452]; /* Round struct to 512 bytes. */
  522. /* Always set to 0 when writing. */
  523. } __packed;
  524. static int read_disk_sb(struct md_rdev *rdev, int size)
  525. {
  526. BUG_ON(!rdev->sb_page);
  527. if (rdev->sb_loaded)
  528. return 0;
  529. if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
  530. DMERR("Failed to read device superblock");
  531. return -EINVAL;
  532. }
  533. rdev->sb_loaded = 1;
  534. return 0;
  535. }
  536. static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
  537. {
  538. struct md_rdev *r, *t;
  539. uint64_t failed_devices;
  540. struct dm_raid_superblock *sb;
  541. sb = page_address(rdev->sb_page);
  542. failed_devices = le64_to_cpu(sb->failed_devices);
  543. rdev_for_each(r, t, mddev)
  544. if ((r->raid_disk >= 0) && test_bit(Faulty, &r->flags))
  545. failed_devices |= (1ULL << r->raid_disk);
  546. memset(sb, 0, sizeof(*sb));
  547. sb->magic = cpu_to_le32(DM_RAID_MAGIC);
  548. sb->features = cpu_to_le32(0); /* No features yet */
  549. sb->num_devices = cpu_to_le32(mddev->raid_disks);
  550. sb->array_position = cpu_to_le32(rdev->raid_disk);
  551. sb->events = cpu_to_le64(mddev->events);
  552. sb->failed_devices = cpu_to_le64(failed_devices);
  553. sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
  554. sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
  555. sb->level = cpu_to_le32(mddev->level);
  556. sb->layout = cpu_to_le32(mddev->layout);
  557. sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
  558. }
  559. /*
  560. * super_load
  561. *
  562. * This function creates a superblock if one is not found on the device
  563. * and will decide which superblock to use if there's a choice.
  564. *
  565. * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
  566. */
  567. static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
  568. {
  569. int ret;
  570. struct dm_raid_superblock *sb;
  571. struct dm_raid_superblock *refsb;
  572. uint64_t events_sb, events_refsb;
  573. rdev->sb_start = 0;
  574. rdev->sb_size = sizeof(*sb);
  575. ret = read_disk_sb(rdev, rdev->sb_size);
  576. if (ret)
  577. return ret;
  578. sb = page_address(rdev->sb_page);
  579. /*
  580. * Two cases that we want to write new superblocks and rebuild:
  581. * 1) New device (no matching magic number)
  582. * 2) Device specified for rebuild (!In_sync w/ offset == 0)
  583. */
  584. if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
  585. (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
  586. super_sync(rdev->mddev, rdev);
  587. set_bit(FirstUse, &rdev->flags);
  588. /* Force writing of superblocks to disk */
  589. set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
  590. /* Any superblock is better than none, choose that if given */
  591. return refdev ? 0 : 1;
  592. }
  593. if (!refdev)
  594. return 1;
  595. events_sb = le64_to_cpu(sb->events);
  596. refsb = page_address(refdev->sb_page);
  597. events_refsb = le64_to_cpu(refsb->events);
  598. return (events_sb > events_refsb) ? 1 : 0;
  599. }
  600. static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
  601. {
  602. int role;
  603. struct raid_set *rs = container_of(mddev, struct raid_set, md);
  604. uint64_t events_sb;
  605. uint64_t failed_devices;
  606. struct dm_raid_superblock *sb;
  607. uint32_t new_devs = 0;
  608. uint32_t rebuilds = 0;
  609. struct md_rdev *r, *t;
  610. struct dm_raid_superblock *sb2;
  611. sb = page_address(rdev->sb_page);
  612. events_sb = le64_to_cpu(sb->events);
  613. failed_devices = le64_to_cpu(sb->failed_devices);
  614. /*
  615. * Initialise to 1 if this is a new superblock.
  616. */
  617. mddev->events = events_sb ? : 1;
  618. /*
  619. * Reshaping is not currently allowed
  620. */
  621. if ((le32_to_cpu(sb->level) != mddev->level) ||
  622. (le32_to_cpu(sb->layout) != mddev->layout) ||
  623. (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors)) {
  624. DMERR("Reshaping arrays not yet supported.");
  625. return -EINVAL;
  626. }
  627. /* We can only change the number of devices in RAID1 right now */
  628. if ((rs->raid_type->level != 1) &&
  629. (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
  630. DMERR("Reshaping arrays not yet supported.");
  631. return -EINVAL;
  632. }
  633. if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
  634. mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
  635. /*
  636. * During load, we set FirstUse if a new superblock was written.
  637. * There are two reasons we might not have a superblock:
  638. * 1) The array is brand new - in which case, all of the
  639. * devices must have their In_sync bit set. Also,
  640. * recovery_cp must be 0, unless forced.
  641. * 2) This is a new device being added to an old array
  642. * and the new device needs to be rebuilt - in which
  643. * case the In_sync bit will /not/ be set and
  644. * recovery_cp must be MaxSector.
  645. */
  646. rdev_for_each(r, t, mddev) {
  647. if (!test_bit(In_sync, &r->flags)) {
  648. DMINFO("Device %d specified for rebuild: "
  649. "Clearing superblock", r->raid_disk);
  650. rebuilds++;
  651. } else if (test_bit(FirstUse, &r->flags))
  652. new_devs++;
  653. }
  654. if (!rebuilds) {
  655. if (new_devs == mddev->raid_disks) {
  656. DMINFO("Superblocks created for new array");
  657. set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
  658. } else if (new_devs) {
  659. DMERR("New device injected "
  660. "into existing array without 'rebuild' "
  661. "parameter specified");
  662. return -EINVAL;
  663. }
  664. } else if (new_devs) {
  665. DMERR("'rebuild' devices cannot be "
  666. "injected into an array with other first-time devices");
  667. return -EINVAL;
  668. } else if (mddev->recovery_cp != MaxSector) {
  669. DMERR("'rebuild' specified while array is not in-sync");
  670. return -EINVAL;
  671. }
  672. /*
  673. * Now we set the Faulty bit for those devices that are
  674. * recorded in the superblock as failed.
  675. */
  676. rdev_for_each(r, t, mddev) {
  677. if (!r->sb_page)
  678. continue;
  679. sb2 = page_address(r->sb_page);
  680. sb2->failed_devices = 0;
  681. /*
  682. * Check for any device re-ordering.
  683. */
  684. if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
  685. role = le32_to_cpu(sb2->array_position);
  686. if (role != r->raid_disk) {
  687. if (rs->raid_type->level != 1) {
  688. rs->ti->error = "Cannot change device "
  689. "positions in RAID array";
  690. return -EINVAL;
  691. }
  692. DMINFO("RAID1 device #%d now at position #%d",
  693. role, r->raid_disk);
  694. }
  695. /*
  696. * Partial recovery is performed on
  697. * returning failed devices.
  698. */
  699. if (failed_devices & (1 << role))
  700. set_bit(Faulty, &r->flags);
  701. }
  702. }
  703. return 0;
  704. }
  705. static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
  706. {
  707. struct dm_raid_superblock *sb = page_address(rdev->sb_page);
  708. /*
  709. * If mddev->events is not set, we know we have not yet initialized
  710. * the array.
  711. */
  712. if (!mddev->events && super_init_validation(mddev, rdev))
  713. return -EINVAL;
  714. mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
  715. rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
  716. if (!test_bit(FirstUse, &rdev->flags)) {
  717. rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
  718. if (rdev->recovery_offset != MaxSector)
  719. clear_bit(In_sync, &rdev->flags);
  720. }
  721. /*
  722. * If a device comes back, set it as not In_sync and no longer faulty.
  723. */
  724. if (test_bit(Faulty, &rdev->flags)) {
  725. clear_bit(Faulty, &rdev->flags);
  726. clear_bit(In_sync, &rdev->flags);
  727. rdev->saved_raid_disk = rdev->raid_disk;
  728. rdev->recovery_offset = 0;
  729. }
  730. clear_bit(FirstUse, &rdev->flags);
  731. return 0;
  732. }
  733. /*
  734. * Analyse superblocks and select the freshest.
  735. */
  736. static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
  737. {
  738. int ret;
  739. struct md_rdev *rdev, *freshest, *tmp;
  740. struct mddev *mddev = &rs->md;
  741. freshest = NULL;
  742. rdev_for_each(rdev, tmp, mddev) {
  743. if (!rdev->meta_bdev)
  744. continue;
  745. ret = super_load(rdev, freshest);
  746. switch (ret) {
  747. case 1:
  748. freshest = rdev;
  749. break;
  750. case 0:
  751. break;
  752. default:
  753. ti->error = "Failed to load superblock";
  754. return ret;
  755. }
  756. }
  757. if (!freshest)
  758. return 0;
  759. /*
  760. * Validation of the freshest device provides the source of
  761. * validation for the remaining devices.
  762. */
  763. ti->error = "Unable to assemble array: Invalid superblocks";
  764. if (super_validate(mddev, freshest))
  765. return -EINVAL;
  766. rdev_for_each(rdev, tmp, mddev)
  767. if ((rdev != freshest) && super_validate(mddev, rdev))
  768. return -EINVAL;
  769. return 0;
  770. }
  771. /*
  772. * Construct a RAID4/5/6 mapping:
  773. * Args:
  774. * <raid_type> <#raid_params> <raid_params> \
  775. * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
  776. *
  777. * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
  778. * details on possible <raid_params>.
  779. */
  780. static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
  781. {
  782. int ret;
  783. struct raid_type *rt;
  784. unsigned long num_raid_params, num_raid_devs;
  785. struct raid_set *rs = NULL;
  786. /* Must have at least <raid_type> <#raid_params> */
  787. if (argc < 2) {
  788. ti->error = "Too few arguments";
  789. return -EINVAL;
  790. }
  791. /* raid type */
  792. rt = get_raid_type(argv[0]);
  793. if (!rt) {
  794. ti->error = "Unrecognised raid_type";
  795. return -EINVAL;
  796. }
  797. argc--;
  798. argv++;
  799. /* number of RAID parameters */
  800. if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) {
  801. ti->error = "Cannot understand number of RAID parameters";
  802. return -EINVAL;
  803. }
  804. argc--;
  805. argv++;
  806. /* Skip over RAID params for now and find out # of devices */
  807. if (num_raid_params + 1 > argc) {
  808. ti->error = "Arguments do not agree with counts given";
  809. return -EINVAL;
  810. }
  811. if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
  812. (num_raid_devs >= INT_MAX)) {
  813. ti->error = "Cannot understand number of raid devices";
  814. return -EINVAL;
  815. }
  816. rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
  817. if (IS_ERR(rs))
  818. return PTR_ERR(rs);
  819. ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
  820. if (ret)
  821. goto bad;
  822. ret = -EINVAL;
  823. argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
  824. argv += num_raid_params + 1;
  825. if (argc != (num_raid_devs * 2)) {
  826. ti->error = "Supplied RAID devices does not match the count given";
  827. goto bad;
  828. }
  829. ret = dev_parms(rs, argv);
  830. if (ret)
  831. goto bad;
  832. rs->md.sync_super = super_sync;
  833. ret = analyse_superblocks(ti, rs);
  834. if (ret)
  835. goto bad;
  836. INIT_WORK(&rs->md.event_work, do_table_event);
  837. ti->private = rs;
  838. ti->num_flush_requests = 1;
  839. mutex_lock(&rs->md.reconfig_mutex);
  840. ret = md_run(&rs->md);
  841. rs->md.in_sync = 0; /* Assume already marked dirty */
  842. mutex_unlock(&rs->md.reconfig_mutex);
  843. if (ret) {
  844. ti->error = "Fail to run raid array";
  845. goto bad;
  846. }
  847. rs->callbacks.congested_fn = raid_is_congested;
  848. dm_table_add_target_callbacks(ti->table, &rs->callbacks);
  849. mddev_suspend(&rs->md);
  850. return 0;
  851. bad:
  852. context_free(rs);
  853. return ret;
  854. }
  855. static void raid_dtr(struct dm_target *ti)
  856. {
  857. struct raid_set *rs = ti->private;
  858. list_del_init(&rs->callbacks.list);
  859. md_stop(&rs->md);
  860. context_free(rs);
  861. }
  862. static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context)
  863. {
  864. struct raid_set *rs = ti->private;
  865. struct mddev *mddev = &rs->md;
  866. mddev->pers->make_request(mddev, bio);
  867. return DM_MAPIO_SUBMITTED;
  868. }
  869. static int raid_status(struct dm_target *ti, status_type_t type,
  870. char *result, unsigned maxlen)
  871. {
  872. struct raid_set *rs = ti->private;
  873. unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
  874. unsigned sz = 0;
  875. int i, array_in_sync = 0;
  876. sector_t sync;
  877. switch (type) {
  878. case STATUSTYPE_INFO:
  879. DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
  880. if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
  881. sync = rs->md.curr_resync_completed;
  882. else
  883. sync = rs->md.recovery_cp;
  884. if (sync >= rs->md.resync_max_sectors) {
  885. array_in_sync = 1;
  886. sync = rs->md.resync_max_sectors;
  887. } else {
  888. /*
  889. * The array may be doing an initial sync, or it may
  890. * be rebuilding individual components. If all the
  891. * devices are In_sync, then it is the array that is
  892. * being initialized.
  893. */
  894. for (i = 0; i < rs->md.raid_disks; i++)
  895. if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
  896. array_in_sync = 1;
  897. }
  898. /*
  899. * Status characters:
  900. * 'D' = Dead/Failed device
  901. * 'a' = Alive but not in-sync
  902. * 'A' = Alive and in-sync
  903. */
  904. for (i = 0; i < rs->md.raid_disks; i++) {
  905. if (test_bit(Faulty, &rs->dev[i].rdev.flags))
  906. DMEMIT("D");
  907. else if (!array_in_sync ||
  908. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  909. DMEMIT("a");
  910. else
  911. DMEMIT("A");
  912. }
  913. /*
  914. * In-sync ratio:
  915. * The in-sync ratio shows the progress of:
  916. * - Initializing the array
  917. * - Rebuilding a subset of devices of the array
  918. * The user can distinguish between the two by referring
  919. * to the status characters.
  920. */
  921. DMEMIT(" %llu/%llu",
  922. (unsigned long long) sync,
  923. (unsigned long long) rs->md.resync_max_sectors);
  924. break;
  925. case STATUSTYPE_TABLE:
  926. /* The string you would use to construct this array */
  927. for (i = 0; i < rs->md.raid_disks; i++) {
  928. if ((rs->print_flags & DMPF_REBUILD) &&
  929. rs->dev[i].data_dev &&
  930. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  931. raid_param_cnt += 2; /* for rebuilds */
  932. if (rs->dev[i].data_dev &&
  933. test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  934. raid_param_cnt += 2;
  935. }
  936. raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
  937. if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
  938. raid_param_cnt--;
  939. DMEMIT("%s %u %u", rs->raid_type->name,
  940. raid_param_cnt, rs->md.chunk_sectors);
  941. if ((rs->print_flags & DMPF_SYNC) &&
  942. (rs->md.recovery_cp == MaxSector))
  943. DMEMIT(" sync");
  944. if (rs->print_flags & DMPF_NOSYNC)
  945. DMEMIT(" nosync");
  946. for (i = 0; i < rs->md.raid_disks; i++)
  947. if ((rs->print_flags & DMPF_REBUILD) &&
  948. rs->dev[i].data_dev &&
  949. !test_bit(In_sync, &rs->dev[i].rdev.flags))
  950. DMEMIT(" rebuild %u", i);
  951. if (rs->print_flags & DMPF_DAEMON_SLEEP)
  952. DMEMIT(" daemon_sleep %lu",
  953. rs->md.bitmap_info.daemon_sleep);
  954. if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
  955. DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
  956. if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
  957. DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
  958. for (i = 0; i < rs->md.raid_disks; i++)
  959. if (rs->dev[i].data_dev &&
  960. test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  961. DMEMIT(" write_mostly %u", i);
  962. if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
  963. DMEMIT(" max_write_behind %lu",
  964. rs->md.bitmap_info.max_write_behind);
  965. if (rs->print_flags & DMPF_STRIPE_CACHE) {
  966. struct r5conf *conf = rs->md.private;
  967. /* convert from kiB to sectors */
  968. DMEMIT(" stripe_cache %d",
  969. conf ? conf->max_nr_stripes * 2 : 0);
  970. }
  971. if (rs->print_flags & DMPF_REGION_SIZE)
  972. DMEMIT(" region_size %lu",
  973. rs->md.bitmap_info.chunksize >> 9);
  974. DMEMIT(" %d", rs->md.raid_disks);
  975. for (i = 0; i < rs->md.raid_disks; i++) {
  976. if (rs->dev[i].meta_dev)
  977. DMEMIT(" %s", rs->dev[i].meta_dev->name);
  978. else
  979. DMEMIT(" -");
  980. if (rs->dev[i].data_dev)
  981. DMEMIT(" %s", rs->dev[i].data_dev->name);
  982. else
  983. DMEMIT(" -");
  984. }
  985. }
  986. return 0;
  987. }
  988. static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  989. {
  990. struct raid_set *rs = ti->private;
  991. unsigned i;
  992. int ret = 0;
  993. for (i = 0; !ret && i < rs->md.raid_disks; i++)
  994. if (rs->dev[i].data_dev)
  995. ret = fn(ti,
  996. rs->dev[i].data_dev,
  997. 0, /* No offset on data devs */
  998. rs->md.dev_sectors,
  999. data);
  1000. return ret;
  1001. }
  1002. static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1003. {
  1004. struct raid_set *rs = ti->private;
  1005. unsigned chunk_size = rs->md.chunk_sectors << 9;
  1006. struct r5conf *conf = rs->md.private;
  1007. blk_limits_io_min(limits, chunk_size);
  1008. blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
  1009. }
  1010. static void raid_presuspend(struct dm_target *ti)
  1011. {
  1012. struct raid_set *rs = ti->private;
  1013. md_stop_writes(&rs->md);
  1014. }
  1015. static void raid_postsuspend(struct dm_target *ti)
  1016. {
  1017. struct raid_set *rs = ti->private;
  1018. mddev_suspend(&rs->md);
  1019. }
  1020. static void raid_resume(struct dm_target *ti)
  1021. {
  1022. struct raid_set *rs = ti->private;
  1023. if (!rs->bitmap_loaded) {
  1024. bitmap_load(&rs->md);
  1025. rs->bitmap_loaded = 1;
  1026. } else
  1027. md_wakeup_thread(rs->md.thread);
  1028. mddev_resume(&rs->md);
  1029. }
  1030. static struct target_type raid_target = {
  1031. .name = "raid",
  1032. .version = {1, 1, 0},
  1033. .module = THIS_MODULE,
  1034. .ctr = raid_ctr,
  1035. .dtr = raid_dtr,
  1036. .map = raid_map,
  1037. .status = raid_status,
  1038. .iterate_devices = raid_iterate_devices,
  1039. .io_hints = raid_io_hints,
  1040. .presuspend = raid_presuspend,
  1041. .postsuspend = raid_postsuspend,
  1042. .resume = raid_resume,
  1043. };
  1044. static int __init dm_raid_init(void)
  1045. {
  1046. return dm_register_target(&raid_target);
  1047. }
  1048. static void __exit dm_raid_exit(void)
  1049. {
  1050. dm_unregister_target(&raid_target);
  1051. }
  1052. module_init(dm_raid_init);
  1053. module_exit(dm_raid_exit);
  1054. MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
  1055. MODULE_ALIAS("dm-raid4");
  1056. MODULE_ALIAS("dm-raid5");
  1057. MODULE_ALIAS("dm-raid6");
  1058. MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
  1059. MODULE_LICENSE("GPL");