dm-table.c 21 KB

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