dm-table.c 20 KB

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