dm-table.c 23 KB

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