dm-table.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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 (ie. blk_size[major] is set), this checks an area
  340. * of a destination device is valid.
  341. */
  342. static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len)
  343. {
  344. sector_t dev_size;
  345. dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT;
  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. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  420. sector_t len, int mode, struct dm_dev **result)
  421. {
  422. int r = __table_get_device(ti->table, ti, path,
  423. start, len, mode, result);
  424. if (!r) {
  425. request_queue_t *q = bdev_get_queue((*result)->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->seg_boundary_mask =
  456. min_not_zero(rs->seg_boundary_mask,
  457. q->seg_boundary_mask);
  458. rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  459. }
  460. return r;
  461. }
  462. /*
  463. * Decrement a devices use count and remove it if necessary.
  464. */
  465. void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
  466. {
  467. if (atomic_dec_and_test(&dd->count)) {
  468. close_dev(dd, ti->table->md);
  469. list_del(&dd->list);
  470. kfree(dd);
  471. }
  472. }
  473. /*
  474. * Checks to see if the target joins onto the end of the table.
  475. */
  476. static int adjoin(struct dm_table *table, struct dm_target *ti)
  477. {
  478. struct dm_target *prev;
  479. if (!table->num_targets)
  480. return !ti->begin;
  481. prev = &table->targets[table->num_targets - 1];
  482. return (ti->begin == (prev->begin + prev->len));
  483. }
  484. /*
  485. * Used to dynamically allocate the arg array.
  486. */
  487. static char **realloc_argv(unsigned *array_size, char **old_argv)
  488. {
  489. char **argv;
  490. unsigned new_size;
  491. new_size = *array_size ? *array_size * 2 : 64;
  492. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  493. if (argv) {
  494. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  495. *array_size = new_size;
  496. }
  497. kfree(old_argv);
  498. return argv;
  499. }
  500. /*
  501. * Destructively splits up the argument list to pass to ctr.
  502. */
  503. int dm_split_args(int *argc, char ***argvp, char *input)
  504. {
  505. char *start, *end = input, *out, **argv = NULL;
  506. unsigned array_size = 0;
  507. *argc = 0;
  508. if (!input) {
  509. *argvp = NULL;
  510. return 0;
  511. }
  512. argv = realloc_argv(&array_size, argv);
  513. if (!argv)
  514. return -ENOMEM;
  515. while (1) {
  516. start = end;
  517. /* Skip whitespace */
  518. while (*start && isspace(*start))
  519. start++;
  520. if (!*start)
  521. break; /* success, we hit the end */
  522. /* 'out' is used to remove any back-quotes */
  523. end = out = start;
  524. while (*end) {
  525. /* Everything apart from '\0' can be quoted */
  526. if (*end == '\\' && *(end + 1)) {
  527. *out++ = *(end + 1);
  528. end += 2;
  529. continue;
  530. }
  531. if (isspace(*end))
  532. break; /* end of token */
  533. *out++ = *end++;
  534. }
  535. /* have we already filled the array ? */
  536. if ((*argc + 1) > array_size) {
  537. argv = realloc_argv(&array_size, argv);
  538. if (!argv)
  539. return -ENOMEM;
  540. }
  541. /* we know this is whitespace */
  542. if (*end)
  543. end++;
  544. /* terminate the string and put it in the array */
  545. *out = '\0';
  546. argv[*argc] = start;
  547. (*argc)++;
  548. }
  549. *argvp = argv;
  550. return 0;
  551. }
  552. static void check_for_valid_limits(struct io_restrictions *rs)
  553. {
  554. if (!rs->max_sectors)
  555. rs->max_sectors = SAFE_MAX_SECTORS;
  556. if (!rs->max_phys_segments)
  557. rs->max_phys_segments = MAX_PHYS_SEGMENTS;
  558. if (!rs->max_hw_segments)
  559. rs->max_hw_segments = MAX_HW_SEGMENTS;
  560. if (!rs->hardsect_size)
  561. rs->hardsect_size = 1 << SECTOR_SHIFT;
  562. if (!rs->max_segment_size)
  563. rs->max_segment_size = MAX_SEGMENT_SIZE;
  564. if (!rs->seg_boundary_mask)
  565. rs->seg_boundary_mask = -1;
  566. }
  567. int dm_table_add_target(struct dm_table *t, const char *type,
  568. sector_t start, sector_t len, char *params)
  569. {
  570. int r = -EINVAL, argc;
  571. char **argv;
  572. struct dm_target *tgt;
  573. if ((r = check_space(t)))
  574. return r;
  575. tgt = t->targets + t->num_targets;
  576. memset(tgt, 0, sizeof(*tgt));
  577. if (!len) {
  578. DMERR("%s: zero-length target", dm_device_name(t->md));
  579. return -EINVAL;
  580. }
  581. tgt->type = dm_get_target_type(type);
  582. if (!tgt->type) {
  583. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  584. type);
  585. return -EINVAL;
  586. }
  587. tgt->table = t;
  588. tgt->begin = start;
  589. tgt->len = len;
  590. tgt->error = "Unknown error";
  591. /*
  592. * Does this target adjoin the previous one ?
  593. */
  594. if (!adjoin(t, tgt)) {
  595. tgt->error = "Gap in table";
  596. r = -EINVAL;
  597. goto bad;
  598. }
  599. r = dm_split_args(&argc, &argv, params);
  600. if (r) {
  601. tgt->error = "couldn't split parameters (insufficient memory)";
  602. goto bad;
  603. }
  604. r = tgt->type->ctr(tgt, argc, argv);
  605. kfree(argv);
  606. if (r)
  607. goto bad;
  608. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  609. /* FIXME: the plan is to combine high here and then have
  610. * the merge fn apply the target level restrictions. */
  611. combine_restrictions_low(&t->limits, &tgt->limits);
  612. return 0;
  613. bad:
  614. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  615. dm_put_target_type(tgt->type);
  616. return r;
  617. }
  618. static int setup_indexes(struct dm_table *t)
  619. {
  620. int i;
  621. unsigned int total = 0;
  622. sector_t *indexes;
  623. /* allocate the space for *all* the indexes */
  624. for (i = t->depth - 2; i >= 0; i--) {
  625. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  626. total += t->counts[i];
  627. }
  628. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  629. if (!indexes)
  630. return -ENOMEM;
  631. /* set up internal nodes, bottom-up */
  632. for (i = t->depth - 2, total = 0; i >= 0; i--) {
  633. t->index[i] = indexes;
  634. indexes += (KEYS_PER_NODE * t->counts[i]);
  635. setup_btree_index(i, t);
  636. }
  637. return 0;
  638. }
  639. /*
  640. * Builds the btree to index the map.
  641. */
  642. int dm_table_complete(struct dm_table *t)
  643. {
  644. int r = 0;
  645. unsigned int leaf_nodes;
  646. check_for_valid_limits(&t->limits);
  647. /* how many indexes will the btree have ? */
  648. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  649. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  650. /* leaf layer has already been set up */
  651. t->counts[t->depth - 1] = leaf_nodes;
  652. t->index[t->depth - 1] = t->highs;
  653. if (t->depth >= 2)
  654. r = setup_indexes(t);
  655. return r;
  656. }
  657. static DEFINE_MUTEX(_event_lock);
  658. void dm_table_event_callback(struct dm_table *t,
  659. void (*fn)(void *), void *context)
  660. {
  661. mutex_lock(&_event_lock);
  662. t->event_fn = fn;
  663. t->event_context = context;
  664. mutex_unlock(&_event_lock);
  665. }
  666. void dm_table_event(struct dm_table *t)
  667. {
  668. /*
  669. * You can no longer call dm_table_event() from interrupt
  670. * context, use a bottom half instead.
  671. */
  672. BUG_ON(in_interrupt());
  673. mutex_lock(&_event_lock);
  674. if (t->event_fn)
  675. t->event_fn(t->event_context);
  676. mutex_unlock(&_event_lock);
  677. }
  678. sector_t dm_table_get_size(struct dm_table *t)
  679. {
  680. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  681. }
  682. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  683. {
  684. if (index >= t->num_targets)
  685. return NULL;
  686. return t->targets + index;
  687. }
  688. /*
  689. * Search the btree for the correct target.
  690. */
  691. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  692. {
  693. unsigned int l, n = 0, k = 0;
  694. sector_t *node;
  695. for (l = 0; l < t->depth; l++) {
  696. n = get_child(n, k);
  697. node = get_node(t, l, n);
  698. for (k = 0; k < KEYS_PER_NODE; k++)
  699. if (node[k] >= sector)
  700. break;
  701. }
  702. return &t->targets[(KEYS_PER_NODE * n) + k];
  703. }
  704. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
  705. {
  706. /*
  707. * Make sure we obey the optimistic sub devices
  708. * restrictions.
  709. */
  710. blk_queue_max_sectors(q, t->limits.max_sectors);
  711. q->max_phys_segments = t->limits.max_phys_segments;
  712. q->max_hw_segments = t->limits.max_hw_segments;
  713. q->hardsect_size = t->limits.hardsect_size;
  714. q->max_segment_size = t->limits.max_segment_size;
  715. q->seg_boundary_mask = t->limits.seg_boundary_mask;
  716. if (t->limits.no_cluster)
  717. q->queue_flags &= ~(1 << QUEUE_FLAG_CLUSTER);
  718. else
  719. q->queue_flags |= (1 << QUEUE_FLAG_CLUSTER);
  720. }
  721. unsigned int dm_table_get_num_targets(struct dm_table *t)
  722. {
  723. return t->num_targets;
  724. }
  725. struct list_head *dm_table_get_devices(struct dm_table *t)
  726. {
  727. return &t->devices;
  728. }
  729. int dm_table_get_mode(struct dm_table *t)
  730. {
  731. return t->mode;
  732. }
  733. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  734. {
  735. int i = t->num_targets;
  736. struct dm_target *ti = t->targets;
  737. while (i--) {
  738. if (postsuspend) {
  739. if (ti->type->postsuspend)
  740. ti->type->postsuspend(ti);
  741. } else if (ti->type->presuspend)
  742. ti->type->presuspend(ti);
  743. ti++;
  744. }
  745. }
  746. void dm_table_presuspend_targets(struct dm_table *t)
  747. {
  748. if (!t)
  749. return;
  750. return suspend_targets(t, 0);
  751. }
  752. void dm_table_postsuspend_targets(struct dm_table *t)
  753. {
  754. if (!t)
  755. return;
  756. return suspend_targets(t, 1);
  757. }
  758. void dm_table_resume_targets(struct dm_table *t)
  759. {
  760. int i;
  761. for (i = 0; i < t->num_targets; i++) {
  762. struct dm_target *ti = t->targets + i;
  763. if (ti->type->resume)
  764. ti->type->resume(ti);
  765. }
  766. }
  767. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  768. {
  769. struct list_head *d, *devices;
  770. int r = 0;
  771. devices = dm_table_get_devices(t);
  772. for (d = devices->next; d != devices; d = d->next) {
  773. struct dm_dev *dd = list_entry(d, struct dm_dev, list);
  774. request_queue_t *q = bdev_get_queue(dd->bdev);
  775. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  776. }
  777. return r;
  778. }
  779. void dm_table_unplug_all(struct dm_table *t)
  780. {
  781. struct list_head *d, *devices = dm_table_get_devices(t);
  782. for (d = devices->next; d != devices; d = d->next) {
  783. struct dm_dev *dd = list_entry(d, struct dm_dev, list);
  784. request_queue_t *q = bdev_get_queue(dd->bdev);
  785. if (q->unplug_fn)
  786. q->unplug_fn(q);
  787. }
  788. }
  789. int dm_table_flush_all(struct dm_table *t)
  790. {
  791. struct list_head *d, *devices = dm_table_get_devices(t);
  792. int ret = 0;
  793. for (d = devices->next; d != devices; d = d->next) {
  794. struct dm_dev *dd = list_entry(d, struct dm_dev, list);
  795. request_queue_t *q = bdev_get_queue(dd->bdev);
  796. int err;
  797. if (!q->issue_flush_fn)
  798. err = -EOPNOTSUPP;
  799. else
  800. err = q->issue_flush_fn(q, dd->bdev->bd_disk, NULL);
  801. if (!ret)
  802. ret = err;
  803. }
  804. return ret;
  805. }
  806. struct mapped_device *dm_table_get_md(struct dm_table *t)
  807. {
  808. dm_get(t->md);
  809. return t->md;
  810. }
  811. EXPORT_SYMBOL(dm_vcalloc);
  812. EXPORT_SYMBOL(dm_get_device);
  813. EXPORT_SYMBOL(dm_put_device);
  814. EXPORT_SYMBOL(dm_table_event);
  815. EXPORT_SYMBOL(dm_table_get_size);
  816. EXPORT_SYMBOL(dm_table_get_mode);
  817. EXPORT_SYMBOL(dm_table_get_md);
  818. EXPORT_SYMBOL(dm_table_put);
  819. EXPORT_SYMBOL(dm_table_get);
  820. EXPORT_SYMBOL(dm_table_unplug_all);
  821. EXPORT_SYMBOL(dm_table_flush_all);