dm-table.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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. /*
  49. * Indicates the rw permissions for the new logical
  50. * device. This should be a combination of FMODE_READ
  51. * and FMODE_WRITE.
  52. */
  53. fmode_t mode;
  54. /* a list of devices used by this table */
  55. struct list_head devices;
  56. /* events get handed up using this callback */
  57. void (*event_fn)(void *);
  58. void *event_context;
  59. struct dm_md_mempools *mempools;
  60. };
  61. /*
  62. * Similar to ceiling(log_size(n))
  63. */
  64. static unsigned int int_log(unsigned int n, unsigned int base)
  65. {
  66. int result = 0;
  67. while (n > 1) {
  68. n = dm_div_up(n, base);
  69. result++;
  70. }
  71. return result;
  72. }
  73. /*
  74. * Calculate the index of the child node of the n'th node k'th key.
  75. */
  76. static inline unsigned int get_child(unsigned int n, unsigned int k)
  77. {
  78. return (n * CHILDREN_PER_NODE) + k;
  79. }
  80. /*
  81. * Return the n'th node of level l from table t.
  82. */
  83. static inline sector_t *get_node(struct dm_table *t,
  84. unsigned int l, unsigned int n)
  85. {
  86. return t->index[l] + (n * KEYS_PER_NODE);
  87. }
  88. /*
  89. * Return the highest key that you could lookup from the n'th
  90. * node on level l of the btree.
  91. */
  92. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  93. {
  94. for (; l < t->depth - 1; l++)
  95. n = get_child(n, CHILDREN_PER_NODE - 1);
  96. if (n >= t->counts[l])
  97. return (sector_t) - 1;
  98. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  99. }
  100. /*
  101. * Fills in a level of the btree based on the highs of the level
  102. * below it.
  103. */
  104. static int setup_btree_index(unsigned int l, struct dm_table *t)
  105. {
  106. unsigned int n, k;
  107. sector_t *node;
  108. for (n = 0U; n < t->counts[l]; n++) {
  109. node = get_node(t, l, n);
  110. for (k = 0U; k < KEYS_PER_NODE; k++)
  111. node[k] = high(t, l + 1, get_child(n, k));
  112. }
  113. return 0;
  114. }
  115. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  116. {
  117. unsigned long size;
  118. void *addr;
  119. /*
  120. * Check that we're not going to overflow.
  121. */
  122. if (nmemb > (ULONG_MAX / elem_size))
  123. return NULL;
  124. size = nmemb * elem_size;
  125. addr = vmalloc(size);
  126. if (addr)
  127. memset(addr, 0, size);
  128. return addr;
  129. }
  130. /*
  131. * highs, and targets are managed as dynamic arrays during a
  132. * table load.
  133. */
  134. static int alloc_targets(struct dm_table *t, unsigned int num)
  135. {
  136. sector_t *n_highs;
  137. struct dm_target *n_targets;
  138. int n = t->num_targets;
  139. /*
  140. * Allocate both the target array and offset array at once.
  141. * Append an empty entry to catch sectors beyond the end of
  142. * the device.
  143. */
  144. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  145. sizeof(sector_t));
  146. if (!n_highs)
  147. return -ENOMEM;
  148. n_targets = (struct dm_target *) (n_highs + num);
  149. if (n) {
  150. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  151. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  152. }
  153. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  154. vfree(t->highs);
  155. t->num_allocated = num;
  156. t->highs = n_highs;
  157. t->targets = n_targets;
  158. return 0;
  159. }
  160. int dm_table_create(struct dm_table **result, fmode_t mode,
  161. unsigned num_targets, struct mapped_device *md)
  162. {
  163. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  164. if (!t)
  165. return -ENOMEM;
  166. INIT_LIST_HEAD(&t->devices);
  167. atomic_set(&t->holders, 0);
  168. if (!num_targets)
  169. num_targets = KEYS_PER_NODE;
  170. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  171. if (alloc_targets(t, num_targets)) {
  172. kfree(t);
  173. t = NULL;
  174. return -ENOMEM;
  175. }
  176. t->mode = mode;
  177. t->md = md;
  178. *result = t;
  179. return 0;
  180. }
  181. static void free_devices(struct list_head *devices)
  182. {
  183. struct list_head *tmp, *next;
  184. list_for_each_safe(tmp, next, devices) {
  185. struct dm_dev_internal *dd =
  186. list_entry(tmp, struct dm_dev_internal, list);
  187. DMWARN("dm_table_destroy: dm_put_device call missing for %s",
  188. dd->dm_dev.name);
  189. kfree(dd);
  190. }
  191. }
  192. void dm_table_destroy(struct dm_table *t)
  193. {
  194. unsigned int i;
  195. while (atomic_read(&t->holders))
  196. msleep(1);
  197. smp_mb();
  198. /* free the indexes (see dm_table_complete) */
  199. if (t->depth >= 2)
  200. vfree(t->index[t->depth - 2]);
  201. /* free the targets */
  202. for (i = 0; i < t->num_targets; i++) {
  203. struct dm_target *tgt = t->targets + i;
  204. if (tgt->type->dtr)
  205. tgt->type->dtr(tgt);
  206. dm_put_target_type(tgt->type);
  207. }
  208. vfree(t->highs);
  209. /* free the device list */
  210. if (t->devices.next != &t->devices)
  211. free_devices(&t->devices);
  212. dm_free_md_mempools(t->mempools);
  213. kfree(t);
  214. }
  215. void dm_table_get(struct dm_table *t)
  216. {
  217. atomic_inc(&t->holders);
  218. }
  219. void dm_table_put(struct dm_table *t)
  220. {
  221. if (!t)
  222. return;
  223. smp_mb__before_atomic_dec();
  224. atomic_dec(&t->holders);
  225. }
  226. /*
  227. * Checks to see if we need to extend highs or targets.
  228. */
  229. static inline int check_space(struct dm_table *t)
  230. {
  231. if (t->num_targets >= t->num_allocated)
  232. return alloc_targets(t, t->num_allocated * 2);
  233. return 0;
  234. }
  235. /*
  236. * See if we've already got a device in the list.
  237. */
  238. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  239. {
  240. struct dm_dev_internal *dd;
  241. list_for_each_entry (dd, l, list)
  242. if (dd->dm_dev.bdev->bd_dev == dev)
  243. return dd;
  244. return NULL;
  245. }
  246. /*
  247. * Open a device so we can use it as a map destination.
  248. */
  249. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  250. struct mapped_device *md)
  251. {
  252. static char *_claim_ptr = "I belong to device-mapper";
  253. struct block_device *bdev;
  254. int r;
  255. BUG_ON(d->dm_dev.bdev);
  256. bdev = open_by_devnum(dev, d->dm_dev.mode);
  257. if (IS_ERR(bdev))
  258. return PTR_ERR(bdev);
  259. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  260. if (r)
  261. blkdev_put(bdev, d->dm_dev.mode);
  262. else
  263. d->dm_dev.bdev = bdev;
  264. return r;
  265. }
  266. /*
  267. * Close a device that we've been using.
  268. */
  269. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  270. {
  271. if (!d->dm_dev.bdev)
  272. return;
  273. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  274. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
  275. d->dm_dev.bdev = NULL;
  276. }
  277. /*
  278. * If possible, this checks an area of a destination device is invalid.
  279. */
  280. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  281. sector_t start, sector_t len, void *data)
  282. {
  283. struct queue_limits *limits = data;
  284. struct block_device *bdev = dev->bdev;
  285. sector_t dev_size =
  286. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  287. unsigned short logical_block_size_sectors =
  288. limits->logical_block_size >> SECTOR_SHIFT;
  289. char b[BDEVNAME_SIZE];
  290. if (!dev_size)
  291. return 0;
  292. if ((start >= dev_size) || (start + len > dev_size)) {
  293. DMWARN("%s: %s too small for target: "
  294. "start=%llu, len=%llu, dev_size=%llu",
  295. dm_device_name(ti->table->md), bdevname(bdev, b),
  296. (unsigned long long)start,
  297. (unsigned long long)len,
  298. (unsigned long long)dev_size);
  299. return 1;
  300. }
  301. if (logical_block_size_sectors <= 1)
  302. return 0;
  303. if (start & (logical_block_size_sectors - 1)) {
  304. DMWARN("%s: start=%llu not aligned to h/w "
  305. "logical block size %u of %s",
  306. dm_device_name(ti->table->md),
  307. (unsigned long long)start,
  308. limits->logical_block_size, bdevname(bdev, b));
  309. return 1;
  310. }
  311. if (len & (logical_block_size_sectors - 1)) {
  312. DMWARN("%s: len=%llu not aligned to h/w "
  313. "logical block size %u of %s",
  314. dm_device_name(ti->table->md),
  315. (unsigned long long)len,
  316. limits->logical_block_size, bdevname(bdev, b));
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. /*
  322. * This upgrades the mode on an already open dm_dev, being
  323. * careful to leave things as they were if we fail to reopen the
  324. * device and not to touch the existing bdev field in case
  325. * it is accessed concurrently inside dm_table_any_congested().
  326. */
  327. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  328. struct mapped_device *md)
  329. {
  330. int r;
  331. struct dm_dev_internal dd_new, dd_old;
  332. dd_new = dd_old = *dd;
  333. dd_new.dm_dev.mode |= new_mode;
  334. dd_new.dm_dev.bdev = NULL;
  335. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  336. if (r)
  337. return r;
  338. dd->dm_dev.mode |= new_mode;
  339. close_dev(&dd_old, md);
  340. return 0;
  341. }
  342. /*
  343. * Add a device to the list, or just increment the usage count if
  344. * it's already present.
  345. */
  346. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  347. const char *path, sector_t start, sector_t len,
  348. fmode_t mode, struct dm_dev **result)
  349. {
  350. int r;
  351. dev_t uninitialized_var(dev);
  352. struct dm_dev_internal *dd;
  353. unsigned int major, minor;
  354. BUG_ON(!t);
  355. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  356. /* Extract the major/minor numbers */
  357. dev = MKDEV(major, minor);
  358. if (MAJOR(dev) != major || MINOR(dev) != minor)
  359. return -EOVERFLOW;
  360. } else {
  361. /* convert the path to a device */
  362. struct block_device *bdev = lookup_bdev(path);
  363. if (IS_ERR(bdev))
  364. return PTR_ERR(bdev);
  365. dev = bdev->bd_dev;
  366. bdput(bdev);
  367. }
  368. dd = find_device(&t->devices, dev);
  369. if (!dd) {
  370. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  371. if (!dd)
  372. return -ENOMEM;
  373. dd->dm_dev.mode = mode;
  374. dd->dm_dev.bdev = NULL;
  375. if ((r = open_dev(dd, dev, t->md))) {
  376. kfree(dd);
  377. return r;
  378. }
  379. format_dev_t(dd->dm_dev.name, dev);
  380. atomic_set(&dd->count, 0);
  381. list_add(&dd->list, &t->devices);
  382. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  383. r = upgrade_mode(dd, mode, t->md);
  384. if (r)
  385. return r;
  386. }
  387. atomic_inc(&dd->count);
  388. *result = &dd->dm_dev;
  389. return 0;
  390. }
  391. /*
  392. * Returns the minimum that is _not_ zero, unless both are zero.
  393. */
  394. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  395. int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  396. sector_t start, sector_t len, void *data)
  397. {
  398. struct queue_limits *limits = data;
  399. struct block_device *bdev = dev->bdev;
  400. struct request_queue *q = bdev_get_queue(bdev);
  401. char b[BDEVNAME_SIZE];
  402. if (unlikely(!q)) {
  403. DMWARN("%s: Cannot set limits for nonexistent device %s",
  404. dm_device_name(ti->table->md), bdevname(bdev, b));
  405. return 0;
  406. }
  407. if (blk_stack_limits(limits, &q->limits, start << 9) < 0)
  408. DMWARN("%s: target device %s is misaligned: "
  409. "physical_block_size=%u, logical_block_size=%u, "
  410. "alignment_offset=%u, start=%llu",
  411. dm_device_name(ti->table->md), bdevname(bdev, b),
  412. q->limits.physical_block_size,
  413. q->limits.logical_block_size,
  414. q->limits.alignment_offset,
  415. (unsigned long long) start << 9);
  416. /*
  417. * Check if merge fn is supported.
  418. * If not we'll force DM to use PAGE_SIZE or
  419. * smaller I/O, just to be safe.
  420. */
  421. if (q->merge_bvec_fn && !ti->type->merge)
  422. limits->max_sectors =
  423. min_not_zero(limits->max_sectors,
  424. (unsigned int) (PAGE_SIZE >> 9));
  425. return 0;
  426. }
  427. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  428. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  429. sector_t len, fmode_t mode, struct dm_dev **result)
  430. {
  431. return __table_get_device(ti->table, ti, path,
  432. start, len, 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. 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. 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. int dm_table_complete(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. static DEFINE_MUTEX(_event_lock);
  750. void dm_table_event_callback(struct dm_table *t,
  751. void (*fn)(void *), void *context)
  752. {
  753. mutex_lock(&_event_lock);
  754. t->event_fn = fn;
  755. t->event_context = context;
  756. mutex_unlock(&_event_lock);
  757. }
  758. void dm_table_event(struct dm_table *t)
  759. {
  760. /*
  761. * You can no longer call dm_table_event() from interrupt
  762. * context, use a bottom half instead.
  763. */
  764. BUG_ON(in_interrupt());
  765. mutex_lock(&_event_lock);
  766. if (t->event_fn)
  767. t->event_fn(t->event_context);
  768. mutex_unlock(&_event_lock);
  769. }
  770. sector_t dm_table_get_size(struct dm_table *t)
  771. {
  772. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  773. }
  774. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  775. {
  776. if (index >= t->num_targets)
  777. return NULL;
  778. return t->targets + index;
  779. }
  780. /*
  781. * Search the btree for the correct target.
  782. *
  783. * Caller should check returned pointer with dm_target_is_valid()
  784. * to trap I/O beyond end of device.
  785. */
  786. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  787. {
  788. unsigned int l, n = 0, k = 0;
  789. sector_t *node;
  790. for (l = 0; l < t->depth; l++) {
  791. n = get_child(n, k);
  792. node = get_node(t, l, n);
  793. for (k = 0; k < KEYS_PER_NODE; k++)
  794. if (node[k] >= sector)
  795. break;
  796. }
  797. return &t->targets[(KEYS_PER_NODE * n) + k];
  798. }
  799. /*
  800. * Establish the new table's queue_limits and validate them.
  801. */
  802. int dm_calculate_queue_limits(struct dm_table *table,
  803. struct queue_limits *limits)
  804. {
  805. struct dm_target *uninitialized_var(ti);
  806. struct queue_limits ti_limits;
  807. unsigned i = 0;
  808. blk_set_default_limits(limits);
  809. while (i < dm_table_get_num_targets(table)) {
  810. blk_set_default_limits(&ti_limits);
  811. ti = dm_table_get_target(table, i++);
  812. if (!ti->type->iterate_devices)
  813. goto combine_limits;
  814. /*
  815. * Combine queue limits of all the devices this target uses.
  816. */
  817. ti->type->iterate_devices(ti, dm_set_device_limits,
  818. &ti_limits);
  819. /* Set I/O hints portion of queue limits */
  820. if (ti->type->io_hints)
  821. ti->type->io_hints(ti, &ti_limits);
  822. /*
  823. * Check each device area is consistent with the target's
  824. * overall queue limits.
  825. */
  826. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  827. &ti_limits))
  828. return -EINVAL;
  829. combine_limits:
  830. /*
  831. * Merge this target's queue limits into the overall limits
  832. * for the table.
  833. */
  834. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  835. DMWARN("%s: target device "
  836. "(start sect %llu len %llu) "
  837. "is misaligned",
  838. dm_device_name(table->md),
  839. (unsigned long long) ti->begin,
  840. (unsigned long long) ti->len);
  841. }
  842. return validate_hardware_logical_block_alignment(table, limits);
  843. }
  844. /*
  845. * Set the integrity profile for this device if all devices used have
  846. * matching profiles.
  847. */
  848. static void dm_table_set_integrity(struct dm_table *t)
  849. {
  850. struct list_head *devices = dm_table_get_devices(t);
  851. struct dm_dev_internal *prev = NULL, *dd = NULL;
  852. if (!blk_get_integrity(dm_disk(t->md)))
  853. return;
  854. list_for_each_entry(dd, devices, list) {
  855. if (prev &&
  856. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  857. dd->dm_dev.bdev->bd_disk) < 0) {
  858. DMWARN("%s: integrity not set: %s and %s mismatch",
  859. dm_device_name(t->md),
  860. prev->dm_dev.bdev->bd_disk->disk_name,
  861. dd->dm_dev.bdev->bd_disk->disk_name);
  862. goto no_integrity;
  863. }
  864. prev = dd;
  865. }
  866. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  867. goto no_integrity;
  868. blk_integrity_register(dm_disk(t->md),
  869. bdev_get_integrity(prev->dm_dev.bdev));
  870. return;
  871. no_integrity:
  872. blk_integrity_register(dm_disk(t->md), NULL);
  873. return;
  874. }
  875. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  876. struct queue_limits *limits)
  877. {
  878. /*
  879. * Each target device in the table has a data area that should normally
  880. * be aligned such that the DM device's alignment_offset is 0.
  881. * FIXME: Propagate alignment_offsets up the stack and warn of
  882. * sub-optimal or inconsistent settings.
  883. */
  884. limits->alignment_offset = 0;
  885. limits->misaligned = 0;
  886. /*
  887. * Copy table's limits to the DM device's request_queue
  888. */
  889. q->limits = *limits;
  890. if (limits->no_cluster)
  891. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  892. else
  893. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  894. dm_table_set_integrity(t);
  895. /*
  896. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  897. * visible to other CPUs because, once the flag is set, incoming bios
  898. * are processed by request-based dm, which refers to the queue
  899. * settings.
  900. * Until the flag set, bios are passed to bio-based dm and queued to
  901. * md->deferred where queue settings are not needed yet.
  902. * Those bios are passed to request-based dm at the resume time.
  903. */
  904. smp_mb();
  905. if (dm_table_request_based(t))
  906. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  907. }
  908. unsigned int dm_table_get_num_targets(struct dm_table *t)
  909. {
  910. return t->num_targets;
  911. }
  912. struct list_head *dm_table_get_devices(struct dm_table *t)
  913. {
  914. return &t->devices;
  915. }
  916. fmode_t dm_table_get_mode(struct dm_table *t)
  917. {
  918. return t->mode;
  919. }
  920. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  921. {
  922. int i = t->num_targets;
  923. struct dm_target *ti = t->targets;
  924. while (i--) {
  925. if (postsuspend) {
  926. if (ti->type->postsuspend)
  927. ti->type->postsuspend(ti);
  928. } else if (ti->type->presuspend)
  929. ti->type->presuspend(ti);
  930. ti++;
  931. }
  932. }
  933. void dm_table_presuspend_targets(struct dm_table *t)
  934. {
  935. if (!t)
  936. return;
  937. suspend_targets(t, 0);
  938. }
  939. void dm_table_postsuspend_targets(struct dm_table *t)
  940. {
  941. if (!t)
  942. return;
  943. suspend_targets(t, 1);
  944. }
  945. int dm_table_resume_targets(struct dm_table *t)
  946. {
  947. int i, r = 0;
  948. for (i = 0; i < t->num_targets; i++) {
  949. struct dm_target *ti = t->targets + i;
  950. if (!ti->type->preresume)
  951. continue;
  952. r = ti->type->preresume(ti);
  953. if (r)
  954. return r;
  955. }
  956. for (i = 0; i < t->num_targets; i++) {
  957. struct dm_target *ti = t->targets + i;
  958. if (ti->type->resume)
  959. ti->type->resume(ti);
  960. }
  961. return 0;
  962. }
  963. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  964. {
  965. struct dm_dev_internal *dd;
  966. struct list_head *devices = dm_table_get_devices(t);
  967. int r = 0;
  968. list_for_each_entry(dd, devices, list) {
  969. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  970. char b[BDEVNAME_SIZE];
  971. if (likely(q))
  972. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  973. else
  974. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  975. dm_device_name(t->md),
  976. bdevname(dd->dm_dev.bdev, b));
  977. }
  978. return r;
  979. }
  980. int dm_table_any_busy_target(struct dm_table *t)
  981. {
  982. unsigned i;
  983. struct dm_target *ti;
  984. for (i = 0; i < t->num_targets; i++) {
  985. ti = t->targets + i;
  986. if (ti->type->busy && ti->type->busy(ti))
  987. return 1;
  988. }
  989. return 0;
  990. }
  991. void dm_table_unplug_all(struct dm_table *t)
  992. {
  993. struct dm_dev_internal *dd;
  994. struct list_head *devices = dm_table_get_devices(t);
  995. list_for_each_entry(dd, devices, list) {
  996. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  997. char b[BDEVNAME_SIZE];
  998. if (likely(q))
  999. blk_unplug(q);
  1000. else
  1001. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  1002. dm_device_name(t->md),
  1003. bdevname(dd->dm_dev.bdev, b));
  1004. }
  1005. }
  1006. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1007. {
  1008. dm_get(t->md);
  1009. return t->md;
  1010. }
  1011. EXPORT_SYMBOL(dm_vcalloc);
  1012. EXPORT_SYMBOL(dm_get_device);
  1013. EXPORT_SYMBOL(dm_put_device);
  1014. EXPORT_SYMBOL(dm_table_event);
  1015. EXPORT_SYMBOL(dm_table_get_size);
  1016. EXPORT_SYMBOL(dm_table_get_mode);
  1017. EXPORT_SYMBOL(dm_table_get_md);
  1018. EXPORT_SYMBOL(dm_table_put);
  1019. EXPORT_SYMBOL(dm_table_get);
  1020. EXPORT_SYMBOL(dm_table_unplug_all);