dm-table.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. /*
  34. * Indicates the rw permissions for the new logical
  35. * device. This should be a combination of FMODE_READ
  36. * and FMODE_WRITE.
  37. */
  38. int mode;
  39. /* a list of devices used by this table */
  40. struct list_head devices;
  41. /*
  42. * These are optimistic limits taken from all the
  43. * targets, some targets will need smaller limits.
  44. */
  45. struct io_restrictions limits;
  46. /* events get handed up using this callback */
  47. void (*event_fn)(void *);
  48. void *event_context;
  49. };
  50. /*
  51. * Similar to ceiling(log_size(n))
  52. */
  53. static unsigned int int_log(unsigned int n, unsigned int base)
  54. {
  55. int result = 0;
  56. while (n > 1) {
  57. n = dm_div_up(n, base);
  58. result++;
  59. }
  60. return result;
  61. }
  62. /*
  63. * Returns the minimum that is _not_ zero, unless both are zero.
  64. */
  65. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  66. /*
  67. * Combine two io_restrictions, always taking the lower value.
  68. */
  69. static void combine_restrictions_low(struct io_restrictions *lhs,
  70. struct io_restrictions *rhs)
  71. {
  72. lhs->max_sectors =
  73. min_not_zero(lhs->max_sectors, rhs->max_sectors);
  74. lhs->max_phys_segments =
  75. min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments);
  76. lhs->max_hw_segments =
  77. min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments);
  78. lhs->hardsect_size = max(lhs->hardsect_size, rhs->hardsect_size);
  79. lhs->max_segment_size =
  80. min_not_zero(lhs->max_segment_size, rhs->max_segment_size);
  81. lhs->max_hw_sectors =
  82. min_not_zero(lhs->max_hw_sectors, rhs->max_hw_sectors);
  83. lhs->seg_boundary_mask =
  84. min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask);
  85. lhs->bounce_pfn = min_not_zero(lhs->bounce_pfn, rhs->bounce_pfn);
  86. lhs->no_cluster |= rhs->no_cluster;
  87. }
  88. /*
  89. * Calculate the index of the child node of the n'th node k'th key.
  90. */
  91. static inline unsigned int get_child(unsigned int n, unsigned int k)
  92. {
  93. return (n * CHILDREN_PER_NODE) + k;
  94. }
  95. /*
  96. * Return the n'th node of level l from table t.
  97. */
  98. static inline sector_t *get_node(struct dm_table *t,
  99. unsigned int l, unsigned int n)
  100. {
  101. return t->index[l] + (n * KEYS_PER_NODE);
  102. }
  103. /*
  104. * Return the highest key that you could lookup from the n'th
  105. * node on level l of the btree.
  106. */
  107. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  108. {
  109. for (; l < t->depth - 1; l++)
  110. n = get_child(n, CHILDREN_PER_NODE - 1);
  111. if (n >= t->counts[l])
  112. return (sector_t) - 1;
  113. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  114. }
  115. /*
  116. * Fills in a level of the btree based on the highs of the level
  117. * below it.
  118. */
  119. static int setup_btree_index(unsigned int l, struct dm_table *t)
  120. {
  121. unsigned int n, k;
  122. sector_t *node;
  123. for (n = 0U; n < t->counts[l]; n++) {
  124. node = get_node(t, l, n);
  125. for (k = 0U; k < KEYS_PER_NODE; k++)
  126. node[k] = high(t, l + 1, get_child(n, k));
  127. }
  128. return 0;
  129. }
  130. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  131. {
  132. unsigned long size;
  133. void *addr;
  134. /*
  135. * Check that we're not going to overflow.
  136. */
  137. if (nmemb > (ULONG_MAX / elem_size))
  138. return NULL;
  139. size = nmemb * elem_size;
  140. addr = vmalloc(size);
  141. if (addr)
  142. memset(addr, 0, size);
  143. return addr;
  144. }
  145. /*
  146. * highs, and targets are managed as dynamic arrays during a
  147. * table load.
  148. */
  149. static int alloc_targets(struct dm_table *t, unsigned int num)
  150. {
  151. sector_t *n_highs;
  152. struct dm_target *n_targets;
  153. int n = t->num_targets;
  154. /*
  155. * Allocate both the target array and offset array at once.
  156. * Append an empty entry to catch sectors beyond the end of
  157. * the device.
  158. */
  159. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  160. sizeof(sector_t));
  161. if (!n_highs)
  162. return -ENOMEM;
  163. n_targets = (struct dm_target *) (n_highs + num);
  164. if (n) {
  165. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  166. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  167. }
  168. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  169. vfree(t->highs);
  170. t->num_allocated = num;
  171. t->highs = n_highs;
  172. t->targets = n_targets;
  173. return 0;
  174. }
  175. int dm_table_create(struct dm_table **result, int mode,
  176. unsigned num_targets, struct mapped_device *md)
  177. {
  178. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  179. if (!t)
  180. return -ENOMEM;
  181. INIT_LIST_HEAD(&t->devices);
  182. atomic_set(&t->holders, 1);
  183. if (!num_targets)
  184. num_targets = KEYS_PER_NODE;
  185. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  186. if (alloc_targets(t, num_targets)) {
  187. kfree(t);
  188. t = NULL;
  189. return -ENOMEM;
  190. }
  191. t->mode = mode;
  192. t->md = md;
  193. *result = t;
  194. return 0;
  195. }
  196. static void free_devices(struct list_head *devices)
  197. {
  198. struct list_head *tmp, *next;
  199. list_for_each_safe(tmp, next, devices) {
  200. struct dm_dev_internal *dd =
  201. list_entry(tmp, struct dm_dev_internal, list);
  202. kfree(dd);
  203. }
  204. }
  205. static void table_destroy(struct dm_table *t)
  206. {
  207. unsigned int i;
  208. /* free the indexes (see dm_table_complete) */
  209. if (t->depth >= 2)
  210. vfree(t->index[t->depth - 2]);
  211. /* free the targets */
  212. for (i = 0; i < t->num_targets; i++) {
  213. struct dm_target *tgt = t->targets + i;
  214. if (tgt->type->dtr)
  215. tgt->type->dtr(tgt);
  216. dm_put_target_type(tgt->type);
  217. }
  218. vfree(t->highs);
  219. /* free the device list */
  220. if (t->devices.next != &t->devices) {
  221. DMWARN("devices still present during destroy: "
  222. "dm_table_remove_device calls missing");
  223. free_devices(&t->devices);
  224. }
  225. kfree(t);
  226. }
  227. void dm_table_get(struct dm_table *t)
  228. {
  229. atomic_inc(&t->holders);
  230. }
  231. void dm_table_put(struct dm_table *t)
  232. {
  233. if (!t)
  234. return;
  235. if (atomic_dec_and_test(&t->holders))
  236. table_destroy(t);
  237. }
  238. /*
  239. * Checks to see if we need to extend highs or targets.
  240. */
  241. static inline int check_space(struct dm_table *t)
  242. {
  243. if (t->num_targets >= t->num_allocated)
  244. return alloc_targets(t, t->num_allocated * 2);
  245. return 0;
  246. }
  247. /*
  248. * Convert a device path to a dev_t.
  249. */
  250. static int lookup_device(const char *path, dev_t *dev)
  251. {
  252. struct block_device *bdev = lookup_bdev(path);
  253. if (IS_ERR(bdev))
  254. return PTR_ERR(bdev);
  255. *dev = bdev->bd_dev;
  256. bdput(bdev);
  257. return 0;
  258. }
  259. /*
  260. * See if we've already got a device in the list.
  261. */
  262. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  263. {
  264. struct dm_dev_internal *dd;
  265. list_for_each_entry (dd, l, list)
  266. if (dd->dm_dev.bdev->bd_dev == dev)
  267. return dd;
  268. return NULL;
  269. }
  270. /*
  271. * Open a device so we can use it as a map destination.
  272. */
  273. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  274. struct mapped_device *md)
  275. {
  276. static char *_claim_ptr = "I belong to device-mapper";
  277. struct block_device *bdev;
  278. int r;
  279. BUG_ON(d->dm_dev.bdev);
  280. bdev = open_by_devnum(dev, d->dm_dev.mode);
  281. if (IS_ERR(bdev))
  282. return PTR_ERR(bdev);
  283. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  284. if (r)
  285. blkdev_put(bdev);
  286. else
  287. d->dm_dev.bdev = bdev;
  288. return r;
  289. }
  290. /*
  291. * Close a device that we've been using.
  292. */
  293. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  294. {
  295. if (!d->dm_dev.bdev)
  296. return;
  297. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  298. blkdev_put(d->dm_dev.bdev);
  299. d->dm_dev.bdev = NULL;
  300. }
  301. /*
  302. * If possible, this checks an area of a destination device is valid.
  303. */
  304. static int check_device_area(struct dm_dev_internal *dd, sector_t start,
  305. sector_t len)
  306. {
  307. sector_t dev_size = dd->dm_dev.bdev->bd_inode->i_size >> SECTOR_SHIFT;
  308. if (!dev_size)
  309. return 1;
  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_internal *dd, int new_mode,
  318. struct mapped_device *md)
  319. {
  320. int r;
  321. struct dm_dev_internal dd_copy;
  322. dev_t dev = dd->dm_dev.bdev->bd_dev;
  323. dd_copy = *dd;
  324. dd->dm_dev.mode |= new_mode;
  325. dd->dm_dev.bdev = NULL;
  326. r = open_dev(dd, dev, md);
  327. if (!r)
  328. close_dev(&dd_copy, md);
  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 uninitialized_var(dev);
  343. struct dm_dev_internal *dd;
  344. unsigned int major, minor;
  345. BUG_ON(!t);
  346. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  347. /* Extract the major/minor numbers */
  348. dev = MKDEV(major, minor);
  349. if (MAJOR(dev) != major || MINOR(dev) != minor)
  350. return -EOVERFLOW;
  351. } else {
  352. /* convert the path to a device */
  353. if ((r = lookup_device(path, &dev)))
  354. return r;
  355. }
  356. dd = find_device(&t->devices, dev);
  357. if (!dd) {
  358. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  359. if (!dd)
  360. return -ENOMEM;
  361. dd->dm_dev.mode = mode;
  362. dd->dm_dev.bdev = NULL;
  363. if ((r = open_dev(dd, dev, t->md))) {
  364. kfree(dd);
  365. return r;
  366. }
  367. format_dev_t(dd->dm_dev.name, dev);
  368. atomic_set(&dd->count, 0);
  369. list_add(&dd->list, &t->devices);
  370. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  371. r = upgrade_mode(dd, mode, t->md);
  372. if (r)
  373. return r;
  374. }
  375. atomic_inc(&dd->count);
  376. if (!check_device_area(dd, start, len)) {
  377. DMWARN("device %s too small for target", path);
  378. dm_put_device(ti, &dd->dm_dev);
  379. return -EINVAL;
  380. }
  381. *result = &dd->dm_dev;
  382. return 0;
  383. }
  384. void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
  385. {
  386. struct request_queue *q = bdev_get_queue(bdev);
  387. struct io_restrictions *rs = &ti->limits;
  388. char b[BDEVNAME_SIZE];
  389. if (unlikely(!q)) {
  390. DMWARN("%s: Cannot set limits for nonexistent device %s",
  391. dm_device_name(ti->table->md), bdevname(bdev, b));
  392. return;
  393. }
  394. /*
  395. * Combine the device limits low.
  396. *
  397. * FIXME: if we move an io_restriction struct
  398. * into q this would just be a call to
  399. * combine_restrictions_low()
  400. */
  401. rs->max_sectors =
  402. min_not_zero(rs->max_sectors, q->max_sectors);
  403. /*
  404. * Check if merge fn is supported.
  405. * If not we'll force DM to use PAGE_SIZE or
  406. * smaller I/O, just to be safe.
  407. */
  408. if (q->merge_bvec_fn && !ti->type->merge)
  409. rs->max_sectors =
  410. min_not_zero(rs->max_sectors,
  411. (unsigned int) (PAGE_SIZE >> 9));
  412. rs->max_phys_segments =
  413. min_not_zero(rs->max_phys_segments,
  414. q->max_phys_segments);
  415. rs->max_hw_segments =
  416. min_not_zero(rs->max_hw_segments, q->max_hw_segments);
  417. rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size);
  418. rs->max_segment_size =
  419. min_not_zero(rs->max_segment_size, q->max_segment_size);
  420. rs->max_hw_sectors =
  421. min_not_zero(rs->max_hw_sectors, q->max_hw_sectors);
  422. rs->seg_boundary_mask =
  423. min_not_zero(rs->seg_boundary_mask,
  424. q->seg_boundary_mask);
  425. rs->bounce_pfn = min_not_zero(rs->bounce_pfn, q->bounce_pfn);
  426. rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  427. }
  428. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  429. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  430. sector_t len, int mode, struct dm_dev **result)
  431. {
  432. int r = __table_get_device(ti->table, ti, path,
  433. start, len, mode, result);
  434. if (!r)
  435. dm_set_device_limits(ti, (*result)->bdev);
  436. return r;
  437. }
  438. /*
  439. * Decrement a devices use count and remove it if necessary.
  440. */
  441. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  442. {
  443. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  444. dm_dev);
  445. if (atomic_dec_and_test(&dd->count)) {
  446. close_dev(dd, ti->table->md);
  447. list_del(&dd->list);
  448. kfree(dd);
  449. }
  450. }
  451. /*
  452. * Checks to see if the target joins onto the end of the table.
  453. */
  454. static int adjoin(struct dm_table *table, struct dm_target *ti)
  455. {
  456. struct dm_target *prev;
  457. if (!table->num_targets)
  458. return !ti->begin;
  459. prev = &table->targets[table->num_targets - 1];
  460. return (ti->begin == (prev->begin + prev->len));
  461. }
  462. /*
  463. * Used to dynamically allocate the arg array.
  464. */
  465. static char **realloc_argv(unsigned *array_size, char **old_argv)
  466. {
  467. char **argv;
  468. unsigned new_size;
  469. new_size = *array_size ? *array_size * 2 : 64;
  470. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  471. if (argv) {
  472. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  473. *array_size = new_size;
  474. }
  475. kfree(old_argv);
  476. return argv;
  477. }
  478. /*
  479. * Destructively splits up the argument list to pass to ctr.
  480. */
  481. int dm_split_args(int *argc, char ***argvp, char *input)
  482. {
  483. char *start, *end = input, *out, **argv = NULL;
  484. unsigned array_size = 0;
  485. *argc = 0;
  486. if (!input) {
  487. *argvp = NULL;
  488. return 0;
  489. }
  490. argv = realloc_argv(&array_size, argv);
  491. if (!argv)
  492. return -ENOMEM;
  493. while (1) {
  494. start = end;
  495. /* Skip whitespace */
  496. while (*start && isspace(*start))
  497. start++;
  498. if (!*start)
  499. break; /* success, we hit the end */
  500. /* 'out' is used to remove any back-quotes */
  501. end = out = start;
  502. while (*end) {
  503. /* Everything apart from '\0' can be quoted */
  504. if (*end == '\\' && *(end + 1)) {
  505. *out++ = *(end + 1);
  506. end += 2;
  507. continue;
  508. }
  509. if (isspace(*end))
  510. break; /* end of token */
  511. *out++ = *end++;
  512. }
  513. /* have we already filled the array ? */
  514. if ((*argc + 1) > array_size) {
  515. argv = realloc_argv(&array_size, argv);
  516. if (!argv)
  517. return -ENOMEM;
  518. }
  519. /* we know this is whitespace */
  520. if (*end)
  521. end++;
  522. /* terminate the string and put it in the array */
  523. *out = '\0';
  524. argv[*argc] = start;
  525. (*argc)++;
  526. }
  527. *argvp = argv;
  528. return 0;
  529. }
  530. static void check_for_valid_limits(struct io_restrictions *rs)
  531. {
  532. if (!rs->max_sectors)
  533. rs->max_sectors = SAFE_MAX_SECTORS;
  534. if (!rs->max_hw_sectors)
  535. rs->max_hw_sectors = SAFE_MAX_SECTORS;
  536. if (!rs->max_phys_segments)
  537. rs->max_phys_segments = MAX_PHYS_SEGMENTS;
  538. if (!rs->max_hw_segments)
  539. rs->max_hw_segments = MAX_HW_SEGMENTS;
  540. if (!rs->hardsect_size)
  541. rs->hardsect_size = 1 << SECTOR_SHIFT;
  542. if (!rs->max_segment_size)
  543. rs->max_segment_size = MAX_SEGMENT_SIZE;
  544. if (!rs->seg_boundary_mask)
  545. rs->seg_boundary_mask = -1;
  546. if (!rs->bounce_pfn)
  547. rs->bounce_pfn = -1;
  548. }
  549. int dm_table_add_target(struct dm_table *t, const char *type,
  550. sector_t start, sector_t len, char *params)
  551. {
  552. int r = -EINVAL, argc;
  553. char **argv;
  554. struct dm_target *tgt;
  555. if ((r = check_space(t)))
  556. return r;
  557. tgt = t->targets + t->num_targets;
  558. memset(tgt, 0, sizeof(*tgt));
  559. if (!len) {
  560. DMERR("%s: zero-length target", dm_device_name(t->md));
  561. return -EINVAL;
  562. }
  563. tgt->type = dm_get_target_type(type);
  564. if (!tgt->type) {
  565. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  566. type);
  567. return -EINVAL;
  568. }
  569. tgt->table = t;
  570. tgt->begin = start;
  571. tgt->len = len;
  572. tgt->error = "Unknown error";
  573. /*
  574. * Does this target adjoin the previous one ?
  575. */
  576. if (!adjoin(t, tgt)) {
  577. tgt->error = "Gap in table";
  578. r = -EINVAL;
  579. goto bad;
  580. }
  581. r = dm_split_args(&argc, &argv, params);
  582. if (r) {
  583. tgt->error = "couldn't split parameters (insufficient memory)";
  584. goto bad;
  585. }
  586. r = tgt->type->ctr(tgt, argc, argv);
  587. kfree(argv);
  588. if (r)
  589. goto bad;
  590. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  591. /* FIXME: the plan is to combine high here and then have
  592. * the merge fn apply the target level restrictions. */
  593. combine_restrictions_low(&t->limits, &tgt->limits);
  594. return 0;
  595. bad:
  596. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  597. dm_put_target_type(tgt->type);
  598. return r;
  599. }
  600. static int setup_indexes(struct dm_table *t)
  601. {
  602. int i;
  603. unsigned int total = 0;
  604. sector_t *indexes;
  605. /* allocate the space for *all* the indexes */
  606. for (i = t->depth - 2; i >= 0; i--) {
  607. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  608. total += t->counts[i];
  609. }
  610. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  611. if (!indexes)
  612. return -ENOMEM;
  613. /* set up internal nodes, bottom-up */
  614. for (i = t->depth - 2; i >= 0; i--) {
  615. t->index[i] = indexes;
  616. indexes += (KEYS_PER_NODE * t->counts[i]);
  617. setup_btree_index(i, t);
  618. }
  619. return 0;
  620. }
  621. /*
  622. * Builds the btree to index the map.
  623. */
  624. int dm_table_complete(struct dm_table *t)
  625. {
  626. int r = 0;
  627. unsigned int leaf_nodes;
  628. check_for_valid_limits(&t->limits);
  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. int 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. EXPORT_SYMBOL(dm_vcalloc);
  801. EXPORT_SYMBOL(dm_get_device);
  802. EXPORT_SYMBOL(dm_put_device);
  803. EXPORT_SYMBOL(dm_table_event);
  804. EXPORT_SYMBOL(dm_table_get_size);
  805. EXPORT_SYMBOL(dm_table_get_mode);
  806. EXPORT_SYMBOL(dm_table_get_md);
  807. EXPORT_SYMBOL(dm_table_put);
  808. EXPORT_SYMBOL(dm_table_get);
  809. EXPORT_SYMBOL(dm_table_unplug_all);