dm-table.c 21 KB

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