dm-table.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * Copyright (C) 2001 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 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 <linux/delay.h>
  17. #include <asm/atomic.h>
  18. #define DM_MSG_PREFIX "table"
  19. #define MAX_DEPTH 16
  20. #define NODE_SIZE L1_CACHE_BYTES
  21. #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
  22. #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
  23. /*
  24. * The table has always exactly one reference from either mapped_device->map
  25. * or hash_cell->new_map. This reference is not counted in table->holders.
  26. * A pair of dm_create_table/dm_destroy_table functions is used for table
  27. * creation/destruction.
  28. *
  29. * Temporary references from the other code increase table->holders. A pair
  30. * of dm_table_get/dm_table_put functions is used to manipulate it.
  31. *
  32. * When the table is about to be destroyed, we wait for table->holders to
  33. * drop to zero.
  34. */
  35. struct dm_table {
  36. struct mapped_device *md;
  37. atomic_t holders;
  38. /* btree table */
  39. unsigned int depth;
  40. unsigned int counts[MAX_DEPTH]; /* in nodes */
  41. sector_t *index[MAX_DEPTH];
  42. unsigned int num_targets;
  43. unsigned int num_allocated;
  44. sector_t *highs;
  45. struct dm_target *targets;
  46. /*
  47. * Indicates the rw permissions for the new logical
  48. * device. This should be a combination of FMODE_READ
  49. * and FMODE_WRITE.
  50. */
  51. fmode_t mode;
  52. /* a list of devices used by this table */
  53. struct list_head devices;
  54. /*
  55. * These are optimistic limits taken from all the
  56. * targets, some targets will need smaller limits.
  57. */
  58. struct io_restrictions limits;
  59. /* events get handed up using this callback */
  60. void (*event_fn)(void *);
  61. void *event_context;
  62. };
  63. /*
  64. * Similar to ceiling(log_size(n))
  65. */
  66. static unsigned int int_log(unsigned int n, unsigned int base)
  67. {
  68. int result = 0;
  69. while (n > 1) {
  70. n = dm_div_up(n, base);
  71. result++;
  72. }
  73. return result;
  74. }
  75. /*
  76. * Returns the minimum that is _not_ zero, unless both are zero.
  77. */
  78. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  79. /*
  80. * Combine two io_restrictions, always taking the lower value.
  81. */
  82. static void combine_restrictions_low(struct io_restrictions *lhs,
  83. struct io_restrictions *rhs)
  84. {
  85. lhs->max_sectors =
  86. min_not_zero(lhs->max_sectors, rhs->max_sectors);
  87. lhs->max_phys_segments =
  88. min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments);
  89. lhs->max_hw_segments =
  90. min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments);
  91. lhs->logical_block_size = max(lhs->logical_block_size,
  92. rhs->logical_block_size);
  93. lhs->max_segment_size =
  94. min_not_zero(lhs->max_segment_size, rhs->max_segment_size);
  95. lhs->max_hw_sectors =
  96. min_not_zero(lhs->max_hw_sectors, rhs->max_hw_sectors);
  97. lhs->seg_boundary_mask =
  98. min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask);
  99. lhs->bounce_pfn = min_not_zero(lhs->bounce_pfn, rhs->bounce_pfn);
  100. lhs->no_cluster |= rhs->no_cluster;
  101. }
  102. /*
  103. * Calculate the index of the child node of the n'th node k'th key.
  104. */
  105. static inline unsigned int get_child(unsigned int n, unsigned int k)
  106. {
  107. return (n * CHILDREN_PER_NODE) + k;
  108. }
  109. /*
  110. * Return the n'th node of level l from table t.
  111. */
  112. static inline sector_t *get_node(struct dm_table *t,
  113. unsigned int l, unsigned int n)
  114. {
  115. return t->index[l] + (n * KEYS_PER_NODE);
  116. }
  117. /*
  118. * Return the highest key that you could lookup from the n'th
  119. * node on level l of the btree.
  120. */
  121. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  122. {
  123. for (; l < t->depth - 1; l++)
  124. n = get_child(n, CHILDREN_PER_NODE - 1);
  125. if (n >= t->counts[l])
  126. return (sector_t) - 1;
  127. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  128. }
  129. /*
  130. * Fills in a level of the btree based on the highs of the level
  131. * below it.
  132. */
  133. static int setup_btree_index(unsigned int l, struct dm_table *t)
  134. {
  135. unsigned int n, k;
  136. sector_t *node;
  137. for (n = 0U; n < t->counts[l]; n++) {
  138. node = get_node(t, l, n);
  139. for (k = 0U; k < KEYS_PER_NODE; k++)
  140. node[k] = high(t, l + 1, get_child(n, k));
  141. }
  142. return 0;
  143. }
  144. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  145. {
  146. unsigned long size;
  147. void *addr;
  148. /*
  149. * Check that we're not going to overflow.
  150. */
  151. if (nmemb > (ULONG_MAX / elem_size))
  152. return NULL;
  153. size = nmemb * elem_size;
  154. addr = vmalloc(size);
  155. if (addr)
  156. memset(addr, 0, size);
  157. return addr;
  158. }
  159. /*
  160. * highs, and targets are managed as dynamic arrays during a
  161. * table load.
  162. */
  163. static int alloc_targets(struct dm_table *t, unsigned int num)
  164. {
  165. sector_t *n_highs;
  166. struct dm_target *n_targets;
  167. int n = t->num_targets;
  168. /*
  169. * Allocate both the target array and offset array at once.
  170. * Append an empty entry to catch sectors beyond the end of
  171. * the device.
  172. */
  173. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  174. sizeof(sector_t));
  175. if (!n_highs)
  176. return -ENOMEM;
  177. n_targets = (struct dm_target *) (n_highs + num);
  178. if (n) {
  179. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  180. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  181. }
  182. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  183. vfree(t->highs);
  184. t->num_allocated = num;
  185. t->highs = n_highs;
  186. t->targets = n_targets;
  187. return 0;
  188. }
  189. int dm_table_create(struct dm_table **result, fmode_t mode,
  190. unsigned num_targets, struct mapped_device *md)
  191. {
  192. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  193. if (!t)
  194. return -ENOMEM;
  195. INIT_LIST_HEAD(&t->devices);
  196. atomic_set(&t->holders, 0);
  197. if (!num_targets)
  198. num_targets = KEYS_PER_NODE;
  199. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  200. if (alloc_targets(t, num_targets)) {
  201. kfree(t);
  202. t = NULL;
  203. return -ENOMEM;
  204. }
  205. t->mode = mode;
  206. t->md = md;
  207. *result = t;
  208. return 0;
  209. }
  210. static void free_devices(struct list_head *devices)
  211. {
  212. struct list_head *tmp, *next;
  213. list_for_each_safe(tmp, next, devices) {
  214. struct dm_dev_internal *dd =
  215. list_entry(tmp, struct dm_dev_internal, list);
  216. DMWARN("dm_table_destroy: dm_put_device call missing for %s",
  217. dd->dm_dev.name);
  218. kfree(dd);
  219. }
  220. }
  221. void dm_table_destroy(struct dm_table *t)
  222. {
  223. unsigned int i;
  224. while (atomic_read(&t->holders))
  225. msleep(1);
  226. smp_mb();
  227. /* free the indexes (see dm_table_complete) */
  228. if (t->depth >= 2)
  229. vfree(t->index[t->depth - 2]);
  230. /* free the targets */
  231. for (i = 0; i < t->num_targets; i++) {
  232. struct dm_target *tgt = t->targets + i;
  233. if (tgt->type->dtr)
  234. tgt->type->dtr(tgt);
  235. dm_put_target_type(tgt->type);
  236. }
  237. vfree(t->highs);
  238. /* free the device list */
  239. if (t->devices.next != &t->devices)
  240. free_devices(&t->devices);
  241. kfree(t);
  242. }
  243. void dm_table_get(struct dm_table *t)
  244. {
  245. atomic_inc(&t->holders);
  246. }
  247. void dm_table_put(struct dm_table *t)
  248. {
  249. if (!t)
  250. return;
  251. smp_mb__before_atomic_dec();
  252. atomic_dec(&t->holders);
  253. }
  254. /*
  255. * Checks to see if we need to extend highs or targets.
  256. */
  257. static inline int check_space(struct dm_table *t)
  258. {
  259. if (t->num_targets >= t->num_allocated)
  260. return alloc_targets(t, t->num_allocated * 2);
  261. return 0;
  262. }
  263. /*
  264. * See if we've already got a device in the list.
  265. */
  266. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  267. {
  268. struct dm_dev_internal *dd;
  269. list_for_each_entry (dd, l, list)
  270. if (dd->dm_dev.bdev->bd_dev == dev)
  271. return dd;
  272. return NULL;
  273. }
  274. /*
  275. * Open a device so we can use it as a map destination.
  276. */
  277. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  278. struct mapped_device *md)
  279. {
  280. static char *_claim_ptr = "I belong to device-mapper";
  281. struct block_device *bdev;
  282. int r;
  283. BUG_ON(d->dm_dev.bdev);
  284. bdev = open_by_devnum(dev, d->dm_dev.mode);
  285. if (IS_ERR(bdev))
  286. return PTR_ERR(bdev);
  287. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  288. if (r)
  289. blkdev_put(bdev, d->dm_dev.mode);
  290. else
  291. d->dm_dev.bdev = bdev;
  292. return r;
  293. }
  294. /*
  295. * Close a device that we've been using.
  296. */
  297. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  298. {
  299. if (!d->dm_dev.bdev)
  300. return;
  301. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  302. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
  303. d->dm_dev.bdev = NULL;
  304. }
  305. /*
  306. * If possible, this checks an area of a destination device is valid.
  307. */
  308. static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev,
  309. sector_t start, sector_t len)
  310. {
  311. sector_t dev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  312. unsigned short logical_block_size_sectors =
  313. ti->limits.logical_block_size >> SECTOR_SHIFT;
  314. char b[BDEVNAME_SIZE];
  315. if (!dev_size)
  316. return 1;
  317. if ((start >= dev_size) || (start + len > dev_size)) {
  318. DMWARN("%s: %s too small for target",
  319. dm_device_name(ti->table->md), bdevname(bdev, b));
  320. return 0;
  321. }
  322. if (logical_block_size_sectors <= 1)
  323. return 1;
  324. if (start & (logical_block_size_sectors - 1)) {
  325. DMWARN("%s: start=%llu not aligned to h/w "
  326. "logical block size %hu of %s",
  327. dm_device_name(ti->table->md),
  328. (unsigned long long)start,
  329. ti->limits.logical_block_size, bdevname(bdev, b));
  330. return 0;
  331. }
  332. if (len & (logical_block_size_sectors - 1)) {
  333. DMWARN("%s: len=%llu not aligned to h/w "
  334. "logical block size %hu of %s",
  335. dm_device_name(ti->table->md),
  336. (unsigned long long)len,
  337. ti->limits.logical_block_size, bdevname(bdev, b));
  338. return 0;
  339. }
  340. return 1;
  341. }
  342. /*
  343. * This upgrades the mode on an already open dm_dev, being
  344. * careful to leave things as they were if we fail to reopen the
  345. * device and not to touch the existing bdev field in case
  346. * it is accessed concurrently inside dm_table_any_congested().
  347. */
  348. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  349. struct mapped_device *md)
  350. {
  351. int r;
  352. struct dm_dev_internal dd_new, dd_old;
  353. dd_new = dd_old = *dd;
  354. dd_new.dm_dev.mode |= new_mode;
  355. dd_new.dm_dev.bdev = NULL;
  356. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  357. if (r)
  358. return r;
  359. dd->dm_dev.mode |= new_mode;
  360. close_dev(&dd_old, md);
  361. return 0;
  362. }
  363. /*
  364. * Add a device to the list, or just increment the usage count if
  365. * it's already present.
  366. */
  367. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  368. const char *path, sector_t start, sector_t len,
  369. fmode_t mode, struct dm_dev **result)
  370. {
  371. int r;
  372. dev_t uninitialized_var(dev);
  373. struct dm_dev_internal *dd;
  374. unsigned int major, minor;
  375. BUG_ON(!t);
  376. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  377. /* Extract the major/minor numbers */
  378. dev = MKDEV(major, minor);
  379. if (MAJOR(dev) != major || MINOR(dev) != minor)
  380. return -EOVERFLOW;
  381. } else {
  382. /* convert the path to a device */
  383. struct block_device *bdev = lookup_bdev(path);
  384. if (IS_ERR(bdev))
  385. return PTR_ERR(bdev);
  386. dev = bdev->bd_dev;
  387. bdput(bdev);
  388. }
  389. dd = find_device(&t->devices, dev);
  390. if (!dd) {
  391. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  392. if (!dd)
  393. return -ENOMEM;
  394. dd->dm_dev.mode = mode;
  395. dd->dm_dev.bdev = NULL;
  396. if ((r = open_dev(dd, dev, t->md))) {
  397. kfree(dd);
  398. return r;
  399. }
  400. format_dev_t(dd->dm_dev.name, dev);
  401. atomic_set(&dd->count, 0);
  402. list_add(&dd->list, &t->devices);
  403. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  404. r = upgrade_mode(dd, mode, t->md);
  405. if (r)
  406. return r;
  407. }
  408. atomic_inc(&dd->count);
  409. *result = &dd->dm_dev;
  410. return 0;
  411. }
  412. void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
  413. {
  414. struct request_queue *q = bdev_get_queue(bdev);
  415. struct io_restrictions *rs = &ti->limits;
  416. char b[BDEVNAME_SIZE];
  417. if (unlikely(!q)) {
  418. DMWARN("%s: Cannot set limits for nonexistent device %s",
  419. dm_device_name(ti->table->md), bdevname(bdev, b));
  420. return;
  421. }
  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, queue_max_sectors(q));
  431. /*
  432. * Check if merge fn is supported.
  433. * If not we'll force DM to use PAGE_SIZE or
  434. * smaller I/O, just to be safe.
  435. */
  436. if (q->merge_bvec_fn && !ti->type->merge)
  437. rs->max_sectors =
  438. min_not_zero(rs->max_sectors,
  439. (unsigned int) (PAGE_SIZE >> 9));
  440. rs->max_phys_segments =
  441. min_not_zero(rs->max_phys_segments,
  442. queue_max_phys_segments(q));
  443. rs->max_hw_segments =
  444. min_not_zero(rs->max_hw_segments, queue_max_hw_segments(q));
  445. rs->logical_block_size = max(rs->logical_block_size,
  446. queue_logical_block_size(q));
  447. rs->max_segment_size =
  448. min_not_zero(rs->max_segment_size, queue_max_segment_size(q));
  449. rs->max_hw_sectors =
  450. min_not_zero(rs->max_hw_sectors, queue_max_hw_sectors(q));
  451. rs->seg_boundary_mask =
  452. min_not_zero(rs->seg_boundary_mask,
  453. queue_segment_boundary(q));
  454. rs->bounce_pfn = min_not_zero(rs->bounce_pfn, queue_bounce_pfn(q));
  455. rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  456. }
  457. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  458. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  459. sector_t len, fmode_t mode, struct dm_dev **result)
  460. {
  461. int r = __table_get_device(ti->table, ti, path,
  462. start, len, mode, result);
  463. if (r)
  464. return r;
  465. dm_set_device_limits(ti, (*result)->bdev);
  466. if (!device_area_is_valid(ti, (*result)->bdev, start, len)) {
  467. dm_put_device(ti, *result);
  468. *result = NULL;
  469. return -EINVAL;
  470. }
  471. return r;
  472. }
  473. /*
  474. * Decrement a devices use count and remove it if necessary.
  475. */
  476. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  477. {
  478. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  479. dm_dev);
  480. if (atomic_dec_and_test(&dd->count)) {
  481. close_dev(dd, ti->table->md);
  482. list_del(&dd->list);
  483. kfree(dd);
  484. }
  485. }
  486. /*
  487. * Checks to see if the target joins onto the end of the table.
  488. */
  489. static int adjoin(struct dm_table *table, struct dm_target *ti)
  490. {
  491. struct dm_target *prev;
  492. if (!table->num_targets)
  493. return !ti->begin;
  494. prev = &table->targets[table->num_targets - 1];
  495. return (ti->begin == (prev->begin + prev->len));
  496. }
  497. /*
  498. * Used to dynamically allocate the arg array.
  499. */
  500. static char **realloc_argv(unsigned *array_size, char **old_argv)
  501. {
  502. char **argv;
  503. unsigned new_size;
  504. new_size = *array_size ? *array_size * 2 : 64;
  505. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  506. if (argv) {
  507. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  508. *array_size = new_size;
  509. }
  510. kfree(old_argv);
  511. return argv;
  512. }
  513. /*
  514. * Destructively splits up the argument list to pass to ctr.
  515. */
  516. int dm_split_args(int *argc, char ***argvp, char *input)
  517. {
  518. char *start, *end = input, *out, **argv = NULL;
  519. unsigned array_size = 0;
  520. *argc = 0;
  521. if (!input) {
  522. *argvp = NULL;
  523. return 0;
  524. }
  525. argv = realloc_argv(&array_size, argv);
  526. if (!argv)
  527. return -ENOMEM;
  528. while (1) {
  529. start = end;
  530. /* Skip whitespace */
  531. while (*start && isspace(*start))
  532. start++;
  533. if (!*start)
  534. break; /* success, we hit the end */
  535. /* 'out' is used to remove any back-quotes */
  536. end = out = start;
  537. while (*end) {
  538. /* Everything apart from '\0' can be quoted */
  539. if (*end == '\\' && *(end + 1)) {
  540. *out++ = *(end + 1);
  541. end += 2;
  542. continue;
  543. }
  544. if (isspace(*end))
  545. break; /* end of token */
  546. *out++ = *end++;
  547. }
  548. /* have we already filled the array ? */
  549. if ((*argc + 1) > array_size) {
  550. argv = realloc_argv(&array_size, argv);
  551. if (!argv)
  552. return -ENOMEM;
  553. }
  554. /* we know this is whitespace */
  555. if (*end)
  556. end++;
  557. /* terminate the string and put it in the array */
  558. *out = '\0';
  559. argv[*argc] = start;
  560. (*argc)++;
  561. }
  562. *argvp = argv;
  563. return 0;
  564. }
  565. static void check_for_valid_limits(struct io_restrictions *rs)
  566. {
  567. if (!rs->max_sectors)
  568. rs->max_sectors = SAFE_MAX_SECTORS;
  569. if (!rs->max_hw_sectors)
  570. rs->max_hw_sectors = SAFE_MAX_SECTORS;
  571. if (!rs->max_phys_segments)
  572. rs->max_phys_segments = MAX_PHYS_SEGMENTS;
  573. if (!rs->max_hw_segments)
  574. rs->max_hw_segments = MAX_HW_SEGMENTS;
  575. if (!rs->logical_block_size)
  576. rs->logical_block_size = 1 << SECTOR_SHIFT;
  577. if (!rs->max_segment_size)
  578. rs->max_segment_size = MAX_SEGMENT_SIZE;
  579. if (!rs->seg_boundary_mask)
  580. rs->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  581. if (!rs->bounce_pfn)
  582. rs->bounce_pfn = -1;
  583. }
  584. int dm_table_add_target(struct dm_table *t, const char *type,
  585. sector_t start, sector_t len, char *params)
  586. {
  587. int r = -EINVAL, argc;
  588. char **argv;
  589. struct dm_target *tgt;
  590. if ((r = check_space(t)))
  591. return r;
  592. tgt = t->targets + t->num_targets;
  593. memset(tgt, 0, sizeof(*tgt));
  594. if (!len) {
  595. DMERR("%s: zero-length target", dm_device_name(t->md));
  596. return -EINVAL;
  597. }
  598. tgt->type = dm_get_target_type(type);
  599. if (!tgt->type) {
  600. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  601. type);
  602. return -EINVAL;
  603. }
  604. tgt->table = t;
  605. tgt->begin = start;
  606. tgt->len = len;
  607. tgt->error = "Unknown error";
  608. /*
  609. * Does this target adjoin the previous one ?
  610. */
  611. if (!adjoin(t, tgt)) {
  612. tgt->error = "Gap in table";
  613. r = -EINVAL;
  614. goto bad;
  615. }
  616. r = dm_split_args(&argc, &argv, params);
  617. if (r) {
  618. tgt->error = "couldn't split parameters (insufficient memory)";
  619. goto bad;
  620. }
  621. r = tgt->type->ctr(tgt, argc, argv);
  622. kfree(argv);
  623. if (r)
  624. goto bad;
  625. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  626. /* FIXME: the plan is to combine high here and then have
  627. * the merge fn apply the target level restrictions. */
  628. combine_restrictions_low(&t->limits, &tgt->limits);
  629. return 0;
  630. bad:
  631. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  632. dm_put_target_type(tgt->type);
  633. return r;
  634. }
  635. static int setup_indexes(struct dm_table *t)
  636. {
  637. int i;
  638. unsigned int total = 0;
  639. sector_t *indexes;
  640. /* allocate the space for *all* the indexes */
  641. for (i = t->depth - 2; i >= 0; i--) {
  642. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  643. total += t->counts[i];
  644. }
  645. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  646. if (!indexes)
  647. return -ENOMEM;
  648. /* set up internal nodes, bottom-up */
  649. for (i = t->depth - 2; i >= 0; i--) {
  650. t->index[i] = indexes;
  651. indexes += (KEYS_PER_NODE * t->counts[i]);
  652. setup_btree_index(i, t);
  653. }
  654. return 0;
  655. }
  656. /*
  657. * Builds the btree to index the map.
  658. */
  659. int dm_table_complete(struct dm_table *t)
  660. {
  661. int r = 0;
  662. unsigned int leaf_nodes;
  663. check_for_valid_limits(&t->limits);
  664. /* how many indexes will the btree have ? */
  665. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  666. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  667. /* leaf layer has already been set up */
  668. t->counts[t->depth - 1] = leaf_nodes;
  669. t->index[t->depth - 1] = t->highs;
  670. if (t->depth >= 2)
  671. r = setup_indexes(t);
  672. return r;
  673. }
  674. static DEFINE_MUTEX(_event_lock);
  675. void dm_table_event_callback(struct dm_table *t,
  676. void (*fn)(void *), void *context)
  677. {
  678. mutex_lock(&_event_lock);
  679. t->event_fn = fn;
  680. t->event_context = context;
  681. mutex_unlock(&_event_lock);
  682. }
  683. void dm_table_event(struct dm_table *t)
  684. {
  685. /*
  686. * You can no longer call dm_table_event() from interrupt
  687. * context, use a bottom half instead.
  688. */
  689. BUG_ON(in_interrupt());
  690. mutex_lock(&_event_lock);
  691. if (t->event_fn)
  692. t->event_fn(t->event_context);
  693. mutex_unlock(&_event_lock);
  694. }
  695. sector_t dm_table_get_size(struct dm_table *t)
  696. {
  697. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  698. }
  699. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  700. {
  701. if (index >= t->num_targets)
  702. return NULL;
  703. return t->targets + index;
  704. }
  705. /*
  706. * Search the btree for the correct target.
  707. *
  708. * Caller should check returned pointer with dm_target_is_valid()
  709. * to trap I/O beyond end of device.
  710. */
  711. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  712. {
  713. unsigned int l, n = 0, k = 0;
  714. sector_t *node;
  715. for (l = 0; l < t->depth; l++) {
  716. n = get_child(n, k);
  717. node = get_node(t, l, n);
  718. for (k = 0; k < KEYS_PER_NODE; k++)
  719. if (node[k] >= sector)
  720. break;
  721. }
  722. return &t->targets[(KEYS_PER_NODE * n) + k];
  723. }
  724. /*
  725. * Set the integrity profile for this device if all devices used have
  726. * matching profiles.
  727. */
  728. static void dm_table_set_integrity(struct dm_table *t)
  729. {
  730. struct list_head *devices = dm_table_get_devices(t);
  731. struct dm_dev_internal *prev = NULL, *dd = NULL;
  732. if (!blk_get_integrity(dm_disk(t->md)))
  733. return;
  734. list_for_each_entry(dd, devices, list) {
  735. if (prev &&
  736. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  737. dd->dm_dev.bdev->bd_disk) < 0) {
  738. DMWARN("%s: integrity not set: %s and %s mismatch",
  739. dm_device_name(t->md),
  740. prev->dm_dev.bdev->bd_disk->disk_name,
  741. dd->dm_dev.bdev->bd_disk->disk_name);
  742. goto no_integrity;
  743. }
  744. prev = dd;
  745. }
  746. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  747. goto no_integrity;
  748. blk_integrity_register(dm_disk(t->md),
  749. bdev_get_integrity(prev->dm_dev.bdev));
  750. return;
  751. no_integrity:
  752. blk_integrity_register(dm_disk(t->md), NULL);
  753. return;
  754. }
  755. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
  756. {
  757. /*
  758. * Make sure we obey the optimistic sub devices
  759. * restrictions.
  760. */
  761. blk_queue_max_sectors(q, t->limits.max_sectors);
  762. blk_queue_max_phys_segments(q, t->limits.max_phys_segments);
  763. blk_queue_max_hw_segments(q, t->limits.max_hw_segments);
  764. blk_queue_logical_block_size(q, t->limits.logical_block_size);
  765. blk_queue_max_segment_size(q, t->limits.max_segment_size);
  766. blk_queue_max_hw_sectors(q, t->limits.max_hw_sectors);
  767. blk_queue_segment_boundary(q, t->limits.seg_boundary_mask);
  768. blk_queue_bounce_limit(q, t->limits.bounce_pfn);
  769. if (t->limits.no_cluster)
  770. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  771. else
  772. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  773. dm_table_set_integrity(t);
  774. }
  775. unsigned int dm_table_get_num_targets(struct dm_table *t)
  776. {
  777. return t->num_targets;
  778. }
  779. struct list_head *dm_table_get_devices(struct dm_table *t)
  780. {
  781. return &t->devices;
  782. }
  783. fmode_t dm_table_get_mode(struct dm_table *t)
  784. {
  785. return t->mode;
  786. }
  787. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  788. {
  789. int i = t->num_targets;
  790. struct dm_target *ti = t->targets;
  791. while (i--) {
  792. if (postsuspend) {
  793. if (ti->type->postsuspend)
  794. ti->type->postsuspend(ti);
  795. } else if (ti->type->presuspend)
  796. ti->type->presuspend(ti);
  797. ti++;
  798. }
  799. }
  800. void dm_table_presuspend_targets(struct dm_table *t)
  801. {
  802. if (!t)
  803. return;
  804. suspend_targets(t, 0);
  805. }
  806. void dm_table_postsuspend_targets(struct dm_table *t)
  807. {
  808. if (!t)
  809. return;
  810. suspend_targets(t, 1);
  811. }
  812. int dm_table_resume_targets(struct dm_table *t)
  813. {
  814. int i, r = 0;
  815. for (i = 0; i < t->num_targets; i++) {
  816. struct dm_target *ti = t->targets + i;
  817. if (!ti->type->preresume)
  818. continue;
  819. r = ti->type->preresume(ti);
  820. if (r)
  821. return r;
  822. }
  823. for (i = 0; i < t->num_targets; i++) {
  824. struct dm_target *ti = t->targets + i;
  825. if (ti->type->resume)
  826. ti->type->resume(ti);
  827. }
  828. return 0;
  829. }
  830. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  831. {
  832. struct dm_dev_internal *dd;
  833. struct list_head *devices = dm_table_get_devices(t);
  834. int r = 0;
  835. list_for_each_entry(dd, devices, list) {
  836. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  837. char b[BDEVNAME_SIZE];
  838. if (likely(q))
  839. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  840. else
  841. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  842. dm_device_name(t->md),
  843. bdevname(dd->dm_dev.bdev, b));
  844. }
  845. return r;
  846. }
  847. void dm_table_unplug_all(struct dm_table *t)
  848. {
  849. struct dm_dev_internal *dd;
  850. struct list_head *devices = dm_table_get_devices(t);
  851. list_for_each_entry(dd, devices, list) {
  852. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  853. char b[BDEVNAME_SIZE];
  854. if (likely(q))
  855. blk_unplug(q);
  856. else
  857. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  858. dm_device_name(t->md),
  859. bdevname(dd->dm_dev.bdev, b));
  860. }
  861. }
  862. struct mapped_device *dm_table_get_md(struct dm_table *t)
  863. {
  864. dm_get(t->md);
  865. return t->md;
  866. }
  867. EXPORT_SYMBOL(dm_vcalloc);
  868. EXPORT_SYMBOL(dm_get_device);
  869. EXPORT_SYMBOL(dm_put_device);
  870. EXPORT_SYMBOL(dm_table_event);
  871. EXPORT_SYMBOL(dm_table_get_size);
  872. EXPORT_SYMBOL(dm_table_get_mode);
  873. EXPORT_SYMBOL(dm_table_get_md);
  874. EXPORT_SYMBOL(dm_table_put);
  875. EXPORT_SYMBOL(dm_table_get);
  876. EXPORT_SYMBOL(dm_table_unplug_all);