dm-table.c 29 KB

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