dm-raid.c 47 KB

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