dm-table.c 37 KB

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