dm-table.c 20 KB

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