dm-table.c 39 KB

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