dm-table.c 38 KB

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