dm-table.c 24 KB

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