dm-table.c 22 KB

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