dm-table.c 21 KB

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