dm-table.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. if (!t)
  196. return;
  197. while (atomic_read(&t->holders))
  198. msleep(1);
  199. smp_mb();
  200. /* free the indexes (see dm_table_complete) */
  201. if (t->depth >= 2)
  202. vfree(t->index[t->depth - 2]);
  203. /* free the targets */
  204. for (i = 0; i < t->num_targets; i++) {
  205. struct dm_target *tgt = t->targets + i;
  206. if (tgt->type->dtr)
  207. tgt->type->dtr(tgt);
  208. dm_put_target_type(tgt->type);
  209. }
  210. vfree(t->highs);
  211. /* free the device list */
  212. if (t->devices.next != &t->devices)
  213. free_devices(&t->devices);
  214. dm_free_md_mempools(t->mempools);
  215. kfree(t);
  216. }
  217. void dm_table_get(struct dm_table *t)
  218. {
  219. atomic_inc(&t->holders);
  220. }
  221. void dm_table_put(struct dm_table *t)
  222. {
  223. if (!t)
  224. return;
  225. smp_mb__before_atomic_dec();
  226. atomic_dec(&t->holders);
  227. }
  228. /*
  229. * Checks to see if we need to extend highs or targets.
  230. */
  231. static inline int check_space(struct dm_table *t)
  232. {
  233. if (t->num_targets >= t->num_allocated)
  234. return alloc_targets(t, t->num_allocated * 2);
  235. return 0;
  236. }
  237. /*
  238. * See if we've already got a device in the list.
  239. */
  240. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  241. {
  242. struct dm_dev_internal *dd;
  243. list_for_each_entry (dd, l, list)
  244. if (dd->dm_dev.bdev->bd_dev == dev)
  245. return dd;
  246. return NULL;
  247. }
  248. /*
  249. * Open a device so we can use it as a map destination.
  250. */
  251. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  252. struct mapped_device *md)
  253. {
  254. static char *_claim_ptr = "I belong to device-mapper";
  255. struct block_device *bdev;
  256. int r;
  257. BUG_ON(d->dm_dev.bdev);
  258. bdev = open_by_devnum(dev, d->dm_dev.mode);
  259. if (IS_ERR(bdev))
  260. return PTR_ERR(bdev);
  261. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  262. if (r)
  263. blkdev_put(bdev, d->dm_dev.mode);
  264. else
  265. d->dm_dev.bdev = bdev;
  266. return r;
  267. }
  268. /*
  269. * Close a device that we've been using.
  270. */
  271. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  272. {
  273. if (!d->dm_dev.bdev)
  274. return;
  275. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  276. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
  277. d->dm_dev.bdev = NULL;
  278. }
  279. /*
  280. * If possible, this checks an area of a destination device is invalid.
  281. */
  282. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  283. sector_t start, sector_t len, void *data)
  284. {
  285. struct queue_limits *limits = data;
  286. struct block_device *bdev = dev->bdev;
  287. sector_t dev_size =
  288. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  289. unsigned short logical_block_size_sectors =
  290. limits->logical_block_size >> SECTOR_SHIFT;
  291. char b[BDEVNAME_SIZE];
  292. if (!dev_size)
  293. return 0;
  294. if ((start >= dev_size) || (start + len > dev_size)) {
  295. DMWARN("%s: %s too small for target: "
  296. "start=%llu, len=%llu, dev_size=%llu",
  297. dm_device_name(ti->table->md), bdevname(bdev, b),
  298. (unsigned long long)start,
  299. (unsigned long long)len,
  300. (unsigned long long)dev_size);
  301. return 1;
  302. }
  303. if (logical_block_size_sectors <= 1)
  304. return 0;
  305. if (start & (logical_block_size_sectors - 1)) {
  306. DMWARN("%s: start=%llu not aligned to h/w "
  307. "logical block size %u of %s",
  308. dm_device_name(ti->table->md),
  309. (unsigned long long)start,
  310. limits->logical_block_size, bdevname(bdev, b));
  311. return 1;
  312. }
  313. if (len & (logical_block_size_sectors - 1)) {
  314. DMWARN("%s: len=%llu not aligned to h/w "
  315. "logical block size %u of %s",
  316. dm_device_name(ti->table->md),
  317. (unsigned long long)len,
  318. limits->logical_block_size, bdevname(bdev, b));
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * This upgrades the mode on an already open dm_dev, being
  325. * careful to leave things as they were if we fail to reopen the
  326. * device and not to touch the existing bdev field in case
  327. * it is accessed concurrently inside dm_table_any_congested().
  328. */
  329. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  330. struct mapped_device *md)
  331. {
  332. int r;
  333. struct dm_dev_internal dd_new, dd_old;
  334. dd_new = dd_old = *dd;
  335. dd_new.dm_dev.mode |= new_mode;
  336. dd_new.dm_dev.bdev = NULL;
  337. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  338. if (r)
  339. return r;
  340. dd->dm_dev.mode |= new_mode;
  341. close_dev(&dd_old, md);
  342. return 0;
  343. }
  344. /*
  345. * Add a device to the list, or just increment the usage count if
  346. * it's already present.
  347. */
  348. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  349. const char *path, sector_t start, sector_t len,
  350. fmode_t mode, struct dm_dev **result)
  351. {
  352. int r;
  353. dev_t uninitialized_var(dev);
  354. struct dm_dev_internal *dd;
  355. unsigned int major, minor;
  356. BUG_ON(!t);
  357. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  358. /* Extract the major/minor numbers */
  359. dev = MKDEV(major, minor);
  360. if (MAJOR(dev) != major || MINOR(dev) != minor)
  361. return -EOVERFLOW;
  362. } else {
  363. /* convert the path to a device */
  364. struct block_device *bdev = lookup_bdev(path);
  365. if (IS_ERR(bdev))
  366. return PTR_ERR(bdev);
  367. dev = bdev->bd_dev;
  368. bdput(bdev);
  369. }
  370. dd = find_device(&t->devices, dev);
  371. if (!dd) {
  372. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  373. if (!dd)
  374. return -ENOMEM;
  375. dd->dm_dev.mode = mode;
  376. dd->dm_dev.bdev = NULL;
  377. if ((r = open_dev(dd, dev, t->md))) {
  378. kfree(dd);
  379. return r;
  380. }
  381. format_dev_t(dd->dm_dev.name, dev);
  382. atomic_set(&dd->count, 0);
  383. list_add(&dd->list, &t->devices);
  384. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  385. r = upgrade_mode(dd, mode, t->md);
  386. if (r)
  387. return r;
  388. }
  389. atomic_inc(&dd->count);
  390. *result = &dd->dm_dev;
  391. return 0;
  392. }
  393. /*
  394. * Returns the minimum that is _not_ zero, unless both are zero.
  395. */
  396. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  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 (blk_stack_limits(limits, &q->limits, start << 9) < 0)
  410. DMWARN("%s: target device %s is misaligned: "
  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 << 9);
  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. limits->max_sectors =
  425. min_not_zero(limits->max_sectors,
  426. (unsigned int) (PAGE_SIZE >> 9));
  427. return 0;
  428. }
  429. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  430. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  431. sector_t len, fmode_t mode, struct dm_dev **result)
  432. {
  433. return __table_get_device(ti->table, ti, path,
  434. start, len, mode, result);
  435. }
  436. /*
  437. * Decrement a devices use count and remove it if necessary.
  438. */
  439. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  440. {
  441. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  442. dm_dev);
  443. if (atomic_dec_and_test(&dd->count)) {
  444. close_dev(dd, ti->table->md);
  445. list_del(&dd->list);
  446. kfree(dd);
  447. }
  448. }
  449. /*
  450. * Checks to see if the target joins onto the end of the table.
  451. */
  452. static int adjoin(struct dm_table *table, struct dm_target *ti)
  453. {
  454. struct dm_target *prev;
  455. if (!table->num_targets)
  456. return !ti->begin;
  457. prev = &table->targets[table->num_targets - 1];
  458. return (ti->begin == (prev->begin + prev->len));
  459. }
  460. /*
  461. * Used to dynamically allocate the arg array.
  462. */
  463. static char **realloc_argv(unsigned *array_size, char **old_argv)
  464. {
  465. char **argv;
  466. unsigned new_size;
  467. new_size = *array_size ? *array_size * 2 : 64;
  468. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  469. if (argv) {
  470. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  471. *array_size = new_size;
  472. }
  473. kfree(old_argv);
  474. return argv;
  475. }
  476. /*
  477. * Destructively splits up the argument list to pass to ctr.
  478. */
  479. int dm_split_args(int *argc, char ***argvp, char *input)
  480. {
  481. char *start, *end = input, *out, **argv = NULL;
  482. unsigned array_size = 0;
  483. *argc = 0;
  484. if (!input) {
  485. *argvp = NULL;
  486. return 0;
  487. }
  488. argv = realloc_argv(&array_size, argv);
  489. if (!argv)
  490. return -ENOMEM;
  491. while (1) {
  492. /* Skip whitespace */
  493. start = skip_spaces(end);
  494. if (!*start)
  495. break; /* success, we hit the end */
  496. /* 'out' is used to remove any back-quotes */
  497. end = out = start;
  498. while (*end) {
  499. /* Everything apart from '\0' can be quoted */
  500. if (*end == '\\' && *(end + 1)) {
  501. *out++ = *(end + 1);
  502. end += 2;
  503. continue;
  504. }
  505. if (isspace(*end))
  506. break; /* end of token */
  507. *out++ = *end++;
  508. }
  509. /* have we already filled the array ? */
  510. if ((*argc + 1) > array_size) {
  511. argv = realloc_argv(&array_size, argv);
  512. if (!argv)
  513. return -ENOMEM;
  514. }
  515. /* we know this is whitespace */
  516. if (*end)
  517. end++;
  518. /* terminate the string and put it in the array */
  519. *out = '\0';
  520. argv[*argc] = start;
  521. (*argc)++;
  522. }
  523. *argvp = argv;
  524. return 0;
  525. }
  526. /*
  527. * Impose necessary and sufficient conditions on a devices's table such
  528. * that any incoming bio which respects its logical_block_size can be
  529. * processed successfully. If it falls across the boundary between
  530. * two or more targets, the size of each piece it gets split into must
  531. * be compatible with the logical_block_size of the target processing it.
  532. */
  533. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  534. struct queue_limits *limits)
  535. {
  536. /*
  537. * This function uses arithmetic modulo the logical_block_size
  538. * (in units of 512-byte sectors).
  539. */
  540. unsigned short device_logical_block_size_sects =
  541. limits->logical_block_size >> SECTOR_SHIFT;
  542. /*
  543. * Offset of the start of the next table entry, mod logical_block_size.
  544. */
  545. unsigned short next_target_start = 0;
  546. /*
  547. * Given an aligned bio that extends beyond the end of a
  548. * target, how many sectors must the next target handle?
  549. */
  550. unsigned short remaining = 0;
  551. struct dm_target *uninitialized_var(ti);
  552. struct queue_limits ti_limits;
  553. unsigned i = 0;
  554. /*
  555. * Check each entry in the table in turn.
  556. */
  557. while (i < dm_table_get_num_targets(table)) {
  558. ti = dm_table_get_target(table, i++);
  559. blk_set_default_limits(&ti_limits);
  560. /* combine all target devices' limits */
  561. if (ti->type->iterate_devices)
  562. ti->type->iterate_devices(ti, dm_set_device_limits,
  563. &ti_limits);
  564. /*
  565. * If the remaining sectors fall entirely within this
  566. * table entry are they compatible with its logical_block_size?
  567. */
  568. if (remaining < ti->len &&
  569. remaining & ((ti_limits.logical_block_size >>
  570. SECTOR_SHIFT) - 1))
  571. break; /* Error */
  572. next_target_start =
  573. (unsigned short) ((next_target_start + ti->len) &
  574. (device_logical_block_size_sects - 1));
  575. remaining = next_target_start ?
  576. device_logical_block_size_sects - next_target_start : 0;
  577. }
  578. if (remaining) {
  579. DMWARN("%s: table line %u (start sect %llu len %llu) "
  580. "not aligned to h/w logical block size %u",
  581. dm_device_name(table->md), i,
  582. (unsigned long long) ti->begin,
  583. (unsigned long long) ti->len,
  584. limits->logical_block_size);
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. int dm_table_add_target(struct dm_table *t, const char *type,
  590. sector_t start, sector_t len, char *params)
  591. {
  592. int r = -EINVAL, argc;
  593. char **argv;
  594. struct dm_target *tgt;
  595. if ((r = check_space(t)))
  596. return r;
  597. tgt = t->targets + t->num_targets;
  598. memset(tgt, 0, sizeof(*tgt));
  599. if (!len) {
  600. DMERR("%s: zero-length target", dm_device_name(t->md));
  601. return -EINVAL;
  602. }
  603. tgt->type = dm_get_target_type(type);
  604. if (!tgt->type) {
  605. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  606. type);
  607. return -EINVAL;
  608. }
  609. tgt->table = t;
  610. tgt->begin = start;
  611. tgt->len = len;
  612. tgt->error = "Unknown error";
  613. /*
  614. * Does this target adjoin the previous one ?
  615. */
  616. if (!adjoin(t, tgt)) {
  617. tgt->error = "Gap in table";
  618. r = -EINVAL;
  619. goto bad;
  620. }
  621. r = dm_split_args(&argc, &argv, params);
  622. if (r) {
  623. tgt->error = "couldn't split parameters (insufficient memory)";
  624. goto bad;
  625. }
  626. r = tgt->type->ctr(tgt, argc, argv);
  627. kfree(argv);
  628. if (r)
  629. goto bad;
  630. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  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. 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. int dm_table_complete(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. static DEFINE_MUTEX(_event_lock);
  752. void dm_table_event_callback(struct dm_table *t,
  753. void (*fn)(void *), void *context)
  754. {
  755. mutex_lock(&_event_lock);
  756. t->event_fn = fn;
  757. t->event_context = context;
  758. mutex_unlock(&_event_lock);
  759. }
  760. void dm_table_event(struct dm_table *t)
  761. {
  762. /*
  763. * You can no longer call dm_table_event() from interrupt
  764. * context, use a bottom half instead.
  765. */
  766. BUG_ON(in_interrupt());
  767. mutex_lock(&_event_lock);
  768. if (t->event_fn)
  769. t->event_fn(t->event_context);
  770. mutex_unlock(&_event_lock);
  771. }
  772. sector_t dm_table_get_size(struct dm_table *t)
  773. {
  774. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  775. }
  776. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  777. {
  778. if (index >= t->num_targets)
  779. return NULL;
  780. return t->targets + index;
  781. }
  782. /*
  783. * Search the btree for the correct target.
  784. *
  785. * Caller should check returned pointer with dm_target_is_valid()
  786. * to trap I/O beyond end of device.
  787. */
  788. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  789. {
  790. unsigned int l, n = 0, k = 0;
  791. sector_t *node;
  792. for (l = 0; l < t->depth; l++) {
  793. n = get_child(n, k);
  794. node = get_node(t, l, n);
  795. for (k = 0; k < KEYS_PER_NODE; k++)
  796. if (node[k] >= sector)
  797. break;
  798. }
  799. return &t->targets[(KEYS_PER_NODE * n) + k];
  800. }
  801. /*
  802. * Establish the new table's queue_limits and validate them.
  803. */
  804. int dm_calculate_queue_limits(struct dm_table *table,
  805. struct queue_limits *limits)
  806. {
  807. struct dm_target *uninitialized_var(ti);
  808. struct queue_limits ti_limits;
  809. unsigned i = 0;
  810. blk_set_default_limits(limits);
  811. while (i < dm_table_get_num_targets(table)) {
  812. blk_set_default_limits(&ti_limits);
  813. ti = dm_table_get_target(table, i++);
  814. if (!ti->type->iterate_devices)
  815. goto combine_limits;
  816. /*
  817. * Combine queue limits of all the devices this target uses.
  818. */
  819. ti->type->iterate_devices(ti, dm_set_device_limits,
  820. &ti_limits);
  821. /* Set I/O hints portion of queue limits */
  822. if (ti->type->io_hints)
  823. ti->type->io_hints(ti, &ti_limits);
  824. /*
  825. * Check each device area is consistent with the target's
  826. * overall queue limits.
  827. */
  828. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  829. &ti_limits))
  830. return -EINVAL;
  831. combine_limits:
  832. /*
  833. * Merge this target's queue limits into the overall limits
  834. * for the table.
  835. */
  836. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  837. DMWARN("%s: target device "
  838. "(start sect %llu len %llu) "
  839. "is misaligned",
  840. dm_device_name(table->md),
  841. (unsigned long long) ti->begin,
  842. (unsigned long long) ti->len);
  843. }
  844. return validate_hardware_logical_block_alignment(table, limits);
  845. }
  846. /*
  847. * Set the integrity profile for this device if all devices used have
  848. * matching profiles.
  849. */
  850. static void dm_table_set_integrity(struct dm_table *t)
  851. {
  852. struct list_head *devices = dm_table_get_devices(t);
  853. struct dm_dev_internal *prev = NULL, *dd = NULL;
  854. if (!blk_get_integrity(dm_disk(t->md)))
  855. return;
  856. list_for_each_entry(dd, devices, list) {
  857. if (prev &&
  858. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  859. dd->dm_dev.bdev->bd_disk) < 0) {
  860. DMWARN("%s: integrity not set: %s and %s mismatch",
  861. dm_device_name(t->md),
  862. prev->dm_dev.bdev->bd_disk->disk_name,
  863. dd->dm_dev.bdev->bd_disk->disk_name);
  864. goto no_integrity;
  865. }
  866. prev = dd;
  867. }
  868. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  869. goto no_integrity;
  870. blk_integrity_register(dm_disk(t->md),
  871. bdev_get_integrity(prev->dm_dev.bdev));
  872. return;
  873. no_integrity:
  874. blk_integrity_register(dm_disk(t->md), NULL);
  875. return;
  876. }
  877. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  878. struct queue_limits *limits)
  879. {
  880. /*
  881. * Each target device in the table has a data area that should normally
  882. * be aligned such that the DM device's alignment_offset is 0.
  883. * FIXME: Propagate alignment_offsets up the stack and warn of
  884. * sub-optimal or inconsistent settings.
  885. */
  886. limits->alignment_offset = 0;
  887. limits->misaligned = 0;
  888. /*
  889. * Copy table's limits to the DM device's request_queue
  890. */
  891. q->limits = *limits;
  892. if (limits->no_cluster)
  893. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  894. else
  895. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  896. dm_table_set_integrity(t);
  897. /*
  898. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  899. * visible to other CPUs because, once the flag is set, incoming bios
  900. * are processed by request-based dm, which refers to the queue
  901. * settings.
  902. * Until the flag set, bios are passed to bio-based dm and queued to
  903. * md->deferred where queue settings are not needed yet.
  904. * Those bios are passed to request-based dm at the resume time.
  905. */
  906. smp_mb();
  907. if (dm_table_request_based(t))
  908. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  909. }
  910. unsigned int dm_table_get_num_targets(struct dm_table *t)
  911. {
  912. return t->num_targets;
  913. }
  914. struct list_head *dm_table_get_devices(struct dm_table *t)
  915. {
  916. return &t->devices;
  917. }
  918. fmode_t dm_table_get_mode(struct dm_table *t)
  919. {
  920. return t->mode;
  921. }
  922. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  923. {
  924. int i = t->num_targets;
  925. struct dm_target *ti = t->targets;
  926. while (i--) {
  927. if (postsuspend) {
  928. if (ti->type->postsuspend)
  929. ti->type->postsuspend(ti);
  930. } else if (ti->type->presuspend)
  931. ti->type->presuspend(ti);
  932. ti++;
  933. }
  934. }
  935. void dm_table_presuspend_targets(struct dm_table *t)
  936. {
  937. if (!t)
  938. return;
  939. suspend_targets(t, 0);
  940. }
  941. void dm_table_postsuspend_targets(struct dm_table *t)
  942. {
  943. if (!t)
  944. return;
  945. suspend_targets(t, 1);
  946. }
  947. int dm_table_resume_targets(struct dm_table *t)
  948. {
  949. int i, r = 0;
  950. for (i = 0; i < t->num_targets; i++) {
  951. struct dm_target *ti = t->targets + i;
  952. if (!ti->type->preresume)
  953. continue;
  954. r = ti->type->preresume(ti);
  955. if (r)
  956. return r;
  957. }
  958. for (i = 0; i < t->num_targets; i++) {
  959. struct dm_target *ti = t->targets + i;
  960. if (ti->type->resume)
  961. ti->type->resume(ti);
  962. }
  963. return 0;
  964. }
  965. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  966. {
  967. struct dm_dev_internal *dd;
  968. struct list_head *devices = dm_table_get_devices(t);
  969. int r = 0;
  970. list_for_each_entry(dd, devices, list) {
  971. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  972. char b[BDEVNAME_SIZE];
  973. if (likely(q))
  974. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  975. else
  976. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  977. dm_device_name(t->md),
  978. bdevname(dd->dm_dev.bdev, b));
  979. }
  980. return r;
  981. }
  982. int dm_table_any_busy_target(struct dm_table *t)
  983. {
  984. unsigned i;
  985. struct dm_target *ti;
  986. for (i = 0; i < t->num_targets; i++) {
  987. ti = t->targets + i;
  988. if (ti->type->busy && ti->type->busy(ti))
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. void dm_table_unplug_all(struct dm_table *t)
  994. {
  995. struct dm_dev_internal *dd;
  996. struct list_head *devices = dm_table_get_devices(t);
  997. list_for_each_entry(dd, devices, list) {
  998. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  999. char b[BDEVNAME_SIZE];
  1000. if (likely(q))
  1001. blk_unplug(q);
  1002. else
  1003. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  1004. dm_device_name(t->md),
  1005. bdevname(dd->dm_dev.bdev, b));
  1006. }
  1007. }
  1008. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1009. {
  1010. dm_get(t->md);
  1011. return t->md;
  1012. }
  1013. EXPORT_SYMBOL(dm_vcalloc);
  1014. EXPORT_SYMBOL(dm_get_device);
  1015. EXPORT_SYMBOL(dm_put_device);
  1016. EXPORT_SYMBOL(dm_table_event);
  1017. EXPORT_SYMBOL(dm_table_get_size);
  1018. EXPORT_SYMBOL(dm_table_get_mode);
  1019. EXPORT_SYMBOL(dm_table_get_md);
  1020. EXPORT_SYMBOL(dm_table_put);
  1021. EXPORT_SYMBOL(dm_table_get);
  1022. EXPORT_SYMBOL(dm_table_unplug_all);