dm-table.c 41 KB

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