dm-table.c 39 KB

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