dm-raid.c 32 KB

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