dm-table.c 19 KB

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