dm-table.c 20 KB

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