dm-table.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. /*
  2. * Copyright (C) 2001 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include <linux/module.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/namei.h>
  12. #include <linux/ctype.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include <linux/atomic.h>
  19. #include <linux/blk-mq.h>
  20. #include <linux/mount.h>
  21. #define DM_MSG_PREFIX "table"
  22. #define MAX_DEPTH 16
  23. #define NODE_SIZE L1_CACHE_BYTES
  24. #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
  25. #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
  26. struct dm_table {
  27. struct mapped_device *md;
  28. unsigned type;
  29. /* btree table */
  30. unsigned int depth;
  31. unsigned int counts[MAX_DEPTH]; /* in nodes */
  32. sector_t *index[MAX_DEPTH];
  33. unsigned int num_targets;
  34. unsigned int num_allocated;
  35. sector_t *highs;
  36. struct dm_target *targets;
  37. struct target_type *immutable_target_type;
  38. unsigned integrity_supported:1;
  39. unsigned singleton:1;
  40. /*
  41. * Indicates the rw permissions for the new logical
  42. * device. This should be a combination of FMODE_READ
  43. * and FMODE_WRITE.
  44. */
  45. fmode_t mode;
  46. /* a list of devices used by this table */
  47. struct list_head devices;
  48. /* events get handed up using this callback */
  49. void (*event_fn)(void *);
  50. void *event_context;
  51. struct dm_md_mempools *mempools;
  52. struct list_head target_callbacks;
  53. };
  54. /*
  55. * Similar to ceiling(log_size(n))
  56. */
  57. static unsigned int int_log(unsigned int n, unsigned int base)
  58. {
  59. int result = 0;
  60. while (n > 1) {
  61. n = dm_div_up(n, base);
  62. result++;
  63. }
  64. return result;
  65. }
  66. /*
  67. * Calculate the index of the child node of the n'th node k'th key.
  68. */
  69. static inline unsigned int get_child(unsigned int n, unsigned int k)
  70. {
  71. return (n * CHILDREN_PER_NODE) + k;
  72. }
  73. /*
  74. * Return the n'th node of level l from table t.
  75. */
  76. static inline sector_t *get_node(struct dm_table *t,
  77. unsigned int l, unsigned int n)
  78. {
  79. return t->index[l] + (n * KEYS_PER_NODE);
  80. }
  81. /*
  82. * Return the highest key that you could lookup from the n'th
  83. * node on level l of the btree.
  84. */
  85. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  86. {
  87. for (; l < t->depth - 1; l++)
  88. n = get_child(n, CHILDREN_PER_NODE - 1);
  89. if (n >= t->counts[l])
  90. return (sector_t) - 1;
  91. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  92. }
  93. /*
  94. * Fills in a level of the btree based on the highs of the level
  95. * below it.
  96. */
  97. static int setup_btree_index(unsigned int l, struct dm_table *t)
  98. {
  99. unsigned int n, k;
  100. sector_t *node;
  101. for (n = 0U; n < t->counts[l]; n++) {
  102. node = get_node(t, l, n);
  103. for (k = 0U; k < KEYS_PER_NODE; k++)
  104. node[k] = high(t, l + 1, get_child(n, k));
  105. }
  106. return 0;
  107. }
  108. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  109. {
  110. unsigned long size;
  111. void *addr;
  112. /*
  113. * Check that we're not going to overflow.
  114. */
  115. if (nmemb > (ULONG_MAX / elem_size))
  116. return NULL;
  117. size = nmemb * elem_size;
  118. addr = vzalloc(size);
  119. return addr;
  120. }
  121. EXPORT_SYMBOL(dm_vcalloc);
  122. /*
  123. * highs, and targets are managed as dynamic arrays during a
  124. * table load.
  125. */
  126. static int alloc_targets(struct dm_table *t, unsigned int num)
  127. {
  128. sector_t *n_highs;
  129. struct dm_target *n_targets;
  130. /*
  131. * Allocate both the target array and offset array at once.
  132. * Append an empty entry to catch sectors beyond the end of
  133. * the device.
  134. */
  135. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  136. sizeof(sector_t));
  137. if (!n_highs)
  138. return -ENOMEM;
  139. n_targets = (struct dm_target *) (n_highs + num);
  140. memset(n_highs, -1, sizeof(*n_highs) * num);
  141. vfree(t->highs);
  142. t->num_allocated = num;
  143. t->highs = n_highs;
  144. t->targets = n_targets;
  145. return 0;
  146. }
  147. int dm_table_create(struct dm_table **result, fmode_t mode,
  148. unsigned num_targets, struct mapped_device *md)
  149. {
  150. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  151. if (!t)
  152. return -ENOMEM;
  153. INIT_LIST_HEAD(&t->devices);
  154. INIT_LIST_HEAD(&t->target_callbacks);
  155. if (!num_targets)
  156. num_targets = KEYS_PER_NODE;
  157. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  158. if (!num_targets) {
  159. kfree(t);
  160. return -ENOMEM;
  161. }
  162. if (alloc_targets(t, num_targets)) {
  163. kfree(t);
  164. return -ENOMEM;
  165. }
  166. t->mode = mode;
  167. t->md = md;
  168. *result = t;
  169. return 0;
  170. }
  171. static void free_devices(struct list_head *devices, struct mapped_device *md)
  172. {
  173. struct list_head *tmp, *next;
  174. list_for_each_safe(tmp, next, devices) {
  175. struct dm_dev_internal *dd =
  176. list_entry(tmp, struct dm_dev_internal, list);
  177. DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
  178. dm_device_name(md), dd->dm_dev->name);
  179. dm_put_table_device(md, dd->dm_dev);
  180. kfree(dd);
  181. }
  182. }
  183. void dm_table_destroy(struct dm_table *t)
  184. {
  185. unsigned int i;
  186. if (!t)
  187. return;
  188. /* free the indexes */
  189. if (t->depth >= 2)
  190. vfree(t->index[t->depth - 2]);
  191. /* free the targets */
  192. for (i = 0; i < t->num_targets; i++) {
  193. struct dm_target *tgt = t->targets + i;
  194. if (tgt->type->dtr)
  195. tgt->type->dtr(tgt);
  196. dm_put_target_type(tgt->type);
  197. }
  198. vfree(t->highs);
  199. /* free the device list */
  200. free_devices(&t->devices, t->md);
  201. dm_free_md_mempools(t->mempools);
  202. kfree(t);
  203. }
  204. /*
  205. * See if we've already got a device in the list.
  206. */
  207. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  208. {
  209. struct dm_dev_internal *dd;
  210. list_for_each_entry (dd, l, list)
  211. if (dd->dm_dev->bdev->bd_dev == dev)
  212. return dd;
  213. return NULL;
  214. }
  215. /*
  216. * If possible, this checks an area of a destination device is invalid.
  217. */
  218. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  219. sector_t start, sector_t len, void *data)
  220. {
  221. struct request_queue *q;
  222. struct queue_limits *limits = data;
  223. struct block_device *bdev = dev->bdev;
  224. sector_t dev_size =
  225. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  226. unsigned short logical_block_size_sectors =
  227. limits->logical_block_size >> SECTOR_SHIFT;
  228. char b[BDEVNAME_SIZE];
  229. /*
  230. * Some devices exist without request functions,
  231. * such as loop devices not yet bound to backing files.
  232. * Forbid the use of such devices.
  233. */
  234. q = bdev_get_queue(bdev);
  235. if (!q || !q->make_request_fn) {
  236. DMWARN("%s: %s is not yet initialised: "
  237. "start=%llu, len=%llu, dev_size=%llu",
  238. dm_device_name(ti->table->md), bdevname(bdev, b),
  239. (unsigned long long)start,
  240. (unsigned long long)len,
  241. (unsigned long long)dev_size);
  242. return 1;
  243. }
  244. if (!dev_size)
  245. return 0;
  246. if ((start >= dev_size) || (start + len > dev_size)) {
  247. DMWARN("%s: %s too small for target: "
  248. "start=%llu, len=%llu, dev_size=%llu",
  249. dm_device_name(ti->table->md), bdevname(bdev, b),
  250. (unsigned long long)start,
  251. (unsigned long long)len,
  252. (unsigned long long)dev_size);
  253. return 1;
  254. }
  255. if (logical_block_size_sectors <= 1)
  256. return 0;
  257. if (start & (logical_block_size_sectors - 1)) {
  258. DMWARN("%s: start=%llu not aligned to h/w "
  259. "logical block size %u of %s",
  260. dm_device_name(ti->table->md),
  261. (unsigned long long)start,
  262. limits->logical_block_size, bdevname(bdev, b));
  263. return 1;
  264. }
  265. if (len & (logical_block_size_sectors - 1)) {
  266. DMWARN("%s: len=%llu not aligned to h/w "
  267. "logical block size %u of %s",
  268. dm_device_name(ti->table->md),
  269. (unsigned long long)len,
  270. limits->logical_block_size, bdevname(bdev, b));
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. /*
  276. * This upgrades the mode on an already open dm_dev, being
  277. * careful to leave things as they were if we fail to reopen the
  278. * device and not to touch the existing bdev field in case
  279. * it is accessed concurrently inside dm_table_any_congested().
  280. */
  281. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  282. struct mapped_device *md)
  283. {
  284. int r;
  285. struct dm_dev *old_dev, *new_dev;
  286. old_dev = dd->dm_dev;
  287. r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
  288. dd->dm_dev->mode | new_mode, &new_dev);
  289. if (r)
  290. return r;
  291. dd->dm_dev = new_dev;
  292. dm_put_table_device(md, old_dev);
  293. return 0;
  294. }
  295. /*
  296. * Convert the path to a device
  297. */
  298. dev_t dm_get_dev_t(const char *path)
  299. {
  300. dev_t uninitialized_var(dev);
  301. struct block_device *bdev;
  302. bdev = lookup_bdev(path);
  303. if (IS_ERR(bdev))
  304. dev = name_to_dev_t(path);
  305. else {
  306. dev = bdev->bd_dev;
  307. bdput(bdev);
  308. }
  309. return dev;
  310. }
  311. EXPORT_SYMBOL_GPL(dm_get_dev_t);
  312. /*
  313. * Add a device to the list, or just increment the usage count if
  314. * it's already present.
  315. */
  316. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  317. struct dm_dev **result)
  318. {
  319. int r;
  320. dev_t dev;
  321. struct dm_dev_internal *dd;
  322. struct dm_table *t = ti->table;
  323. BUG_ON(!t);
  324. dev = dm_get_dev_t(path);
  325. if (!dev)
  326. return -ENODEV;
  327. dd = find_device(&t->devices, dev);
  328. if (!dd) {
  329. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  330. if (!dd)
  331. return -ENOMEM;
  332. if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
  333. kfree(dd);
  334. return r;
  335. }
  336. atomic_set(&dd->count, 0);
  337. list_add(&dd->list, &t->devices);
  338. } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
  339. r = upgrade_mode(dd, mode, t->md);
  340. if (r)
  341. return r;
  342. }
  343. atomic_inc(&dd->count);
  344. *result = dd->dm_dev;
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(dm_get_device);
  348. static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  349. sector_t start, sector_t len, void *data)
  350. {
  351. struct queue_limits *limits = data;
  352. struct block_device *bdev = dev->bdev;
  353. struct request_queue *q = bdev_get_queue(bdev);
  354. char b[BDEVNAME_SIZE];
  355. if (unlikely(!q)) {
  356. DMWARN("%s: Cannot set limits for nonexistent device %s",
  357. dm_device_name(ti->table->md), bdevname(bdev, b));
  358. return 0;
  359. }
  360. if (bdev_stack_limits(limits, bdev, start) < 0)
  361. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  362. "physical_block_size=%u, logical_block_size=%u, "
  363. "alignment_offset=%u, start=%llu",
  364. dm_device_name(ti->table->md), bdevname(bdev, b),
  365. q->limits.physical_block_size,
  366. q->limits.logical_block_size,
  367. q->limits.alignment_offset,
  368. (unsigned long long) start << SECTOR_SHIFT);
  369. return 0;
  370. }
  371. /*
  372. * Decrement a device's use count and remove it if necessary.
  373. */
  374. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  375. {
  376. int found = 0;
  377. struct list_head *devices = &ti->table->devices;
  378. struct dm_dev_internal *dd;
  379. list_for_each_entry(dd, devices, list) {
  380. if (dd->dm_dev == d) {
  381. found = 1;
  382. break;
  383. }
  384. }
  385. if (!found) {
  386. DMWARN("%s: device %s not in table devices list",
  387. dm_device_name(ti->table->md), d->name);
  388. return;
  389. }
  390. if (atomic_dec_and_test(&dd->count)) {
  391. dm_put_table_device(ti->table->md, d);
  392. list_del(&dd->list);
  393. kfree(dd);
  394. }
  395. }
  396. EXPORT_SYMBOL(dm_put_device);
  397. /*
  398. * Checks to see if the target joins onto the end of the table.
  399. */
  400. static int adjoin(struct dm_table *table, struct dm_target *ti)
  401. {
  402. struct dm_target *prev;
  403. if (!table->num_targets)
  404. return !ti->begin;
  405. prev = &table->targets[table->num_targets - 1];
  406. return (ti->begin == (prev->begin + prev->len));
  407. }
  408. /*
  409. * Used to dynamically allocate the arg array.
  410. *
  411. * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
  412. * process messages even if some device is suspended. These messages have a
  413. * small fixed number of arguments.
  414. *
  415. * On the other hand, dm-switch needs to process bulk data using messages and
  416. * excessive use of GFP_NOIO could cause trouble.
  417. */
  418. static char **realloc_argv(unsigned *array_size, char **old_argv)
  419. {
  420. char **argv;
  421. unsigned new_size;
  422. gfp_t gfp;
  423. if (*array_size) {
  424. new_size = *array_size * 2;
  425. gfp = GFP_KERNEL;
  426. } else {
  427. new_size = 8;
  428. gfp = GFP_NOIO;
  429. }
  430. argv = kmalloc(new_size * sizeof(*argv), gfp);
  431. if (argv) {
  432. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  433. *array_size = new_size;
  434. }
  435. kfree(old_argv);
  436. return argv;
  437. }
  438. /*
  439. * Destructively splits up the argument list to pass to ctr.
  440. */
  441. int dm_split_args(int *argc, char ***argvp, char *input)
  442. {
  443. char *start, *end = input, *out, **argv = NULL;
  444. unsigned array_size = 0;
  445. *argc = 0;
  446. if (!input) {
  447. *argvp = NULL;
  448. return 0;
  449. }
  450. argv = realloc_argv(&array_size, argv);
  451. if (!argv)
  452. return -ENOMEM;
  453. while (1) {
  454. /* Skip whitespace */
  455. start = skip_spaces(end);
  456. if (!*start)
  457. break; /* success, we hit the end */
  458. /* 'out' is used to remove any back-quotes */
  459. end = out = start;
  460. while (*end) {
  461. /* Everything apart from '\0' can be quoted */
  462. if (*end == '\\' && *(end + 1)) {
  463. *out++ = *(end + 1);
  464. end += 2;
  465. continue;
  466. }
  467. if (isspace(*end))
  468. break; /* end of token */
  469. *out++ = *end++;
  470. }
  471. /* have we already filled the array ? */
  472. if ((*argc + 1) > array_size) {
  473. argv = realloc_argv(&array_size, argv);
  474. if (!argv)
  475. return -ENOMEM;
  476. }
  477. /* we know this is whitespace */
  478. if (*end)
  479. end++;
  480. /* terminate the string and put it in the array */
  481. *out = '\0';
  482. argv[*argc] = start;
  483. (*argc)++;
  484. }
  485. *argvp = argv;
  486. return 0;
  487. }
  488. /*
  489. * Impose necessary and sufficient conditions on a devices's table such
  490. * that any incoming bio which respects its logical_block_size can be
  491. * processed successfully. If it falls across the boundary between
  492. * two or more targets, the size of each piece it gets split into must
  493. * be compatible with the logical_block_size of the target processing it.
  494. */
  495. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  496. struct queue_limits *limits)
  497. {
  498. /*
  499. * This function uses arithmetic modulo the logical_block_size
  500. * (in units of 512-byte sectors).
  501. */
  502. unsigned short device_logical_block_size_sects =
  503. limits->logical_block_size >> SECTOR_SHIFT;
  504. /*
  505. * Offset of the start of the next table entry, mod logical_block_size.
  506. */
  507. unsigned short next_target_start = 0;
  508. /*
  509. * Given an aligned bio that extends beyond the end of a
  510. * target, how many sectors must the next target handle?
  511. */
  512. unsigned short remaining = 0;
  513. struct dm_target *uninitialized_var(ti);
  514. struct queue_limits ti_limits;
  515. unsigned i = 0;
  516. /*
  517. * Check each entry in the table in turn.
  518. */
  519. while (i < dm_table_get_num_targets(table)) {
  520. ti = dm_table_get_target(table, i++);
  521. blk_set_stacking_limits(&ti_limits);
  522. /* combine all target devices' limits */
  523. if (ti->type->iterate_devices)
  524. ti->type->iterate_devices(ti, dm_set_device_limits,
  525. &ti_limits);
  526. /*
  527. * If the remaining sectors fall entirely within this
  528. * table entry are they compatible with its logical_block_size?
  529. */
  530. if (remaining < ti->len &&
  531. remaining & ((ti_limits.logical_block_size >>
  532. SECTOR_SHIFT) - 1))
  533. break; /* Error */
  534. next_target_start =
  535. (unsigned short) ((next_target_start + ti->len) &
  536. (device_logical_block_size_sects - 1));
  537. remaining = next_target_start ?
  538. device_logical_block_size_sects - next_target_start : 0;
  539. }
  540. if (remaining) {
  541. DMWARN("%s: table line %u (start sect %llu len %llu) "
  542. "not aligned to h/w logical block size %u",
  543. dm_device_name(table->md), i,
  544. (unsigned long long) ti->begin,
  545. (unsigned long long) ti->len,
  546. limits->logical_block_size);
  547. return -EINVAL;
  548. }
  549. return 0;
  550. }
  551. int dm_table_add_target(struct dm_table *t, const char *type,
  552. sector_t start, sector_t len, char *params)
  553. {
  554. int r = -EINVAL, argc;
  555. char **argv;
  556. struct dm_target *tgt;
  557. if (t->singleton) {
  558. DMERR("%s: target type %s must appear alone in table",
  559. dm_device_name(t->md), t->targets->type->name);
  560. return -EINVAL;
  561. }
  562. BUG_ON(t->num_targets >= t->num_allocated);
  563. tgt = t->targets + t->num_targets;
  564. memset(tgt, 0, sizeof(*tgt));
  565. if (!len) {
  566. DMERR("%s: zero-length target", dm_device_name(t->md));
  567. return -EINVAL;
  568. }
  569. tgt->type = dm_get_target_type(type);
  570. if (!tgt->type) {
  571. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  572. type);
  573. return -EINVAL;
  574. }
  575. if (dm_target_needs_singleton(tgt->type)) {
  576. if (t->num_targets) {
  577. DMERR("%s: target type %s must appear alone in table",
  578. dm_device_name(t->md), type);
  579. return -EINVAL;
  580. }
  581. t->singleton = 1;
  582. }
  583. if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
  584. DMERR("%s: target type %s may not be included in read-only tables",
  585. dm_device_name(t->md), type);
  586. return -EINVAL;
  587. }
  588. if (t->immutable_target_type) {
  589. if (t->immutable_target_type != tgt->type) {
  590. DMERR("%s: immutable target type %s cannot be mixed with other target types",
  591. dm_device_name(t->md), t->immutable_target_type->name);
  592. return -EINVAL;
  593. }
  594. } else if (dm_target_is_immutable(tgt->type)) {
  595. if (t->num_targets) {
  596. DMERR("%s: immutable target type %s cannot be mixed with other target types",
  597. dm_device_name(t->md), tgt->type->name);
  598. return -EINVAL;
  599. }
  600. t->immutable_target_type = tgt->type;
  601. }
  602. tgt->table = t;
  603. tgt->begin = start;
  604. tgt->len = len;
  605. tgt->error = "Unknown error";
  606. /*
  607. * Does this target adjoin the previous one ?
  608. */
  609. if (!adjoin(t, tgt)) {
  610. tgt->error = "Gap in table";
  611. r = -EINVAL;
  612. goto bad;
  613. }
  614. r = dm_split_args(&argc, &argv, params);
  615. if (r) {
  616. tgt->error = "couldn't split parameters (insufficient memory)";
  617. goto bad;
  618. }
  619. r = tgt->type->ctr(tgt, argc, argv);
  620. kfree(argv);
  621. if (r)
  622. goto bad;
  623. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  624. if (!tgt->num_discard_bios && tgt->discards_supported)
  625. DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
  626. dm_device_name(t->md), type);
  627. return 0;
  628. bad:
  629. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  630. dm_put_target_type(tgt->type);
  631. return r;
  632. }
  633. /*
  634. * Target argument parsing helpers.
  635. */
  636. static int validate_next_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  637. unsigned *value, char **error, unsigned grouped)
  638. {
  639. const char *arg_str = dm_shift_arg(arg_set);
  640. char dummy;
  641. if (!arg_str ||
  642. (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
  643. (*value < arg->min) ||
  644. (*value > arg->max) ||
  645. (grouped && arg_set->argc < *value)) {
  646. *error = arg->error;
  647. return -EINVAL;
  648. }
  649. return 0;
  650. }
  651. int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  652. unsigned *value, char **error)
  653. {
  654. return validate_next_arg(arg, arg_set, value, error, 0);
  655. }
  656. EXPORT_SYMBOL(dm_read_arg);
  657. int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
  658. unsigned *value, char **error)
  659. {
  660. return validate_next_arg(arg, arg_set, value, error, 1);
  661. }
  662. EXPORT_SYMBOL(dm_read_arg_group);
  663. const char *dm_shift_arg(struct dm_arg_set *as)
  664. {
  665. char *r;
  666. if (as->argc) {
  667. as->argc--;
  668. r = *as->argv;
  669. as->argv++;
  670. return r;
  671. }
  672. return NULL;
  673. }
  674. EXPORT_SYMBOL(dm_shift_arg);
  675. void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
  676. {
  677. BUG_ON(as->argc < num_args);
  678. as->argc -= num_args;
  679. as->argv += num_args;
  680. }
  681. EXPORT_SYMBOL(dm_consume_args);
  682. static bool __table_type_request_based(unsigned table_type)
  683. {
  684. return (table_type == DM_TYPE_REQUEST_BASED ||
  685. table_type == DM_TYPE_MQ_REQUEST_BASED);
  686. }
  687. static int dm_table_set_type(struct dm_table *t)
  688. {
  689. unsigned i;
  690. unsigned bio_based = 0, request_based = 0, hybrid = 0;
  691. bool use_blk_mq = false;
  692. struct dm_target *tgt;
  693. struct dm_dev_internal *dd;
  694. struct list_head *devices;
  695. unsigned live_md_type = dm_get_md_type(t->md);
  696. for (i = 0; i < t->num_targets; i++) {
  697. tgt = t->targets + i;
  698. if (dm_target_hybrid(tgt))
  699. hybrid = 1;
  700. else if (dm_target_request_based(tgt))
  701. request_based = 1;
  702. else
  703. bio_based = 1;
  704. if (bio_based && request_based) {
  705. DMWARN("Inconsistent table: different target types"
  706. " can't be mixed up");
  707. return -EINVAL;
  708. }
  709. }
  710. if (hybrid && !bio_based && !request_based) {
  711. /*
  712. * The targets can work either way.
  713. * Determine the type from the live device.
  714. * Default to bio-based if device is new.
  715. */
  716. if (__table_type_request_based(live_md_type))
  717. request_based = 1;
  718. else
  719. bio_based = 1;
  720. }
  721. if (bio_based) {
  722. /* We must use this table as bio-based */
  723. t->type = DM_TYPE_BIO_BASED;
  724. return 0;
  725. }
  726. BUG_ON(!request_based); /* No targets in this table */
  727. /*
  728. * Request-based dm supports only tables that have a single target now.
  729. * To support multiple targets, request splitting support is needed,
  730. * and that needs lots of changes in the block-layer.
  731. * (e.g. request completion process for partial completion.)
  732. */
  733. if (t->num_targets > 1) {
  734. DMWARN("Request-based dm doesn't support multiple targets yet");
  735. return -EINVAL;
  736. }
  737. /* Non-request-stackable devices can't be used for request-based dm */
  738. devices = dm_table_get_devices(t);
  739. list_for_each_entry(dd, devices, list) {
  740. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  741. if (!blk_queue_stackable(q)) {
  742. DMERR("table load rejected: including"
  743. " non-request-stackable devices");
  744. return -EINVAL;
  745. }
  746. if (q->mq_ops)
  747. use_blk_mq = true;
  748. }
  749. if (use_blk_mq) {
  750. /* verify _all_ devices in the table are blk-mq devices */
  751. list_for_each_entry(dd, devices, list)
  752. if (!bdev_get_queue(dd->dm_dev->bdev)->mq_ops) {
  753. DMERR("table load rejected: not all devices"
  754. " are blk-mq request-stackable");
  755. return -EINVAL;
  756. }
  757. t->type = DM_TYPE_MQ_REQUEST_BASED;
  758. } else if (list_empty(devices) && __table_type_request_based(live_md_type)) {
  759. /* inherit live MD type */
  760. t->type = live_md_type;
  761. } else
  762. t->type = DM_TYPE_REQUEST_BASED;
  763. return 0;
  764. }
  765. unsigned dm_table_get_type(struct dm_table *t)
  766. {
  767. return t->type;
  768. }
  769. struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
  770. {
  771. return t->immutable_target_type;
  772. }
  773. struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
  774. {
  775. /* Immutable target is implicitly a singleton */
  776. if (t->num_targets > 1 ||
  777. !dm_target_is_immutable(t->targets[0].type))
  778. return NULL;
  779. return t->targets;
  780. }
  781. struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
  782. {
  783. struct dm_target *uninitialized_var(ti);
  784. unsigned i = 0;
  785. while (i < dm_table_get_num_targets(t)) {
  786. ti = dm_table_get_target(t, i++);
  787. if (dm_target_is_wildcard(ti->type))
  788. return ti;
  789. }
  790. return NULL;
  791. }
  792. bool dm_table_request_based(struct dm_table *t)
  793. {
  794. return __table_type_request_based(dm_table_get_type(t));
  795. }
  796. bool dm_table_mq_request_based(struct dm_table *t)
  797. {
  798. return dm_table_get_type(t) == DM_TYPE_MQ_REQUEST_BASED;
  799. }
  800. static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
  801. {
  802. unsigned type = dm_table_get_type(t);
  803. unsigned per_io_data_size = 0;
  804. struct dm_target *tgt;
  805. unsigned i;
  806. if (unlikely(type == DM_TYPE_NONE)) {
  807. DMWARN("no table type is set, can't allocate mempools");
  808. return -EINVAL;
  809. }
  810. if (type == DM_TYPE_BIO_BASED)
  811. for (i = 0; i < t->num_targets; i++) {
  812. tgt = t->targets + i;
  813. per_io_data_size = max(per_io_data_size, tgt->per_io_data_size);
  814. }
  815. t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported, per_io_data_size);
  816. if (!t->mempools)
  817. return -ENOMEM;
  818. return 0;
  819. }
  820. void dm_table_free_md_mempools(struct dm_table *t)
  821. {
  822. dm_free_md_mempools(t->mempools);
  823. t->mempools = NULL;
  824. }
  825. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  826. {
  827. return t->mempools;
  828. }
  829. static int setup_indexes(struct dm_table *t)
  830. {
  831. int i;
  832. unsigned int total = 0;
  833. sector_t *indexes;
  834. /* allocate the space for *all* the indexes */
  835. for (i = t->depth - 2; i >= 0; i--) {
  836. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  837. total += t->counts[i];
  838. }
  839. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  840. if (!indexes)
  841. return -ENOMEM;
  842. /* set up internal nodes, bottom-up */
  843. for (i = t->depth - 2; i >= 0; i--) {
  844. t->index[i] = indexes;
  845. indexes += (KEYS_PER_NODE * t->counts[i]);
  846. setup_btree_index(i, t);
  847. }
  848. return 0;
  849. }
  850. /*
  851. * Builds the btree to index the map.
  852. */
  853. static int dm_table_build_index(struct dm_table *t)
  854. {
  855. int r = 0;
  856. unsigned int leaf_nodes;
  857. /* how many indexes will the btree have ? */
  858. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  859. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  860. /* leaf layer has already been set up */
  861. t->counts[t->depth - 1] = leaf_nodes;
  862. t->index[t->depth - 1] = t->highs;
  863. if (t->depth >= 2)
  864. r = setup_indexes(t);
  865. return r;
  866. }
  867. static bool integrity_profile_exists(struct gendisk *disk)
  868. {
  869. return !!blk_get_integrity(disk);
  870. }
  871. /*
  872. * Get a disk whose integrity profile reflects the table's profile.
  873. * Returns NULL if integrity support was inconsistent or unavailable.
  874. */
  875. static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
  876. {
  877. struct list_head *devices = dm_table_get_devices(t);
  878. struct dm_dev_internal *dd = NULL;
  879. struct gendisk *prev_disk = NULL, *template_disk = NULL;
  880. list_for_each_entry(dd, devices, list) {
  881. template_disk = dd->dm_dev->bdev->bd_disk;
  882. if (!integrity_profile_exists(template_disk))
  883. goto no_integrity;
  884. else if (prev_disk &&
  885. blk_integrity_compare(prev_disk, template_disk) < 0)
  886. goto no_integrity;
  887. prev_disk = template_disk;
  888. }
  889. return template_disk;
  890. no_integrity:
  891. if (prev_disk)
  892. DMWARN("%s: integrity not set: %s and %s profile mismatch",
  893. dm_device_name(t->md),
  894. prev_disk->disk_name,
  895. template_disk->disk_name);
  896. return NULL;
  897. }
  898. /*
  899. * Register the mapped device for blk_integrity support if the
  900. * underlying devices have an integrity profile. But all devices may
  901. * not have matching profiles (checking all devices isn't reliable
  902. * during table load because this table may use other DM device(s) which
  903. * must be resumed before they will have an initialized integity
  904. * profile). Consequently, stacked DM devices force a 2 stage integrity
  905. * profile validation: First pass during table load, final pass during
  906. * resume.
  907. */
  908. static int dm_table_register_integrity(struct dm_table *t)
  909. {
  910. struct mapped_device *md = t->md;
  911. struct gendisk *template_disk = NULL;
  912. template_disk = dm_table_get_integrity_disk(t);
  913. if (!template_disk)
  914. return 0;
  915. if (!integrity_profile_exists(dm_disk(md))) {
  916. t->integrity_supported = 1;
  917. /*
  918. * Register integrity profile during table load; we can do
  919. * this because the final profile must match during resume.
  920. */
  921. blk_integrity_register(dm_disk(md),
  922. blk_get_integrity(template_disk));
  923. return 0;
  924. }
  925. /*
  926. * If DM device already has an initialized integrity
  927. * profile the new profile should not conflict.
  928. */
  929. if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
  930. DMWARN("%s: conflict with existing integrity profile: "
  931. "%s profile mismatch",
  932. dm_device_name(t->md),
  933. template_disk->disk_name);
  934. return 1;
  935. }
  936. /* Preserve existing integrity profile */
  937. t->integrity_supported = 1;
  938. return 0;
  939. }
  940. /*
  941. * Prepares the table for use by building the indices,
  942. * setting the type, and allocating mempools.
  943. */
  944. int dm_table_complete(struct dm_table *t)
  945. {
  946. int r;
  947. r = dm_table_set_type(t);
  948. if (r) {
  949. DMERR("unable to set table type");
  950. return r;
  951. }
  952. r = dm_table_build_index(t);
  953. if (r) {
  954. DMERR("unable to build btrees");
  955. return r;
  956. }
  957. r = dm_table_register_integrity(t);
  958. if (r) {
  959. DMERR("could not register integrity profile.");
  960. return r;
  961. }
  962. r = dm_table_alloc_md_mempools(t, t->md);
  963. if (r)
  964. DMERR("unable to allocate mempools");
  965. return r;
  966. }
  967. static DEFINE_MUTEX(_event_lock);
  968. void dm_table_event_callback(struct dm_table *t,
  969. void (*fn)(void *), void *context)
  970. {
  971. mutex_lock(&_event_lock);
  972. t->event_fn = fn;
  973. t->event_context = context;
  974. mutex_unlock(&_event_lock);
  975. }
  976. void dm_table_event(struct dm_table *t)
  977. {
  978. /*
  979. * You can no longer call dm_table_event() from interrupt
  980. * context, use a bottom half instead.
  981. */
  982. BUG_ON(in_interrupt());
  983. mutex_lock(&_event_lock);
  984. if (t->event_fn)
  985. t->event_fn(t->event_context);
  986. mutex_unlock(&_event_lock);
  987. }
  988. EXPORT_SYMBOL(dm_table_event);
  989. sector_t dm_table_get_size(struct dm_table *t)
  990. {
  991. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  992. }
  993. EXPORT_SYMBOL(dm_table_get_size);
  994. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  995. {
  996. if (index >= t->num_targets)
  997. return NULL;
  998. return t->targets + index;
  999. }
  1000. /*
  1001. * Search the btree for the correct target.
  1002. *
  1003. * Caller should check returned pointer with dm_target_is_valid()
  1004. * to trap I/O beyond end of device.
  1005. */
  1006. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  1007. {
  1008. unsigned int l, n = 0, k = 0;
  1009. sector_t *node;
  1010. for (l = 0; l < t->depth; l++) {
  1011. n = get_child(n, k);
  1012. node = get_node(t, l, n);
  1013. for (k = 0; k < KEYS_PER_NODE; k++)
  1014. if (node[k] >= sector)
  1015. break;
  1016. }
  1017. return &t->targets[(KEYS_PER_NODE * n) + k];
  1018. }
  1019. static int count_device(struct dm_target *ti, struct dm_dev *dev,
  1020. sector_t start, sector_t len, void *data)
  1021. {
  1022. unsigned *num_devices = data;
  1023. (*num_devices)++;
  1024. return 0;
  1025. }
  1026. /*
  1027. * Check whether a table has no data devices attached using each
  1028. * target's iterate_devices method.
  1029. * Returns false if the result is unknown because a target doesn't
  1030. * support iterate_devices.
  1031. */
  1032. bool dm_table_has_no_data_devices(struct dm_table *table)
  1033. {
  1034. struct dm_target *uninitialized_var(ti);
  1035. unsigned i = 0, num_devices = 0;
  1036. while (i < dm_table_get_num_targets(table)) {
  1037. ti = dm_table_get_target(table, i++);
  1038. if (!ti->type->iterate_devices)
  1039. return false;
  1040. ti->type->iterate_devices(ti, count_device, &num_devices);
  1041. if (num_devices)
  1042. return false;
  1043. }
  1044. return true;
  1045. }
  1046. /*
  1047. * Establish the new table's queue_limits and validate them.
  1048. */
  1049. int dm_calculate_queue_limits(struct dm_table *table,
  1050. struct queue_limits *limits)
  1051. {
  1052. struct dm_target *uninitialized_var(ti);
  1053. struct queue_limits ti_limits;
  1054. unsigned i = 0;
  1055. blk_set_stacking_limits(limits);
  1056. while (i < dm_table_get_num_targets(table)) {
  1057. blk_set_stacking_limits(&ti_limits);
  1058. ti = dm_table_get_target(table, i++);
  1059. if (!ti->type->iterate_devices)
  1060. goto combine_limits;
  1061. /*
  1062. * Combine queue limits of all the devices this target uses.
  1063. */
  1064. ti->type->iterate_devices(ti, dm_set_device_limits,
  1065. &ti_limits);
  1066. /* Set I/O hints portion of queue limits */
  1067. if (ti->type->io_hints)
  1068. ti->type->io_hints(ti, &ti_limits);
  1069. /*
  1070. * Check each device area is consistent with the target's
  1071. * overall queue limits.
  1072. */
  1073. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  1074. &ti_limits))
  1075. return -EINVAL;
  1076. combine_limits:
  1077. /*
  1078. * Merge this target's queue limits into the overall limits
  1079. * for the table.
  1080. */
  1081. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  1082. DMWARN("%s: adding target device "
  1083. "(start sect %llu len %llu) "
  1084. "caused an alignment inconsistency",
  1085. dm_device_name(table->md),
  1086. (unsigned long long) ti->begin,
  1087. (unsigned long long) ti->len);
  1088. }
  1089. return validate_hardware_logical_block_alignment(table, limits);
  1090. }
  1091. /*
  1092. * Verify that all devices have an integrity profile that matches the
  1093. * DM device's registered integrity profile. If the profiles don't
  1094. * match then unregister the DM device's integrity profile.
  1095. */
  1096. static void dm_table_verify_integrity(struct dm_table *t)
  1097. {
  1098. struct gendisk *template_disk = NULL;
  1099. if (t->integrity_supported) {
  1100. /*
  1101. * Verify that the original integrity profile
  1102. * matches all the devices in this table.
  1103. */
  1104. template_disk = dm_table_get_integrity_disk(t);
  1105. if (template_disk &&
  1106. blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
  1107. return;
  1108. }
  1109. if (integrity_profile_exists(dm_disk(t->md))) {
  1110. DMWARN("%s: unable to establish an integrity profile",
  1111. dm_device_name(t->md));
  1112. blk_integrity_unregister(dm_disk(t->md));
  1113. }
  1114. }
  1115. static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
  1116. sector_t start, sector_t len, void *data)
  1117. {
  1118. unsigned flush = (*(unsigned *)data);
  1119. struct request_queue *q = bdev_get_queue(dev->bdev);
  1120. return q && (q->flush_flags & flush);
  1121. }
  1122. static bool dm_table_supports_flush(struct dm_table *t, unsigned flush)
  1123. {
  1124. struct dm_target *ti;
  1125. unsigned i = 0;
  1126. /*
  1127. * Require at least one underlying device to support flushes.
  1128. * t->devices includes internal dm devices such as mirror logs
  1129. * so we need to use iterate_devices here, which targets
  1130. * supporting flushes must provide.
  1131. */
  1132. while (i < dm_table_get_num_targets(t)) {
  1133. ti = dm_table_get_target(t, i++);
  1134. if (!ti->num_flush_bios)
  1135. continue;
  1136. if (ti->flush_supported)
  1137. return true;
  1138. if (ti->type->iterate_devices &&
  1139. ti->type->iterate_devices(ti, device_flush_capable, &flush))
  1140. return true;
  1141. }
  1142. return false;
  1143. }
  1144. static bool dm_table_discard_zeroes_data(struct dm_table *t)
  1145. {
  1146. struct dm_target *ti;
  1147. unsigned i = 0;
  1148. /* Ensure that all targets supports discard_zeroes_data. */
  1149. while (i < dm_table_get_num_targets(t)) {
  1150. ti = dm_table_get_target(t, i++);
  1151. if (ti->discard_zeroes_data_unsupported)
  1152. return false;
  1153. }
  1154. return true;
  1155. }
  1156. static int device_is_nonrot(struct dm_target *ti, struct dm_dev *dev,
  1157. sector_t start, sector_t len, void *data)
  1158. {
  1159. struct request_queue *q = bdev_get_queue(dev->bdev);
  1160. return q && blk_queue_nonrot(q);
  1161. }
  1162. static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
  1163. sector_t start, sector_t len, void *data)
  1164. {
  1165. struct request_queue *q = bdev_get_queue(dev->bdev);
  1166. return q && !blk_queue_add_random(q);
  1167. }
  1168. static int queue_supports_sg_merge(struct dm_target *ti, struct dm_dev *dev,
  1169. sector_t start, sector_t len, void *data)
  1170. {
  1171. struct request_queue *q = bdev_get_queue(dev->bdev);
  1172. return q && !test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
  1173. }
  1174. static bool dm_table_all_devices_attribute(struct dm_table *t,
  1175. iterate_devices_callout_fn func)
  1176. {
  1177. struct dm_target *ti;
  1178. unsigned i = 0;
  1179. while (i < dm_table_get_num_targets(t)) {
  1180. ti = dm_table_get_target(t, i++);
  1181. if (!ti->type->iterate_devices ||
  1182. !ti->type->iterate_devices(ti, func, NULL))
  1183. return false;
  1184. }
  1185. return true;
  1186. }
  1187. static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
  1188. sector_t start, sector_t len, void *data)
  1189. {
  1190. struct request_queue *q = bdev_get_queue(dev->bdev);
  1191. return q && !q->limits.max_write_same_sectors;
  1192. }
  1193. static bool dm_table_supports_write_same(struct dm_table *t)
  1194. {
  1195. struct dm_target *ti;
  1196. unsigned i = 0;
  1197. while (i < dm_table_get_num_targets(t)) {
  1198. ti = dm_table_get_target(t, i++);
  1199. if (!ti->num_write_same_bios)
  1200. return false;
  1201. if (!ti->type->iterate_devices ||
  1202. ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1208. sector_t start, sector_t len, void *data)
  1209. {
  1210. struct request_queue *q = bdev_get_queue(dev->bdev);
  1211. return q && blk_queue_discard(q);
  1212. }
  1213. static bool dm_table_supports_discards(struct dm_table *t)
  1214. {
  1215. struct dm_target *ti;
  1216. unsigned i = 0;
  1217. /*
  1218. * Unless any target used by the table set discards_supported,
  1219. * require at least one underlying device to support discards.
  1220. * t->devices includes internal dm devices such as mirror logs
  1221. * so we need to use iterate_devices here, which targets
  1222. * supporting discard selectively must provide.
  1223. */
  1224. while (i < dm_table_get_num_targets(t)) {
  1225. ti = dm_table_get_target(t, i++);
  1226. if (!ti->num_discard_bios)
  1227. continue;
  1228. if (ti->discards_supported)
  1229. return true;
  1230. if (ti->type->iterate_devices &&
  1231. ti->type->iterate_devices(ti, device_discard_capable, NULL))
  1232. return true;
  1233. }
  1234. return false;
  1235. }
  1236. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  1237. struct queue_limits *limits)
  1238. {
  1239. unsigned flush = 0;
  1240. /*
  1241. * Copy table's limits to the DM device's request_queue
  1242. */
  1243. q->limits = *limits;
  1244. if (!dm_table_supports_discards(t))
  1245. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  1246. else
  1247. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1248. if (dm_table_supports_flush(t, REQ_FLUSH)) {
  1249. flush |= REQ_FLUSH;
  1250. if (dm_table_supports_flush(t, REQ_FUA))
  1251. flush |= REQ_FUA;
  1252. }
  1253. blk_queue_flush(q, flush);
  1254. if (!dm_table_discard_zeroes_data(t))
  1255. q->limits.discard_zeroes_data = 0;
  1256. /* Ensure that all underlying devices are non-rotational. */
  1257. if (dm_table_all_devices_attribute(t, device_is_nonrot))
  1258. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  1259. else
  1260. queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
  1261. if (!dm_table_supports_write_same(t))
  1262. q->limits.max_write_same_sectors = 0;
  1263. if (dm_table_all_devices_attribute(t, queue_supports_sg_merge))
  1264. queue_flag_clear_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  1265. else
  1266. queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  1267. dm_table_verify_integrity(t);
  1268. /*
  1269. * Determine whether or not this queue's I/O timings contribute
  1270. * to the entropy pool, Only request-based targets use this.
  1271. * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
  1272. * have it set.
  1273. */
  1274. if (blk_queue_add_random(q) && dm_table_all_devices_attribute(t, device_is_not_random))
  1275. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
  1276. /*
  1277. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  1278. * visible to other CPUs because, once the flag is set, incoming bios
  1279. * are processed by request-based dm, which refers to the queue
  1280. * settings.
  1281. * Until the flag set, bios are passed to bio-based dm and queued to
  1282. * md->deferred where queue settings are not needed yet.
  1283. * Those bios are passed to request-based dm at the resume time.
  1284. */
  1285. smp_mb();
  1286. if (dm_table_request_based(t))
  1287. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  1288. }
  1289. unsigned int dm_table_get_num_targets(struct dm_table *t)
  1290. {
  1291. return t->num_targets;
  1292. }
  1293. struct list_head *dm_table_get_devices(struct dm_table *t)
  1294. {
  1295. return &t->devices;
  1296. }
  1297. fmode_t dm_table_get_mode(struct dm_table *t)
  1298. {
  1299. return t->mode;
  1300. }
  1301. EXPORT_SYMBOL(dm_table_get_mode);
  1302. enum suspend_mode {
  1303. PRESUSPEND,
  1304. PRESUSPEND_UNDO,
  1305. POSTSUSPEND,
  1306. };
  1307. static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
  1308. {
  1309. int i = t->num_targets;
  1310. struct dm_target *ti = t->targets;
  1311. while (i--) {
  1312. switch (mode) {
  1313. case PRESUSPEND:
  1314. if (ti->type->presuspend)
  1315. ti->type->presuspend(ti);
  1316. break;
  1317. case PRESUSPEND_UNDO:
  1318. if (ti->type->presuspend_undo)
  1319. ti->type->presuspend_undo(ti);
  1320. break;
  1321. case POSTSUSPEND:
  1322. if (ti->type->postsuspend)
  1323. ti->type->postsuspend(ti);
  1324. break;
  1325. }
  1326. ti++;
  1327. }
  1328. }
  1329. void dm_table_presuspend_targets(struct dm_table *t)
  1330. {
  1331. if (!t)
  1332. return;
  1333. suspend_targets(t, PRESUSPEND);
  1334. }
  1335. void dm_table_presuspend_undo_targets(struct dm_table *t)
  1336. {
  1337. if (!t)
  1338. return;
  1339. suspend_targets(t, PRESUSPEND_UNDO);
  1340. }
  1341. void dm_table_postsuspend_targets(struct dm_table *t)
  1342. {
  1343. if (!t)
  1344. return;
  1345. suspend_targets(t, POSTSUSPEND);
  1346. }
  1347. int dm_table_resume_targets(struct dm_table *t)
  1348. {
  1349. int i, r = 0;
  1350. for (i = 0; i < t->num_targets; i++) {
  1351. struct dm_target *ti = t->targets + i;
  1352. if (!ti->type->preresume)
  1353. continue;
  1354. r = ti->type->preresume(ti);
  1355. if (r) {
  1356. DMERR("%s: %s: preresume failed, error = %d",
  1357. dm_device_name(t->md), ti->type->name, r);
  1358. return r;
  1359. }
  1360. }
  1361. for (i = 0; i < t->num_targets; i++) {
  1362. struct dm_target *ti = t->targets + i;
  1363. if (ti->type->resume)
  1364. ti->type->resume(ti);
  1365. }
  1366. return 0;
  1367. }
  1368. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  1369. {
  1370. list_add(&cb->list, &t->target_callbacks);
  1371. }
  1372. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1373. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1374. {
  1375. struct dm_dev_internal *dd;
  1376. struct list_head *devices = dm_table_get_devices(t);
  1377. struct dm_target_callbacks *cb;
  1378. int r = 0;
  1379. list_for_each_entry(dd, devices, list) {
  1380. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  1381. char b[BDEVNAME_SIZE];
  1382. if (likely(q))
  1383. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  1384. else
  1385. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1386. dm_device_name(t->md),
  1387. bdevname(dd->dm_dev->bdev, b));
  1388. }
  1389. list_for_each_entry(cb, &t->target_callbacks, list)
  1390. if (cb->congested_fn)
  1391. r |= cb->congested_fn(cb, bdi_bits);
  1392. return r;
  1393. }
  1394. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1395. {
  1396. return t->md;
  1397. }
  1398. EXPORT_SYMBOL(dm_table_get_md);
  1399. void dm_table_run_md_queue_async(struct dm_table *t)
  1400. {
  1401. struct mapped_device *md;
  1402. struct request_queue *queue;
  1403. unsigned long flags;
  1404. if (!dm_table_request_based(t))
  1405. return;
  1406. md = dm_table_get_md(t);
  1407. queue = dm_get_md_queue(md);
  1408. if (queue) {
  1409. if (queue->mq_ops)
  1410. blk_mq_run_hw_queues(queue, true);
  1411. else {
  1412. spin_lock_irqsave(queue->queue_lock, flags);
  1413. blk_run_queue_async(queue);
  1414. spin_unlock_irqrestore(queue->queue_lock, flags);
  1415. }
  1416. }
  1417. }
  1418. EXPORT_SYMBOL(dm_table_run_md_queue_async);