dm-table.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 <asm/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. unsigned discards_supported:1;
  49. /*
  50. * Indicates the rw permissions for the new logical
  51. * device. This should be a combination of FMODE_READ
  52. * and FMODE_WRITE.
  53. */
  54. fmode_t mode;
  55. /* a list of devices used by this table */
  56. struct list_head devices;
  57. /* events get handed up using this callback */
  58. void (*event_fn)(void *);
  59. void *event_context;
  60. struct dm_md_mempools *mempools;
  61. struct list_head target_callbacks;
  62. };
  63. /*
  64. * Similar to ceiling(log_size(n))
  65. */
  66. static unsigned int int_log(unsigned int n, unsigned int base)
  67. {
  68. int result = 0;
  69. while (n > 1) {
  70. n = dm_div_up(n, base);
  71. result++;
  72. }
  73. return result;
  74. }
  75. /*
  76. * Calculate the index of the child node of the n'th node k'th key.
  77. */
  78. static inline unsigned int get_child(unsigned int n, unsigned int k)
  79. {
  80. return (n * CHILDREN_PER_NODE) + k;
  81. }
  82. /*
  83. * Return the n'th node of level l from table t.
  84. */
  85. static inline sector_t *get_node(struct dm_table *t,
  86. unsigned int l, unsigned int n)
  87. {
  88. return t->index[l] + (n * KEYS_PER_NODE);
  89. }
  90. /*
  91. * Return the highest key that you could lookup from the n'th
  92. * node on level l of the btree.
  93. */
  94. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  95. {
  96. for (; l < t->depth - 1; l++)
  97. n = get_child(n, CHILDREN_PER_NODE - 1);
  98. if (n >= t->counts[l])
  99. return (sector_t) - 1;
  100. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  101. }
  102. /*
  103. * Fills in a level of the btree based on the highs of the level
  104. * below it.
  105. */
  106. static int setup_btree_index(unsigned int l, struct dm_table *t)
  107. {
  108. unsigned int n, k;
  109. sector_t *node;
  110. for (n = 0U; n < t->counts[l]; n++) {
  111. node = get_node(t, l, n);
  112. for (k = 0U; k < KEYS_PER_NODE; k++)
  113. node[k] = high(t, l + 1, get_child(n, k));
  114. }
  115. return 0;
  116. }
  117. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  118. {
  119. unsigned long size;
  120. void *addr;
  121. /*
  122. * Check that we're not going to overflow.
  123. */
  124. if (nmemb > (ULONG_MAX / elem_size))
  125. return NULL;
  126. size = nmemb * elem_size;
  127. addr = vmalloc(size);
  128. if (addr)
  129. memset(addr, 0, size);
  130. return addr;
  131. }
  132. /*
  133. * highs, and targets are managed as dynamic arrays during a
  134. * table load.
  135. */
  136. static int alloc_targets(struct dm_table *t, unsigned int num)
  137. {
  138. sector_t *n_highs;
  139. struct dm_target *n_targets;
  140. int n = t->num_targets;
  141. /*
  142. * Allocate both the target array and offset array at once.
  143. * Append an empty entry to catch sectors beyond the end of
  144. * the device.
  145. */
  146. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  147. sizeof(sector_t));
  148. if (!n_highs)
  149. return -ENOMEM;
  150. n_targets = (struct dm_target *) (n_highs + num);
  151. if (n) {
  152. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  153. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  154. }
  155. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  156. vfree(t->highs);
  157. t->num_allocated = num;
  158. t->highs = n_highs;
  159. t->targets = n_targets;
  160. return 0;
  161. }
  162. int dm_table_create(struct dm_table **result, fmode_t mode,
  163. unsigned num_targets, struct mapped_device *md)
  164. {
  165. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  166. if (!t)
  167. return -ENOMEM;
  168. INIT_LIST_HEAD(&t->devices);
  169. INIT_LIST_HEAD(&t->target_callbacks);
  170. atomic_set(&t->holders, 0);
  171. t->discards_supported = 1;
  172. if (!num_targets)
  173. num_targets = KEYS_PER_NODE;
  174. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  175. if (alloc_targets(t, num_targets)) {
  176. kfree(t);
  177. t = NULL;
  178. return -ENOMEM;
  179. }
  180. t->mode = mode;
  181. t->md = md;
  182. *result = t;
  183. return 0;
  184. }
  185. static void free_devices(struct list_head *devices)
  186. {
  187. struct list_head *tmp, *next;
  188. list_for_each_safe(tmp, next, devices) {
  189. struct dm_dev_internal *dd =
  190. list_entry(tmp, struct dm_dev_internal, list);
  191. DMWARN("dm_table_destroy: dm_put_device call missing for %s",
  192. dd->dm_dev.name);
  193. kfree(dd);
  194. }
  195. }
  196. void dm_table_destroy(struct dm_table *t)
  197. {
  198. unsigned int i;
  199. if (!t)
  200. return;
  201. while (atomic_read(&t->holders))
  202. msleep(1);
  203. smp_mb();
  204. /* free the indexes */
  205. if (t->depth >= 2)
  206. vfree(t->index[t->depth - 2]);
  207. /* free the targets */
  208. for (i = 0; i < t->num_targets; i++) {
  209. struct dm_target *tgt = t->targets + i;
  210. if (tgt->type->dtr)
  211. tgt->type->dtr(tgt);
  212. dm_put_target_type(tgt->type);
  213. }
  214. vfree(t->highs);
  215. /* free the device list */
  216. if (t->devices.next != &t->devices)
  217. free_devices(&t->devices);
  218. dm_free_md_mempools(t->mempools);
  219. kfree(t);
  220. }
  221. void dm_table_get(struct dm_table *t)
  222. {
  223. atomic_inc(&t->holders);
  224. }
  225. void dm_table_put(struct dm_table *t)
  226. {
  227. if (!t)
  228. return;
  229. smp_mb__before_atomic_dec();
  230. atomic_dec(&t->holders);
  231. }
  232. /*
  233. * Checks to see if we need to extend highs or targets.
  234. */
  235. static inline int check_space(struct dm_table *t)
  236. {
  237. if (t->num_targets >= t->num_allocated)
  238. return alloc_targets(t, t->num_allocated * 2);
  239. return 0;
  240. }
  241. /*
  242. * See if we've already got a device in the list.
  243. */
  244. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  245. {
  246. struct dm_dev_internal *dd;
  247. list_for_each_entry (dd, l, list)
  248. if (dd->dm_dev.bdev->bd_dev == dev)
  249. return dd;
  250. return NULL;
  251. }
  252. /*
  253. * Open a device so we can use it as a map destination.
  254. */
  255. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  256. struct mapped_device *md)
  257. {
  258. static char *_claim_ptr = "I belong to device-mapper";
  259. struct block_device *bdev;
  260. int r;
  261. BUG_ON(d->dm_dev.bdev);
  262. bdev = blkdev_get_by_dev(dev, d->dm_dev.mode | FMODE_EXCL, _claim_ptr);
  263. if (IS_ERR(bdev))
  264. return PTR_ERR(bdev);
  265. r = bd_link_disk_holder(bdev, dm_disk(md));
  266. if (r) {
  267. blkdev_put(bdev, d->dm_dev.mode | FMODE_EXCL);
  268. return r;
  269. }
  270. d->dm_dev.bdev = bdev;
  271. return 0;
  272. }
  273. /*
  274. * Close a device that we've been using.
  275. */
  276. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  277. {
  278. if (!d->dm_dev.bdev)
  279. return;
  280. bd_unlink_disk_holder(d->dm_dev.bdev, dm_disk(md));
  281. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode | FMODE_EXCL);
  282. d->dm_dev.bdev = NULL;
  283. }
  284. /*
  285. * If possible, this checks an area of a destination device is invalid.
  286. */
  287. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  288. sector_t start, sector_t len, void *data)
  289. {
  290. struct queue_limits *limits = data;
  291. struct block_device *bdev = dev->bdev;
  292. sector_t dev_size =
  293. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  294. unsigned short logical_block_size_sectors =
  295. limits->logical_block_size >> SECTOR_SHIFT;
  296. char b[BDEVNAME_SIZE];
  297. if (!dev_size)
  298. return 0;
  299. if ((start >= dev_size) || (start + len > dev_size)) {
  300. DMWARN("%s: %s too small for target: "
  301. "start=%llu, len=%llu, dev_size=%llu",
  302. dm_device_name(ti->table->md), bdevname(bdev, b),
  303. (unsigned long long)start,
  304. (unsigned long long)len,
  305. (unsigned long long)dev_size);
  306. return 1;
  307. }
  308. if (logical_block_size_sectors <= 1)
  309. return 0;
  310. if (start & (logical_block_size_sectors - 1)) {
  311. DMWARN("%s: start=%llu not aligned to h/w "
  312. "logical block size %u of %s",
  313. dm_device_name(ti->table->md),
  314. (unsigned long long)start,
  315. limits->logical_block_size, bdevname(bdev, b));
  316. return 1;
  317. }
  318. if (len & (logical_block_size_sectors - 1)) {
  319. DMWARN("%s: len=%llu not aligned to h/w "
  320. "logical block size %u of %s",
  321. dm_device_name(ti->table->md),
  322. (unsigned long long)len,
  323. limits->logical_block_size, bdevname(bdev, b));
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. /*
  329. * This upgrades the mode on an already open dm_dev, being
  330. * careful to leave things as they were if we fail to reopen the
  331. * device and not to touch the existing bdev field in case
  332. * it is accessed concurrently inside dm_table_any_congested().
  333. */
  334. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  335. struct mapped_device *md)
  336. {
  337. int r;
  338. struct dm_dev_internal dd_new, dd_old;
  339. dd_new = dd_old = *dd;
  340. dd_new.dm_dev.mode |= new_mode;
  341. dd_new.dm_dev.bdev = NULL;
  342. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  343. if (r)
  344. return r;
  345. dd->dm_dev.mode |= new_mode;
  346. close_dev(&dd_old, md);
  347. return 0;
  348. }
  349. /*
  350. * Add a device to the list, or just increment the usage count if
  351. * it's already present.
  352. */
  353. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  354. const char *path, fmode_t mode, struct dm_dev **result)
  355. {
  356. int r;
  357. dev_t uninitialized_var(dev);
  358. struct dm_dev_internal *dd;
  359. unsigned int major, minor;
  360. BUG_ON(!t);
  361. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  362. /* Extract the major/minor numbers */
  363. dev = MKDEV(major, minor);
  364. if (MAJOR(dev) != major || MINOR(dev) != minor)
  365. return -EOVERFLOW;
  366. } else {
  367. /* convert the path to a device */
  368. struct block_device *bdev = lookup_bdev(path);
  369. if (IS_ERR(bdev))
  370. return PTR_ERR(bdev);
  371. dev = bdev->bd_dev;
  372. bdput(bdev);
  373. }
  374. dd = find_device(&t->devices, dev);
  375. if (!dd) {
  376. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  377. if (!dd)
  378. return -ENOMEM;
  379. dd->dm_dev.mode = mode;
  380. dd->dm_dev.bdev = NULL;
  381. if ((r = open_dev(dd, dev, t->md))) {
  382. kfree(dd);
  383. return r;
  384. }
  385. format_dev_t(dd->dm_dev.name, dev);
  386. atomic_set(&dd->count, 0);
  387. list_add(&dd->list, &t->devices);
  388. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  389. r = upgrade_mode(dd, mode, t->md);
  390. if (r)
  391. return r;
  392. }
  393. atomic_inc(&dd->count);
  394. *result = &dd->dm_dev;
  395. return 0;
  396. }
  397. int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  398. sector_t start, sector_t len, void *data)
  399. {
  400. struct queue_limits *limits = data;
  401. struct block_device *bdev = dev->bdev;
  402. struct request_queue *q = bdev_get_queue(bdev);
  403. char b[BDEVNAME_SIZE];
  404. if (unlikely(!q)) {
  405. DMWARN("%s: Cannot set limits for nonexistent device %s",
  406. dm_device_name(ti->table->md), bdevname(bdev, b));
  407. return 0;
  408. }
  409. if (bdev_stack_limits(limits, bdev, start) < 0)
  410. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  411. "physical_block_size=%u, logical_block_size=%u, "
  412. "alignment_offset=%u, start=%llu",
  413. dm_device_name(ti->table->md), bdevname(bdev, b),
  414. q->limits.physical_block_size,
  415. q->limits.logical_block_size,
  416. q->limits.alignment_offset,
  417. (unsigned long long) start << SECTOR_SHIFT);
  418. /*
  419. * Check if merge fn is supported.
  420. * If not we'll force DM to use PAGE_SIZE or
  421. * smaller I/O, just to be safe.
  422. */
  423. if (q->merge_bvec_fn && !ti->type->merge)
  424. blk_limits_max_hw_sectors(limits,
  425. (unsigned int) (PAGE_SIZE >> 9));
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  429. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  430. struct dm_dev **result)
  431. {
  432. return __table_get_device(ti->table, ti, path, mode, result);
  433. }
  434. /*
  435. * Decrement a devices use count and remove it if necessary.
  436. */
  437. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  438. {
  439. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  440. dm_dev);
  441. if (atomic_dec_and_test(&dd->count)) {
  442. close_dev(dd, ti->table->md);
  443. list_del(&dd->list);
  444. kfree(dd);
  445. }
  446. }
  447. /*
  448. * Checks to see if the target joins onto the end of the table.
  449. */
  450. static int adjoin(struct dm_table *table, struct dm_target *ti)
  451. {
  452. struct dm_target *prev;
  453. if (!table->num_targets)
  454. return !ti->begin;
  455. prev = &table->targets[table->num_targets - 1];
  456. return (ti->begin == (prev->begin + prev->len));
  457. }
  458. /*
  459. * Used to dynamically allocate the arg array.
  460. */
  461. static char **realloc_argv(unsigned *array_size, char **old_argv)
  462. {
  463. char **argv;
  464. unsigned new_size;
  465. new_size = *array_size ? *array_size * 2 : 64;
  466. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  467. if (argv) {
  468. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  469. *array_size = new_size;
  470. }
  471. kfree(old_argv);
  472. return argv;
  473. }
  474. /*
  475. * Destructively splits up the argument list to pass to ctr.
  476. */
  477. int dm_split_args(int *argc, char ***argvp, char *input)
  478. {
  479. char *start, *end = input, *out, **argv = NULL;
  480. unsigned array_size = 0;
  481. *argc = 0;
  482. if (!input) {
  483. *argvp = NULL;
  484. return 0;
  485. }
  486. argv = realloc_argv(&array_size, argv);
  487. if (!argv)
  488. return -ENOMEM;
  489. while (1) {
  490. /* Skip whitespace */
  491. start = skip_spaces(end);
  492. if (!*start)
  493. break; /* success, we hit the end */
  494. /* 'out' is used to remove any back-quotes */
  495. end = out = start;
  496. while (*end) {
  497. /* Everything apart from '\0' can be quoted */
  498. if (*end == '\\' && *(end + 1)) {
  499. *out++ = *(end + 1);
  500. end += 2;
  501. continue;
  502. }
  503. if (isspace(*end))
  504. break; /* end of token */
  505. *out++ = *end++;
  506. }
  507. /* have we already filled the array ? */
  508. if ((*argc + 1) > array_size) {
  509. argv = realloc_argv(&array_size, argv);
  510. if (!argv)
  511. return -ENOMEM;
  512. }
  513. /* we know this is whitespace */
  514. if (*end)
  515. end++;
  516. /* terminate the string and put it in the array */
  517. *out = '\0';
  518. argv[*argc] = start;
  519. (*argc)++;
  520. }
  521. *argvp = argv;
  522. return 0;
  523. }
  524. /*
  525. * Impose necessary and sufficient conditions on a devices's table such
  526. * that any incoming bio which respects its logical_block_size can be
  527. * processed successfully. If it falls across the boundary between
  528. * two or more targets, the size of each piece it gets split into must
  529. * be compatible with the logical_block_size of the target processing it.
  530. */
  531. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  532. struct queue_limits *limits)
  533. {
  534. /*
  535. * This function uses arithmetic modulo the logical_block_size
  536. * (in units of 512-byte sectors).
  537. */
  538. unsigned short device_logical_block_size_sects =
  539. limits->logical_block_size >> SECTOR_SHIFT;
  540. /*
  541. * Offset of the start of the next table entry, mod logical_block_size.
  542. */
  543. unsigned short next_target_start = 0;
  544. /*
  545. * Given an aligned bio that extends beyond the end of a
  546. * target, how many sectors must the next target handle?
  547. */
  548. unsigned short remaining = 0;
  549. struct dm_target *uninitialized_var(ti);
  550. struct queue_limits ti_limits;
  551. unsigned i = 0;
  552. /*
  553. * Check each entry in the table in turn.
  554. */
  555. while (i < dm_table_get_num_targets(table)) {
  556. ti = dm_table_get_target(table, i++);
  557. blk_set_default_limits(&ti_limits);
  558. /* combine all target devices' limits */
  559. if (ti->type->iterate_devices)
  560. ti->type->iterate_devices(ti, dm_set_device_limits,
  561. &ti_limits);
  562. /*
  563. * If the remaining sectors fall entirely within this
  564. * table entry are they compatible with its logical_block_size?
  565. */
  566. if (remaining < ti->len &&
  567. remaining & ((ti_limits.logical_block_size >>
  568. SECTOR_SHIFT) - 1))
  569. break; /* Error */
  570. next_target_start =
  571. (unsigned short) ((next_target_start + ti->len) &
  572. (device_logical_block_size_sects - 1));
  573. remaining = next_target_start ?
  574. device_logical_block_size_sects - next_target_start : 0;
  575. }
  576. if (remaining) {
  577. DMWARN("%s: table line %u (start sect %llu len %llu) "
  578. "not aligned to h/w logical block size %u",
  579. dm_device_name(table->md), i,
  580. (unsigned long long) ti->begin,
  581. (unsigned long long) ti->len,
  582. limits->logical_block_size);
  583. return -EINVAL;
  584. }
  585. return 0;
  586. }
  587. int dm_table_add_target(struct dm_table *t, const char *type,
  588. sector_t start, sector_t len, char *params)
  589. {
  590. int r = -EINVAL, argc;
  591. char **argv;
  592. struct dm_target *tgt;
  593. if ((r = check_space(t)))
  594. return r;
  595. tgt = t->targets + t->num_targets;
  596. memset(tgt, 0, sizeof(*tgt));
  597. if (!len) {
  598. DMERR("%s: zero-length target", dm_device_name(t->md));
  599. return -EINVAL;
  600. }
  601. tgt->type = dm_get_target_type(type);
  602. if (!tgt->type) {
  603. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  604. type);
  605. return -EINVAL;
  606. }
  607. tgt->table = t;
  608. tgt->begin = start;
  609. tgt->len = len;
  610. tgt->error = "Unknown error";
  611. /*
  612. * Does this target adjoin the previous one ?
  613. */
  614. if (!adjoin(t, tgt)) {
  615. tgt->error = "Gap in table";
  616. r = -EINVAL;
  617. goto bad;
  618. }
  619. r = dm_split_args(&argc, &argv, params);
  620. if (r) {
  621. tgt->error = "couldn't split parameters (insufficient memory)";
  622. goto bad;
  623. }
  624. r = tgt->type->ctr(tgt, argc, argv);
  625. kfree(argv);
  626. if (r)
  627. goto bad;
  628. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  629. if (!tgt->num_discard_requests)
  630. t->discards_supported = 0;
  631. return 0;
  632. bad:
  633. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  634. dm_put_target_type(tgt->type);
  635. return r;
  636. }
  637. static int dm_table_set_type(struct dm_table *t)
  638. {
  639. unsigned i;
  640. unsigned bio_based = 0, request_based = 0;
  641. struct dm_target *tgt;
  642. struct dm_dev_internal *dd;
  643. struct list_head *devices;
  644. for (i = 0; i < t->num_targets; i++) {
  645. tgt = t->targets + i;
  646. if (dm_target_request_based(tgt))
  647. request_based = 1;
  648. else
  649. bio_based = 1;
  650. if (bio_based && request_based) {
  651. DMWARN("Inconsistent table: different target types"
  652. " can't be mixed up");
  653. return -EINVAL;
  654. }
  655. }
  656. if (bio_based) {
  657. /* We must use this table as bio-based */
  658. t->type = DM_TYPE_BIO_BASED;
  659. return 0;
  660. }
  661. BUG_ON(!request_based); /* No targets in this table */
  662. /* Non-request-stackable devices can't be used for request-based dm */
  663. devices = dm_table_get_devices(t);
  664. list_for_each_entry(dd, devices, list) {
  665. if (!blk_queue_stackable(bdev_get_queue(dd->dm_dev.bdev))) {
  666. DMWARN("table load rejected: including"
  667. " non-request-stackable devices");
  668. return -EINVAL;
  669. }
  670. }
  671. /*
  672. * Request-based dm supports only tables that have a single target now.
  673. * To support multiple targets, request splitting support is needed,
  674. * and that needs lots of changes in the block-layer.
  675. * (e.g. request completion process for partial completion.)
  676. */
  677. if (t->num_targets > 1) {
  678. DMWARN("Request-based dm doesn't support multiple targets yet");
  679. return -EINVAL;
  680. }
  681. t->type = DM_TYPE_REQUEST_BASED;
  682. return 0;
  683. }
  684. unsigned dm_table_get_type(struct dm_table *t)
  685. {
  686. return t->type;
  687. }
  688. bool dm_table_request_based(struct dm_table *t)
  689. {
  690. return dm_table_get_type(t) == DM_TYPE_REQUEST_BASED;
  691. }
  692. int dm_table_alloc_md_mempools(struct dm_table *t)
  693. {
  694. unsigned type = dm_table_get_type(t);
  695. if (unlikely(type == DM_TYPE_NONE)) {
  696. DMWARN("no table type is set, can't allocate mempools");
  697. return -EINVAL;
  698. }
  699. t->mempools = dm_alloc_md_mempools(type);
  700. if (!t->mempools)
  701. return -ENOMEM;
  702. return 0;
  703. }
  704. void dm_table_free_md_mempools(struct dm_table *t)
  705. {
  706. dm_free_md_mempools(t->mempools);
  707. t->mempools = NULL;
  708. }
  709. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  710. {
  711. return t->mempools;
  712. }
  713. static int setup_indexes(struct dm_table *t)
  714. {
  715. int i;
  716. unsigned int total = 0;
  717. sector_t *indexes;
  718. /* allocate the space for *all* the indexes */
  719. for (i = t->depth - 2; i >= 0; i--) {
  720. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  721. total += t->counts[i];
  722. }
  723. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  724. if (!indexes)
  725. return -ENOMEM;
  726. /* set up internal nodes, bottom-up */
  727. for (i = t->depth - 2; i >= 0; i--) {
  728. t->index[i] = indexes;
  729. indexes += (KEYS_PER_NODE * t->counts[i]);
  730. setup_btree_index(i, t);
  731. }
  732. return 0;
  733. }
  734. /*
  735. * Builds the btree to index the map.
  736. */
  737. static int dm_table_build_index(struct dm_table *t)
  738. {
  739. int r = 0;
  740. unsigned int leaf_nodes;
  741. /* how many indexes will the btree have ? */
  742. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  743. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  744. /* leaf layer has already been set up */
  745. t->counts[t->depth - 1] = leaf_nodes;
  746. t->index[t->depth - 1] = t->highs;
  747. if (t->depth >= 2)
  748. r = setup_indexes(t);
  749. return r;
  750. }
  751. /*
  752. * Register the mapped device for blk_integrity support if
  753. * the underlying devices support it.
  754. */
  755. static int dm_table_prealloc_integrity(struct dm_table *t, struct mapped_device *md)
  756. {
  757. struct list_head *devices = dm_table_get_devices(t);
  758. struct dm_dev_internal *dd;
  759. list_for_each_entry(dd, devices, list)
  760. if (bdev_get_integrity(dd->dm_dev.bdev))
  761. return blk_integrity_register(dm_disk(md), NULL);
  762. return 0;
  763. }
  764. /*
  765. * Prepares the table for use by building the indices,
  766. * setting the type, and allocating mempools.
  767. */
  768. int dm_table_complete(struct dm_table *t)
  769. {
  770. int r;
  771. r = dm_table_set_type(t);
  772. if (r) {
  773. DMERR("unable to set table type");
  774. return r;
  775. }
  776. r = dm_table_build_index(t);
  777. if (r) {
  778. DMERR("unable to build btrees");
  779. return r;
  780. }
  781. r = dm_table_prealloc_integrity(t, t->md);
  782. if (r) {
  783. DMERR("could not register integrity profile.");
  784. return r;
  785. }
  786. r = dm_table_alloc_md_mempools(t);
  787. if (r)
  788. DMERR("unable to allocate mempools");
  789. return r;
  790. }
  791. static DEFINE_MUTEX(_event_lock);
  792. void dm_table_event_callback(struct dm_table *t,
  793. void (*fn)(void *), void *context)
  794. {
  795. mutex_lock(&_event_lock);
  796. t->event_fn = fn;
  797. t->event_context = context;
  798. mutex_unlock(&_event_lock);
  799. }
  800. void dm_table_event(struct dm_table *t)
  801. {
  802. /*
  803. * You can no longer call dm_table_event() from interrupt
  804. * context, use a bottom half instead.
  805. */
  806. BUG_ON(in_interrupt());
  807. mutex_lock(&_event_lock);
  808. if (t->event_fn)
  809. t->event_fn(t->event_context);
  810. mutex_unlock(&_event_lock);
  811. }
  812. sector_t dm_table_get_size(struct dm_table *t)
  813. {
  814. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  815. }
  816. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  817. {
  818. if (index >= t->num_targets)
  819. return NULL;
  820. return t->targets + index;
  821. }
  822. /*
  823. * Search the btree for the correct target.
  824. *
  825. * Caller should check returned pointer with dm_target_is_valid()
  826. * to trap I/O beyond end of device.
  827. */
  828. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  829. {
  830. unsigned int l, n = 0, k = 0;
  831. sector_t *node;
  832. for (l = 0; l < t->depth; l++) {
  833. n = get_child(n, k);
  834. node = get_node(t, l, n);
  835. for (k = 0; k < KEYS_PER_NODE; k++)
  836. if (node[k] >= sector)
  837. break;
  838. }
  839. return &t->targets[(KEYS_PER_NODE * n) + k];
  840. }
  841. /*
  842. * Establish the new table's queue_limits and validate them.
  843. */
  844. int dm_calculate_queue_limits(struct dm_table *table,
  845. struct queue_limits *limits)
  846. {
  847. struct dm_target *uninitialized_var(ti);
  848. struct queue_limits ti_limits;
  849. unsigned i = 0;
  850. blk_set_default_limits(limits);
  851. while (i < dm_table_get_num_targets(table)) {
  852. blk_set_default_limits(&ti_limits);
  853. ti = dm_table_get_target(table, i++);
  854. if (!ti->type->iterate_devices)
  855. goto combine_limits;
  856. /*
  857. * Combine queue limits of all the devices this target uses.
  858. */
  859. ti->type->iterate_devices(ti, dm_set_device_limits,
  860. &ti_limits);
  861. /* Set I/O hints portion of queue limits */
  862. if (ti->type->io_hints)
  863. ti->type->io_hints(ti, &ti_limits);
  864. /*
  865. * Check each device area is consistent with the target's
  866. * overall queue limits.
  867. */
  868. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  869. &ti_limits))
  870. return -EINVAL;
  871. combine_limits:
  872. /*
  873. * Merge this target's queue limits into the overall limits
  874. * for the table.
  875. */
  876. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  877. DMWARN("%s: adding target device "
  878. "(start sect %llu len %llu) "
  879. "caused an alignment inconsistency",
  880. dm_device_name(table->md),
  881. (unsigned long long) ti->begin,
  882. (unsigned long long) ti->len);
  883. }
  884. return validate_hardware_logical_block_alignment(table, limits);
  885. }
  886. /*
  887. * Set the integrity profile for this device if all devices used have
  888. * matching profiles.
  889. */
  890. static void dm_table_set_integrity(struct dm_table *t)
  891. {
  892. struct list_head *devices = dm_table_get_devices(t);
  893. struct dm_dev_internal *prev = NULL, *dd = NULL;
  894. if (!blk_get_integrity(dm_disk(t->md)))
  895. return;
  896. list_for_each_entry(dd, devices, list) {
  897. if (prev &&
  898. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  899. dd->dm_dev.bdev->bd_disk) < 0) {
  900. DMWARN("%s: integrity not set: %s and %s mismatch",
  901. dm_device_name(t->md),
  902. prev->dm_dev.bdev->bd_disk->disk_name,
  903. dd->dm_dev.bdev->bd_disk->disk_name);
  904. goto no_integrity;
  905. }
  906. prev = dd;
  907. }
  908. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  909. goto no_integrity;
  910. blk_integrity_register(dm_disk(t->md),
  911. bdev_get_integrity(prev->dm_dev.bdev));
  912. return;
  913. no_integrity:
  914. blk_integrity_register(dm_disk(t->md), NULL);
  915. return;
  916. }
  917. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  918. struct queue_limits *limits)
  919. {
  920. /*
  921. * Copy table's limits to the DM device's request_queue
  922. */
  923. q->limits = *limits;
  924. if (!dm_table_supports_discards(t))
  925. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  926. else
  927. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  928. dm_table_set_integrity(t);
  929. /*
  930. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  931. * visible to other CPUs because, once the flag is set, incoming bios
  932. * are processed by request-based dm, which refers to the queue
  933. * settings.
  934. * Until the flag set, bios are passed to bio-based dm and queued to
  935. * md->deferred where queue settings are not needed yet.
  936. * Those bios are passed to request-based dm at the resume time.
  937. */
  938. smp_mb();
  939. if (dm_table_request_based(t))
  940. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  941. }
  942. unsigned int dm_table_get_num_targets(struct dm_table *t)
  943. {
  944. return t->num_targets;
  945. }
  946. struct list_head *dm_table_get_devices(struct dm_table *t)
  947. {
  948. return &t->devices;
  949. }
  950. fmode_t dm_table_get_mode(struct dm_table *t)
  951. {
  952. return t->mode;
  953. }
  954. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  955. {
  956. int i = t->num_targets;
  957. struct dm_target *ti = t->targets;
  958. while (i--) {
  959. if (postsuspend) {
  960. if (ti->type->postsuspend)
  961. ti->type->postsuspend(ti);
  962. } else if (ti->type->presuspend)
  963. ti->type->presuspend(ti);
  964. ti++;
  965. }
  966. }
  967. void dm_table_presuspend_targets(struct dm_table *t)
  968. {
  969. if (!t)
  970. return;
  971. suspend_targets(t, 0);
  972. }
  973. void dm_table_postsuspend_targets(struct dm_table *t)
  974. {
  975. if (!t)
  976. return;
  977. suspend_targets(t, 1);
  978. }
  979. int dm_table_resume_targets(struct dm_table *t)
  980. {
  981. int i, r = 0;
  982. for (i = 0; i < t->num_targets; i++) {
  983. struct dm_target *ti = t->targets + i;
  984. if (!ti->type->preresume)
  985. continue;
  986. r = ti->type->preresume(ti);
  987. if (r)
  988. return r;
  989. }
  990. for (i = 0; i < t->num_targets; i++) {
  991. struct dm_target *ti = t->targets + i;
  992. if (ti->type->resume)
  993. ti->type->resume(ti);
  994. }
  995. return 0;
  996. }
  997. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  998. {
  999. list_add(&cb->list, &t->target_callbacks);
  1000. }
  1001. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1002. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1003. {
  1004. struct dm_dev_internal *dd;
  1005. struct list_head *devices = dm_table_get_devices(t);
  1006. struct dm_target_callbacks *cb;
  1007. int r = 0;
  1008. list_for_each_entry(dd, devices, list) {
  1009. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  1010. char b[BDEVNAME_SIZE];
  1011. if (likely(q))
  1012. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  1013. else
  1014. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1015. dm_device_name(t->md),
  1016. bdevname(dd->dm_dev.bdev, b));
  1017. }
  1018. list_for_each_entry(cb, &t->target_callbacks, list)
  1019. if (cb->congested_fn)
  1020. r |= cb->congested_fn(cb, bdi_bits);
  1021. return r;
  1022. }
  1023. int dm_table_any_busy_target(struct dm_table *t)
  1024. {
  1025. unsigned i;
  1026. struct dm_target *ti;
  1027. for (i = 0; i < t->num_targets; i++) {
  1028. ti = t->targets + i;
  1029. if (ti->type->busy && ti->type->busy(ti))
  1030. return 1;
  1031. }
  1032. return 0;
  1033. }
  1034. void dm_table_unplug_all(struct dm_table *t)
  1035. {
  1036. struct dm_dev_internal *dd;
  1037. struct list_head *devices = dm_table_get_devices(t);
  1038. struct dm_target_callbacks *cb;
  1039. list_for_each_entry(dd, devices, list) {
  1040. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  1041. char b[BDEVNAME_SIZE];
  1042. if (likely(q))
  1043. blk_unplug(q);
  1044. else
  1045. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  1046. dm_device_name(t->md),
  1047. bdevname(dd->dm_dev.bdev, b));
  1048. }
  1049. list_for_each_entry(cb, &t->target_callbacks, list)
  1050. if (cb->unplug_fn)
  1051. cb->unplug_fn(cb);
  1052. }
  1053. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1054. {
  1055. return t->md;
  1056. }
  1057. static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1058. sector_t start, sector_t len, void *data)
  1059. {
  1060. struct request_queue *q = bdev_get_queue(dev->bdev);
  1061. return q && blk_queue_discard(q);
  1062. }
  1063. bool dm_table_supports_discards(struct dm_table *t)
  1064. {
  1065. struct dm_target *ti;
  1066. unsigned i = 0;
  1067. if (!t->discards_supported)
  1068. return 0;
  1069. /*
  1070. * Ensure that at least one underlying device supports discards.
  1071. * t->devices includes internal dm devices such as mirror logs
  1072. * so we need to use iterate_devices here, which targets
  1073. * supporting discard must provide.
  1074. */
  1075. while (i < dm_table_get_num_targets(t)) {
  1076. ti = dm_table_get_target(t, i++);
  1077. if (ti->type->iterate_devices &&
  1078. ti->type->iterate_devices(ti, device_discard_capable, NULL))
  1079. return 1;
  1080. }
  1081. return 0;
  1082. }
  1083. EXPORT_SYMBOL(dm_vcalloc);
  1084. EXPORT_SYMBOL(dm_get_device);
  1085. EXPORT_SYMBOL(dm_put_device);
  1086. EXPORT_SYMBOL(dm_table_event);
  1087. EXPORT_SYMBOL(dm_table_get_size);
  1088. EXPORT_SYMBOL(dm_table_get_mode);
  1089. EXPORT_SYMBOL(dm_table_get_md);
  1090. EXPORT_SYMBOL(dm_table_put);
  1091. EXPORT_SYMBOL(dm_table_get);
  1092. EXPORT_SYMBOL(dm_table_unplug_all);