dm-table.c 21 KB

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