dm-table.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. int dm_create_error_table(struct dm_table **result, struct mapped_device *md)
  197. {
  198. struct dm_table *t;
  199. sector_t dev_size = 1;
  200. int r;
  201. /*
  202. * Find current size of device.
  203. * Default to 1 sector if inactive.
  204. */
  205. t = dm_get_table(md);
  206. if (t) {
  207. dev_size = dm_table_get_size(t);
  208. dm_table_put(t);
  209. }
  210. r = dm_table_create(&t, FMODE_READ, 1, md);
  211. if (r)
  212. return r;
  213. r = dm_table_add_target(t, "error", 0, dev_size, NULL);
  214. if (r)
  215. goto out;
  216. r = dm_table_complete(t);
  217. if (r)
  218. goto out;
  219. *result = t;
  220. out:
  221. if (r)
  222. dm_table_put(t);
  223. return r;
  224. }
  225. EXPORT_SYMBOL_GPL(dm_create_error_table);
  226. static void free_devices(struct list_head *devices)
  227. {
  228. struct list_head *tmp, *next;
  229. for (tmp = devices->next; tmp != devices; tmp = next) {
  230. struct dm_dev *dd = list_entry(tmp, struct dm_dev, list);
  231. next = tmp->next;
  232. kfree(dd);
  233. }
  234. }
  235. static void table_destroy(struct dm_table *t)
  236. {
  237. unsigned int i;
  238. /* free the indexes (see dm_table_complete) */
  239. if (t->depth >= 2)
  240. vfree(t->index[t->depth - 2]);
  241. /* free the targets */
  242. for (i = 0; i < t->num_targets; i++) {
  243. struct dm_target *tgt = t->targets + i;
  244. if (tgt->type->dtr)
  245. tgt->type->dtr(tgt);
  246. dm_put_target_type(tgt->type);
  247. }
  248. vfree(t->highs);
  249. /* free the device list */
  250. if (t->devices.next != &t->devices) {
  251. DMWARN("devices still present during destroy: "
  252. "dm_table_remove_device calls missing");
  253. free_devices(&t->devices);
  254. }
  255. kfree(t);
  256. }
  257. void dm_table_get(struct dm_table *t)
  258. {
  259. atomic_inc(&t->holders);
  260. }
  261. void dm_table_put(struct dm_table *t)
  262. {
  263. if (!t)
  264. return;
  265. if (atomic_dec_and_test(&t->holders))
  266. table_destroy(t);
  267. }
  268. /*
  269. * Checks to see if we need to extend highs or targets.
  270. */
  271. static inline int check_space(struct dm_table *t)
  272. {
  273. if (t->num_targets >= t->num_allocated)
  274. return alloc_targets(t, t->num_allocated * 2);
  275. return 0;
  276. }
  277. /*
  278. * Convert a device path to a dev_t.
  279. */
  280. static int lookup_device(const char *path, dev_t *dev)
  281. {
  282. int r;
  283. struct nameidata nd;
  284. struct inode *inode;
  285. if ((r = path_lookup(path, LOOKUP_FOLLOW, &nd)))
  286. return r;
  287. inode = nd.dentry->d_inode;
  288. if (!inode) {
  289. r = -ENOENT;
  290. goto out;
  291. }
  292. if (!S_ISBLK(inode->i_mode)) {
  293. r = -ENOTBLK;
  294. goto out;
  295. }
  296. *dev = inode->i_rdev;
  297. out:
  298. path_release(&nd);
  299. return r;
  300. }
  301. /*
  302. * See if we've already got a device in the list.
  303. */
  304. static struct dm_dev *find_device(struct list_head *l, dev_t dev)
  305. {
  306. struct dm_dev *dd;
  307. list_for_each_entry (dd, l, list)
  308. if (dd->bdev->bd_dev == dev)
  309. return dd;
  310. return NULL;
  311. }
  312. /*
  313. * Open a device so we can use it as a map destination.
  314. */
  315. static int open_dev(struct dm_dev *d, dev_t dev, struct mapped_device *md)
  316. {
  317. static char *_claim_ptr = "I belong to device-mapper";
  318. struct block_device *bdev;
  319. int r;
  320. BUG_ON(d->bdev);
  321. bdev = open_by_devnum(dev, d->mode);
  322. if (IS_ERR(bdev))
  323. return PTR_ERR(bdev);
  324. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  325. if (r)
  326. blkdev_put(bdev);
  327. else
  328. d->bdev = bdev;
  329. return r;
  330. }
  331. /*
  332. * Close a device that we've been using.
  333. */
  334. static void close_dev(struct dm_dev *d, struct mapped_device *md)
  335. {
  336. if (!d->bdev)
  337. return;
  338. bd_release_from_disk(d->bdev, dm_disk(md));
  339. blkdev_put(d->bdev);
  340. d->bdev = NULL;
  341. }
  342. /*
  343. * If possible, this checks an area of a destination device is valid.
  344. */
  345. static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len)
  346. {
  347. sector_t dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT;
  348. if (!dev_size)
  349. return 1;
  350. return ((start < dev_size) && (len <= (dev_size - start)));
  351. }
  352. /*
  353. * This upgrades the mode on an already open dm_dev. Being
  354. * careful to leave things as they were if we fail to reopen the
  355. * device.
  356. */
  357. static int upgrade_mode(struct dm_dev *dd, int new_mode, struct mapped_device *md)
  358. {
  359. int r;
  360. struct dm_dev dd_copy;
  361. dev_t dev = dd->bdev->bd_dev;
  362. dd_copy = *dd;
  363. dd->mode |= new_mode;
  364. dd->bdev = NULL;
  365. r = open_dev(dd, dev, md);
  366. if (!r)
  367. close_dev(&dd_copy, md);
  368. else
  369. *dd = dd_copy;
  370. return r;
  371. }
  372. /*
  373. * Add a device to the list, or just increment the usage count if
  374. * it's already present.
  375. */
  376. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  377. const char *path, sector_t start, sector_t len,
  378. int mode, struct dm_dev **result)
  379. {
  380. int r;
  381. dev_t dev;
  382. struct dm_dev *dd;
  383. unsigned int major, minor;
  384. BUG_ON(!t);
  385. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  386. /* Extract the major/minor numbers */
  387. dev = MKDEV(major, minor);
  388. if (MAJOR(dev) != major || MINOR(dev) != minor)
  389. return -EOVERFLOW;
  390. } else {
  391. /* convert the path to a device */
  392. if ((r = lookup_device(path, &dev)))
  393. return r;
  394. }
  395. dd = find_device(&t->devices, dev);
  396. if (!dd) {
  397. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  398. if (!dd)
  399. return -ENOMEM;
  400. dd->mode = mode;
  401. dd->bdev = NULL;
  402. if ((r = open_dev(dd, dev, t->md))) {
  403. kfree(dd);
  404. return r;
  405. }
  406. format_dev_t(dd->name, dev);
  407. atomic_set(&dd->count, 0);
  408. list_add(&dd->list, &t->devices);
  409. } else if (dd->mode != (mode | dd->mode)) {
  410. r = upgrade_mode(dd, mode, t->md);
  411. if (r)
  412. return r;
  413. }
  414. atomic_inc(&dd->count);
  415. if (!check_device_area(dd, start, len)) {
  416. DMWARN("device %s too small for target", path);
  417. dm_put_device(ti, dd);
  418. return -EINVAL;
  419. }
  420. *result = dd;
  421. return 0;
  422. }
  423. void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
  424. {
  425. struct request_queue *q = bdev_get_queue(bdev);
  426. struct io_restrictions *rs = &ti->limits;
  427. /*
  428. * Combine the device limits low.
  429. *
  430. * FIXME: if we move an io_restriction struct
  431. * into q this would just be a call to
  432. * combine_restrictions_low()
  433. */
  434. rs->max_sectors =
  435. min_not_zero(rs->max_sectors, q->max_sectors);
  436. /* FIXME: Device-Mapper on top of RAID-0 breaks because DM
  437. * currently doesn't honor MD's merge_bvec_fn routine.
  438. * In this case, we'll force DM to use PAGE_SIZE or
  439. * smaller I/O, just to be safe. A better fix is in the
  440. * works, but add this for the time being so it will at
  441. * least operate correctly.
  442. */
  443. if (q->merge_bvec_fn)
  444. rs->max_sectors =
  445. min_not_zero(rs->max_sectors,
  446. (unsigned int) (PAGE_SIZE >> 9));
  447. rs->max_phys_segments =
  448. min_not_zero(rs->max_phys_segments,
  449. q->max_phys_segments);
  450. rs->max_hw_segments =
  451. min_not_zero(rs->max_hw_segments, q->max_hw_segments);
  452. rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size);
  453. rs->max_segment_size =
  454. min_not_zero(rs->max_segment_size, q->max_segment_size);
  455. rs->max_hw_sectors =
  456. min_not_zero(rs->max_hw_sectors, q->max_hw_sectors);
  457. rs->seg_boundary_mask =
  458. min_not_zero(rs->seg_boundary_mask,
  459. q->seg_boundary_mask);
  460. rs->bounce_pfn = min_not_zero(rs->bounce_pfn, q->bounce_pfn);
  461. rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  462. }
  463. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  464. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  465. sector_t len, int mode, struct dm_dev **result)
  466. {
  467. int r = __table_get_device(ti->table, ti, path,
  468. start, len, mode, result);
  469. if (!r)
  470. dm_set_device_limits(ti, (*result)->bdev);
  471. return r;
  472. }
  473. /*
  474. * Decrement a devices use count and remove it if necessary.
  475. */
  476. void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
  477. {
  478. if (atomic_dec_and_test(&dd->count)) {
  479. close_dev(dd, ti->table->md);
  480. list_del(&dd->list);
  481. kfree(dd);
  482. }
  483. }
  484. /*
  485. * Checks to see if the target joins onto the end of the table.
  486. */
  487. static int adjoin(struct dm_table *table, struct dm_target *ti)
  488. {
  489. struct dm_target *prev;
  490. if (!table->num_targets)
  491. return !ti->begin;
  492. prev = &table->targets[table->num_targets - 1];
  493. return (ti->begin == (prev->begin + prev->len));
  494. }
  495. /*
  496. * Used to dynamically allocate the arg array.
  497. */
  498. static char **realloc_argv(unsigned *array_size, char **old_argv)
  499. {
  500. char **argv;
  501. unsigned new_size;
  502. new_size = *array_size ? *array_size * 2 : 64;
  503. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  504. if (argv) {
  505. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  506. *array_size = new_size;
  507. }
  508. kfree(old_argv);
  509. return argv;
  510. }
  511. /*
  512. * Destructively splits up the argument list to pass to ctr.
  513. */
  514. int dm_split_args(int *argc, char ***argvp, char *input)
  515. {
  516. char *start, *end = input, *out, **argv = NULL;
  517. unsigned array_size = 0;
  518. *argc = 0;
  519. if (!input) {
  520. *argvp = NULL;
  521. return 0;
  522. }
  523. argv = realloc_argv(&array_size, argv);
  524. if (!argv)
  525. return -ENOMEM;
  526. while (1) {
  527. start = end;
  528. /* Skip whitespace */
  529. while (*start && isspace(*start))
  530. start++;
  531. if (!*start)
  532. break; /* success, we hit the end */
  533. /* 'out' is used to remove any back-quotes */
  534. end = out = start;
  535. while (*end) {
  536. /* Everything apart from '\0' can be quoted */
  537. if (*end == '\\' && *(end + 1)) {
  538. *out++ = *(end + 1);
  539. end += 2;
  540. continue;
  541. }
  542. if (isspace(*end))
  543. break; /* end of token */
  544. *out++ = *end++;
  545. }
  546. /* have we already filled the array ? */
  547. if ((*argc + 1) > array_size) {
  548. argv = realloc_argv(&array_size, argv);
  549. if (!argv)
  550. return -ENOMEM;
  551. }
  552. /* we know this is whitespace */
  553. if (*end)
  554. end++;
  555. /* terminate the string and put it in the array */
  556. *out = '\0';
  557. argv[*argc] = start;
  558. (*argc)++;
  559. }
  560. *argvp = argv;
  561. return 0;
  562. }
  563. static void check_for_valid_limits(struct io_restrictions *rs)
  564. {
  565. if (!rs->max_sectors)
  566. rs->max_sectors = SAFE_MAX_SECTORS;
  567. if (!rs->max_hw_sectors)
  568. rs->max_hw_sectors = SAFE_MAX_SECTORS;
  569. if (!rs->max_phys_segments)
  570. rs->max_phys_segments = MAX_PHYS_SEGMENTS;
  571. if (!rs->max_hw_segments)
  572. rs->max_hw_segments = MAX_HW_SEGMENTS;
  573. if (!rs->hardsect_size)
  574. rs->hardsect_size = 1 << SECTOR_SHIFT;
  575. if (!rs->max_segment_size)
  576. rs->max_segment_size = MAX_SEGMENT_SIZE;
  577. if (!rs->seg_boundary_mask)
  578. rs->seg_boundary_mask = -1;
  579. if (!rs->bounce_pfn)
  580. rs->bounce_pfn = -1;
  581. }
  582. int dm_table_add_target(struct dm_table *t, const char *type,
  583. sector_t start, sector_t len, char *params)
  584. {
  585. int r = -EINVAL, argc;
  586. char **argv;
  587. struct dm_target *tgt;
  588. if ((r = check_space(t)))
  589. return r;
  590. tgt = t->targets + t->num_targets;
  591. memset(tgt, 0, sizeof(*tgt));
  592. if (!len) {
  593. DMERR("%s: zero-length target", dm_device_name(t->md));
  594. return -EINVAL;
  595. }
  596. tgt->type = dm_get_target_type(type);
  597. if (!tgt->type) {
  598. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  599. type);
  600. return -EINVAL;
  601. }
  602. tgt->table = t;
  603. tgt->begin = start;
  604. tgt->len = len;
  605. tgt->error = "Unknown error";
  606. /*
  607. * Does this target adjoin the previous one ?
  608. */
  609. if (!adjoin(t, tgt)) {
  610. tgt->error = "Gap in table";
  611. r = -EINVAL;
  612. goto bad;
  613. }
  614. r = dm_split_args(&argc, &argv, params);
  615. if (r) {
  616. tgt->error = "couldn't split parameters (insufficient memory)";
  617. goto bad;
  618. }
  619. r = tgt->type->ctr(tgt, argc, argv);
  620. kfree(argv);
  621. if (r)
  622. goto bad;
  623. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  624. /* FIXME: the plan is to combine high here and then have
  625. * the merge fn apply the target level restrictions. */
  626. combine_restrictions_low(&t->limits, &tgt->limits);
  627. return 0;
  628. bad:
  629. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  630. dm_put_target_type(tgt->type);
  631. return r;
  632. }
  633. static int setup_indexes(struct dm_table *t)
  634. {
  635. int i;
  636. unsigned int total = 0;
  637. sector_t *indexes;
  638. /* allocate the space for *all* the indexes */
  639. for (i = t->depth - 2; i >= 0; i--) {
  640. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  641. total += t->counts[i];
  642. }
  643. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  644. if (!indexes)
  645. return -ENOMEM;
  646. /* set up internal nodes, bottom-up */
  647. for (i = t->depth - 2, total = 0; i >= 0; i--) {
  648. t->index[i] = indexes;
  649. indexes += (KEYS_PER_NODE * t->counts[i]);
  650. setup_btree_index(i, t);
  651. }
  652. return 0;
  653. }
  654. /*
  655. * Builds the btree to index the map.
  656. */
  657. int dm_table_complete(struct dm_table *t)
  658. {
  659. int r = 0;
  660. unsigned int leaf_nodes;
  661. check_for_valid_limits(&t->limits);
  662. /* how many indexes will the btree have ? */
  663. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  664. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  665. /* leaf layer has already been set up */
  666. t->counts[t->depth - 1] = leaf_nodes;
  667. t->index[t->depth - 1] = t->highs;
  668. if (t->depth >= 2)
  669. r = setup_indexes(t);
  670. return r;
  671. }
  672. static DEFINE_MUTEX(_event_lock);
  673. void dm_table_event_callback(struct dm_table *t,
  674. void (*fn)(void *), void *context)
  675. {
  676. mutex_lock(&_event_lock);
  677. t->event_fn = fn;
  678. t->event_context = context;
  679. mutex_unlock(&_event_lock);
  680. }
  681. void dm_table_event(struct dm_table *t)
  682. {
  683. /*
  684. * You can no longer call dm_table_event() from interrupt
  685. * context, use a bottom half instead.
  686. */
  687. BUG_ON(in_interrupt());
  688. mutex_lock(&_event_lock);
  689. if (t->event_fn)
  690. t->event_fn(t->event_context);
  691. mutex_unlock(&_event_lock);
  692. }
  693. sector_t dm_table_get_size(struct dm_table *t)
  694. {
  695. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  696. }
  697. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  698. {
  699. if (index >= t->num_targets)
  700. return NULL;
  701. return t->targets + index;
  702. }
  703. /*
  704. * Search the btree for the correct target.
  705. *
  706. * Caller should check returned pointer with dm_target_is_valid()
  707. * to trap I/O beyond end of device.
  708. */
  709. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  710. {
  711. unsigned int l, n = 0, k = 0;
  712. sector_t *node;
  713. for (l = 0; l < t->depth; l++) {
  714. n = get_child(n, k);
  715. node = get_node(t, l, n);
  716. for (k = 0; k < KEYS_PER_NODE; k++)
  717. if (node[k] >= sector)
  718. break;
  719. }
  720. return &t->targets[(KEYS_PER_NODE * n) + k];
  721. }
  722. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
  723. {
  724. /*
  725. * Make sure we obey the optimistic sub devices
  726. * restrictions.
  727. */
  728. blk_queue_max_sectors(q, t->limits.max_sectors);
  729. q->max_phys_segments = t->limits.max_phys_segments;
  730. q->max_hw_segments = t->limits.max_hw_segments;
  731. q->hardsect_size = t->limits.hardsect_size;
  732. q->max_segment_size = t->limits.max_segment_size;
  733. q->max_hw_sectors = t->limits.max_hw_sectors;
  734. q->seg_boundary_mask = t->limits.seg_boundary_mask;
  735. q->bounce_pfn = t->limits.bounce_pfn;
  736. if (t->limits.no_cluster)
  737. q->queue_flags &= ~(1 << QUEUE_FLAG_CLUSTER);
  738. else
  739. q->queue_flags |= (1 << QUEUE_FLAG_CLUSTER);
  740. }
  741. unsigned int dm_table_get_num_targets(struct dm_table *t)
  742. {
  743. return t->num_targets;
  744. }
  745. struct list_head *dm_table_get_devices(struct dm_table *t)
  746. {
  747. return &t->devices;
  748. }
  749. int dm_table_get_mode(struct dm_table *t)
  750. {
  751. return t->mode;
  752. }
  753. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  754. {
  755. int i = t->num_targets;
  756. struct dm_target *ti = t->targets;
  757. while (i--) {
  758. if (postsuspend) {
  759. if (ti->type->postsuspend)
  760. ti->type->postsuspend(ti);
  761. } else if (ti->type->presuspend)
  762. ti->type->presuspend(ti);
  763. ti++;
  764. }
  765. }
  766. void dm_table_presuspend_targets(struct dm_table *t)
  767. {
  768. if (!t)
  769. return;
  770. return suspend_targets(t, 0);
  771. }
  772. void dm_table_postsuspend_targets(struct dm_table *t)
  773. {
  774. if (!t)
  775. return;
  776. return suspend_targets(t, 1);
  777. }
  778. int dm_table_resume_targets(struct dm_table *t)
  779. {
  780. int i, r = 0;
  781. for (i = 0; i < t->num_targets; i++) {
  782. struct dm_target *ti = t->targets + i;
  783. if (!ti->type->preresume)
  784. continue;
  785. r = ti->type->preresume(ti);
  786. if (r)
  787. return r;
  788. }
  789. for (i = 0; i < t->num_targets; i++) {
  790. struct dm_target *ti = t->targets + i;
  791. if (ti->type->resume)
  792. ti->type->resume(ti);
  793. }
  794. return 0;
  795. }
  796. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  797. {
  798. struct list_head *d, *devices;
  799. int r = 0;
  800. devices = dm_table_get_devices(t);
  801. for (d = devices->next; d != devices; d = d->next) {
  802. struct dm_dev *dd = list_entry(d, struct dm_dev, list);
  803. struct request_queue *q = bdev_get_queue(dd->bdev);
  804. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  805. }
  806. return r;
  807. }
  808. void dm_table_unplug_all(struct dm_table *t)
  809. {
  810. struct list_head *d, *devices = dm_table_get_devices(t);
  811. for (d = devices->next; d != devices; d = d->next) {
  812. struct dm_dev *dd = list_entry(d, struct dm_dev, list);
  813. struct request_queue *q = bdev_get_queue(dd->bdev);
  814. blk_unplug(q);
  815. }
  816. }
  817. struct mapped_device *dm_table_get_md(struct dm_table *t)
  818. {
  819. dm_get(t->md);
  820. return t->md;
  821. }
  822. EXPORT_SYMBOL(dm_vcalloc);
  823. EXPORT_SYMBOL(dm_get_device);
  824. EXPORT_SYMBOL(dm_put_device);
  825. EXPORT_SYMBOL(dm_table_event);
  826. EXPORT_SYMBOL(dm_table_get_size);
  827. EXPORT_SYMBOL(dm_table_get_mode);
  828. EXPORT_SYMBOL(dm_table_get_md);
  829. EXPORT_SYMBOL(dm_table_put);
  830. EXPORT_SYMBOL(dm_table_get);
  831. EXPORT_SYMBOL(dm_table_unplug_all);