dm-table.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/mutex.h>
  16. #include <linux/delay.h>
  17. #include <asm/atomic.h>
  18. #define DM_MSG_PREFIX "table"
  19. #define MAX_DEPTH 16
  20. #define NODE_SIZE L1_CACHE_BYTES
  21. #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
  22. #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
  23. /*
  24. * The table has always exactly one reference from either mapped_device->map
  25. * or hash_cell->new_map. This reference is not counted in table->holders.
  26. * A pair of dm_create_table/dm_destroy_table functions is used for table
  27. * creation/destruction.
  28. *
  29. * Temporary references from the other code increase table->holders. A pair
  30. * of dm_table_get/dm_table_put functions is used to manipulate it.
  31. *
  32. * When the table is about to be destroyed, we wait for table->holders to
  33. * drop to zero.
  34. */
  35. struct dm_table {
  36. struct mapped_device *md;
  37. atomic_t holders;
  38. /* btree table */
  39. unsigned int depth;
  40. unsigned int counts[MAX_DEPTH]; /* in nodes */
  41. sector_t *index[MAX_DEPTH];
  42. unsigned int num_targets;
  43. unsigned int num_allocated;
  44. sector_t *highs;
  45. struct dm_target *targets;
  46. /*
  47. * Indicates the rw permissions for the new logical
  48. * device. This should be a combination of FMODE_READ
  49. * and FMODE_WRITE.
  50. */
  51. fmode_t mode;
  52. /* a list of devices used by this table */
  53. struct list_head devices;
  54. /*
  55. * These are optimistic limits taken from all the
  56. * targets, some targets will need smaller limits.
  57. */
  58. struct io_restrictions limits;
  59. /* events get handed up using this callback */
  60. void (*event_fn)(void *);
  61. void *event_context;
  62. };
  63. /*
  64. * Similar to ceiling(log_size(n))
  65. */
  66. static unsigned int int_log(unsigned int n, unsigned int base)
  67. {
  68. int result = 0;
  69. while (n > 1) {
  70. n = dm_div_up(n, base);
  71. result++;
  72. }
  73. return result;
  74. }
  75. /*
  76. * Returns the minimum that is _not_ zero, unless both are zero.
  77. */
  78. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  79. /*
  80. * Combine two io_restrictions, always taking the lower value.
  81. */
  82. static void combine_restrictions_low(struct io_restrictions *lhs,
  83. struct io_restrictions *rhs)
  84. {
  85. lhs->max_sectors =
  86. min_not_zero(lhs->max_sectors, rhs->max_sectors);
  87. lhs->max_phys_segments =
  88. min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments);
  89. lhs->max_hw_segments =
  90. min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments);
  91. lhs->logical_block_size = max(lhs->logical_block_size,
  92. rhs->logical_block_size);
  93. lhs->max_segment_size =
  94. min_not_zero(lhs->max_segment_size, rhs->max_segment_size);
  95. lhs->max_hw_sectors =
  96. min_not_zero(lhs->max_hw_sectors, rhs->max_hw_sectors);
  97. lhs->seg_boundary_mask =
  98. min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask);
  99. lhs->bounce_pfn = min_not_zero(lhs->bounce_pfn, rhs->bounce_pfn);
  100. lhs->no_cluster |= rhs->no_cluster;
  101. }
  102. /*
  103. * Calculate the index of the child node of the n'th node k'th key.
  104. */
  105. static inline unsigned int get_child(unsigned int n, unsigned int k)
  106. {
  107. return (n * CHILDREN_PER_NODE) + k;
  108. }
  109. /*
  110. * Return the n'th node of level l from table t.
  111. */
  112. static inline sector_t *get_node(struct dm_table *t,
  113. unsigned int l, unsigned int n)
  114. {
  115. return t->index[l] + (n * KEYS_PER_NODE);
  116. }
  117. /*
  118. * Return the highest key that you could lookup from the n'th
  119. * node on level l of the btree.
  120. */
  121. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  122. {
  123. for (; l < t->depth - 1; l++)
  124. n = get_child(n, CHILDREN_PER_NODE - 1);
  125. if (n >= t->counts[l])
  126. return (sector_t) - 1;
  127. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  128. }
  129. /*
  130. * Fills in a level of the btree based on the highs of the level
  131. * below it.
  132. */
  133. static int setup_btree_index(unsigned int l, struct dm_table *t)
  134. {
  135. unsigned int n, k;
  136. sector_t *node;
  137. for (n = 0U; n < t->counts[l]; n++) {
  138. node = get_node(t, l, n);
  139. for (k = 0U; k < KEYS_PER_NODE; k++)
  140. node[k] = high(t, l + 1, get_child(n, k));
  141. }
  142. return 0;
  143. }
  144. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  145. {
  146. unsigned long size;
  147. void *addr;
  148. /*
  149. * Check that we're not going to overflow.
  150. */
  151. if (nmemb > (ULONG_MAX / elem_size))
  152. return NULL;
  153. size = nmemb * elem_size;
  154. addr = vmalloc(size);
  155. if (addr)
  156. memset(addr, 0, size);
  157. return addr;
  158. }
  159. /*
  160. * highs, and targets are managed as dynamic arrays during a
  161. * table load.
  162. */
  163. static int alloc_targets(struct dm_table *t, unsigned int num)
  164. {
  165. sector_t *n_highs;
  166. struct dm_target *n_targets;
  167. int n = t->num_targets;
  168. /*
  169. * Allocate both the target array and offset array at once.
  170. * Append an empty entry to catch sectors beyond the end of
  171. * the device.
  172. */
  173. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  174. sizeof(sector_t));
  175. if (!n_highs)
  176. return -ENOMEM;
  177. n_targets = (struct dm_target *) (n_highs + num);
  178. if (n) {
  179. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  180. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  181. }
  182. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  183. vfree(t->highs);
  184. t->num_allocated = num;
  185. t->highs = n_highs;
  186. t->targets = n_targets;
  187. return 0;
  188. }
  189. int dm_table_create(struct dm_table **result, fmode_t mode,
  190. unsigned num_targets, struct mapped_device *md)
  191. {
  192. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  193. if (!t)
  194. return -ENOMEM;
  195. INIT_LIST_HEAD(&t->devices);
  196. atomic_set(&t->holders, 0);
  197. if (!num_targets)
  198. num_targets = KEYS_PER_NODE;
  199. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  200. if (alloc_targets(t, num_targets)) {
  201. kfree(t);
  202. t = NULL;
  203. return -ENOMEM;
  204. }
  205. t->mode = mode;
  206. t->md = md;
  207. *result = t;
  208. return 0;
  209. }
  210. static void free_devices(struct list_head *devices)
  211. {
  212. struct list_head *tmp, *next;
  213. list_for_each_safe(tmp, next, devices) {
  214. struct dm_dev_internal *dd =
  215. list_entry(tmp, struct dm_dev_internal, list);
  216. kfree(dd);
  217. }
  218. }
  219. void dm_table_destroy(struct dm_table *t)
  220. {
  221. unsigned int i;
  222. while (atomic_read(&t->holders))
  223. msleep(1);
  224. smp_mb();
  225. /* free the indexes (see dm_table_complete) */
  226. if (t->depth >= 2)
  227. vfree(t->index[t->depth - 2]);
  228. /* free the targets */
  229. for (i = 0; i < t->num_targets; i++) {
  230. struct dm_target *tgt = t->targets + i;
  231. if (tgt->type->dtr)
  232. tgt->type->dtr(tgt);
  233. dm_put_target_type(tgt->type);
  234. }
  235. vfree(t->highs);
  236. /* free the device list */
  237. if (t->devices.next != &t->devices) {
  238. DMWARN("devices still present during destroy: "
  239. "dm_table_remove_device calls missing");
  240. free_devices(&t->devices);
  241. }
  242. kfree(t);
  243. }
  244. void dm_table_get(struct dm_table *t)
  245. {
  246. atomic_inc(&t->holders);
  247. }
  248. void dm_table_put(struct dm_table *t)
  249. {
  250. if (!t)
  251. return;
  252. smp_mb__before_atomic_dec();
  253. atomic_dec(&t->holders);
  254. }
  255. /*
  256. * Checks to see if we need to extend highs or targets.
  257. */
  258. static inline int check_space(struct dm_table *t)
  259. {
  260. if (t->num_targets >= t->num_allocated)
  261. return alloc_targets(t, t->num_allocated * 2);
  262. return 0;
  263. }
  264. /*
  265. * See if we've already got a device in the list.
  266. */
  267. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  268. {
  269. struct dm_dev_internal *dd;
  270. list_for_each_entry (dd, l, list)
  271. if (dd->dm_dev.bdev->bd_dev == dev)
  272. return dd;
  273. return NULL;
  274. }
  275. /*
  276. * Open a device so we can use it as a map destination.
  277. */
  278. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  279. struct mapped_device *md)
  280. {
  281. static char *_claim_ptr = "I belong to device-mapper";
  282. struct block_device *bdev;
  283. int r;
  284. BUG_ON(d->dm_dev.bdev);
  285. bdev = open_by_devnum(dev, d->dm_dev.mode);
  286. if (IS_ERR(bdev))
  287. return PTR_ERR(bdev);
  288. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  289. if (r)
  290. blkdev_put(bdev, d->dm_dev.mode);
  291. else
  292. d->dm_dev.bdev = bdev;
  293. return r;
  294. }
  295. /*
  296. * Close a device that we've been using.
  297. */
  298. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  299. {
  300. if (!d->dm_dev.bdev)
  301. return;
  302. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  303. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
  304. d->dm_dev.bdev = NULL;
  305. }
  306. /*
  307. * If possible, this checks an area of a destination device is valid.
  308. */
  309. static int check_device_area(struct dm_dev_internal *dd, sector_t start,
  310. sector_t len)
  311. {
  312. sector_t dev_size = dd->dm_dev.bdev->bd_inode->i_size >> SECTOR_SHIFT;
  313. if (!dev_size)
  314. return 1;
  315. return ((start < dev_size) && (len <= (dev_size - start)));
  316. }
  317. /*
  318. * This upgrades the mode on an already open dm_dev, being
  319. * careful to leave things as they were if we fail to reopen the
  320. * device and not to touch the existing bdev field in case
  321. * it is accessed concurrently inside dm_table_any_congested().
  322. */
  323. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  324. struct mapped_device *md)
  325. {
  326. int r;
  327. struct dm_dev_internal dd_new, dd_old;
  328. dd_new = dd_old = *dd;
  329. dd_new.dm_dev.mode |= new_mode;
  330. dd_new.dm_dev.bdev = NULL;
  331. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  332. if (r)
  333. return r;
  334. dd->dm_dev.mode |= new_mode;
  335. close_dev(&dd_old, md);
  336. return 0;
  337. }
  338. /*
  339. * Add a device to the list, or just increment the usage count if
  340. * it's already present.
  341. */
  342. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  343. const char *path, sector_t start, sector_t len,
  344. fmode_t mode, struct dm_dev **result)
  345. {
  346. int r;
  347. dev_t uninitialized_var(dev);
  348. struct dm_dev_internal *dd;
  349. unsigned int major, minor;
  350. BUG_ON(!t);
  351. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  352. /* Extract the major/minor numbers */
  353. dev = MKDEV(major, minor);
  354. if (MAJOR(dev) != major || MINOR(dev) != minor)
  355. return -EOVERFLOW;
  356. } else {
  357. /* convert the path to a device */
  358. struct block_device *bdev = lookup_bdev(path);
  359. if (IS_ERR(bdev))
  360. return PTR_ERR(bdev);
  361. dev = bdev->bd_dev;
  362. bdput(bdev);
  363. }
  364. dd = find_device(&t->devices, dev);
  365. if (!dd) {
  366. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  367. if (!dd)
  368. return -ENOMEM;
  369. dd->dm_dev.mode = mode;
  370. dd->dm_dev.bdev = NULL;
  371. if ((r = open_dev(dd, dev, t->md))) {
  372. kfree(dd);
  373. return r;
  374. }
  375. format_dev_t(dd->dm_dev.name, dev);
  376. atomic_set(&dd->count, 0);
  377. list_add(&dd->list, &t->devices);
  378. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  379. r = upgrade_mode(dd, mode, t->md);
  380. if (r)
  381. return r;
  382. }
  383. atomic_inc(&dd->count);
  384. if (!check_device_area(dd, start, len)) {
  385. DMWARN("device %s too small for target", path);
  386. dm_put_device(ti, &dd->dm_dev);
  387. return -EINVAL;
  388. }
  389. *result = &dd->dm_dev;
  390. return 0;
  391. }
  392. void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
  393. {
  394. struct request_queue *q = bdev_get_queue(bdev);
  395. struct io_restrictions *rs = &ti->limits;
  396. char b[BDEVNAME_SIZE];
  397. if (unlikely(!q)) {
  398. DMWARN("%s: Cannot set limits for nonexistent device %s",
  399. dm_device_name(ti->table->md), bdevname(bdev, b));
  400. return;
  401. }
  402. /*
  403. * Combine the device limits low.
  404. *
  405. * FIXME: if we move an io_restriction struct
  406. * into q this would just be a call to
  407. * combine_restrictions_low()
  408. */
  409. rs->max_sectors =
  410. min_not_zero(rs->max_sectors, queue_max_sectors(q));
  411. /*
  412. * Check if merge fn is supported.
  413. * If not we'll force DM to use PAGE_SIZE or
  414. * smaller I/O, just to be safe.
  415. */
  416. if (q->merge_bvec_fn && !ti->type->merge)
  417. rs->max_sectors =
  418. min_not_zero(rs->max_sectors,
  419. (unsigned int) (PAGE_SIZE >> 9));
  420. rs->max_phys_segments =
  421. min_not_zero(rs->max_phys_segments,
  422. queue_max_phys_segments(q));
  423. rs->max_hw_segments =
  424. min_not_zero(rs->max_hw_segments, queue_max_hw_segments(q));
  425. rs->logical_block_size = max(rs->logical_block_size,
  426. queue_logical_block_size(q));
  427. rs->max_segment_size =
  428. min_not_zero(rs->max_segment_size, queue_max_segment_size(q));
  429. rs->max_hw_sectors =
  430. min_not_zero(rs->max_hw_sectors, queue_max_hw_sectors(q));
  431. rs->seg_boundary_mask =
  432. min_not_zero(rs->seg_boundary_mask,
  433. queue_segment_boundary(q));
  434. rs->bounce_pfn = min_not_zero(rs->bounce_pfn, queue_bounce_pfn(q));
  435. rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  436. }
  437. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  438. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  439. sector_t len, fmode_t mode, struct dm_dev **result)
  440. {
  441. int r = __table_get_device(ti->table, ti, path,
  442. start, len, mode, result);
  443. if (!r)
  444. dm_set_device_limits(ti, (*result)->bdev);
  445. return r;
  446. }
  447. /*
  448. * Decrement a devices use count and remove it if necessary.
  449. */
  450. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  451. {
  452. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  453. dm_dev);
  454. if (atomic_dec_and_test(&dd->count)) {
  455. close_dev(dd, ti->table->md);
  456. list_del(&dd->list);
  457. kfree(dd);
  458. }
  459. }
  460. /*
  461. * Checks to see if the target joins onto the end of the table.
  462. */
  463. static int adjoin(struct dm_table *table, struct dm_target *ti)
  464. {
  465. struct dm_target *prev;
  466. if (!table->num_targets)
  467. return !ti->begin;
  468. prev = &table->targets[table->num_targets - 1];
  469. return (ti->begin == (prev->begin + prev->len));
  470. }
  471. /*
  472. * Used to dynamically allocate the arg array.
  473. */
  474. static char **realloc_argv(unsigned *array_size, char **old_argv)
  475. {
  476. char **argv;
  477. unsigned new_size;
  478. new_size = *array_size ? *array_size * 2 : 64;
  479. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  480. if (argv) {
  481. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  482. *array_size = new_size;
  483. }
  484. kfree(old_argv);
  485. return argv;
  486. }
  487. /*
  488. * Destructively splits up the argument list to pass to ctr.
  489. */
  490. int dm_split_args(int *argc, char ***argvp, char *input)
  491. {
  492. char *start, *end = input, *out, **argv = NULL;
  493. unsigned array_size = 0;
  494. *argc = 0;
  495. if (!input) {
  496. *argvp = NULL;
  497. return 0;
  498. }
  499. argv = realloc_argv(&array_size, argv);
  500. if (!argv)
  501. return -ENOMEM;
  502. while (1) {
  503. start = end;
  504. /* Skip whitespace */
  505. while (*start && isspace(*start))
  506. start++;
  507. if (!*start)
  508. break; /* success, we hit the end */
  509. /* 'out' is used to remove any back-quotes */
  510. end = out = start;
  511. while (*end) {
  512. /* Everything apart from '\0' can be quoted */
  513. if (*end == '\\' && *(end + 1)) {
  514. *out++ = *(end + 1);
  515. end += 2;
  516. continue;
  517. }
  518. if (isspace(*end))
  519. break; /* end of token */
  520. *out++ = *end++;
  521. }
  522. /* have we already filled the array ? */
  523. if ((*argc + 1) > array_size) {
  524. argv = realloc_argv(&array_size, argv);
  525. if (!argv)
  526. return -ENOMEM;
  527. }
  528. /* we know this is whitespace */
  529. if (*end)
  530. end++;
  531. /* terminate the string and put it in the array */
  532. *out = '\0';
  533. argv[*argc] = start;
  534. (*argc)++;
  535. }
  536. *argvp = argv;
  537. return 0;
  538. }
  539. static void check_for_valid_limits(struct io_restrictions *rs)
  540. {
  541. if (!rs->max_sectors)
  542. rs->max_sectors = SAFE_MAX_SECTORS;
  543. if (!rs->max_hw_sectors)
  544. rs->max_hw_sectors = SAFE_MAX_SECTORS;
  545. if (!rs->max_phys_segments)
  546. rs->max_phys_segments = MAX_PHYS_SEGMENTS;
  547. if (!rs->max_hw_segments)
  548. rs->max_hw_segments = MAX_HW_SEGMENTS;
  549. if (!rs->logical_block_size)
  550. rs->logical_block_size = 1 << SECTOR_SHIFT;
  551. if (!rs->max_segment_size)
  552. rs->max_segment_size = MAX_SEGMENT_SIZE;
  553. if (!rs->seg_boundary_mask)
  554. rs->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  555. if (!rs->bounce_pfn)
  556. rs->bounce_pfn = -1;
  557. }
  558. int dm_table_add_target(struct dm_table *t, const char *type,
  559. sector_t start, sector_t len, char *params)
  560. {
  561. int r = -EINVAL, argc;
  562. char **argv;
  563. struct dm_target *tgt;
  564. if ((r = check_space(t)))
  565. return r;
  566. tgt = t->targets + t->num_targets;
  567. memset(tgt, 0, sizeof(*tgt));
  568. if (!len) {
  569. DMERR("%s: zero-length target", dm_device_name(t->md));
  570. return -EINVAL;
  571. }
  572. tgt->type = dm_get_target_type(type);
  573. if (!tgt->type) {
  574. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  575. type);
  576. return -EINVAL;
  577. }
  578. tgt->table = t;
  579. tgt->begin = start;
  580. tgt->len = len;
  581. tgt->error = "Unknown error";
  582. /*
  583. * Does this target adjoin the previous one ?
  584. */
  585. if (!adjoin(t, tgt)) {
  586. tgt->error = "Gap in table";
  587. r = -EINVAL;
  588. goto bad;
  589. }
  590. r = dm_split_args(&argc, &argv, params);
  591. if (r) {
  592. tgt->error = "couldn't split parameters (insufficient memory)";
  593. goto bad;
  594. }
  595. r = tgt->type->ctr(tgt, argc, argv);
  596. kfree(argv);
  597. if (r)
  598. goto bad;
  599. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  600. /* FIXME: the plan is to combine high here and then have
  601. * the merge fn apply the target level restrictions. */
  602. combine_restrictions_low(&t->limits, &tgt->limits);
  603. return 0;
  604. bad:
  605. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  606. dm_put_target_type(tgt->type);
  607. return r;
  608. }
  609. static int setup_indexes(struct dm_table *t)
  610. {
  611. int i;
  612. unsigned int total = 0;
  613. sector_t *indexes;
  614. /* allocate the space for *all* the indexes */
  615. for (i = t->depth - 2; i >= 0; i--) {
  616. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  617. total += t->counts[i];
  618. }
  619. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  620. if (!indexes)
  621. return -ENOMEM;
  622. /* set up internal nodes, bottom-up */
  623. for (i = t->depth - 2; i >= 0; i--) {
  624. t->index[i] = indexes;
  625. indexes += (KEYS_PER_NODE * t->counts[i]);
  626. setup_btree_index(i, t);
  627. }
  628. return 0;
  629. }
  630. /*
  631. * Builds the btree to index the map.
  632. */
  633. int dm_table_complete(struct dm_table *t)
  634. {
  635. int r = 0;
  636. unsigned int leaf_nodes;
  637. check_for_valid_limits(&t->limits);
  638. /* how many indexes will the btree have ? */
  639. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  640. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  641. /* leaf layer has already been set up */
  642. t->counts[t->depth - 1] = leaf_nodes;
  643. t->index[t->depth - 1] = t->highs;
  644. if (t->depth >= 2)
  645. r = setup_indexes(t);
  646. return r;
  647. }
  648. static DEFINE_MUTEX(_event_lock);
  649. void dm_table_event_callback(struct dm_table *t,
  650. void (*fn)(void *), void *context)
  651. {
  652. mutex_lock(&_event_lock);
  653. t->event_fn = fn;
  654. t->event_context = context;
  655. mutex_unlock(&_event_lock);
  656. }
  657. void dm_table_event(struct dm_table *t)
  658. {
  659. /*
  660. * You can no longer call dm_table_event() from interrupt
  661. * context, use a bottom half instead.
  662. */
  663. BUG_ON(in_interrupt());
  664. mutex_lock(&_event_lock);
  665. if (t->event_fn)
  666. t->event_fn(t->event_context);
  667. mutex_unlock(&_event_lock);
  668. }
  669. sector_t dm_table_get_size(struct dm_table *t)
  670. {
  671. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  672. }
  673. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  674. {
  675. if (index >= t->num_targets)
  676. return NULL;
  677. return t->targets + index;
  678. }
  679. /*
  680. * Search the btree for the correct target.
  681. *
  682. * Caller should check returned pointer with dm_target_is_valid()
  683. * to trap I/O beyond end of device.
  684. */
  685. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  686. {
  687. unsigned int l, n = 0, k = 0;
  688. sector_t *node;
  689. for (l = 0; l < t->depth; l++) {
  690. n = get_child(n, k);
  691. node = get_node(t, l, n);
  692. for (k = 0; k < KEYS_PER_NODE; k++)
  693. if (node[k] >= sector)
  694. break;
  695. }
  696. return &t->targets[(KEYS_PER_NODE * n) + k];
  697. }
  698. /*
  699. * Set the integrity profile for this device if all devices used have
  700. * matching profiles.
  701. */
  702. static void dm_table_set_integrity(struct dm_table *t)
  703. {
  704. struct list_head *devices = dm_table_get_devices(t);
  705. struct dm_dev_internal *prev = NULL, *dd = NULL;
  706. if (!blk_get_integrity(dm_disk(t->md)))
  707. return;
  708. list_for_each_entry(dd, devices, list) {
  709. if (prev &&
  710. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  711. dd->dm_dev.bdev->bd_disk) < 0) {
  712. DMWARN("%s: integrity not set: %s and %s mismatch",
  713. dm_device_name(t->md),
  714. prev->dm_dev.bdev->bd_disk->disk_name,
  715. dd->dm_dev.bdev->bd_disk->disk_name);
  716. goto no_integrity;
  717. }
  718. prev = dd;
  719. }
  720. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  721. goto no_integrity;
  722. blk_integrity_register(dm_disk(t->md),
  723. bdev_get_integrity(prev->dm_dev.bdev));
  724. return;
  725. no_integrity:
  726. blk_integrity_register(dm_disk(t->md), NULL);
  727. return;
  728. }
  729. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
  730. {
  731. /*
  732. * Make sure we obey the optimistic sub devices
  733. * restrictions.
  734. */
  735. blk_queue_max_sectors(q, t->limits.max_sectors);
  736. blk_queue_max_phys_segments(q, t->limits.max_phys_segments);
  737. blk_queue_max_hw_segments(q, t->limits.max_hw_segments);
  738. blk_queue_logical_block_size(q, t->limits.logical_block_size);
  739. blk_queue_max_segment_size(q, t->limits.max_segment_size);
  740. blk_queue_max_hw_sectors(q, t->limits.max_hw_sectors);
  741. blk_queue_segment_boundary(q, t->limits.seg_boundary_mask);
  742. blk_queue_bounce_limit(q, t->limits.bounce_pfn);
  743. if (t->limits.no_cluster)
  744. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  745. else
  746. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  747. dm_table_set_integrity(t);
  748. }
  749. unsigned int dm_table_get_num_targets(struct dm_table *t)
  750. {
  751. return t->num_targets;
  752. }
  753. struct list_head *dm_table_get_devices(struct dm_table *t)
  754. {
  755. return &t->devices;
  756. }
  757. fmode_t dm_table_get_mode(struct dm_table *t)
  758. {
  759. return t->mode;
  760. }
  761. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  762. {
  763. int i = t->num_targets;
  764. struct dm_target *ti = t->targets;
  765. while (i--) {
  766. if (postsuspend) {
  767. if (ti->type->postsuspend)
  768. ti->type->postsuspend(ti);
  769. } else if (ti->type->presuspend)
  770. ti->type->presuspend(ti);
  771. ti++;
  772. }
  773. }
  774. void dm_table_presuspend_targets(struct dm_table *t)
  775. {
  776. if (!t)
  777. return;
  778. suspend_targets(t, 0);
  779. }
  780. void dm_table_postsuspend_targets(struct dm_table *t)
  781. {
  782. if (!t)
  783. return;
  784. suspend_targets(t, 1);
  785. }
  786. int dm_table_resume_targets(struct dm_table *t)
  787. {
  788. int i, r = 0;
  789. for (i = 0; i < t->num_targets; i++) {
  790. struct dm_target *ti = t->targets + i;
  791. if (!ti->type->preresume)
  792. continue;
  793. r = ti->type->preresume(ti);
  794. if (r)
  795. return r;
  796. }
  797. for (i = 0; i < t->num_targets; i++) {
  798. struct dm_target *ti = t->targets + i;
  799. if (ti->type->resume)
  800. ti->type->resume(ti);
  801. }
  802. return 0;
  803. }
  804. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  805. {
  806. struct dm_dev_internal *dd;
  807. struct list_head *devices = dm_table_get_devices(t);
  808. int r = 0;
  809. list_for_each_entry(dd, devices, list) {
  810. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  811. char b[BDEVNAME_SIZE];
  812. if (likely(q))
  813. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  814. else
  815. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  816. dm_device_name(t->md),
  817. bdevname(dd->dm_dev.bdev, b));
  818. }
  819. return r;
  820. }
  821. void dm_table_unplug_all(struct dm_table *t)
  822. {
  823. struct dm_dev_internal *dd;
  824. struct list_head *devices = dm_table_get_devices(t);
  825. list_for_each_entry(dd, devices, list) {
  826. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  827. char b[BDEVNAME_SIZE];
  828. if (likely(q))
  829. blk_unplug(q);
  830. else
  831. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  832. dm_device_name(t->md),
  833. bdevname(dd->dm_dev.bdev, b));
  834. }
  835. }
  836. struct mapped_device *dm_table_get_md(struct dm_table *t)
  837. {
  838. dm_get(t->md);
  839. return t->md;
  840. }
  841. EXPORT_SYMBOL(dm_vcalloc);
  842. EXPORT_SYMBOL(dm_get_device);
  843. EXPORT_SYMBOL(dm_put_device);
  844. EXPORT_SYMBOL(dm_table_event);
  845. EXPORT_SYMBOL(dm_table_get_size);
  846. EXPORT_SYMBOL(dm_table_get_mode);
  847. EXPORT_SYMBOL(dm_table_get_md);
  848. EXPORT_SYMBOL(dm_table_put);
  849. EXPORT_SYMBOL(dm_table_get);
  850. EXPORT_SYMBOL(dm_table_unplug_all);