dm-table.c 21 KB

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