dm-table.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. int 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, int 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 *dd = list_entry(tmp, struct dm_dev, list);
  201. kfree(dd);
  202. }
  203. }
  204. static void table_destroy(struct dm_table *t)
  205. {
  206. unsigned int i;
  207. /* free the indexes (see dm_table_complete) */
  208. if (t->depth >= 2)
  209. vfree(t->index[t->depth - 2]);
  210. /* free the targets */
  211. for (i = 0; i < t->num_targets; i++) {
  212. struct dm_target *tgt = t->targets + i;
  213. if (tgt->type->dtr)
  214. tgt->type->dtr(tgt);
  215. dm_put_target_type(tgt->type);
  216. }
  217. vfree(t->highs);
  218. /* free the device list */
  219. if (t->devices.next != &t->devices) {
  220. DMWARN("devices still present during destroy: "
  221. "dm_table_remove_device calls missing");
  222. free_devices(&t->devices);
  223. }
  224. kfree(t);
  225. }
  226. void dm_table_get(struct dm_table *t)
  227. {
  228. atomic_inc(&t->holders);
  229. }
  230. void dm_table_put(struct dm_table *t)
  231. {
  232. if (!t)
  233. return;
  234. if (atomic_dec_and_test(&t->holders))
  235. table_destroy(t);
  236. }
  237. /*
  238. * Checks to see if we need to extend highs or targets.
  239. */
  240. static inline int check_space(struct dm_table *t)
  241. {
  242. if (t->num_targets >= t->num_allocated)
  243. return alloc_targets(t, t->num_allocated * 2);
  244. return 0;
  245. }
  246. /*
  247. * Convert a device path to a dev_t.
  248. */
  249. static int lookup_device(const char *path, dev_t *dev)
  250. {
  251. struct block_device *bdev = lookup_bdev(path);
  252. if (IS_ERR(bdev))
  253. return PTR_ERR(bdev);
  254. *dev = bdev->bd_dev;
  255. bdput(bdev);
  256. return 0;
  257. }
  258. /*
  259. * See if we've already got a device in the list.
  260. */
  261. static struct dm_dev *find_device(struct list_head *l, dev_t dev)
  262. {
  263. struct dm_dev *dd;
  264. list_for_each_entry (dd, l, list)
  265. if (dd->bdev->bd_dev == dev)
  266. return dd;
  267. return NULL;
  268. }
  269. /*
  270. * Open a device so we can use it as a map destination.
  271. */
  272. static int open_dev(struct dm_dev *d, dev_t dev, struct mapped_device *md)
  273. {
  274. static char *_claim_ptr = "I belong to device-mapper";
  275. struct block_device *bdev;
  276. int r;
  277. BUG_ON(d->bdev);
  278. bdev = open_by_devnum(dev, d->mode);
  279. if (IS_ERR(bdev))
  280. return PTR_ERR(bdev);
  281. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  282. if (r)
  283. blkdev_put(bdev);
  284. else
  285. d->bdev = bdev;
  286. return r;
  287. }
  288. /*
  289. * Close a device that we've been using.
  290. */
  291. static void close_dev(struct dm_dev *d, struct mapped_device *md)
  292. {
  293. if (!d->bdev)
  294. return;
  295. bd_release_from_disk(d->bdev, dm_disk(md));
  296. blkdev_put(d->bdev);
  297. d->bdev = NULL;
  298. }
  299. /*
  300. * If possible, this checks an area of a destination device is valid.
  301. */
  302. static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len)
  303. {
  304. sector_t dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT;
  305. if (!dev_size)
  306. return 1;
  307. return ((start < dev_size) && (len <= (dev_size - start)));
  308. }
  309. /*
  310. * This upgrades the mode on an already open dm_dev. Being
  311. * careful to leave things as they were if we fail to reopen the
  312. * device.
  313. */
  314. static int upgrade_mode(struct dm_dev *dd, int new_mode, struct mapped_device *md)
  315. {
  316. int r;
  317. struct dm_dev dd_copy;
  318. dev_t dev = dd->bdev->bd_dev;
  319. dd_copy = *dd;
  320. dd->mode |= new_mode;
  321. dd->bdev = NULL;
  322. r = open_dev(dd, dev, md);
  323. if (!r)
  324. close_dev(&dd_copy, md);
  325. else
  326. *dd = dd_copy;
  327. return r;
  328. }
  329. /*
  330. * Add a device to the list, or just increment the usage count if
  331. * it's already present.
  332. */
  333. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  334. const char *path, sector_t start, sector_t len,
  335. int mode, struct dm_dev **result)
  336. {
  337. int r;
  338. dev_t uninitialized_var(dev);
  339. struct dm_dev *dd;
  340. unsigned int major, minor;
  341. BUG_ON(!t);
  342. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  343. /* Extract the major/minor numbers */
  344. dev = MKDEV(major, minor);
  345. if (MAJOR(dev) != major || MINOR(dev) != minor)
  346. return -EOVERFLOW;
  347. } else {
  348. /* convert the path to a device */
  349. if ((r = lookup_device(path, &dev)))
  350. return r;
  351. }
  352. dd = find_device(&t->devices, dev);
  353. if (!dd) {
  354. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  355. if (!dd)
  356. return -ENOMEM;
  357. dd->mode = mode;
  358. dd->bdev = NULL;
  359. if ((r = open_dev(dd, dev, t->md))) {
  360. kfree(dd);
  361. return r;
  362. }
  363. format_dev_t(dd->name, dev);
  364. atomic_set(&dd->count, 0);
  365. list_add(&dd->list, &t->devices);
  366. } else if (dd->mode != (mode | dd->mode)) {
  367. r = upgrade_mode(dd, mode, t->md);
  368. if (r)
  369. return r;
  370. }
  371. atomic_inc(&dd->count);
  372. if (!check_device_area(dd, start, len)) {
  373. DMWARN("device %s too small for target", path);
  374. dm_put_device(ti, dd);
  375. return -EINVAL;
  376. }
  377. *result = dd;
  378. return 0;
  379. }
  380. void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
  381. {
  382. struct request_queue *q = bdev_get_queue(bdev);
  383. struct io_restrictions *rs = &ti->limits;
  384. /*
  385. * Combine the device limits low.
  386. *
  387. * FIXME: if we move an io_restriction struct
  388. * into q this would just be a call to
  389. * combine_restrictions_low()
  390. */
  391. rs->max_sectors =
  392. min_not_zero(rs->max_sectors, q->max_sectors);
  393. /*
  394. * Check if merge fn is supported.
  395. * If not we'll force DM to use PAGE_SIZE or
  396. * smaller I/O, just to be safe.
  397. */
  398. if (q->merge_bvec_fn && !ti->type->merge)
  399. rs->max_sectors =
  400. min_not_zero(rs->max_sectors,
  401. (unsigned int) (PAGE_SIZE >> 9));
  402. rs->max_phys_segments =
  403. min_not_zero(rs->max_phys_segments,
  404. q->max_phys_segments);
  405. rs->max_hw_segments =
  406. min_not_zero(rs->max_hw_segments, q->max_hw_segments);
  407. rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size);
  408. rs->max_segment_size =
  409. min_not_zero(rs->max_segment_size, q->max_segment_size);
  410. rs->max_hw_sectors =
  411. min_not_zero(rs->max_hw_sectors, q->max_hw_sectors);
  412. rs->seg_boundary_mask =
  413. min_not_zero(rs->seg_boundary_mask,
  414. q->seg_boundary_mask);
  415. rs->bounce_pfn = min_not_zero(rs->bounce_pfn, q->bounce_pfn);
  416. rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  417. }
  418. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  419. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  420. sector_t len, int mode, struct dm_dev **result)
  421. {
  422. int r = __table_get_device(ti->table, ti, path,
  423. start, len, mode, result);
  424. if (!r)
  425. dm_set_device_limits(ti, (*result)->bdev);
  426. return r;
  427. }
  428. /*
  429. * Decrement a devices use count and remove it if necessary.
  430. */
  431. void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
  432. {
  433. if (atomic_dec_and_test(&dd->count)) {
  434. close_dev(dd, ti->table->md);
  435. list_del(&dd->list);
  436. kfree(dd);
  437. }
  438. }
  439. /*
  440. * Checks to see if the target joins onto the end of the table.
  441. */
  442. static int adjoin(struct dm_table *table, struct dm_target *ti)
  443. {
  444. struct dm_target *prev;
  445. if (!table->num_targets)
  446. return !ti->begin;
  447. prev = &table->targets[table->num_targets - 1];
  448. return (ti->begin == (prev->begin + prev->len));
  449. }
  450. /*
  451. * Used to dynamically allocate the arg array.
  452. */
  453. static char **realloc_argv(unsigned *array_size, char **old_argv)
  454. {
  455. char **argv;
  456. unsigned new_size;
  457. new_size = *array_size ? *array_size * 2 : 64;
  458. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  459. if (argv) {
  460. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  461. *array_size = new_size;
  462. }
  463. kfree(old_argv);
  464. return argv;
  465. }
  466. /*
  467. * Destructively splits up the argument list to pass to ctr.
  468. */
  469. int dm_split_args(int *argc, char ***argvp, char *input)
  470. {
  471. char *start, *end = input, *out, **argv = NULL;
  472. unsigned array_size = 0;
  473. *argc = 0;
  474. if (!input) {
  475. *argvp = NULL;
  476. return 0;
  477. }
  478. argv = realloc_argv(&array_size, argv);
  479. if (!argv)
  480. return -ENOMEM;
  481. while (1) {
  482. start = end;
  483. /* Skip whitespace */
  484. while (*start && isspace(*start))
  485. start++;
  486. if (!*start)
  487. break; /* success, we hit the end */
  488. /* 'out' is used to remove any back-quotes */
  489. end = out = start;
  490. while (*end) {
  491. /* Everything apart from '\0' can be quoted */
  492. if (*end == '\\' && *(end + 1)) {
  493. *out++ = *(end + 1);
  494. end += 2;
  495. continue;
  496. }
  497. if (isspace(*end))
  498. break; /* end of token */
  499. *out++ = *end++;
  500. }
  501. /* have we already filled the array ? */
  502. if ((*argc + 1) > array_size) {
  503. argv = realloc_argv(&array_size, argv);
  504. if (!argv)
  505. return -ENOMEM;
  506. }
  507. /* we know this is whitespace */
  508. if (*end)
  509. end++;
  510. /* terminate the string and put it in the array */
  511. *out = '\0';
  512. argv[*argc] = start;
  513. (*argc)++;
  514. }
  515. *argvp = argv;
  516. return 0;
  517. }
  518. static void check_for_valid_limits(struct io_restrictions *rs)
  519. {
  520. if (!rs->max_sectors)
  521. rs->max_sectors = SAFE_MAX_SECTORS;
  522. if (!rs->max_hw_sectors)
  523. rs->max_hw_sectors = SAFE_MAX_SECTORS;
  524. if (!rs->max_phys_segments)
  525. rs->max_phys_segments = MAX_PHYS_SEGMENTS;
  526. if (!rs->max_hw_segments)
  527. rs->max_hw_segments = MAX_HW_SEGMENTS;
  528. if (!rs->hardsect_size)
  529. rs->hardsect_size = 1 << SECTOR_SHIFT;
  530. if (!rs->max_segment_size)
  531. rs->max_segment_size = MAX_SEGMENT_SIZE;
  532. if (!rs->seg_boundary_mask)
  533. rs->seg_boundary_mask = -1;
  534. if (!rs->bounce_pfn)
  535. rs->bounce_pfn = -1;
  536. }
  537. int dm_table_add_target(struct dm_table *t, const char *type,
  538. sector_t start, sector_t len, char *params)
  539. {
  540. int r = -EINVAL, argc;
  541. char **argv;
  542. struct dm_target *tgt;
  543. if ((r = check_space(t)))
  544. return r;
  545. tgt = t->targets + t->num_targets;
  546. memset(tgt, 0, sizeof(*tgt));
  547. if (!len) {
  548. DMERR("%s: zero-length target", dm_device_name(t->md));
  549. return -EINVAL;
  550. }
  551. tgt->type = dm_get_target_type(type);
  552. if (!tgt->type) {
  553. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  554. type);
  555. return -EINVAL;
  556. }
  557. tgt->table = t;
  558. tgt->begin = start;
  559. tgt->len = len;
  560. tgt->error = "Unknown error";
  561. /*
  562. * Does this target adjoin the previous one ?
  563. */
  564. if (!adjoin(t, tgt)) {
  565. tgt->error = "Gap in table";
  566. r = -EINVAL;
  567. goto bad;
  568. }
  569. r = dm_split_args(&argc, &argv, params);
  570. if (r) {
  571. tgt->error = "couldn't split parameters (insufficient memory)";
  572. goto bad;
  573. }
  574. r = tgt->type->ctr(tgt, argc, argv);
  575. kfree(argv);
  576. if (r)
  577. goto bad;
  578. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  579. /* FIXME: the plan is to combine high here and then have
  580. * the merge fn apply the target level restrictions. */
  581. combine_restrictions_low(&t->limits, &tgt->limits);
  582. return 0;
  583. bad:
  584. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  585. dm_put_target_type(tgt->type);
  586. return r;
  587. }
  588. static int setup_indexes(struct dm_table *t)
  589. {
  590. int i;
  591. unsigned int total = 0;
  592. sector_t *indexes;
  593. /* allocate the space for *all* the indexes */
  594. for (i = t->depth - 2; i >= 0; i--) {
  595. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  596. total += t->counts[i];
  597. }
  598. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  599. if (!indexes)
  600. return -ENOMEM;
  601. /* set up internal nodes, bottom-up */
  602. for (i = t->depth - 2; i >= 0; i--) {
  603. t->index[i] = indexes;
  604. indexes += (KEYS_PER_NODE * t->counts[i]);
  605. setup_btree_index(i, t);
  606. }
  607. return 0;
  608. }
  609. /*
  610. * Builds the btree to index the map.
  611. */
  612. int dm_table_complete(struct dm_table *t)
  613. {
  614. int r = 0;
  615. unsigned int leaf_nodes;
  616. check_for_valid_limits(&t->limits);
  617. /* how many indexes will the btree have ? */
  618. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  619. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  620. /* leaf layer has already been set up */
  621. t->counts[t->depth - 1] = leaf_nodes;
  622. t->index[t->depth - 1] = t->highs;
  623. if (t->depth >= 2)
  624. r = setup_indexes(t);
  625. return r;
  626. }
  627. static DEFINE_MUTEX(_event_lock);
  628. void dm_table_event_callback(struct dm_table *t,
  629. void (*fn)(void *), void *context)
  630. {
  631. mutex_lock(&_event_lock);
  632. t->event_fn = fn;
  633. t->event_context = context;
  634. mutex_unlock(&_event_lock);
  635. }
  636. void dm_table_event(struct dm_table *t)
  637. {
  638. /*
  639. * You can no longer call dm_table_event() from interrupt
  640. * context, use a bottom half instead.
  641. */
  642. BUG_ON(in_interrupt());
  643. mutex_lock(&_event_lock);
  644. if (t->event_fn)
  645. t->event_fn(t->event_context);
  646. mutex_unlock(&_event_lock);
  647. }
  648. sector_t dm_table_get_size(struct dm_table *t)
  649. {
  650. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  651. }
  652. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  653. {
  654. if (index >= t->num_targets)
  655. return NULL;
  656. return t->targets + index;
  657. }
  658. /*
  659. * Search the btree for the correct target.
  660. *
  661. * Caller should check returned pointer with dm_target_is_valid()
  662. * to trap I/O beyond end of device.
  663. */
  664. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  665. {
  666. unsigned int l, n = 0, k = 0;
  667. sector_t *node;
  668. for (l = 0; l < t->depth; l++) {
  669. n = get_child(n, k);
  670. node = get_node(t, l, n);
  671. for (k = 0; k < KEYS_PER_NODE; k++)
  672. if (node[k] >= sector)
  673. break;
  674. }
  675. return &t->targets[(KEYS_PER_NODE * n) + k];
  676. }
  677. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
  678. {
  679. /*
  680. * Make sure we obey the optimistic sub devices
  681. * restrictions.
  682. */
  683. blk_queue_max_sectors(q, t->limits.max_sectors);
  684. q->max_phys_segments = t->limits.max_phys_segments;
  685. q->max_hw_segments = t->limits.max_hw_segments;
  686. q->hardsect_size = t->limits.hardsect_size;
  687. q->max_segment_size = t->limits.max_segment_size;
  688. q->max_hw_sectors = t->limits.max_hw_sectors;
  689. q->seg_boundary_mask = t->limits.seg_boundary_mask;
  690. q->bounce_pfn = t->limits.bounce_pfn;
  691. if (t->limits.no_cluster)
  692. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  693. else
  694. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  695. }
  696. unsigned int dm_table_get_num_targets(struct dm_table *t)
  697. {
  698. return t->num_targets;
  699. }
  700. struct list_head *dm_table_get_devices(struct dm_table *t)
  701. {
  702. return &t->devices;
  703. }
  704. int dm_table_get_mode(struct dm_table *t)
  705. {
  706. return t->mode;
  707. }
  708. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  709. {
  710. int i = t->num_targets;
  711. struct dm_target *ti = t->targets;
  712. while (i--) {
  713. if (postsuspend) {
  714. if (ti->type->postsuspend)
  715. ti->type->postsuspend(ti);
  716. } else if (ti->type->presuspend)
  717. ti->type->presuspend(ti);
  718. ti++;
  719. }
  720. }
  721. void dm_table_presuspend_targets(struct dm_table *t)
  722. {
  723. if (!t)
  724. return;
  725. suspend_targets(t, 0);
  726. }
  727. void dm_table_postsuspend_targets(struct dm_table *t)
  728. {
  729. if (!t)
  730. return;
  731. suspend_targets(t, 1);
  732. }
  733. int dm_table_resume_targets(struct dm_table *t)
  734. {
  735. int i, r = 0;
  736. for (i = 0; i < t->num_targets; i++) {
  737. struct dm_target *ti = t->targets + i;
  738. if (!ti->type->preresume)
  739. continue;
  740. r = ti->type->preresume(ti);
  741. if (r)
  742. return r;
  743. }
  744. for (i = 0; i < t->num_targets; i++) {
  745. struct dm_target *ti = t->targets + i;
  746. if (ti->type->resume)
  747. ti->type->resume(ti);
  748. }
  749. return 0;
  750. }
  751. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  752. {
  753. struct dm_dev *dd;
  754. struct list_head *devices = dm_table_get_devices(t);
  755. int r = 0;
  756. list_for_each_entry(dd, devices, list) {
  757. struct request_queue *q = bdev_get_queue(dd->bdev);
  758. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  759. }
  760. return r;
  761. }
  762. void dm_table_unplug_all(struct dm_table *t)
  763. {
  764. struct dm_dev *dd;
  765. struct list_head *devices = dm_table_get_devices(t);
  766. list_for_each_entry(dd, devices, list) {
  767. struct request_queue *q = bdev_get_queue(dd->bdev);
  768. blk_unplug(q);
  769. }
  770. }
  771. struct mapped_device *dm_table_get_md(struct dm_table *t)
  772. {
  773. dm_get(t->md);
  774. return t->md;
  775. }
  776. EXPORT_SYMBOL(dm_vcalloc);
  777. EXPORT_SYMBOL(dm_get_device);
  778. EXPORT_SYMBOL(dm_put_device);
  779. EXPORT_SYMBOL(dm_table_event);
  780. EXPORT_SYMBOL(dm_table_get_size);
  781. EXPORT_SYMBOL(dm_table_get_mode);
  782. EXPORT_SYMBOL(dm_table_get_md);
  783. EXPORT_SYMBOL(dm_table_put);
  784. EXPORT_SYMBOL(dm_table_get);
  785. EXPORT_SYMBOL(dm_table_unplug_all);