dm-table.c 19 KB

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